Skip to content

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

Move hit-test logic out of CanvasContextMenu into editor

Danila Poyarkovb2ed74860844
diff --git a/packages/core/src/editor/selection.ts b/packages/core/src/editor/selection.ts
index 1c64ca616..a5708ad8a 100644
--- a/packages/core/src/editor/selection.ts
+++ b/packages/core/src/editor/selection.ts
@@ -1,4 +1,5 @@
import type { Rect } from '../types'
+import type { SceneNode } from '../scene-graph'
import type { SnapGuide } from '../snap'
import type { EditorContext } from './types'
@@ -99,6 +100,35 @@ export function createSelectionActions(ctx: EditorContext) {
return ctx.graph.flattenTree(ctx.state.currentPageId)
}
+ function hitTestAtPoint(cx: number, cy: number, deep = false): SceneNode | null {
+ const scopeId = ctx.state.enteredContainerId
+ if (scopeId) {
+ const scopeNode = ctx.graph.getNode(scopeId)
+ if (!scopeNode) {
+ ctx.state.enteredContainerId = null
+ } else {
+ const abs = ctx.graph.getAbsolutePosition(scopeId)
+ const lx = cx - abs.x
+ const ly = cy - abs.y
+ return deep
+ ? ctx.graph.hitTestDeep(lx, ly, scopeId)
+ : ctx.graph.hitTest(lx, ly, scopeId)
+ }
+ }
+ return deep
+ ? ctx.graph.hitTestDeep(cx, cy, ctx.state.currentPageId)
+ : ctx.graph.hitTest(cx, cy, ctx.state.currentPageId)
+ }
+
+ function selectAtPoint(cx: number, cy: number) {
+ const hit = hitTestAtPoint(cx, cy)
+ if (hit) {
+ if (!ctx.state.selectedIds.has(hit.id)) select([hit.id])
+ } else {
+ clearSelection()
+ }
+ }
+
return {
select,
clearSelection,
@@ -114,6 +144,8 @@ export function createSelectionActions(ctx: EditorContext) {
exitContainer,
getSelectedNodes,
getSelectedNode,
- getLayerTree
+ getLayerTree,
+ hitTestAtPoint,
+ selectAtPoint
}
}
diff --git a/src/components/CanvasContextMenu.vue b/src/components/CanvasContextMenu.vue
index c577a35bd..de5d7a5b1 100644
--- a/src/components/CanvasContextMenu.vue
+++ b/src/components/CanvasContextMenu.vue
@@ -1,42 +1,16 @@
<script setup lang="ts">
import { ContextMenuRoot, ContextMenuTrigger, ContextMenuPortal } from 'reka-ui'
-import { useEditorStore } from '@/stores/editor'
import NodeContextMenuContent from './NodeContextMenuContent.vue'
-const store = useEditorStore()
-
-function onRightClick(e: MouseEvent) {
- const el = e.currentTarget as HTMLElement
- const canvas = el.querySelector('canvas')
- if (!canvas) return
- const rect = canvas.getBoundingClientRect()
- const sx = e.clientX - rect.left
- const sy = e.clientY - rect.top
- const { x: cx, y: cy } = store.screenToCanvas(sx, sy)
-
- 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])
- }
- } else {
- store.clearSelection()
- }
-}
+const { onContextMenu } = defineProps<{
+ onContextMenu?: (e: MouseEvent) => void
+}>()
</script>
<template>
<ContextMenuRoot :modal="false">
- <ContextMenuTrigger as-child @contextmenu="onRightClick">
+ <ContextMenuTrigger as-child @contextmenu="onContextMenu">
<slot />
</ContextMenuTrigger>
diff --git a/src/components/EditorCanvas.vue b/src/components/EditorCanvas.vue
index 9e739c9d8..7edcb71a4 100644
--- a/src/components/EditorCanvas.vue
+++ b/src/components/EditorCanvas.vue
@@ -36,10 +36,18 @@ watch(
)
const cursor = computed(() => toolCursor(store.state.activeTool, cursorOverride.value))
+
+function onContextMenu(e: MouseEvent) {
+ const canvas = canvasRef.value
+ if (!canvas) return
+ const rect = canvas.getBoundingClientRect()
+ const { x: cx, y: cy } = store.screenToCanvas(e.clientX - rect.left, e.clientY - rect.top)
+ store.selectAtPoint(cx, cy)
+}
</script>
<template>
- <CanvasContextMenu>
+ <CanvasContextMenu :on-context-menu="onContextMenu">
<div
data-test-id="canvas-area"
class="canvas-area relative min-h-0 min-w-0 flex-1 overflow-hidden"