AI-native design editor. Open-source Figma alternative.
fix(collab): carry instance overrides to other peers (#1121)
Danila Poyarkovb797e8c1d0e4
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 2e8d2b32d..bcabadf1b 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -17,6 +17,7 @@### Fixed+- People in a shared room see the overrides of instances in the shared document: text, slot content, and every other overridden field arrived empty, and editing such an instance could throw. Rooms opened by earlier builds do not meet rooms opened by this one.- Showing or hiding a layer of an instance that a boolean component property drives sets the property, as in Figma, so every layer it drives follows and Figma shows the instance as OpenPencil does. Before, only that layer changed, which Figma ignored.- Figma shows shader fills and images from older documents in `.fig` files OpenPencil writes: every image is stored under the SHA-1 of its bytes, the only name Figma reads, and a shader keeps its preset through `.fig` files and the Figma clipboard.- HTML, Vue, and React exports name a JPEG, GIF, or WebP image fill by its own type in the data URL instead of calling every image PNG.diff --git a/src/app/collab/node-codec.ts b/src/app/collab/node-codec.tsindex 648cf4e4e..f31ce9b10 100644--- a/src/app/collab/node-codec.ts+++ b/src/app/collab/node-codec.ts@@ -1,11 +1,13 @@import { isEqual } from 'es-toolkit/predicate'-import type {- Fill,- FillType,- GeometryPath,- SceneNode,- SourceMetadata+import {+ deserializeInstanceOverrideState,+ serializeInstanceOverrideState,+ type Fill,+ type FillType,+ type GeometryPath,+ type SceneNode,+ type SourceMetadata} from '@open-pencil/scene-graph'import { copyFills } from '@open-pencil/scene-graph/copy'import { createDefaultSourceMetadata } from '@open-pencil/scene-graph/node-defaults'@@ -42,12 +44,18 @@ type YjsNodeLike = {entries(): IterableIterator<[string, unknown]>}+/**+ * A layer's fields as a shared document can hold them. Yjs stores plain JSON, so the instance+ * overrides, kept in maps, are written as their serialized entries; written as maps they arrived+ * as empty objects, and the other peers lost every override.+ */export function encodeNodeForYjs(node: SceneNode): Record<string, unknown> {const encoded: Record<string, unknown> = {}for (const [key, value] of Object.entries(node)) {if (DERIVED_NODE_FIELDS.has(key as keyof SceneNode) || TREE_FIELDS.has(key)) continueencoded[key] = structuredClone(value)}+ encoded.instanceOverrides = serializeInstanceOverrideState(node.instanceOverrides)return encoded}@@ -76,6 +84,10 @@ export function decodeNodeFromYjs(ynode: YjsNodeLike): Partial<SceneNode> {}props.source = normalizeSourceMetadata(props.source)+ // Rooms written before overrides were serialized hold an empty object, which reads as none.+ if ('instanceOverrides' in props) {+ props.instanceOverrides = deserializeInstanceOverrideState(props.instanceOverrides)+ }if ('fillGeometry' in props) props.fillGeometry = normalizeGeometryPaths(props.fillGeometry)if ('strokeGeometry' in props) props.strokeGeometry = normalizeGeometryPaths(props.strokeGeometry)props.textPicture = nulldiff --git a/src/app/collab/shared-tree/fields.ts b/src/app/collab/shared-tree/fields.tsindex bc9c9eef0..4a5821e44 100644--- a/src/app/collab/shared-tree/fields.ts+++ b/src/app/collab/shared-tree/fields.ts@@ -11,7 +11,7 @@ import { randomIndex } from '@open-pencil/scene-graph/random'* (`src/app/collab/tree/layer-tree.ts`). The document-wide `meta` map holds the room's root and* name, the move clock, and the tree format.*/-export const TREE_FORMAT = 2+export const TREE_FORMAT = 3export const PARENTS_FIELD = 'parents'export const ORDER_KEY_FIELD = 'orderKey'export const PAGE_FIELD = 'page'diff --git a/src/constants.ts b/src/constants.tsindex 6853f94e5..eab0e5436 100644--- a/src/constants.ts+++ b/src/constants.ts@@ -67,7 +67,7 @@ import type { Color } from '@open-pencil/scene-graph/primitives'* The collaboration namespace. It names the shared document format, so builds that sync the layer* tree differently never join each other's rooms; change it with the format.*/-export const COLLAB_APP_ID = 'openpencil/2'+export const COLLAB_APP_ID = 'openpencil/3'// 32 base36 characters provide roughly 165 bits of entropy. The room ID is a// bearer credential and must resist offline enumeration from public signaling topics.export const ROOM_ID_LENGTH = 32diff --git a/tests/engine/collab/yjs-sync.test.ts b/tests/engine/collab/yjs-sync.test.tsindex 429d38762..8689f0833 100644--- a/tests/engine/collab/yjs-sync.test.ts+++ b/tests/engine/collab/yjs-sync.test.ts@@ -51,6 +51,38 @@ describe('collab yjs-sync', () => {expect(decodeNodeFromYjs(ynode).textPicture).toBeNull()})+ test('carries instance overrides to other peers', () => {+ const graph = new SceneGraph()+ const instance = graph.createNode('FRAME', firstPage(graph).id, { name: 'Player' })+ instance.instanceOverrides = {+ self: new Map<string, unknown>([['name', 'Night Shift']]),+ descendants: new Map([['title', new Map<string, unknown>([['text', 'Side B']])]])+ }+ const local = new Y.Doc()+ const ynode = new Y.Map<unknown>()+ local.getMap<Y.Map<unknown>>('nodes').set(instance.id, ynode)+ syncEncodedNodeToYMap(instance, ynode)++ // Through Yjs's own update encoding, as a peer receives it.+ const remote = new Y.Doc()+ Y.applyUpdate(remote, Y.encodeStateAsUpdate(local))+ const received = expectDefined(remote.getMap<Y.Map<unknown>>('nodes').get(instance.id), 'node')++ expect(decodeNodeFromYjs(received).instanceOverrides).toEqual(instance.instanceOverrides)+ })++ test('reads overrides a room stored before they were serialized as none', () => {+ const doc = new Y.Doc()+ const ynode = new Y.Map<unknown>()+ doc.getMap<Y.Map<unknown>>('nodes').set('legacy', ynode)+ ynode.set('instanceOverrides', { self: {}, descendants: {} })++ const { instanceOverrides } = decodeNodeFromYjs(ynode)++ expect(instanceOverrides?.self).toEqual(new Map())+ expect(instanceOverrides?.descendants).toEqual(new Map())+ })+test('normalizes malformed source metadata and geometry at the remote boundary', () => {const doc = new Y.Doc()const ynode = new Y.Map<unknown>()