Skip to content

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

fix(editor): guard unsupported boolean sources

Danila Poyarkov28829a56a9e9
diff --git a/packages/core/src/canvas/boolean.ts b/packages/core/src/canvas/boolean.ts
index c9d5ff38d..8077648d8 100644
--- a/packages/core/src/canvas/boolean.ts
+++ b/packages/core/src/canvas/boolean.ts
@@ -37,6 +37,10 @@ export function nodePathTransform(r: SkiaRenderer, child: SceneNode): number[] {
return r.ck.Matrix.multiply(...transforms)
}
+export function canMakeBooleanSourcePath(node: SceneNode): boolean {
+ return node.type !== 'TEXT' && node.type !== 'SECTION' && node.type !== 'COMPONENT_SET'
+}
+
function lineStrokePath(r: SkiaRenderer, node: SceneNode): Path | null {
const path = new r.ck.Path()
path.moveTo(0, 0)
@@ -51,7 +55,7 @@ export function makeBooleanSourcePath(
graph: SceneGraph
): Path | null {
if (node.type === 'BOOLEAN_OPERATION') return makeBooleanOperationPath(r, node, graph)
- if (node.type === 'TEXT' || node.type === 'SECTION' || node.type === 'COMPONENT_SET') return null
+ if (!canMakeBooleanSourcePath(node)) return null
if (node.type === 'LINE') return lineStrokePath(r, node)
if (node.type === 'ELLIPSE' && node.arcData) return makeArcPath(r, node)
diff --git a/packages/core/src/editor/structure/boolean.ts b/packages/core/src/editor/structure/boolean.ts
index b96e065ee..a07946b43 100644
--- a/packages/core/src/editor/structure/boolean.ts
+++ b/packages/core/src/editor/structure/boolean.ts
@@ -1,7 +1,9 @@
+import { canMakeBooleanSourcePath } from '#core/canvas/boolean'
import { restoreSubtree, snapshotSubtree } from '#core/editor/clipboard/subtree-history'
import type { EditorContext } from '#core/editor/types'
import { computeAbsoluteBounds } from '#core/geometry'
import type { SceneNode } from '#core/scene-graph'
+import { copyFills, copyStrokes } from '#core/scene-graph/copy'
import { selectedNodesInSharedParent } from './selection'
@@ -16,6 +18,7 @@ export function booleanOperationSelected(
const selection = selectedNodesInSharedParent(ctx, selectedNodes)
if (!selection || selection.topLevel.length < 2) return null
const { topLevel, parentId, parent } = selection
+ if (topLevel.some((node) => !canMakeBooleanSourcePath(node))) return null
const prevSelection = new Set(ctx.state.selectedIds)
const childIds = topLevel.map((node) => node.id)
@@ -31,6 +34,8 @@ export function booleanOperationSelected(
y: bounds.y - parentAbs.y,
width: bounds.width,
height: bounds.height,
+ fills: copyFills(topLevel[0].fills),
+ strokes: copyStrokes(topLevel[0].strokes),
booleanOperation: operation
})
const booleanId = booleanNode.id
diff --git a/packages/core/src/editor/structure/flatten.ts b/packages/core/src/editor/structure/flatten.ts
index 4d642bd04..d6b026f21 100644
--- a/packages/core/src/editor/structure/flatten.ts
+++ b/packages/core/src/editor/structure/flatten.ts
@@ -1,4 +1,4 @@
-import { makeBooleanSourcePath, nodePathTransform } from '#core/canvas/boolean'
+import { canMakeBooleanSourcePath, makeBooleanSourcePath, nodePathTransform } from '#core/canvas/boolean'
import { restoreSubtree, snapshotSubtree } from '#core/editor/clipboard/subtree-history'
import type { EditorContext } from '#core/editor/types'
import { parseSVGPath } from '#core/io/formats/svg/parse-path'
@@ -14,6 +14,7 @@ export function flattenSelected(ctx: EditorContext, selectedNodes: SceneNode[])
const selection = selectedNodesInSharedParent(ctx, selectedNodes)
if (!selection) return null
const { topLevel, parentId, parent } = selection
+ if (topLevel.some((node) => !canMakeBooleanSourcePath(node))) return null
const childIds = topLevel.map((node) => node.id)
const childSnapshots = childIds.map((id) => ({ id, subtree: snapshotSubtree(ctx.graph, id) }))
@@ -23,7 +24,10 @@ export function flattenSelected(ctx: EditorContext, selectedNodes: SceneNode[])
for (const node of topLevel) {
const childPath = makeBooleanSourcePath(renderer, node, ctx.graph)
- if (!childPath) continue
+ if (!childPath) {
+ path.delete()
+ return null
+ }
childPath.transform(nodePathTransform(renderer, node))
path.addPath(childPath)
childPath.delete()
diff --git a/tests/engine/editor/structure/boolean.test.ts b/tests/engine/editor/structure/boolean.test.ts
index a759e1ac1..5d9c2b6e5 100644
--- a/tests/engine/editor/structure/boolean.test.ts
+++ b/tests/engine/editor/structure/boolean.test.ts
@@ -16,11 +16,26 @@ describe('booleanOperationSelected', () => {
const booleanNode = editor.graph.getNode(booleanId)
expect(booleanNode?.type).toBe('BOOLEAN_OPERATION')
expect(booleanNode?.booleanOperation).toBe('UNION')
+ expect(booleanNode?.fills).toEqual(first.fills)
expect(booleanNode?.childIds).toEqual([first.id, second.id])
expect(editor.graph.getNode(first.id)?.parentId).toBe(booleanId)
expect(editor.graph.getNode(second.id)?.parentId).toBe(booleanId)
})
+ test('does not wrap unsupported text nodes', () => {
+ const editor = createEditor()
+ const pageId = editor.state.currentPageId
+ const first = editor.graph.createNode('TEXT', pageId, { text: 'Nope' })
+ const second = editor.graph.createNode('RECTANGLE', pageId)
+
+ editor.select([first.id, second.id])
+ const result = editor.booleanOperationSelected('UNION')
+
+ expect(result).toBeNull()
+ expect(editor.graph.getNode(pageId)?.childIds).toEqual([first.id, second.id])
+ expect(editor.state.selectedIds).toEqual(new Set([first.id, second.id]))
+ })
+
test('undo and redo preserve parent order and selection', () => {
const editor = createEditor()
const pageId = editor.state.currentPageId
diff --git a/tests/engine/editor/structure/flatten.test.ts b/tests/engine/editor/structure/flatten.test.ts
index eca84a4d7..bc7394633 100644
--- a/tests/engine/editor/structure/flatten.test.ts
+++ b/tests/engine/editor/structure/flatten.test.ts
@@ -37,6 +37,21 @@ describe('flattenSelected', () => {
surface.delete()
})
+ test('does not flatten unsupported text nodes', async () => {
+ const { editor, surface } = await createEditorWithRenderer()
+ const pageId = editor.state.currentPageId
+ const text = editor.graph.createNode('TEXT', pageId, { text: 'Nope' })
+ const rect = editor.graph.createNode('RECTANGLE', pageId)
+
+ editor.select([text.id, rect.id])
+ const result = editor.flattenSelected()
+
+ expect(result).toBeNull()
+ expect(editor.graph.getNode(pageId)?.childIds).toEqual([text.id, rect.id])
+ expect(editor.state.selectedIds).toEqual(new Set([text.id, rect.id]))
+ surface.delete()
+ })
+
test('undo and redo restore flattened children and vector', async () => {
const { editor, surface } = await createEditorWithRenderer()
const pageId = editor.state.currentPageId