Skip to content

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

Clean remaining component leaks: requestRender, graph access, variable queries

Danila Poyarkov513b16983fb1
diff --git a/packages/core/src/editor/create.ts b/packages/core/src/editor/create.ts
index 19ba87bb3..6937179b7 100644
--- a/packages/core/src/editor/create.ts
+++ b/packages/core/src/editor/create.ts
@@ -205,6 +205,11 @@ export function createEditor(options?: EditorOptions) {
undo,
state,
+ // Graph reads
+ getNode: (id: string) => _graph.getNode(id),
+ getChildren: (id: string) => _graph.getChildren(id),
+ getPages: (includeInternal?: boolean) => _graph.getPages(includeInternal),
+
// Lifecycle
requestRender,
requestRepaint,
diff --git a/packages/core/src/editor/nodes.ts b/packages/core/src/editor/nodes.ts
index 270f84b7f..c83b401db 100644
--- a/packages/core/src/editor/nodes.ts
+++ b/packages/core/src/editor/nodes.ts
@@ -117,5 +117,45 @@ export function createNodeActions(ctx: EditorContext) {
})
}
- return { updateNode, updateNodeWithUndo, setLayoutMode }
+ function bindVariable(nodeId: string, path: string, variableId: string) {
+ const node = ctx.graph.getNode(nodeId)
+ if (!node) return
+ const prevVarId = node.boundVariables[path]
+ ctx.graph.bindVariable(nodeId, path, variableId)
+ ctx.undo.push({
+ label: 'Bind variable',
+ forward: () => {
+ ctx.graph.bindVariable(nodeId, path, variableId)
+ ctx.requestRender()
+ },
+ inverse: () => {
+ if (prevVarId) ctx.graph.bindVariable(nodeId, path, prevVarId)
+ else ctx.graph.unbindVariable(nodeId, path)
+ ctx.requestRender()
+ }
+ })
+ ctx.requestRender()
+ }
+
+ function unbindVariable(nodeId: string, path: string) {
+ const node = ctx.graph.getNode(nodeId)
+ if (!node) return
+ const prevVarId = node.boundVariables[path]
+ if (!prevVarId) return
+ ctx.graph.unbindVariable(nodeId, path)
+ ctx.undo.push({
+ label: 'Unbind variable',
+ forward: () => {
+ ctx.graph.unbindVariable(nodeId, path)
+ ctx.requestRender()
+ },
+ inverse: () => {
+ ctx.graph.bindVariable(nodeId, path, prevVarId)
+ ctx.requestRender()
+ }
+ })
+ ctx.requestRender()
+ }
+
+ return { updateNode, updateNodeWithUndo, setLayoutMode, bindVariable, unbindVariable }
}
diff --git a/packages/core/src/editor/pages.ts b/packages/core/src/editor/pages.ts
index c295b4e0f..a8959819f 100644
--- a/packages/core/src/editor/pages.ts
+++ b/packages/core/src/editor/pages.ts
@@ -74,9 +74,14 @@ export function createPageActions(ctx: EditorContext) {
ctx.graph.updateNode(pageId, { name })
}
+ function setPageColor(color: Color) {
+ ctx.state.pageColor = color
+ ctx.requestRender()
+ }
+
function clearPageViewports() {
pageViewports.clear()
}
- return { switchPage, addPage, deletePage, renamePage, clearPageViewports }
+ return { switchPage, addPage, deletePage, renamePage, setPageColor, clearPageViewports }
}
diff --git a/packages/core/src/editor/variables.ts b/packages/core/src/editor/variables.ts
index b6e355f50..0c636fa7e 100644
--- a/packages/core/src/editor/variables.ts
+++ b/packages/core/src/editor/variables.ts
@@ -1,7 +1,40 @@
-import type { Variable, VariableCollection, VariableValue } from '../scene-graph'
+import type { Variable, VariableCollection, VariableType, VariableValue } from '../scene-graph'
import type { EditorContext } from './types'
export function createVariableActions(ctx: EditorContext) {
+ function getVariablesByType(type: VariableType) {
+ return ctx.graph.getVariablesByType(type)
+ }
+
+ function getVariable(id: string) {
+ return ctx.graph.variables.get(id)
+ }
+
+ function resolveColorVariable(id: string) {
+ return ctx.graph.resolveColorVariable(id)
+ }
+
+ function getVariablesForCollection(collectionId: string) {
+ return ctx.graph.getVariablesForCollection(collectionId)
+ }
+
+ function getCollection(id: string) {
+ return ctx.graph.variableCollections.get(id)
+ }
+
+ function getCollections() {
+ return [...ctx.graph.variableCollections.values()]
+ }
+
+ function getCollectionCount() {
+ return ctx.graph.variableCollections.size
+ }
+
+ function getVariableCount() {
+ return ctx.graph.variables.size
+ }
+
+
function renameCollection(id: string, newName: string) {
const collection = ctx.graph.variableCollections.get(id)
if (!collection) return
@@ -142,6 +175,14 @@ export function createVariableActions(ctx: EditorContext) {
}
return {
+ getVariablesByType,
+ getVariable,
+ resolveColorVariable,
+ getVariablesForCollection,
+ getCollection,
+ getCollections,
+ getCollectionCount,
+ getVariableCount,
renameCollection,
addCollection,
removeCollection,
diff --git a/src/components/PagesPanel.vue b/src/components/PagesPanel.vue
index ae3163d2c..aef82280f 100644
--- a/src/components/PagesPanel.vue
+++ b/src/components/PagesPanel.vue
@@ -14,7 +14,7 @@ function isDivider(page: { name: string; childIds: string[] }) {
const pages = computed(() => {
void store.state.sceneVersion
- return store.graph.getPages()
+ return store.getPages()
})
const rename = useInlineRename((id, name) => store.renamePage(id, name))
diff --git a/src/components/VariablesDialog.vue b/src/components/VariablesDialog.vue
index d8baf6da3..e780292fb 100644
--- a/src/components/VariablesDialog.vue
+++ b/src/components/VariablesDialog.vue
@@ -34,7 +34,7 @@ const searchTerm = ref('')
const collections = computed(() => {
void store.state.sceneVersion
- return [...store.graph.variableCollections.values()]
+ return store.getCollections()
})
const activeTab = ref(collections.value[0]?.id ?? '')
@@ -67,7 +67,7 @@ function startRenameCollection(id: string) {
function commitRenameCollection(id: string, input: HTMLInputElement) {
if (editingCollectionId.value !== id) return
const value = input.value.trim()
- const collection = store.graph.variableCollections.get(id)
+ const collection = store.getCollection(id)
if (collection && value && value !== collection.name) {
store.renameCollection(id, value)
}
@@ -75,14 +75,14 @@ function commitRenameCollection(id: string, input: HTMLInputElement) {
}
const activeModes = computed(() => {
- const col = store.graph.variableCollections.get(activeTab.value)
+ const col = store.getCollection(activeTab.value)
return col?.modes ?? []
})
const variables = computed(() => {
void store.state.sceneVersion
if (!activeTab.value) return []
- const all = store.graph.getVariablesForCollection(activeTab.value)
+ const all = store.getVariablesForCollection(activeTab.value)
if (!searchTerm.value) return all
const q = searchTerm.value.toLowerCase()
return all.filter((v) => v.name.toLowerCase().includes(q))
@@ -93,7 +93,7 @@ function formatModeValue(variable: Variable, modeId: string): string {
if (value === undefined) return '—'
if (typeof value === 'object' && 'r' in value) return colorToHexRaw(value as Color)
if (typeof value === 'object' && 'aliasId' in value) {
- const aliased = store.graph.variables.get(value.aliasId)
+ const aliased = store.getVariable(value.aliasId)
return aliased ? `→ ${aliased.name}` : '→ ?'
}
return String(value)
@@ -131,7 +131,7 @@ function commitValueEdit(variable: Variable, modeId: string, newValue: string) {
}
function addVariable() {
- const col = store.graph.variableCollections.get(activeTab.value)
+ const col = store.getCollection(activeTab.value)
if (!col) return
const id = `var:${randomHex(8)}`
diff --git a/src/components/properties/AppearanceSection.vue b/src/components/properties/AppearanceSection.vue
index afdd8c94e..1eb6dd0c3 100644
--- a/src/components/properties/AppearanceSection.vue
+++ b/src/components/properties/AppearanceSection.vue
@@ -38,7 +38,6 @@ function toggleVisibility() {
const n = node.value
if (!n) return
store.updateNodeWithUndo(n.id, { visible: !n.visible }, 'Toggle visibility')
- store.requestRender()
}
}
diff --git a/src/components/properties/FillSection.vue b/src/components/properties/FillSection.vue
index 08631b8e5..614de05db 100644
--- a/src/components/properties/FillSection.vue
+++ b/src/components/properties/FillSection.vue
@@ -23,27 +23,25 @@ import type { Fill, Variable } from '@open-pencil/core'
const store = useEditor()
-const colorVariables = computed(() => store.graph.getVariablesByType('COLOR'))
+const colorVariables = computed(() => store.getVariablesByType('COLOR'))
function getBoundVariable(nodeId: string, index: number): Variable | undefined {
- const n = store.graph.getNode(nodeId)
+ const n = store.getNode(nodeId)
if (!n) return undefined
const varId = n.boundVariables[`fills/${index}/color`]
- return varId ? store.graph.variables.get(varId) : undefined
+ return varId ? store.getVariable(varId) : undefined
}
-function bindVariable(nodeId: string, index: number, variableId: string) {
- store.graph.bindVariable(nodeId, `fills/${index}/color`, variableId)
- store.requestRender()
+function bindFillVariable(nodeId: string, index: number, variableId: string) {
+ store.bindVariable(nodeId, `fills/${index}/color`, variableId)
}
-function unbindVariable(nodeId: string, index: number) {
- store.graph.unbindVariable(nodeId, `fills/${index}/color`)
- store.requestRender()
+function unbindFillVariable(nodeId: string, index: number) {
+ store.unbindVariable(nodeId, `fills/${index}/color`)
}
function resolvedSwatchStyle(variable: Variable): string {
- const color = store.graph.resolveColorVariable(variable.id)
+ const color = store.resolveColorVariable(variable.id)
if (!color) return 'background: #000'
return `background: ${colorToCSS(color)}`
}
@@ -93,7 +91,7 @@ const filteredVariables = computed(() => {
data-test-id="fill-unbind-variable"
class="cursor-pointer border-none bg-transparent p-0 text-violet-400 hover:text-surface"
title="Detach variable"
- @click="unbindVariable(activeNode.id, i)"
+ @click="unbindFillVariable(activeNode.id, i)"
>
<icon-lucide-unlink class="size-3" />
</button>
@@ -139,7 +137,7 @@ const filteredVariables = computed(() => {
>
<ComboboxRoot
@update:model-value="
- activeNode && bindVariable(activeNode.id, i, ($event as Variable).id)
+ activeNode && bindFillVariable(activeNode.id, i, ($event as Variable).id)
"
>
<ComboboxInput
diff --git a/src/components/properties/LayoutSection.vue b/src/components/properties/LayoutSection.vue
index 2814fdee1..2397c9810 100644
--- a/src/components/properties/LayoutSection.vue
+++ b/src/components/properties/LayoutSection.vue
@@ -20,7 +20,7 @@ const showIndividualPadding = ref(false)
const isInAutoLayout = computed(() => {
const n = node.value
if (!n?.parentId) return false
- const parent = store.graph.getNode(n.parentId)
+ const parent = store.getNode(n.parentId)
return parent ? parent.layoutMode !== 'NONE' : false
})
diff --git a/src/components/properties/PageSection.vue b/src/components/properties/PageSection.vue
index ca7ef34ef..21ddcb74d 100644
--- a/src/components/properties/PageSection.vue
+++ b/src/components/properties/PageSection.vue
@@ -7,8 +7,7 @@ import type { Color } from '@open-pencil/core'
const store = useEditor()
function updateColor(color: Color) {
- store.state.pageColor = color
- store.requestRender()
+ store.setPageColor(color)
}
</script>
diff --git a/src/components/properties/TypographySection.vue b/src/components/properties/TypographySection.vue
index 1efcd9758..b571426b4 100644
--- a/src/components/properties/TypographySection.vue
+++ b/src/components/properties/TypographySection.vue
@@ -40,13 +40,11 @@ async function selectWeight(weight: number) {
const label = WEIGHTS.find((w) => w.value === weight)?.label ?? 'Regular'
await loadFont(node.value.fontFamily, label)
store.updateNodeWithUndo(node.value.id, { fontWeight: weight }, 'Change font weight')
- store.requestRender()
}
function setAlign(align: TextAlign) {
if (!node.value) return
store.updateNodeWithUndo(node.value.id, { textAlignHorizontal: align }, 'Change text alignment')
- store.requestRender()
}
function toggleBold() {
@@ -57,7 +55,6 @@ function toggleBold() {
function toggleItalic() {
if (!node.value) return
store.updateNodeWithUndo(node.value.id, { italic: !node.value.italic }, 'Toggle italic')
- store.requestRender()
}
function toggleDecoration(deco: 'UNDERLINE' | 'STRIKETHROUGH') {
@@ -68,7 +65,6 @@ function toggleDecoration(deco: 'UNDERLINE' | 'STRIKETHROUGH') {
{ textDecoration: current === deco ? 'NONE' : deco },
`Toggle ${deco.toLowerCase()}`
)
- store.requestRender()
}
const activeFormatting = computed(() => {
diff --git a/src/components/properties/VariablesSection.vue b/src/components/properties/VariablesSection.vue
index b5f95be62..c51fbd1ba 100644
--- a/src/components/properties/VariablesSection.vue
+++ b/src/components/properties/VariablesSection.vue
@@ -8,12 +8,12 @@ const emit = defineEmits<{ openDialog: [] }>()
const collectionCount = computed(() => {
void store.state.sceneVersion
- return store.graph.variableCollections.size
+ return store.getCollectionCount()
})
const variableCount = computed(() => {
void store.state.sceneVersion
- return store.graph.variables.size
+ return store.getVariableCount()
})
</script>