AI-native design editor. Open-source Figma alternative.
fix: show the room's layers when a tab opens a Cloud document it shares a room with (#1293)
Danila Poyarkov68f4ef1c66e3
diff --git a/src/app/collab/session.ts b/src/app/collab/session.tsindex 062de946b..b0dc5d12e 100644--- a/src/app/collab/session.ts+++ b/src/app/collab/session.ts@@ -126,6 +126,8 @@ export function openRoomSession({const connectedSince = ref<number | null>(null)let suppressYjsEvents = falselet suppressGraphSync = false+ /** Whether a tab that holds a stored document has given way to the room's copy of it. */+ let followsRoom = falselet disposed = falseconst status = computed(() =>@@ -155,6 +157,16 @@ export function openRoomSession({if (disposed) returnconst rootId = readRoot(meta)hasDocument.value = rootId === undefined ? holdsDocument : store.graph.rootId === rootId+ // The room's document is what a stored document's tab shows once the room holds it.+ if (holdsDocument && hasDocument.value && rootId !== undefined && !followsRoom) {+ followsRoom = true+ suppressGraphSync = true+ try {+ sync.dropLayersOutsideRoom()+ } finally {+ suppressGraphSync = false+ }+ }// Newcomers wait for someone who has the file, so peers say whether they do.if (awareness.getLocalState()?.hasFile !== hasDocument.value) {awareness.setLocalStateField('hasFile', hasDocument.value)diff --git a/src/app/collab/yjs-sync.ts b/src/app/collab/yjs-sync.tsindex ab560ce8a..ebecc95ed 100644--- a/src/app/collab/yjs-sync.ts+++ b/src/app/collab/yjs-sync.ts@@ -348,5 +348,33 @@ export function createYjsGraphSync({})}- return { syncNodeToYjs, syncLocalEdit, syncAllNodesToYjs, applyYjsToGraph }+ /**+ * Removes the layers under the document's root that the room does not hold. A stored document+ * opened in several tabs numbers its layers afresh each time, so its copies of layers the room+ * already has would otherwise sit beside the room's own.+ */+ function dropLayersOutsideRoom() {+ const graph = getStore().graph+ const ynodes = getYnodes()+ if (!ynodes) return+ const outside: string[] = []+ const visit = (id: string) => {+ for (const childId of graph.getNode(id)?.childIds ?? []) {+ if (ynodes.has(childId)) visit(childId)+ else outside.push(childId)+ }+ }+ visit(graph.rootId)+ for (const id of outside) graph.deleteNode(id)+ // The page the tab was on may have been the file's own; it moves to one of the room's.+ if (outside.length > 0) ensureCurrentPageExists(getStore())+ }++ return {+ syncNodeToYjs,+ syncLocalEdit,+ syncAllNodesToYjs,+ applyYjsToGraph,+ dropLayersOutsideRoom+ }}diff --git a/tests/integration/collab/cloud-relay.test.ts b/tests/integration/collab/cloud-relay.test.tsindex 58060172a..15b13303e 100644--- a/tests/integration/collab/cloud-relay.test.ts+++ b/tests/integration/collab/cloud-relay.test.ts@@ -126,6 +126,97 @@ describe('collaboration through the Cloud relay', () => {})})+describe('Cloud document rooms, reopening', () => {+ test('a tab that opens the stored file shows the room’s layers, not the file’s copies', async () => {+ const relay = await startRelay()+ const documentId = crypto.randomUUID()+ const roomId = `cloud:${documentId}:0`+ const [grace] = await openedTwice()+ const first = openCloudDocumentRoom(grace, {+ roomId,+ transport: relay.join(documentId, 'Grace', 'edit', 'user'),+ permission: 'edit'+ })+ cleanups.push(() => {+ first.stop()+ first.session.dispose()+ })+ await until(() => first.session.roomHasDocument())+ const page = grace.graph.getPages()[0]+ if (!page) throw new Error('Expected a page')+ const dot = grace.graph.createNode('ELLIPSE', page.id, { name: 'Dot' })++ // The saver uploads the document; opening that file numbers its layers afresh.+ const saved = await exportFigFile(grace.graph)+ const reopened = createEditorStore(await parseFigFile(Uint8Array.from(saved).buffer))+ cleanups.push(() => reopened.preparationController.dispose())+ const second = openCloudDocumentRoom(reopened, {+ roomId,+ transport: relay.join(documentId, 'Ada', 'edit', 'user'),+ permission: 'edit'+ })+ cleanups.push(() => {+ second.stop()+ second.session.dispose()+ })+ await until(() => reopened.graph.getNode(dot.id) !== undefined)+ const layers = (store: typeof grace) =>+ store.graph+ .getPages()+ .flatMap((candidate) => candidate.childIds)+ .map((id) => store.graph.getNode(id)?.name)+ .sort()+ expect(layers(reopened)).toEqual(layers(grace))+ })++ test('a tab left on a page only its file had moves to one of the room’s pages', async () => {+ const relay = await startRelay()+ const documentId = crypto.randomUUID()+ const roomId = `cloud:${documentId}:0`+ const [grace, earlier] = await openedTwice()+ const first = openCloudDocumentRoom(grace, {+ roomId,+ transport: relay.join(documentId, 'Grace', 'edit', 'user'),+ permission: 'edit'+ })+ cleanups.push(() => {+ first.stop()+ first.session.dispose()+ })+ await until(() => first.session.roomHasDocument())+ // An earlier visit leaves this device a saved copy of the room, which the next opens first.+ const visit = openCloudDocumentRoom(earlier, {+ roomId,+ transport: relay.join(documentId, 'Ada', 'edit', 'user'),+ permission: 'edit'+ })+ await until(() => visit.session.hasDocument.value && visit.session.peers.value.length > 0)+ visit.stop()+ visit.session.dispose()++ const [reopened] = await openedTwice()+ await until(() => reopened.state.preparation === null)+ // A page the room never had, as the file's own copy of a page can be.+ const stray = reopened.graph.addPage('Stray')+ reopened.state.currentPageId = stray.id+ const second = openCloudDocumentRoom(reopened, {+ roomId,+ transport: relay.join(documentId, 'Ada', 'edit', 'user'),+ permission: 'edit'+ })+ cleanups.push(() => {+ second.stop()+ second.session.dispose()+ })+ await until(() => reopened.graph.getNode(stray.id) === undefined)+ // One of the room's pages, which the tab that seeded the room has too.+ await until(() =>+ grace.graph.getPages().some((page) => page.id === reopened.state.currentPageId)+ )+ expect(reopened.graph.getNode(reopened.state.currentPageId)?.type).toBe('CANVAS')+ })+})+describe('Cloud document rooms, version history', () => {test('a tab reloads its history when the server says it changed', async () => {const relay = await startRelay()