AI-native design editor. Open-source Figma alternative.
fix(pen): record descendant overrides as instance overrides (#1463)
Marcos Velásquez340d5ee2325a
diff --git a/bun.lock b/bun.lockindex 264077a11..287e087d1 100644--- a/bun.lock+++ b/bun.lock@@ -523,6 +523,7 @@"name": "@open-pencil/pen","version": "0.16.0","dependencies": {+ "es-toolkit": "^1.52.0","valibot": "^1.5.0",},"devDependencies": {diff --git a/changes/1463/fixed.md b/changes/1463/fixed.mdnew file mode 100644index 000000000..aee169093--- /dev/null+++ b/changes/1463/fixed.md@@ -0,0 +1 @@+- Property overrides in `.pen` instances, such as a changed label or a hidden layer, survive a sync and saving as `.fig`, instead of falling back to the component's values.diff --git a/packages/pen/package.json b/packages/pen/package.jsonindex 5100ca00e..0c2822132 100644--- a/packages/pen/package.json+++ b/packages/pen/package.json@@ -36,6 +36,7 @@"typecheck": "bunx tsc --noEmit -p tsconfig.json"},"dependencies": {+ "es-toolkit": "^1.52.0","valibot": "^1.5.0"},"devDependencies": {diff --git a/packages/pen/src/instances.ts b/packages/pen/src/instances.tsindex 102b9a71b..5fdd22f68 100644--- a/packages/pen/src/instances.ts+++ b/packages/pen/src/instances.ts@@ -1,5 +1,12 @@+import { pick } from 'es-toolkit/object'+import { isEqual } from 'es-toolkit/predicate'+import { generateId, type SceneGraph, type SceneNode } from '@open-pencil/scene-graph'-import { populateInstanceChildren } from '@open-pencil/scene-graph/instances'+import {+ INSTANCE_OVERRIDE_FIELDS,+ populateInstanceChildren,+ recordInstanceOverride+} from '@open-pencil/scene-graph/instances'import { applyPenProperties, createSceneNode, type Build } from './nodes'import type { PenNode, PenOverride } from './schema'@@ -71,15 +78,30 @@ function replaceLayer(build: Build, target: SceneNode, replacement: PenOverride)})}+/**+ * Records as the instance's overrides the fields an override changed on a copy, so they survive+ * the instance's next sync and a save, as an edit made in the editor would.+ */+function recordChangedFields(+ graph: SceneGraph,+ target: SceneNode,+ before: Partial<SceneNode>+): void {+ const changed = INSTANCE_OVERRIDE_FIELDS.filter((field) => !isEqual(before[field], target[field]))+ recordInstanceOverride(graph, target.id, changed)+}+function overrideLayer(build: Build, target: SceneNode, override: PenOverride): void {const { graph, ctx, effective } = buildconst base = effective.get(target.id) ?? { type: 'frame', id: target.id }const { descendants: _descendants, children, ...fields } = overrideconst merged: PenNode = { ...base, ...fields, type: base.type, id: base.id }+ const before = structuredClone(pick(target, INSTANCE_OVERRIDE_FIELDS))inAncestorThemes(build, target, () => {const leave = merged.theme ? ctx.enterTheme(merged.theme) : undefinedtry {applyPenProperties(target, merged, build)+ recordChangedFields(graph, target, before)if (children) {const replaced = target.childIdsfor (const childId of replaced) graph.deleteNode(childId)diff --git a/packages/pen/tests/instances.test.ts b/packages/pen/tests/instances.test.tsindex 5ef603c00..5c1efa563 100644--- a/packages/pen/tests/instances.test.ts+++ b/packages/pen/tests/instances.test.ts@@ -2,6 +2,7 @@ import { describe, expect, test } from 'bun:test'import type { PenNode } from '@open-pencil/pen'import type { SceneGraph, SceneNode } from '@open-pencil/scene-graph'+import { hasInstanceOverride, syncInstance } from '@open-pencil/scene-graph/instances'import { expectDefined, getNodeOrThrow } from './helpers/assert'import { parseDocument } from './helpers/document'@@ -85,6 +86,26 @@ describe('parsePenFile — instances', () => {expect(getNodeOrThrow(graph, 'label').text).toBe('Button')})+ test('records a descendant override so a sync keeps it', () => {+ const graph = parseDocument([+ button,+ {+ id: 'kept',+ type: 'ref',+ ref: 'button',+ descendants: { label: { content: 'Kept' }, dot: { enabled: false } }+ }+ ])+ const label = copyOf(graph, 'kept', 'label')+ expect(hasInstanceOverride(graph, label.id, 'text')).toBe(true)+ expect(hasInstanceOverride(graph, label.id, 'fills')).toBe(false)++ syncInstance(graph, 'kept')++ expect(copyOf(graph, 'kept', 'label').text).toBe('Kept')+ expect(copyOf(graph, 'kept', 'dot').visible).toBe(false)+ })+test('follows an ID path through a nested instance', () => {const graph = parseDocument([button,