Skip to content

AI-native design editor. Open-source Figma alternative.

fix(collab): send rooms only the names people give documents (#1615)

Danila Poyarkovb643d67df43d
diff --git a/src/app/collab/session.ts b/src/app/collab/session.ts
index 01690a179..9cbe36d36 100644
--- a/src/app/collab/session.ts
+++ b/src/app/collab/session.ts
@@ -254,21 +254,14 @@ export function openRoomSession({
}
})
meta.observe(refreshDocument)
- const stopName = watch(
- () => store.state.documentName,
- (name) => {
- if (
- disposed ||
- store.state.readOnly ||
- !hasDocument.value ||
- readRoot(meta) !== store.graph.rootId ||
- name === readRoomName(meta)
- )
- return
- writeRoomName(meta, name)
- },
- { flush: 'sync' }
- )
+ // A rename reaches the room once the tab shows the room's copy; before that the room's name
+ // wins, as the room's other document data does when the tab starts following it.
+ function publishName(name: string) {
+ if (disposed || store.state.readOnly || !hasDocument.value) return
+ if (readRoot(meta) !== store.graph.rootId || name === readRoomName(meta)) return
+ writeRoomName(meta, name)
+ }
+ const stopName = store.onDocumentRenamed(publishName).off
awareness.on('change', updatePeers)
const connection = connectCollabRoom({
diff --git a/src/app/document/io/create.ts b/src/app/document/io/create.ts
index 9fe8d5b73..fd0272b4f 100644
--- a/src/app/document/io/create.ts
+++ b/src/app/document/io/create.ts
@@ -70,6 +70,7 @@ export function createDocumentIOActions(
return {
renameDocument: sourceActions.renameDocument,
+ onDocumentRenamed: sourceActions.onDocumentRenamed,
downloadBlob,
setViewportSize,
getViewportSize,
diff --git a/src/app/document/io/source/actions.ts b/src/app/document/io/source/actions.ts
index 5d31d1ba3..d0c78599f 100644
--- a/src/app/document/io/source/actions.ts
+++ b/src/app/document/io/source/actions.ts
@@ -1,3 +1,5 @@
+import { createEventHook } from '@vueuse/core'
+
import type { Editor, EditorState } from '@open-pencil/core'
import { exportFigFile } from '@open-pencil/core/io/formats/fig'
@@ -128,12 +130,16 @@ export function createDocumentSourceActions({
setSavedVersion(changes.capture())
}
+ // Only names the person gives; a save's provisional name and the room's own are no rename.
+ const renamed = createEventHook<string>()
+
function renameDocument(name: string) {
const trimmed = name.trim()
if (state.readOnly || !trimmed || trimmed === state.documentName) return
state.documentName = trimmed
// Storage keeps the name with the document; a file's name is its path, which saving leaves as is.
if (getStorageBinding()) changes.markChanged()
+ void renamed.trigger(trimmed)
}
function setDocumentSource(
@@ -265,6 +271,7 @@ export function createDocumentSourceActions({
return {
renameDocument,
+ onDocumentRenamed: renamed.on,
setDocumentSource,
setStorageDocumentSource,
setPlannedFilePath,
diff --git a/src/app/editor/session/modules.ts b/src/app/editor/session/modules.ts
index aae142ac3..40053afa6 100644
--- a/src/app/editor/session/modules.ts
+++ b/src/app/editor/session/modules.ts
@@ -85,6 +85,8 @@ export function createEditorStoreModules(
fitCurrentPageToViewport: documentIO.fitCurrentPageToViewport,
hasUnsavedChanges: documentIO.hasUnsavedChanges,
renameDocument: documentIO.renameDocument,
+ /** Calls back with each name the person gives the document. */
+ onDocumentRenamed: documentIO.onDocumentRenamed,
/** Takes the document as it is now as its saved state, so closing it asks nothing. */
markDocumentSaved: documentIO.markDocumentSaved,
/** Counts the document changed by something outside its history, so it gets saved. */
diff --git a/tests/app/collab/session.test.ts b/tests/app/collab/session.test.ts
index b6addca9f..9c532416d 100644
--- a/tests/app/collab/session.test.ts
+++ b/tests/app/collab/session.test.ts
@@ -80,6 +80,21 @@ describe('room sessions', () => {
})
})
+ test('a name the document takes without a rename stays out of the room', async () => {
+ await withRooms(async (open, settle) => {
+ const host = newStore('Checkout')
+ open({ roomId: ROOM_A, store: host, origin: 'shared' }).shareDocument()
+ const guest = newStore()
+ open({ roomId: ROOM_A, store: guest, origin: 'joined' })
+ await settle()
+
+ // As a save to a new place does while it writes, and undoes when the write fails.
+ host.state.documentName = 'Checkout copy'
+ await settle()
+ expect(guest.state.documentName).toBe('Checkout')
+ })
+ })
+
test("a joined tab shows the sharer's document, with its name, once it arrives", async () => {
await withRooms(async (open, settle) => {
const host = newStore('Checkout flow')