Skip to content

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

Add selection scope: enter containers with dblclick, exit with Escape or click-outside

Anton A S1b743bec948c
diff --git a/src/components/CanvasContextMenu.vue b/src/components/CanvasContextMenu.vue
index 354fd72d6..c577a35bd 100644
--- a/src/components/CanvasContextMenu.vue
+++ b/src/components/CanvasContextMenu.vue
@@ -15,7 +15,15 @@ function onRightClick(e: MouseEvent) {
const sy = e.clientY - rect.top
const { x: cx, y: cy } = store.screenToCanvas(sx, sy)
- const hit = store.graph.hitTest(cx, cy, store.state.currentPageId)
+ let hit = null
+ const scopeId = store.state.enteredContainerId
+ if (scopeId && store.graph.getNode(scopeId)) {
+ const abs = store.graph.getAbsolutePosition(scopeId)
+ hit = store.graph.hitTest(cx - abs.x, cy - abs.y, scopeId)
+ }
+ if (!hit) {
+ hit = store.graph.hitTest(cx, cy, store.state.currentPageId)
+ }
if (hit) {
if (!store.state.selectedIds.has(hit.id)) {
store.select([hit.id])
diff --git a/src/composables/use-canvas-input.ts b/src/composables/use-canvas-input.ts
index 25bb9dc64..c260f662b 100644
--- a/src/composables/use-canvas-input.ts
+++ b/src/composables/use-canvas-input.ts
@@ -294,6 +294,31 @@ export function useCanvasInput(
return { sx, sy, cx, cy }
}
+ function hitTestInScope(cx: number, cy: number, deep: boolean): SceneNode | null {
+ const scopeId = store.state.enteredContainerId
+ if (scopeId) {
+ if (!store.graph.getNode(scopeId)) {
+ store.state.enteredContainerId = null
+ } else {
+ const abs = store.graph.getAbsolutePosition(scopeId)
+ return deep
+ ? store.graph.hitTestDeep(cx - abs.x, cy - abs.y, scopeId)
+ : store.graph.hitTest(cx - abs.x, cy - abs.y, scopeId)
+ }
+ }
+ return deep
+ ? store.graph.hitTestDeep(cx, cy, store.state.currentPageId)
+ : store.graph.hitTest(cx, cy, store.state.currentPageId)
+ }
+
+ function isInsideContainerBounds(cx: number, cy: number, containerId: string): boolean {
+ const container = store.graph.getNode(containerId)
+ if (!container) return false
+ const abs = store.graph.getAbsolutePosition(containerId)
+ return cx >= abs.x && cx <= abs.x + container.width
+ && cy >= abs.y && cy <= abs.y + container.height
+ }
+
function startPanDrag(e: MouseEvent) {
drag.value = {
type: 'pan',
@@ -451,6 +476,34 @@ export function useCanvasInput(
return undefined
}
+ function resolveHit(cx: number, cy: number): SceneNode | null {
+ const titleHit =
+ hitTestFrameTitle(cx, cy) ??
+ hitTestSectionTitle(cx, cy) ??
+ hitTestComponentLabel(cx, cy)
+ if (titleHit) return titleHit
+
+ const hit = hitTestInScope(cx, cy, false)
+ if (hit) return hit
+
+ const scopeId = store.state.enteredContainerId
+ if (!scopeId) return null
+
+ if (isInsideContainerBounds(cx, cy, scopeId)) {
+ store.clearSelection()
+ return null
+ }
+
+ store.exitContainer()
+ const afterExit = hitTestInScope(cx, cy, false)
+ if (afterExit) return afterExit
+
+ if (store.state.enteredContainerId) {
+ store.exitContainer()
+ }
+ return null
+ }
+
function handleSelectDown(e: MouseEvent, sx: number, sy: number, cx: number, cy: number) {
if (store.state.editingTextId && handleTextEditClick(cx, cy, e.shiftKey)) return
@@ -459,15 +512,12 @@ export function useCanvasInput(
if (tryStartRotation(sx, sy)) return
if (tryStartResize(sx, sy, cx, cy)) return
- const hit =
- hitTestFrameTitle(cx, cy) ??
- hitTestSectionTitle(cx, cy) ??
- hitTestComponentLabel(cx, cy) ??
- store.graph.hitTest(cx, cy, store.state.currentPageId)
-
+ const hit = resolveHit(cx, cy)
if (!hit) {
- store.clearSelection()
- drag.value = { type: 'marquee', startX: cx, startY: cy }
+ if (!store.state.enteredContainerId) {
+ store.clearSelection()
+ drag.value = { type: 'marquee', startX: cx, startY: cy }
+ }
return
}
@@ -614,7 +664,7 @@ export function useCanvasInput(
const hit =
hitTestSectionTitle(cx, cy) ??
hitTestComponentLabel(cx, cy) ??
- store.graph.hitTest(cx, cy, store.state.currentPageId)
+ hitTestInScope(cx, cy, false)
store.setHoveredNode(hit && !store.state.selectedIds.has(hit.id) ? hit.id : null)
}
@@ -1087,17 +1137,23 @@ export function useCanvasInput(
? [...store.state.selectedIds][0]
: undefined
const selectedNode = selectedId ? store.graph.getNode(selectedId) : undefined
- const canEnter = selectedNode && selectedId && store.graph.isContainer(selectedId) && !selectedNode.locked
+ const canEnter = selectedNode && selectedId
+ && store.graph.isContainer(selectedId) && !selectedNode.locked
- let hit: SceneNode | null = null
if (canEnter) {
- const abs = store.graph.getAbsolutePosition(selectedId)
- hit = store.graph.hitTestDeep(cx - abs.x, cy - abs.y, selectedId)
- } else {
- hit = hitTestSectionTitle(cx, cy) ??
- hitTestComponentLabel(cx, cy) ??
- store.graph.hitTestDeep(cx, cy, store.state.currentPageId)
+ store.enterContainer(selectedId)
+ const hit = hitTestInScope(cx, cy, false)
+ if (hit) {
+ store.select([hit.id])
+ } else {
+ store.clearSelection()
+ }
+ return
}
+
+ const hit = hitTestSectionTitle(cx, cy) ??
+ hitTestComponentLabel(cx, cy) ??
+ hitTestInScope(cx, cy, true)
if (!hit) return
if (hit.type === 'TEXT') {
diff --git a/src/composables/use-keyboard.ts b/src/composables/use-keyboard.ts
index ef0e2e653..92fedd8fe 100644
--- a/src/composables/use-keyboard.ts
+++ b/src/composables/use-keyboard.ts
@@ -210,6 +210,10 @@ export function useKeyboard() {
store.penCancel()
return
}
+ if (store.state.enteredContainerId) {
+ store.exitContainer()
+ return
+ }
store.clearSelection()
store.setTool('SELECT')
})
diff --git a/src/stores/editor.ts b/src/stores/editor.ts
index f2b1084b1..09577c811 100644
--- a/src/stores/editor.ts
+++ b/src/stores/editor.ts
@@ -204,7 +204,8 @@ export function createEditorStore() {
actionToast: null as string | null,
mobileDrawerSnap: 'closed' as 'closed' | 'half' | 'full',
clipboardHtml: '',
- autosaveEnabled: false
+ autosaveEnabled: false,
+ enteredContainerId: null as string | null
})
const AUTOSAVE_DELAY = 3000
@@ -314,6 +315,7 @@ export function createEditorStore() {
// Switch
state.currentPageId = pageId
+ state.enteredContainerId = null
clearSelection()
// Restore viewport
@@ -380,6 +382,23 @@ export function createEditorStore() {
state.selectedIds = new Set()
}
+ function enterContainer(id: string) {
+ state.enteredContainerId = id
+ }
+
+ function exitContainer() {
+ const entered = state.enteredContainerId
+ if (!entered) return
+ const node = graph.getNode(entered)
+ const parentId = node?.parentId
+ if (parentId && parentId !== state.currentPageId) {
+ state.enteredContainerId = parentId
+ } else {
+ state.enteredContainerId = null
+ }
+ state.selectedIds = new Set(entered ? [entered] : [])
+ }
+
function setMarquee(rect: Rect | null) {
state.marquee = rect
requestRepaint()
@@ -2375,6 +2394,8 @@ export function createEditorStore() {
setTool,
select,
clearSelection,
+ enterContainer,
+ exitContainer,
selectAll,
setMarquee,
setSnapGuides,