AI-native design editor. Open-source Figma alternative.
fix(scene-graph): apply an instance's assignment to a late child (#884)
Danila Poyarkov83cf621a9114
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 5abd63f4a..c98d4c46c 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -80,6 +80,7 @@- Report a failed MCP `save_file` or `new_document` save as an error instead of success, and ask for a path rather than opening a Save dialog when the document has never been saved.- Undo layers that MCP clients and the CLI create, delete, or rearrange in the running app, including `render` and `eval` changes, with Edit → Undo. Previously only their property edits were undoable.- Save a `.fig` file that was opened and not edited yet. In the app the save never finished, and MCP `save_file` timed out without writing the file.+- Show the text, visibility, or swapped component an instance sets when the component gains that layer after the instance was placed, instead of the component's default ([#849](https://github.com/open-pencil/open-pencil/issues/849)).- Show `.fig` thumbnails in the desktop app's recent files, which the app was not permitted to read.- Give paints set through the plugin API, `eval`, and AI and MCP scripts an opacity of 1 and make them visible when the script leaves those out, as Figma does. Such paints were stored without them, which the Design panel could not show.- Keep a layer's other plugin data when you pick or clear a colour in OkHCL. Picking one rewrote every plugin-data entry on the layer, including other plugins' and its export settings, as OkHCL data.diff --git a/packages/scene-graph/src/instances/sync.ts b/packages/scene-graph/src/instances/sync.tsindex bc467ce35..9f104dbc4 100644--- a/packages/scene-graph/src/instances/sync.ts+++ b/packages/scene-graph/src/instances/sync.ts@@ -183,18 +183,71 @@ const PROPERTY_REFERENCE_FIELDS: Partial<Record<ComponentPropertyReferenceField,INSTANCE_SWAP: 'componentId'}-/** The nearest enclosing instance that assigns `propertyId`, if any does. */-function hasEnclosingAssignment(graph: SceneGraph, node: SceneNode, propertyId: string): boolean {+/** Instance links are expected to be shallow; the cap only stops a cycle from hanging sync. */+const INSTANCE_CHAIN_LIMIT = 16++/**+ * Whether this instance's component, or the set it is a variant of, is where `propertyId` is+ * defined. Only a set counts: a component nested in an ordinary component defines its own+ * properties, not the outer one's.+ */+function definesProperty(graph: SceneGraph, instance: SceneNode, propertyId: string): boolean {+ // An instance of an instance links through to the component, so follow the chain to it.+ let component = instance.componentId ? graph.nodes.get(instance.componentId) : undefined+ for (let hops = 0; component?.type === 'INSTANCE' && hops < INSTANCE_CHAIN_LIMIT; hops++) {+ component = component.componentId ? graph.nodes.get(component.componentId) : undefined+ }+ const parent = component?.parentId ? graph.nodes.get(component.parentId) : undefined+ const set = parent?.type === 'COMPONENT_SET' ? parent : undefined+ return [component, set]+ .flatMap((node) => node?.componentPropertyDefinitions ?? [])+ .some((definition) => definition.id === propertyId)+}++/**+ * What the nearest enclosing instance assigns `propertyId`, if any does. A property id belongs+ * to the component that defines it, so the walk stops at an instance of that component even+ * when it assigns nothing; otherwise an outer instance's unrelated property of the same id wins.+ */+function enclosingAssignment(+ graph: SceneGraph,+ node: SceneNode,+ propertyId: string+): string | undefined {let current: SceneNode | undefined = nodewhile (current) {- if (- current.type === 'INSTANCE' &&- Object.hasOwn(current.componentPropertyAssignments, propertyId)- )- return true+ if (current.type === 'INSTANCE') {+ if (Object.hasOwn(current.componentPropertyAssignments, propertyId))+ return current.componentPropertyAssignments[propertyId]+ if (definesProperty(graph, current, propertyId)) return undefined+ }current = current.parentId ? graph.nodes.get(current.parentId) : undefined}- return false+ return undefined+}++function hasEnclosingAssignment(graph: SceneGraph, node: SceneNode, propertyId: string): boolean {+ return enclosingAssignment(graph, node, propertyId) !== undefined+}++/**+ * A component can gain a property-driven layer after an instance of it exists. Pass 4 leaves a+ * driven field alone, so the fresh clone has to take the enclosing instance's assignment here or+ * it keeps the component's default while every other instance layer shows the assigned value.+ */+function applyEnclosingAssignments(graph: SceneGraph, clone: SceneNode): void {+ for (const reference of clone.componentPropertyReferences) {+ const field = PROPERTY_REFERENCE_FIELDS[reference.field]+ if (!field) continue+ const value = enclosingAssignment(graph, clone, reference.propertyId)+ if (value === undefined) continue+ if (field === 'visible') graph.updateNode(clone.id, { visible: value === 'true' })+ else if (field === 'text') graph.updateNode(clone.id, { text: value })+ else if (field === 'componentId' && clone.type === 'INSTANCE' && graph.nodes.has(value)) {+ graph.swapInstanceComponent(clone.id, value)+ }+ }+ for (const child of graph.getChildren(clone.id)) applyEnclosingAssignments(graph, child)}/**@@ -395,6 +448,7 @@ export function syncChildren(if (src.childIds.length > 0) {cloneChildrenWithMapping(graph, compChildId, clone.id)}+ applyEnclosingAssignments(graph, clone)instChildMap.set(compChildId, clone)usedInstChildIds.add(clone.id)}diff --git a/packages/scene-graph/tests/components/property/driven-clones.test.ts b/packages/scene-graph/tests/components/property/driven-clones.test.tsnew file mode 100644index 000000000..c6647b94d--- /dev/null+++ b/packages/scene-graph/tests/components/property/driven-clones.test.ts@@ -0,0 +1,163 @@+import { expect, test } from 'bun:test'++import { SceneGraph } from '@open-pencil/scene-graph'++/**+ * A component can gain a property-driven child after an instance of it exists. Cloning that+ * child into the instance must honour the assignment the instance already made, not the+ * component's default, which is what the instance would show everywhere else.+ */+function graphWithLateChild() {+ const graph = new SceneGraph()+ const page = graph.getPages()[0]+ const component = graph.createNode('COMPONENT', page.id, {+ name: 'NavItem',+ componentPropertyDefinitions: [+ { id: '207:1', name: 'Label', type: 'TEXT', defaultValue: 'Default' }+ ]+ })+ const instance = graph.createInstance(component.id, page.id)+ graph.updateNode(instance.id, { componentPropertyAssignments: { '207:1': 'Assigned' } })+ return { graph, component, instance, page }+}++test('a child added after the instance takes the assigned text', () => {+ const { graph, component, instance } = graphWithLateChild()++ graph.createNode('TEXT', component.id, {+ name: 'Label',+ text: 'Default',+ componentPropertyReferences: [{ propertyId: '207:1', field: 'TEXT' }]+ })+ graph.syncInstances(component.id)++ const label = graph.getChildren(instance.id).find((child) => child.name === 'Label')+ expect(label?.text).toBe('Assigned')+})++test('an instance that assigns nothing still takes the component default', () => {+ const graph = new SceneGraph()+ const page = graph.getPages()[0]+ const component = graph.createNode('COMPONENT', page.id, {+ name: 'NavItem',+ componentPropertyDefinitions: [+ { id: '207:1', name: 'Label', type: 'TEXT', defaultValue: 'Default' }+ ]+ })+ const instance = graph.createInstance(component.id, page.id)++ graph.createNode('TEXT', component.id, {+ name: 'Label',+ text: 'Default',+ componentPropertyReferences: [{ propertyId: '207:1', field: 'TEXT' }]+ })+ graph.syncInstances(component.id)++ const label = graph.getChildren(instance.id).find((child) => child.name === 'Label')+ expect(label?.text).toBe('Default')+})++test('a late visibility-driven child takes the assigned visibility', () => {+ const { graph, component, instance } = graphWithLateChild()+ graph.updateNode(component.id, {+ componentPropertyDefinitions: [+ { id: '207:1', name: 'Label', type: 'TEXT', defaultValue: 'Default' },+ { id: '207:2', name: 'Badge', type: 'BOOLEAN', defaultValue: 'true' }+ ]+ })+ graph.updateNode(instance.id, {+ componentPropertyAssignments: { '207:1': 'Assigned', '207:2': 'false' }+ })++ graph.createNode('RECTANGLE', component.id, {+ name: 'Badge',+ visible: true,+ componentPropertyReferences: [{ propertyId: '207:2', field: 'VISIBLE' }]+ })+ graph.syncInstances(component.id)++ const badge = graph.getChildren(instance.id).find((child) => child.name === 'Badge')+ expect(badge?.visible).toBe(false)+})++/**+ * A property id belongs to the component that defines it. A nested instance of a component that+ * defines the same id owns that reference, so an outer instance's unrelated property of the same+ * id must not reach it.+ */+test('a nested component that defines the id keeps an outer assignment out', () => {+ const graph = new SceneGraph()+ const page = graph.getPages()[0]++ const inner = graph.createNode('COMPONENT', page.id, {+ name: 'Inner',+ componentPropertyDefinitions: [+ { id: 'shared', name: 'Label', type: 'TEXT', defaultValue: 'Inner default' }+ ]+ })+ const outer = graph.createNode('COMPONENT', page.id, {+ name: 'Outer',+ componentPropertyDefinitions: [+ { id: 'shared', name: 'Caption', type: 'TEXT', defaultValue: 'Outer default' }+ ]+ })+ graph.createInstance(inner.id, outer.id)++ const placed = graph.createInstance(outer.id, page.id)+ graph.updateNode(placed.id, { componentPropertyAssignments: { shared: 'Outer assigned' } })++ // Inner gains a layer its own property drives, after everything above exists.+ graph.createNode('TEXT', inner.id, {+ name: 'Inner label',+ text: 'Inner default',+ componentPropertyReferences: [{ propertyId: 'shared', field: 'TEXT' }]+ })+ graph.syncInstances(inner.id)+ graph.syncInstances(outer.id)++ const innerInstance = graph+ .getChildren(placed.id)+ .find((child) => child.type === 'INSTANCE')+ const label = innerInstance+ ? graph.getChildren(innerInstance.id).find((child) => child.name === 'Inner label')+ : undefined++ expect(label?.text).toBe('Inner default')+})++/**+ * Only a component set's definitions reach its variants. A component nested inside an ordinary+ * component defines its own properties, so the outer component's must still reach through it.+ */+test('a component nested in a component does not own the outer property', () => {+ const graph = new SceneGraph()+ const page = graph.getPages()[0]++ const outer = graph.createNode('COMPONENT', page.id, {+ name: 'Outer',+ componentPropertyDefinitions: [+ { id: '207:1', name: 'Label', type: 'TEXT', defaultValue: 'Default' }+ ]+ })+ // A plain COMPONENT nested inside another COMPONENT, defining nothing of its own.+ const nested = graph.createNode('COMPONENT', outer.id, { name: 'Nested' })+ const nestedInstance = graph.createInstance(nested.id, outer.id)++ const placed = graph.createInstance(outer.id, page.id)+ graph.updateNode(placed.id, { componentPropertyAssignments: { '207:1': 'Assigned' } })++ graph.createNode('TEXT', nested.id, {+ name: 'Label',+ text: 'Default',+ componentPropertyReferences: [{ propertyId: '207:1', field: 'TEXT' }]+ })+ graph.syncInstances(nested.id)+ graph.syncInstances(outer.id)++ expect(nestedInstance).toBeDefined()+ const placedNested = graph.getChildren(placed.id).find((child) => child.type === 'INSTANCE')+ const label = placedNested+ ? graph.getChildren(placedNested.id).find((child) => child.name === 'Label')+ : undefined+ expect(label?.text).toBe('Assigned')+})diff --git a/packages/scene-graph/tests/components/property-driven-sync.test.ts b/packages/scene-graph/tests/components/property/driven-sync.test.tssimilarity index 100%rename from packages/scene-graph/tests/components/property-driven-sync.test.tsrename to packages/scene-graph/tests/components/property/driven-sync.test.tsdiff --git a/packages/scene-graph/tests/components/property-targets.test.ts b/packages/scene-graph/tests/components/property/targets.test.tssimilarity index 100%rename from packages/scene-graph/tests/components/property-targets.test.tsrename to packages/scene-graph/tests/components/property/targets.test.ts