Skip to content

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

feat(editor): add boolean operation commands

Danila Poyarkov83c24925e84d
diff --git a/desktop/generated/menu.json b/desktop/generated/menu.json
index f46b53973..b16890255 100644
--- a/desktop/generated/menu.json
+++ b/desktop/generated/menu.json
@@ -194,6 +194,25 @@
{
"type": "separator"
},
+ {
+ "id": "selection.booleanUnion",
+ "label": "Union selection"
+ },
+ {
+ "id": "selection.booleanSubtract",
+ "label": "Subtract selection"
+ },
+ {
+ "id": "selection.booleanIntersect",
+ "label": "Intersect selection"
+ },
+ {
+ "id": "selection.booleanExclude",
+ "label": "Exclude selection"
+ },
+ {
+ "type": "separator"
+ },
{
"id": "selection.createComponent",
"label": "Create Component"
diff --git a/packages/core/src/editor/bridges/structure.ts b/packages/core/src/editor/bridges/structure.ts
index 426f09592..ab4a5b09b 100644
--- a/packages/core/src/editor/bridges/structure.ts
+++ b/packages/core/src/editor/bridges/structure.ts
@@ -9,6 +9,8 @@ export function createStructureBridge(structure: StructureActions, selection: Se
wrapInAutoLayout: () => structure.wrapInAutoLayout(selection.getSelectedNodes()),
groupSelected: () => structure.groupSelected(selection.getSelectedNodes()),
frameSelection: () => structure.frameSelection(selection.getSelectedNodes()),
+ booleanOperationSelected: (operation: 'UNION' | 'SUBTRACT' | 'INTERSECT' | 'EXCLUDE') =>
+ structure.booleanOperationSelected(selection.getSelectedNodes(), operation),
ungroupSelected: () => structure.ungroupSelected(selection.getSelectedNode())
}
}
diff --git a/packages/core/src/editor/structure.ts b/packages/core/src/editor/structure.ts
index 8cbea2e9d..f8a37f6f5 100644
--- a/packages/core/src/editor/structure.ts
+++ b/packages/core/src/editor/structure.ts
@@ -1,6 +1,10 @@
import { DEFAULT_FRAME_FILL } from '#core/constants'
import type { NodeType, SceneNode } from '#core/scene-graph'
+import {
+ booleanOperationSelected as booleanOperationSelectedImpl,
+ type BooleanOperation
+} from './structure/boolean'
import { wrapInAutoLayout as wrapInAutoLayoutImpl } from './structure/auto-layout-wrap'
import { wrapSelectionInContainer as wrapSelectionInContainerImpl } from './structure/container-wrap'
import { ungroupSelected as ungroupImpl } from './structure/group'
@@ -56,6 +60,10 @@ export function createStructureActions(ctx: EditorContext) {
})
}
+ function booleanOperationSelected(selectedNodes: SceneNode[], operation: BooleanOperation) {
+ return booleanOperationSelectedImpl(ctx, isTopLevel, selectedNodes, operation)
+ }
+
function ungroupSelected(selectedNode: SceneNode | undefined) {
ungroupImpl(ctx, selectedNode)
}
@@ -85,6 +93,7 @@ export function createStructureActions(ctx: EditorContext) {
wrapInAutoLayout,
groupSelected,
frameSelection,
+ booleanOperationSelected,
ungroupSelected,
...stateActions,
moveToPage,
diff --git a/packages/core/src/editor/structure/boolean.ts b/packages/core/src/editor/structure/boolean.ts
new file mode 100644
index 000000000..71cd56e13
--- /dev/null
+++ b/packages/core/src/editor/structure/boolean.ts
@@ -0,0 +1,94 @@
+import type { EditorContext } from '#core/editor/types'
+import { computeAbsoluteBounds } from '#core/geometry'
+import type { SceneNode } from '#core/scene-graph'
+
+import { restoreSubtree, snapshotSubtree } from '#core/editor/clipboard/subtree-history'
+
+export type BooleanOperation = 'UNION' | 'SUBTRACT' | 'INTERSECT' | 'EXCLUDE'
+
+export function booleanOperationSelected(
+ ctx: EditorContext,
+ isTopLevel: (parentId: string | null) => boolean,
+ selectedNodes: SceneNode[],
+ operation: BooleanOperation
+) {
+ const selectedSet = new Set(selectedNodes.map((node) => node.id))
+ const topLevel = selectedNodes.filter((node) => !node.parentId || !selectedSet.has(node.parentId))
+ if (topLevel.length < 2 || topLevel.some((node) => node.locked)) return null
+
+ const parentId = topLevel[0].parentId ?? ctx.state.currentPageId
+ if (!topLevel.every((node) => (node.parentId ?? ctx.state.currentPageId) === parentId)) return null
+
+ const parent = ctx.graph.getNode(parentId)
+ if (!parent) return null
+
+ const prevSelection = new Set(ctx.state.selectedIds)
+ const childIds = topLevel.map((node) => node.id)
+ const childSnapshots = childIds.map((id) => ({ id, subtree: snapshotSubtree(ctx.graph, id) }))
+ const origPositions = topLevel.map((node) => ({ id: node.id, x: node.x, y: node.y }))
+ const firstIndex = Math.min(...childIds.map((id) => parent.childIds.indexOf(id)))
+ const parentAbs = isTopLevel(parentId) ? { x: 0, y: 0 } : ctx.graph.getAbsolutePosition(parentId)
+ const bounds = computeAbsoluteBounds(topLevel, (id) => ctx.graph.getAbsolutePosition(id))
+
+ const booleanNode = ctx.graph.createNode('BOOLEAN_OPERATION', parentId, {
+ name: operationLabel(operation),
+ x: bounds.x - parentAbs.x,
+ y: bounds.y - parentAbs.y,
+ width: bounds.width,
+ height: bounds.height,
+ booleanOperation: operation
+ })
+ const booleanId = booleanNode.id
+ ctx.graph.insertChildAt(booleanId, parentId, firstIndex)
+ for (const id of childIds) ctx.graph.reparentNode(id, booleanId)
+ ctx.setSelectedIds(new Set([booleanId]))
+
+ ctx.undo.push({
+ label: operationLabel(operation),
+ forward: () => {
+ const restored = ctx.graph.createNode('BOOLEAN_OPERATION', parentId, {
+ ...booleanNode,
+ childIds: [],
+ id: booleanId
+ })
+ ctx.graph.insertChildAt(restored.id, parentId, firstIndex)
+ for (const id of childIds) ctx.graph.reparentNode(id, restored.id)
+ ctx.setSelectedIds(new Set([restored.id]))
+ },
+ inverse: () => {
+ for (const { id, subtree } of childSnapshots) {
+ const root = subtree.get(id)
+ if (!root) continue
+ if (!ctx.graph.getNode(id)) restoreSubtree(ctx.graph, root, parentId, subtree)
+ else ctx.graph.reparentNode(id, parentId)
+ }
+ for (let i = 0; i < childIds.length; i++) {
+ const id = childIds[i]
+ const pos = origPositions[i]
+ ctx.graph.insertChildAt(id, parentId, firstIndex + i)
+ ctx.graph.updateNode(id, { x: pos.x, y: pos.y })
+ }
+ ctx.graph.deleteNode(booleanId)
+ ctx.setSelectedIds(prevSelection)
+ }
+ })
+
+ return booleanId
+}
+
+function operationLabel(operation: BooleanOperation) {
+ switch (operation) {
+ case 'UNION':
+ return 'Union'
+ case 'SUBTRACT':
+ return 'Subtract'
+ case 'INTERSECT':
+ return 'Intersect'
+ case 'EXCLUDE':
+ return 'Exclude'
+ default: {
+ const exhaustive: never = operation
+ return exhaustive
+ }
+ }
+}
diff --git a/packages/vue/src/editor/commands/registry.ts b/packages/vue/src/editor/commands/registry.ts
index 504f4a608..b73443525 100644
--- a/packages/vue/src/editor/commands/registry.ts
+++ b/packages/vue/src/editor/commands/registry.ts
@@ -63,6 +63,10 @@ export const EDITOR_COMMAND_METADATA = {
keybinding: 'Shift+KeyV',
contextTestId: 'context-flip-vertical'
},
+ 'selection.booleanUnion': { contextTestId: 'context-boolean-union' },
+ 'selection.booleanSubtract': { contextTestId: 'context-boolean-subtract' },
+ 'selection.booleanIntersect': { contextTestId: 'context-boolean-intersect' },
+ 'selection.booleanExclude': { contextTestId: 'context-boolean-exclude' },
'selection.moveToPage': {},
'view.zoom100': { keybinding: '$mod+Digit0' },
'view.zoomFit': { keybinding: ['$mod+Digit1', 'Shift+Digit1'] },
diff --git a/packages/vue/src/editor/commands/selection.ts b/packages/vue/src/editor/commands/selection.ts
index 32abbb690..58c803f09 100644
--- a/packages/vue/src/editor/commands/selection.ts
+++ b/packages/vue/src/editor/commands/selection.ts
@@ -159,6 +159,38 @@ export function createSelectionCommands({
enabled: capabilities.canFlip,
run: () => editor.flipNodes([...selection.selectedIds.value], 'vertical')
},
+ 'selection.booleanUnion': {
+ id: 'selection.booleanUnion',
+ get label() {
+ return t.value.unionSelection
+ },
+ enabled: capabilities.canBooleanOperation,
+ run: () => editor.booleanOperationSelected('UNION')
+ },
+ 'selection.booleanSubtract': {
+ id: 'selection.booleanSubtract',
+ get label() {
+ return t.value.subtractSelection
+ },
+ enabled: capabilities.canBooleanOperation,
+ run: () => editor.booleanOperationSelected('SUBTRACT')
+ },
+ 'selection.booleanIntersect': {
+ id: 'selection.booleanIntersect',
+ get label() {
+ return t.value.intersectSelection
+ },
+ enabled: capabilities.canBooleanOperation,
+ run: () => editor.booleanOperationSelected('INTERSECT')
+ },
+ 'selection.booleanExclude': {
+ id: 'selection.booleanExclude',
+ get label() {
+ return t.value.excludeSelection
+ },
+ enabled: capabilities.canBooleanOperation,
+ run: () => editor.booleanOperationSelected('EXCLUDE')
+ },
'selection.moveToPage': {
id: 'selection.moveToPage',
get label() {
diff --git a/packages/vue/src/editor/commands/types.ts b/packages/vue/src/editor/commands/types.ts
index 3167c4737..684308c9d 100644
--- a/packages/vue/src/editor/commands/types.ts
+++ b/packages/vue/src/editor/commands/types.ts
@@ -21,6 +21,10 @@ export type EditorCommandId =
| 'selection.toggleLock'
| 'selection.flipHorizontal'
| 'selection.flipVertical'
+ | 'selection.booleanUnion'
+ | 'selection.booleanSubtract'
+ | 'selection.booleanIntersect'
+ | 'selection.booleanExclude'
| 'selection.moveToPage'
| 'view.zoom100'
| 'view.zoomFit'
diff --git a/packages/vue/src/editor/menu-model/canvas.ts b/packages/vue/src/editor/menu-model/canvas.ts
index adfedb579..68c4a5914 100644
--- a/packages/vue/src/editor/menu-model/canvas.ts
+++ b/packages/vue/src/editor/menu-model/canvas.ts
@@ -13,10 +13,12 @@ type CanvasMenuCommand =
| 'selection.instanceActions'
| 'selection.ungroupWhenGroup'
| 'selection.moveToPageWhenAvailable'
+ | 'selection.booleanOperationsWhenAvailable'
type CanvasMenuGroup = readonly CanvasMenuCommand[]
type CanvasMenuTranslations = {
+ booleanOperations: string
moveToPage: string
}
@@ -32,6 +34,7 @@ const CANVAS_MENU_GROUPS = [
['selection.duplicate', 'selection.delete'],
['selection.moveToPageWhenAvailable', 'selection.bringToFront', 'selection.sendToBack'],
['selection.group', 'selection.frameSelection', 'selection.ungroupWhenGroup', 'selection.wrapInAutoLayout'],
+ ['selection.booleanOperationsWhenAvailable'],
['selection.componentAction', 'selection.componentSetAction', 'selection.instanceActions'],
['selection.toggleVisibility', 'selection.toggleLock'],
['selection.flipHorizontal', 'selection.flipVertical']
@@ -50,6 +53,17 @@ function moveToPageItem({ otherPages, moveSelectionToPage, selection, t }: Canva
return [{ label: t.moveToPage, sub } satisfies MenuActionNode]
}
+function booleanOperationItems({ commandMenuItem, selection, t }: CanvasMenuOptions): MenuEntry[] {
+ if (!selection.hasSelection.value) return []
+ const commands: EditorCommandId[] = [
+ 'selection.booleanUnion',
+ 'selection.booleanSubtract',
+ 'selection.booleanIntersect',
+ 'selection.booleanExclude'
+ ]
+ return [{ label: t.booleanOperations, sub: commands.map((command) => commandMenuItem(command)) }]
+}
+
function componentItems({ commandMenuItem, selection }: CanvasMenuOptions): MenuEntry[] {
return [
selection.isComponent.value
@@ -74,6 +88,8 @@ function conditionalCommand(command: CanvasMenuCommand, options: CanvasMenuOptio
switch (command) {
case 'selection.moveToPageWhenAvailable':
return moveToPageItem(options)
+ case 'selection.booleanOperationsWhenAvailable':
+ return booleanOperationItems(options)
case 'selection.componentAction':
return componentItems(options)
case 'selection.componentSetAction':
diff --git a/packages/vue/src/editor/menu-model/types.ts b/packages/vue/src/editor/menu-model/types.ts
index 3f14a49bd..ce2fdc294 100644
--- a/packages/vue/src/editor/menu-model/types.ts
+++ b/packages/vue/src/editor/menu-model/types.ts
@@ -1,3 +1,5 @@
+import type { Component } from 'vue'
+
import type { EditorCommandId } from '#vue/editor/commands/types'
import type { TestId } from '#vue/testing/test-id'
@@ -5,6 +7,7 @@ export interface MenuActionNode {
separator?: false
id?: EditorCommandId
label: string
+ icon?: Component
shortcut?: string
action?: () => void
disabled?: boolean
diff --git a/packages/vue/src/editor/selection-capabilities/use.ts b/packages/vue/src/editor/selection-capabilities/use.ts
index 539a7fd88..b16750cd9 100644
--- a/packages/vue/src/editor/selection-capabilities/use.ts
+++ b/packages/vue/src/editor/selection-capabilities/use.ts
@@ -35,6 +35,7 @@ export function useSelectionCapabilities() {
canToggleVisibility: computed(() => hasSelection.value),
canToggleLock: computed(() => hasSelection.value),
canFlip: computed(() => hasSelection.value),
+ canBooleanOperation: computed(() => selectedCount.value >= 2),
canGoToMainComponent: computed(() => selection.isInstance.value),
canCreateInstance: computed(() => selectedNode.value?.type === 'COMPONENT'),
canMoveToPage: useSceneComputed(() => hasSelection.value && editor.graph.getPages().length > 1),
diff --git a/packages/vue/src/i18n/messages.ts b/packages/vue/src/i18n/messages.ts
index 6cf1b2afe..4b1a24be9 100644
--- a/packages/vue/src/i18n/messages.ts
+++ b/packages/vue/src/i18n/messages.ts
@@ -70,7 +70,8 @@ export const menuMessages = i18n('menu', {
copyAsPNG: 'Copy as PNG',
copyAsJSX: 'Copy as JSX',
copyNodeId: 'Copy node ID',
- copyXPath: 'Copy XPath'
+ copyXPath: 'Copy XPath',
+ booleanOperations: 'Boolean operations'
})
export const commandMessages = i18n('commands', {
@@ -93,6 +94,11 @@ export const commandMessages = i18n('commands', {
sendToBack: 'Send to back',
showHide: 'Show/Hide',
lockUnlock: 'Lock/Unlock',
+ unionSelection: 'Union selection',
+ subtractSelection: 'Subtract selection',
+ intersectSelection: 'Intersect selection',
+ excludeSelection: 'Exclude selection',
+ booleanOperations: 'Boolean operations',
flipHorizontal: 'Flip horizontal',
flipVertical: 'Flip vertical',
moveToPage: 'Move to page',
diff --git a/packages/vue/src/locales/de.json b/packages/vue/src/locales/de.json
index f736eb18c..d7ff09dd4 100644
--- a/packages/vue/src/locales/de.json
+++ b/packages/vue/src/locales/de.json
@@ -40,7 +40,8 @@
"copyAsJSX": "Als JSX kopieren",
"copyNodeId": "Knoten-ID kopieren",
"copyXPath": "XPath kopieren",
- "checkUpdates": "Nach Updates suchen…"
+ "checkUpdates": "Nach Updates suchen…",
+ "booleanOperations": "Boolean operations"
},
"commands": {
"undo": "Rückgängig",
@@ -66,6 +67,11 @@
"frameSelection": "Frame selection",
"showHide": "Sichtbarkeit",
"lockUnlock": "Sperren",
+ "unionSelection": "Union selection",
+ "subtractSelection": "Subtract selection",
+ "intersectSelection": "Intersect selection",
+ "excludeSelection": "Exclude selection",
+ "booleanOperations": "Boolean operations",
"flipHorizontal": "Horizontal spiegeln",
"flipVertical": "Vertikal spiegeln"
},
diff --git a/packages/vue/src/locales/es.json b/packages/vue/src/locales/es.json
index 8c7668bbf..f5101f091 100644
--- a/packages/vue/src/locales/es.json
+++ b/packages/vue/src/locales/es.json
@@ -40,7 +40,8 @@
"copyAsJSX": "Copiar como JSX",
"copyNodeId": "Copiar ID del nodo",
"copyXPath": "Copiar XPath",
- "checkUpdates": "Buscar actualizaciones…"
+ "checkUpdates": "Buscar actualizaciones…",
+ "booleanOperations": "Boolean operations"
},
"commands": {
"undo": "Deshacer",
@@ -66,6 +67,11 @@
"frameSelection": "Frame selection",
"showHide": "Visibilidad",
"lockUnlock": "Bloqueo",
+ "unionSelection": "Union selection",
+ "subtractSelection": "Subtract selection",
+ "intersectSelection": "Intersect selection",
+ "excludeSelection": "Exclude selection",
+ "booleanOperations": "Boolean operations",
"flipHorizontal": "Voltear horizontalmente",
"flipVertical": "Voltear verticalmente"
},
diff --git a/packages/vue/src/locales/fr.json b/packages/vue/src/locales/fr.json
index 2e7a0cc1f..edca09929 100644
--- a/packages/vue/src/locales/fr.json
+++ b/packages/vue/src/locales/fr.json
@@ -40,7 +40,8 @@
"copyAsJSX": "Copier en JSX",
"copyNodeId": "Copier l'ID du nœud",
"copyXPath": "Copier le XPath",
- "checkUpdates": "Rechercher des mises à jour…"
+ "checkUpdates": "Rechercher des mises à jour…",
+ "booleanOperations": "Boolean operations"
},
"commands": {
"undo": "Annuler",
@@ -66,6 +67,11 @@
"frameSelection": "Frame selection",
"showHide": "Visibilité",
"lockUnlock": "Verrouillage",
+ "unionSelection": "Union selection",
+ "subtractSelection": "Subtract selection",
+ "intersectSelection": "Intersect selection",
+ "excludeSelection": "Exclude selection",
+ "booleanOperations": "Boolean operations",
"flipHorizontal": "Retourner horizontalement",
"flipVertical": "Retourner verticalement"
},
diff --git a/packages/vue/src/locales/it.json b/packages/vue/src/locales/it.json
index eb6e6db34..e732b1bac 100644
--- a/packages/vue/src/locales/it.json
+++ b/packages/vue/src/locales/it.json
@@ -40,7 +40,8 @@
"copyAsJSX": "Copia come JSX",
"copyNodeId": "Copia ID nodo",
"copyXPath": "Copia XPath",
- "checkUpdates": "Controlla aggiornamenti…"
+ "checkUpdates": "Controlla aggiornamenti…",
+ "booleanOperations": "Boolean operations"
},
"commands": {
"undo": "Annulla",
@@ -66,6 +67,11 @@
"frameSelection": "Frame selection",
"showHide": "Visibilità",
"lockUnlock": "Blocco",
+ "unionSelection": "Union selection",
+ "subtractSelection": "Subtract selection",
+ "intersectSelection": "Intersect selection",
+ "excludeSelection": "Exclude selection",
+ "booleanOperations": "Boolean operations",
"flipHorizontal": "Rifletti orizzontalmente",
"flipVertical": "Rifletti verticalmente"
},
diff --git a/packages/vue/src/locales/pl.json b/packages/vue/src/locales/pl.json
index e9b8c7016..b8ba8e028 100644
--- a/packages/vue/src/locales/pl.json
+++ b/packages/vue/src/locales/pl.json
@@ -40,7 +40,8 @@
"copyAsJSX": "Kopiuj jako JSX",
"copyNodeId": "Kopiuj ID węzła",
"copyXPath": "Kopiuj XPath",
- "checkUpdates": "Sprawdź aktualizacje…"
+ "checkUpdates": "Sprawdź aktualizacje…",
+ "booleanOperations": "Boolean operations"
},
"commands": {
"undo": "Cofnij",
@@ -66,6 +67,11 @@
"frameSelection": "Frame selection",
"showHide": "Widoczność",
"lockUnlock": "Blokada",
+ "unionSelection": "Union selection",
+ "subtractSelection": "Subtract selection",
+ "intersectSelection": "Intersect selection",
+ "excludeSelection": "Exclude selection",
+ "booleanOperations": "Boolean operations",
"flipHorizontal": "Odbij poziomo",
"flipVertical": "Odbij pionowo"
},
diff --git a/packages/vue/src/locales/ru.json b/packages/vue/src/locales/ru.json
index c3a9a9db2..dc6370c12 100644
--- a/packages/vue/src/locales/ru.json
+++ b/packages/vue/src/locales/ru.json
@@ -40,7 +40,8 @@
"copyAsJSX": "Копировать как JSX",
"copyNodeId": "Копировать ID узла",
"copyXPath": "Копировать XPath",
- "checkUpdates": "Проверить обновления…"
+ "checkUpdates": "Проверить обновления…",
+ "booleanOperations": "Boolean operations"
},
"commands": {
"undo": "Отменить",
@@ -66,6 +67,11 @@
"frameSelection": "Frame selection",
"showHide": "Видимость",
"lockUnlock": "Блокировка",
+ "unionSelection": "Union selection",
+ "subtractSelection": "Subtract selection",
+ "intersectSelection": "Intersect selection",
+ "excludeSelection": "Exclude selection",
+ "booleanOperations": "Boolean operations",
"flipHorizontal": "Отразить по горизонтали",
"flipVertical": "Отразить по вертикали"
},
diff --git a/packages/vue/src/locales/zh-CN.json b/packages/vue/src/locales/zh-CN.json
index 99a84e66a..1563f0699 100644
--- a/packages/vue/src/locales/zh-CN.json
+++ b/packages/vue/src/locales/zh-CN.json
@@ -40,7 +40,8 @@
"copyAsJSX": "复制为 JSX",
"copyNodeId": "复制节点 ID",
"copyXPath": "复制 XPath",
- "checkUpdates": "检查更新…"
+ "checkUpdates": "检查更新…",
+ "booleanOperations": "Boolean operations"
},
"commands": {
"undo": "撤销",
@@ -66,6 +67,11 @@
"frameSelection": "Frame selection",
"showHide": "切换可见性",
"lockUnlock": "切换锁定",
+ "unionSelection": "Union selection",
+ "subtractSelection": "Subtract selection",
+ "intersectSelection": "Intersect selection",
+ "excludeSelection": "Exclude selection",
+ "booleanOperations": "Boolean operations",
"flipHorizontal": "水平翻转",
"flipVertical": "垂直翻转"
},
diff --git a/src/app/shell/menu/schema.ts b/src/app/shell/menu/schema.ts
index d1ca25196..7ab66a3bb 100644
--- a/src/app/shell/menu/schema.ts
+++ b/src/app/shell/menu/schema.ts
@@ -149,6 +149,27 @@ export const APP_MENU_SCHEMA = [
command: 'selection.ungroup'
},
{ type: 'separator' },
+ {
+ id: 'selection.booleanUnion',
+ label: 'Union selection',
+ command: 'selection.booleanUnion'
+ },
+ {
+ id: 'selection.booleanSubtract',
+ label: 'Subtract selection',
+ command: 'selection.booleanSubtract'
+ },
+ {
+ id: 'selection.booleanIntersect',
+ label: 'Intersect selection',
+ command: 'selection.booleanIntersect'
+ },
+ {
+ id: 'selection.booleanExclude',
+ label: 'Exclude selection',
+ command: 'selection.booleanExclude'
+ },
+ { type: 'separator' },
{
id: 'selection.createComponent',
label: 'Create Component',
diff --git a/src/app/shell/menu/use.ts b/src/app/shell/menu/use.ts
index 08fcb54f2..cd3f15495 100644
--- a/src/app/shell/menu/use.ts
+++ b/src/app/shell/menu/use.ts
@@ -25,6 +25,10 @@ const COMMAND_MENU_IDS = new Set<string>([
'selection.createComponentSet',
'selection.detachInstance',
'selection.wrapInAutoLayout',
+ 'selection.booleanUnion',
+ 'selection.booleanSubtract',
+ 'selection.booleanIntersect',
+ 'selection.booleanExclude',
'selection.bringToFront',
'selection.sendToBack',
'view.zoom100',
diff --git a/src/components/CanvasMenu.vue b/src/components/CanvasMenu.vue
index 558c81f3c..95a3b587e 100644
--- a/src/components/CanvasMenu.vue
+++ b/src/components/CanvasMenu.vue
@@ -8,6 +8,10 @@ import {
ContextMenuSubContent,
ContextMenuPortal
} from 'reka-ui'
+import IconCombine from '~icons/lucide/combine'
+import IconCopyMinus from '~icons/lucide/copy-minus'
+import IconCopyX from '~icons/lucide/copy-x'
+import IconSquaresIntersect from '~icons/lucide/squares-intersect'
import {
vTestId,
useEditorCommands,
@@ -17,6 +21,7 @@ import {
editorCommandMetadata,
formatShortcut
} from '@open-pencil/vue'
+import type { Component } from 'vue'
import type { EditorCommandId } from '@open-pencil/vue'
import { useEditorStore } from '@/app/editor/active-store'
@@ -50,9 +55,21 @@ const cls = {
sep: menuCls.separator
}
+const booleanCommandIcons = {
+ 'selection.booleanUnion': IconCombine,
+ 'selection.booleanSubtract': IconCopyMinus,
+ 'selection.booleanIntersect': IconSquaresIntersect,
+ 'selection.booleanExclude': IconCopyX
+} satisfies Partial<Record<EditorCommandId, Component>>
+
function contextCommandTestId(id: EditorCommandId | undefined): string | undefined {
return id ? editorCommandMetadata(id).contextTestId : undefined
}
+
+function contextCommandIcon(id: EditorCommandId | undefined): Component | undefined {
+ if (!id) return undefined
+ return (booleanCommandIcons as Partial<Record<EditorCommandId, Component>>)[id]
+}
</script>
<template>
@@ -128,6 +145,11 @@ function contextCommandTestId(id: EditorCommandId | undefined): string | undefin
@select="!sub.separator && sub.action?.()"
>
<template v-if="!sub.separator">
+ <component
+ :is="contextCommandIcon(sub.id)"
+ v-if="contextCommandIcon(sub.id)"
+ class="mr-2 size-3.5 text-muted"
+ />
<span class="flex-1">{{ sub.label }}</span>
<span v-if="sub.shortcut" class="text-[11px] text-muted">{{ sub.shortcut }}</span>
</template>
@@ -142,6 +164,11 @@ function contextCommandTestId(id: EditorCommandId | undefined): string | undefin
:disabled="item.disabled"
@select="item.action?.()"
>
+ <component
+ :is="contextCommandIcon(item.id)"
+ v-if="contextCommandIcon(item.id)"
+ class="mr-2 size-3.5 text-muted"
+ />
<span class="flex-1">{{ item.label }}</span>
<span
v-if="item.shortcut"
diff --git a/tests/engine/editor/structure/boolean.test.ts b/tests/engine/editor/structure/boolean.test.ts
new file mode 100644
index 000000000..a759e1ac1
--- /dev/null
+++ b/tests/engine/editor/structure/boolean.test.ts
@@ -0,0 +1,46 @@
+import { describe, expect, test } from 'bun:test'
+
+import { createEditor } from '@open-pencil/core/editor'
+
+describe('booleanOperationSelected', () => {
+ test('wraps selected nodes in a boolean operation container', () => {
+ const editor = createEditor()
+ const pageId = editor.state.currentPageId
+ const first = editor.graph.createNode('RECTANGLE', pageId, { x: 10, y: 20, width: 30, height: 40 })
+ const second = editor.graph.createNode('ELLIPSE', pageId, { x: 80, y: 90, width: 20, height: 10 })
+
+ editor.select([first.id, second.id])
+ editor.booleanOperationSelected('UNION')
+
+ const [booleanId] = [...editor.state.selectedIds]
+ const booleanNode = editor.graph.getNode(booleanId)
+ expect(booleanNode?.type).toBe('BOOLEAN_OPERATION')
+ expect(booleanNode?.booleanOperation).toBe('UNION')
+ 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('undo and redo preserve parent order and selection', () => {
+ const editor = createEditor()
+ const pageId = editor.state.currentPageId
+ const before = editor.graph.createNode('RECTANGLE', pageId, { name: 'Before' })
+ const first = editor.graph.createNode('RECTANGLE', pageId, { name: 'First' })
+ const second = editor.graph.createNode('ELLIPSE', pageId, { name: 'Second' })
+ const after = editor.graph.createNode('RECTANGLE', pageId, { name: 'After' })
+
+ editor.select([first.id, second.id])
+ editor.booleanOperationSelected('EXCLUDE')
+ const [booleanId] = [...editor.state.selectedIds]
+ expect(editor.graph.getNode(pageId)?.childIds).toEqual([before.id, booleanId, after.id])
+
+ editor.undo.undo()
+ expect(editor.graph.getNode(pageId)?.childIds).toEqual([before.id, first.id, second.id, after.id])
+ expect(editor.state.selectedIds).toEqual(new Set([first.id, second.id]))
+
+ editor.undo.redo()
+ expect(editor.graph.getNode(pageId)?.childIds).toEqual([before.id, booleanId, after.id])
+ expect(editor.graph.getNode(booleanId)?.booleanOperation).toBe('EXCLUDE')
+ expect(editor.state.selectedIds).toEqual(new Set([booleanId]))
+ })
+})
diff --git a/tests/engine/vue/editor/menu-model/canvas.test.ts b/tests/engine/vue/editor/menu-model/canvas.test.ts
index d28e1cd38..6f225d913 100644
--- a/tests/engine/vue/editor/menu-model/canvas.test.ts
+++ b/tests/engine/vue/editor/menu-model/canvas.test.ts
@@ -24,7 +24,7 @@ function options(overrides: Partial<CanvasMenuOptions> = {}): CanvasMenuOptions
otherPages: [],
moveSelectionToPage: (pageId: string) => pageId,
selection: selection(),
- t: { moveToPage: 'Move to page' },
+ t: { booleanOperations: 'Boolean operations', moveToPage: 'Move to page' },
...overrides
} as CanvasMenuOptions
}
@@ -46,6 +46,8 @@ describe('buildCanvasContextMenu', () => {
'selection.frameSelection',
'selection.wrapInAutoLayout',
'---',
+ 'Boolean operations',
+ '---',
'selection.createComponent',
'---',
'selection.toggleVisibility',