AI-native design editor. Open-source Figma alternative.
fix(editor): hide or show a property-driven instance layer by its property (#1113)
Danila Poyarkov6643f901a40c
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 791d1bb39..1209a19c1 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -17,6 +17,7 @@### Fixed+- 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.- Saving an opened `.fig` file writes only what an edit changed in a layer or variable, and keeps the rest as Figma saved it. That includes data OpenPencil does not model, an instance's library link and saved overrides, and an edited text's letter spacing in percent and font names. Before, saving rewrote each edited layer whole. Importing design tokens or adding a mode also rewrote every layer and variable in the file that uses variable modes.diff --git a/packages/core/src/editor/components/authoring/visibility-edit.ts b/packages/core/src/editor/components/authoring/visibility-edit.tsnew file mode 100644index 000000000..4dae52554--- /dev/null+++ b/packages/core/src/editor/components/authoring/visibility-edit.ts@@ -0,0 +1,76 @@+import { isEqual } from 'es-toolkit/predicate'++import {+ applyComponentPropertyValue,+ cloneInstanceOverrideState,+ componentPropertyDefinitions,+ findComponentPropertyTargets+} from '@open-pencil/scene-graph'+import type { SceneNode } from '@open-pencil/scene-graph'++import type { EditorContext } from '#core/editor/types'++/** Whether a boolean property drives the layer's visibility. */+export function bindsVisibility(node: SceneNode): boolean {+ return node.componentPropertyReferences.some((reference) => reference.field === 'VISIBLE')+}++/**+ * Showing or hiding a layer of an instance that a boolean property drives sets the property, as+ * in Figma: the instance that defines it takes the value, and every layer it drives follows. A+ * bare visibility override would not reach Figma, which shows such layers by the property. The+ * caller records the edit itself; this records the property change beside it. Returns whether a+ * property drives the layer, and so took the value.+ */+export function applyBoundVisibilityEdit(+ ctx: EditorContext,+ nodeId: string,+ visible: boolean+): boolean {+ const node = ctx.graph.getNode(nodeId)+ const propertyId = node?.componentPropertyReferences.find(+ (reference) => reference.field === 'VISIBLE'+ )?.propertyId+ if (!node?.parentId || !propertyId) return false+ const instance = ctx.graph.closest(+ node.parentId,+ (item) =>+ item.type === 'INSTANCE' &&+ componentPropertyDefinitions(ctx.graph, item).some(+ (definition) => definition.id === propertyId+ )+ )+ const definition = instance+ ? componentPropertyDefinitions(ctx.graph, instance).find((item) => item.id === propertyId)+ : undefined+ if (!instance || definition?.type !== 'BOOLEAN') return false+ const layers = findComponentPropertyTargets(ctx.graph, instance, propertyId).flatMap((target) =>+ target.field === 'VISIBLE' ? [target.node.id] : []+ )+ const snapshot = () => ({+ componentPropertyAssignments: { ...instance.componentPropertyAssignments },+ instanceOverrides: cloneInstanceOverrideState(instance.instanceOverrides),+ layers: layers.map((id) => [id, ctx.graph.getNode(id)?.visible ?? true] as const)+ })+ const before = snapshot()+ applyComponentPropertyValue(ctx.graph, instance.id, definition, String(visible))+ const after = snapshot()+ // The property already had the value, as when another layer it drives was just shown.+ if (isEqual(before, after)) return true+ const apply = (values: typeof before) => {+ ctx.graph.updateNode(instance.id, {+ componentPropertyAssignments: { ...values.componentPropertyAssignments },+ instanceOverrides: cloneInstanceOverrideState(values.instanceOverrides)+ })+ for (const [id, shown] of values.layers) ctx.graph.updateNode(id, { visible: shown })+ ctx.runLayoutForNode(instance.id)+ ctx.requestRender()+ }+ ctx.runLayoutForNode(instance.id)+ ctx.undo.push({+ label: visible ? 'Show layer' : 'Hide layer',+ forward: () => apply(after),+ inverse: () => apply(before)+ })+ return true+}diff --git a/packages/core/src/editor/nodes.ts b/packages/core/src/editor/nodes.tsindex 33595fb4e..bcfceade2 100644--- a/packages/core/src/editor/nodes.ts+++ b/packages/core/src/editor/nodes.ts@@ -11,6 +11,7 @@ import {import { textAutoResizeChanges } from '#core/layout/text-auto-resize'import { reconcileVariableLayouts } from '#core/layout/variables'+import { applyBoundVisibilityEdit, bindsVisibility } from './components/authoring/visibility-edit'import { createLayoutModeActions } from './layout-mode'import { createNodePreviewActions } from './node-preview'import { createNudgeActions } from './nudge'@@ -58,6 +59,21 @@ export function createNodeActions(ctx: EditorContext) {function updateNodeWithUndo(id: string, changes: Partial<SceneNode>, label = 'Update') {const node = ctx.graph.getNode(id)if (!node) return+ // A layer a boolean property shows or hides sets the property too, in the same undo step.+ // It takes the value even where the layer already shows it, so the two agree.+ const visible = changes.visible+ if (visible !== undefined && bindsVisibility(node)) {+ ctx.undo.runBatch(label, () => {+ recordNodeUpdate(node, changes, label)+ applyBoundVisibilityEdit(ctx, id, visible)+ })+ return+ }+ recordNodeUpdate(node, changes, label)+ }++ function recordNodeUpdate(node: SceneNode, changes: Partial<SceneNode>, label: string) {+ const { id } = node// Same ordering rationale as updateNode: reflowed path-text glyphs win.const nextChanges = styleDetachmentChanges(node, {...changes,diff --git a/packages/core/src/editor/structure/state.ts b/packages/core/src/editor/structure/state.tsindex 13a14f637..ef743730a 100644--- a/packages/core/src/editor/structure/state.ts+++ b/packages/core/src/editor/structure/state.ts@@ -1,19 +1,32 @@import { findInstanceAncestor, setInstanceOverride } from '@open-pencil/scene-graph'import { assertNodeEditable } from '#core/editor/capabilities'+import {+ applyBoundVisibilityEdit,+ bindsVisibility+} from '#core/editor/components/authoring/visibility-edit'import type { EditorContext } from '#core/editor/types'export function createStructureStateActions(ctx: EditorContext) {- function toggleNodeVisibility(id: string) {- assertNodeEditable(ctx.graph, id)+ /** Shows or hides a layer; one a boolean property drives sets the property, as in Figma. */+ function setNodeVisibility(id: string, visible: boolean) {const node = ctx.graph.getNode(id)if (!node) returnconst owner = findInstanceAncestor(ctx.graph, id)+ // The property takes the value even where the layer already shows it, so the two agree.+ if (owner && bindsVisibility(node) && applyBoundVisibilityEdit(ctx, id, visible)) return+ if (node.visible === visible) returnif (owner) setInstanceOverride(owner.instanceOverrides, owner.id, id, 'visible', true)- ctx.graph.updateNode(id, { visible: !node.visible })+ ctx.graph.updateNode(id, { visible })if (node.parentId) ctx.runLayoutForNode(node.parentId)}+ function toggleNodeVisibility(id: string) {+ assertNodeEditable(ctx.graph, id)+ const node = ctx.graph.getNode(id)+ if (node) setNodeVisibility(id, !node.visible)+ }+function toggleNodeLock(id: string) {assertNodeEditable(ctx.graph, id)const node = ctx.graph.getNode(id)@@ -23,7 +36,13 @@ export function createStructureStateActions(ctx: EditorContext) {function toggleVisibility() {for (const id of ctx.state.selectedIds) assertNodeEditable(ctx.graph, id)- for (const id of ctx.state.selectedIds) toggleNodeVisibility(id)+ // Each layer takes the opposite of what it showed before any of them changed: layers a+ // property drives together would otherwise toggle it back and forth.+ const targets = [...ctx.state.selectedIds].flatMap((id) => {+ const node = ctx.graph.getNode(id)+ return node ? [[id, !node.visible] as const] : []+ })+ for (const [id, visible] of targets) setNodeVisibility(id, visible)}function toggleLock() {diff --git a/packages/core/tests/editor/components/properties.test.ts b/packages/core/tests/editor/components/properties.test.tsindex 446dd464d..b431d6195 100644--- a/packages/core/tests/editor/components/properties.test.ts+++ b/packages/core/tests/editor/components/properties.test.ts@@ -41,6 +41,49 @@ function setupComponentProperties() {}describe('component property actions', () => {+ test('hiding a layer a boolean property shows sets the property, as Figma does', () => {+ const { editor, component, instance } = setupComponentProperties()+ editor.graph.createNode('FRAME', component.id, {+ name: 'Dot',+ componentPropertyReferences: [{ propertyId: '10:2', field: 'VISIBLE' }]+ })+ editor.graph.syncInstance(instance.id)+ const badge = childByName(editor, instance.id, 'Badge')+ if (!badge) throw new Error('Expected badge copy')++ editor.updateNodeWithUndo(badge.id, { visible: false }, 'Hide')+ expect(editor.graph.getNode(instance.id)?.componentPropertyAssignments['10:2']).toBe('false')+ // Every layer the property drives follows it.+ expect(childByName(editor, instance.id, 'Dot')?.visible).toBe(false)++ editor.undo.undo()+ expect(editor.graph.getNode(instance.id)?.componentPropertyAssignments['10:2']).toBeUndefined()+ expect(childByName(editor, instance.id, 'Badge')?.visible).toBe(true)+ expect(childByName(editor, instance.id, 'Dot')?.visible).toBe(true)++ editor.toggleNodeVisibility(badge.id)+ expect(editor.graph.getNode(instance.id)?.componentPropertyAssignments['10:2']).toBe('false')+ expect(childByName(editor, instance.id, 'Dot')?.visible).toBe(false)+ editor.undo.undo()+ expect(childByName(editor, instance.id, 'Badge')?.visible).toBe(true)+ expect(childByName(editor, instance.id, 'Dot')?.visible).toBe(true)++ // A layer showing what its property does not still sets the property when shown again.+ editor.updateNodeWithUndo(badge.id, { visible: false }, 'Hide')+ editor.updateNode(badge.id, { visible: true })+ editor.updateNodeWithUndo(badge.id, { visible: true }, 'Show')+ expect(editor.graph.getNode(instance.id)?.componentPropertyAssignments['10:2']).toBe('true')+ expect(childByName(editor, instance.id, 'Dot')?.visible).toBe(true)++ // Layers the property drives together, selected together, set it once.+ const dot = childByName(editor, instance.id, 'Dot')+ editor.select([badge.id, dot?.id ?? ''])+ editor.toggleVisibility()+ expect(editor.graph.getNode(instance.id)?.componentPropertyAssignments['10:2']).toBe('false')+ expect(childByName(editor, instance.id, 'Badge')?.visible).toBe(false)+ expect(childByName(editor, instance.id, 'Dot')?.visible).toBe(false)+ })+test('applies and undoes text, boolean, and instance-swap properties', () => {const { editor, instance, iconA, iconB } = setupComponentProperties()