Skip to content

AI-native design editor. Open-source Figma alternative.

refactor(scene-graph): align component property modules

Danila Poyarkovdb9049dac9ba
diff --git a/packages/core/src/editor/components/properties.ts b/packages/core/src/editor/components/properties.ts
index 40c37e044..928d453db 100644
--- a/packages/core/src/editor/components/properties.ts
+++ b/packages/core/src/editor/components/properties.ts
@@ -6,19 +6,13 @@ import {
} from '@open-pencil/scene-graph'
import type {
ComponentPropertyDefinition,
- ComponentPropertyReferenceField,
+ ComponentPropertyTarget,
SceneNode
} from '@open-pencil/scene-graph'
import { assertNodeEditable } from '#core/editor/capabilities'
import type { EditorContext } from '#core/editor/types'
-interface PropertyTarget {
- node: SceneNode
- field: ComponentPropertyReferenceField
- source: SceneNode
-}
-
function definitionsForInstance(
ctx: Pick<EditorContext, 'graph'>,
instance: SceneNode
@@ -30,16 +24,15 @@ function propertyTarget(
ctx: Pick<EditorContext, 'graph'>,
instance: SceneNode,
propertyId: string
-): PropertyTarget | null {
- const target = findComponentPropertyTarget(ctx.graph, instance, propertyId)
- return target
+): ComponentPropertyTarget | null {
+ return findComponentPropertyTarget(ctx.graph, instance, propertyId)
}
function swapTargetId(ctx: Pick<EditorContext, 'graph'>, value: string): string | null {
return resolveComponentPropertyValue(ctx.graph, value)?.id ?? null
}
-function targetValue(target: PropertyTarget | null): string {
+function targetValue(target: ComponentPropertyTarget | null): string {
if (!target) return ''
if (target.field === 'TEXT') return target.node.text
if (target.field === 'VISIBLE') return String(target.node.visible)
@@ -50,8 +43,9 @@ function applyPropertyValue(
instanceId: string,
definition: ComponentPropertyDefinition,
value: string
-): void {
- applyComponentPropertyValue(ctx.graph, instanceId, definition, value)
+): boolean {
+ const result = applyComponentPropertyValue(ctx.graph, instanceId, definition, value)
+ return definition.type !== 'INSTANCE_SWAP' || result !== null
}
export function reapplyInstanceComponentProperties(
@@ -114,7 +108,7 @@ export function createComponentPropertyActions(
? (swapTargetId(ctx, assignedValue) ?? assignedValue)
: targetValue(target)
- applyPropertyValue(ctx, instanceId, definition, value)
+ if (!applyPropertyValue(ctx, instanceId, definition, value)) return
ctx.undo.push({
label: `Change ${definition.name}`,
forward: () => {
diff --git a/packages/scene-graph/src/component-properties.ts b/packages/scene-graph/src/components/properties.ts
similarity index 97%
rename from packages/scene-graph/src/component-properties.ts
rename to packages/scene-graph/src/components/properties.ts
index 233ab9b73..e56238ad8 100644
--- a/packages/scene-graph/src/component-properties.ts
+++ b/packages/scene-graph/src/components/properties.ts
@@ -1,9 +1,9 @@
-import type { SceneGraph } from './index'
+import type { SceneGraph } from '../index'
import type {
ComponentPropertyDefinition,
ComponentPropertyReferenceField,
SceneNode
-} from './types'
+} from '../types'
export interface ComponentPropertyTarget {
node: SceneNode
@@ -134,7 +134,7 @@ export function applyComponentPropertyValue(
},
overrides
})
- return target
+ return definition.type === 'INSTANCE_SWAP' ? target : instance
}
export function removeComponentProperty(
diff --git a/packages/scene-graph/src/index.ts b/packages/scene-graph/src/index.ts
index 391a0bc9c..0657626fb 100644
--- a/packages/scene-graph/src/index.ts
+++ b/packages/scene-graph/src/index.ts
@@ -1,6 +1,6 @@
/* eslint-disable max-lines -- SceneGraph exposes a stable facade over domain modules */
export * from './images'
-export * from './component-properties'
+export * from './components/properties'
export * from './copy'
export { copyInstanceComponentProps } from './instances'
export * from './snap'
diff --git a/tests/engine/scene-graph/component-properties.test.ts b/tests/engine/scene-graph/components/properties.test.ts
similarity index 87%
rename from tests/engine/scene-graph/component-properties.test.ts
rename to tests/engine/scene-graph/components/properties.test.ts
index 28ab00ff3..be57af5ce 100644
--- a/tests/engine/scene-graph/component-properties.test.ts
+++ b/tests/engine/scene-graph/components/properties.test.ts
@@ -77,18 +77,26 @@ describe('component properties', () => {
]
})
graph.updateNode(slot.id, {
- componentPropertyReferences: [
- { propertyId: 'prop:swap', field: 'INSTANCE_SWAP' },
- { propertyId: 'prop:text', field: 'TEXT' },
- { propertyId: 'prop:visible', field: 'VISIBLE' }
- ]
+ componentPropertyReferences: [{ propertyId: 'prop:swap', field: 'INSTANCE_SWAP' }]
})
- const instance = graph.createInstance(host.id, page.id)
- if (!instance) throw new Error('host instance creation failed')
const text = graph.createNode('TEXT', slot.id)
graph.updateNode(text.id, {
+ text: '',
componentPropertyReferences: [{ propertyId: 'prop:text', field: 'TEXT' }]
})
+ const visibility = graph.createNode('FRAME', slot.id)
+ graph.updateNode(visibility.id, {
+ componentPropertyReferences: [{ propertyId: 'prop:visible', field: 'VISIBLE' }]
+ })
+ const instance = graph.createInstance(host.id, page.id)
+ if (!instance) throw new Error('host instance creation failed')
+
+ const textDefinition = host.componentPropertyDefinitions[1]
+ applyComponentPropertyValue(graph, instance.id, textDefinition, 'Save')
+ expect(findComponentPropertyTarget(graph, instance, 'prop:text')?.node.text).toBe('Save')
+ const visibleDefinition = host.componentPropertyDefinitions[2]
+ applyComponentPropertyValue(graph, instance.id, visibleDefinition, 'false')
+ expect(findComponentPropertyTarget(graph, instance, 'prop:visible')?.node.visible).toBe(false)
const swapDefinition = host.componentPropertyDefinitions[0]
const swapped = applyComponentPropertyValue(graph, instance.id, swapDefinition, alternate.id)