Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix(graphics): tolerate canvas node kinds from newer clients (#9593)
Eric Hayesf6cf5df5651c
diff --git a/packages/graphics/src/loro/README.md b/packages/graphics/src/loro/README.mdindex e4f733e297..db832ccf77 100644--- a/packages/graphics/src/loro/README.md+++ b/packages/graphics/src/loro/README.md@@ -23,6 +23,11 @@ before its backend. Memory-backed hosts keep their original behavior.coherent `pose` register: local matrix, geometry, coordinateparent ID, and a captured world matrix. Fill and stroke are separate registers,so moving and restyling the same shape can merge independently.+- Node kinds are open. A client skips a kind it does not know (with its subtree)+ when projecting, and writes it back untouched after the sibling that preceded+ it, so a new kind can ship without breaking or deleting through older clients.+ Geometry is validated per known kind at projection, not by the schema. Deleting+ or ungrouping a group in an older client still removes unknown children.- Ordered Loro children project to unique core sort keys. These keys are derived,not CRDT fields; concurrent key collisions are handled by Loro's ordered tree.Core commands become moves of affected tree nodes, not full-list replacements.diff --git a/packages/graphics/src/loro/scene-mapping.ts b/packages/graphics/src/loro/scene-mapping.tsindex 08e343fe0d..39d184cd6a 100644--- a/packages/graphics/src/loro/scene-mapping.ts+++ b/packages/graphics/src/loro/scene-mapping.ts@@ -6,12 +6,7 @@ import {sameMatrix,} from '../core/affine';import { resolveAppearance } from '../core/appearance';-import type {- GraphicsDocument,- GraphicsItem,- ShapeGeometryMap,- ShapeKind,-} from '../core/model';+import type { GraphicsDocument, GraphicsItem, ShapeKind } from '../core/model';import { sortKeysBetween } from '../core/ordering';import {children,@@ -20,7 +15,7 @@ import {worldMatrix,} from '../core/scene';import { canLabel } from '../core/shapes/label';-import { isShape, shapePayload } from '../core/shapes/registry';+import { isShape, isShapeKind, shapePayload } from '../core/shapes/registry';import type { CanvasForest, NodeData } from './schema';export function readScene(forest: CanvasForest, rootId: string) {@@ -37,6 +32,10 @@ export function readScene(forest: CanvasForest, rootId: string) {pose = node.data.pose;if (!id || !kind || !pose || Object.hasOwn(items, id))throw new Error('Invalid shared scene node');++ // A newer client's kind: leave it, and its subtree, out of this projection.+ if (kind !== 'group' && !isShapeKind(kind)) return;+const matchingFrame = pose.parentId === parentId;const transform = matchingFrame? pose.transform@@ -97,7 +96,7 @@ function poseGeometry(node: GraphicsItem | undefined) {}function restoreText(kind: ShapeKind,- geometry: ShapeGeometryMap[ShapeKind],+ geometry: object,content: string | null,labelStyle: NodeData['labelStyle']) {@@ -124,9 +123,13 @@ export function writeScene(if (before.rootId !== next.rootId || next.surface)throw new Error('Shared graphics backend supports one unbounded surface');const data = new Map<string, NodeData>();+ const previousChildren = new Map<string, CanvasForest>([+ [before.rootId, forest],+ ]);function collect(nodes: CanvasForest) {for (const node of nodes) {data.set(node.data.id, node.data);+ previousChildren.set(node.data.id, node.children);collect(node.children);}}@@ -136,6 +139,12 @@ export function writeScene(throw new Error('Shared node kinds are immutable');}function project(parent: string): CanvasForest {+ return keepNewerKinds(+ previousChildren.get(parent) ?? [],+ projectKnown(parent)+ );+ }+ function projectKnown(parent: string): CanvasForest {return children(next, parent).map((id) => {const node = next.items[id]!,old = before.items[id],@@ -207,3 +216,27 @@ export function writeScene(}return project(next.rootId);}++const isNewerKind = (node: CanvasForest[number]) =>+ node.data.kind !== 'group' && !isShapeKind(node.data.kind);++/** Nodes of kinds this client cannot read keep their registers, subtree and+ * place after the sibling that preceded them; writing the projection alone+ * would delete them. */+function keepNewerKinds(previous: CanvasForest, known: CanvasForest) {+ if (!previous.some(isNewerKind)) return known;++ const result = [...known];+ let insertAt = 0;+ for (const node of previous) {+ if (isNewerKind(node)) {+ result.splice(insertAt, 0, node);+ insertAt += 1;+ continue;+ }++ const index = result.findIndex((item) => item.data.id === node.data.id);+ if (index !== -1) insertAt = index + 1;+ }+ return result;+}diff --git a/packages/graphics/src/loro/schema.ts b/packages/graphics/src/loro/schema.tsindex 6139377780..236f3c3784 100644--- a/packages/graphics/src/loro/schema.ts+++ b/packages/graphics/src/loro/schema.ts@@ -1,14 +1,13 @@import { type InferType, schema } from '@loro-mirror/core';import type { Matrix } from '../core/affine';-import type { ShapeGeometryMap, ShapeKind } from '../core/model';import { type ShapeLabel, validShapeLabel } from '../core/shapes/label';-import { isShapeKind, shapeDefinitions } from '../core/shapes/registry';+/** Geometry is validated per kind when projected, so newer kinds stay readable. */type Pose = {parentId: string;transform: Matrix;world: Matrix;- geometry: ShapeGeometryMap[ShapeKind] | null;+ geometry: object | null;};function isMatrix(value: unknown): value is Matrix {return (@@ -17,11 +16,6 @@ function isMatrix(value: unknown): value is Matrix {value.every((v) => typeof v === 'number' && Number.isFinite(v)));}-function isGeometry(value: unknown): value is ShapeGeometryMap[ShapeKind] {- return Object.values(shapeDefinitions).some((definition) =>- definition.validateGeometry(value)- );-}function isPose(value: unknown): value is Pose {return (!!value &&@@ -33,7 +27,7 @@ function isPose(value: unknown): value is Pose {'world' in value &&isMatrix(value.world) &&'geometry' in value &&- (value.geometry === null || isGeometry(value.geometry))+ (value.geometry === null || typeof value.geometry === 'object'));}function isLabelStyle(@@ -47,12 +41,12 @@ function isLabelStyle();}-/** Preserve the deployed map registers; pose and label style remain atomic JSON. */+/** Preserve the deployed map registers; pose and label style remain atomic JSON.+ * Kinds are open: clients skip and preserve kinds added after they shipped. */export const canvasNodeSchema = schema.LoroMap({id: schema.String({ required: true }),kind: schema.Value(- (value): value is ShapeKind | 'group' =>- value === 'group' || isShapeKind(value),+ (value): value is string => typeof value === 'string' && !!value,{ required: true }),pose: schema.Value(isPose, { required: true }),diff --git a/packages/graphics/tests/loro-mirror.test.ts b/packages/graphics/tests/loro-mirror.test.tsindex a53cbfe81b..a99b8a7a71 100644--- a/packages/graphics/tests/loro-mirror.test.ts+++ b/packages/graphics/tests/loro-mirror.test.ts@@ -82,3 +82,85 @@ it('opens the deployed native format without conversion, preserving IDs, metadatdoc.commit();doc.free();});++it('skips node kinds from newer clients and preserves them through local edits', () => {+ const doc = new LoroDoc();+ doc.setPeerId('1');+ const tree = doc.getTree('scene');+ tree.enableFractionalIndex(0);+ const pose = (parentId: string) => ({+ parentId,+ transform: IDENTITY,+ world: IDENTITY,+ geometry: { width: 100, height: 80 },+ });+ for (const id of ['a', 'newer', 'b']) {+ const node = tree.createNode();+ node.data.set('id', id);+ node.data.set('kind', id === 'newer' ? 'future-kind' : 'rectangle');+ node.data.set('pose', pose('root'));+ node.data.set('fill', 'red');+ node.data.set('stroke', 'black');+ }+ const group = tree.createNode();+ group.data.set('id', 'g');+ group.data.set('kind', 'group');+ group.data.set('pose', { ...pose('root'), geometry: null });+ const nested = group.createNode();+ nested.data.set('id', 'nested');+ nested.data.set('kind', 'future-kind');+ nested.data.set('pose', { ...pose('g'), geometry: { source: '<p>' } });+ const child = group.createNode();+ child.data.set('id', 'child');+ child.data.set('kind', 'rectangle');+ child.data.set('pose', pose('g'));+ child.data.set('fill', 'red');+ child.data.set('stroke', 'black');+ doc.commit();+ const newerRegisters = tree+ .getNodes()+ .find((n) => n.data.get('id') === 'newer')!+ .data.toJSON();++ const backend = createLoroGraphicsBackendFromDoc(doc, 'root');+ const editor = createGraphicsEditorFromBackend(backend);+ expect(Object.keys(editor.document.items).sort()).toEqual(+ ['a', 'b', 'child', 'g', 'root'].sort()+ );++ editor.select('a');+ editor.deleteSelection();+ editor.select('b');+ editor.reorderSelection('front');+ editor.select('child');+ editor.setSelectionAppearance({ fill: 'green' });++ const ids = (nodes: ReturnType<typeof tree.roots>) =>+ nodes.map((node) => node.data.get('id'));+ expect(ids(tree.roots())).toEqual(['newer', 'g', 'b']);+ expect(ids(group.children() ?? [])).toEqual(['nested', 'child']);+ expect(+ tree+ .getNodes()+ .find((n) => n.data.get('id') === 'newer')!+ .data.toJSON()+ ).toEqual(newerRegisters);++ // A remote peer adding another newer kind does not break the projection.+ const remote = new LoroDoc();+ remote.setPeerId('2');+ remote.import(doc.export({ mode: 'snapshot' }));+ const added = remote.getTree('scene').createNode();+ added.data.set('id', 'remote-newer');+ added.data.set('kind', 'future-kind');+ added.data.set('pose', pose('root'));+ remote.commit();+ backend.receive(remote.export({ mode: 'update', from: doc.oplogVersion() }));+ expect(ids(tree.roots())).toContain('remote-newer');+ expect(editor.document.items['remote-newer']).toBeUndefined();++ editor.dispose();+ backend.dispose();+ doc.free();+ remote.free();+});