Skip to content

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

fix(editor): hit test entered containers in world space

Danila Poyarkov5fd0e60a9931
diff --git a/packages/core/src/editor/selection/hit-test.ts b/packages/core/src/editor/selection/hit-test.ts
index 052116fec..0aeaa0c61 100644
--- a/packages/core/src/editor/selection/hit-test.ts
+++ b/packages/core/src/editor/selection/hit-test.ts
@@ -15,10 +15,7 @@ export function createSelectionHitTestActions(
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, scopeId) : ctx.graph.hitTest(cx, cy, scopeId)
}
}
return deep
diff --git a/packages/vue/src/canvas/pointer/use.ts b/packages/vue/src/canvas/pointer/use.ts
index 8a4b0ea1d..287e08147 100644
--- a/packages/vue/src/canvas/pointer/use.ts
+++ b/packages/vue/src/canvas/pointer/use.ts
@@ -21,7 +21,7 @@ export function createCanvasPointer(
const canvasToLocal = (cx: number, cy: number, scopeId: string) =>
canvasToLocalPoint(cx, cy, scopeId, editor)
const hitTestInScope = (cx: number, cy: number, deep: boolean) =>
- hitTestInEditorScope(cx, cy, deep, editor, canvasToLocal)
+ hitTestInEditorScope(cx, cy, deep, editor)
const isInsideContainerBounds = (cx: number, cy: number, containerId: string) =>
isInsideEditorContainerBounds(cx, cy, containerId, editor, canvasToLocal)
diff --git a/packages/vue/src/shared/input/geometry.ts b/packages/vue/src/shared/input/geometry.ts
index ccb861700..d058aa429 100644
--- a/packages/vue/src/shared/input/geometry.ts
+++ b/packages/vue/src/shared/input/geometry.ts
@@ -47,8 +47,7 @@ export function hitTestInEditorScope(
cx: number,
cy: number,
deep: boolean,
- editor: Editor,
- canvasToLocal: (cx: number, cy: number, scopeId: string) => { lx: number; ly: number }
+ editor: Editor
): SceneNode | null {
const scopeId = editor.state.enteredContainerId
@@ -58,10 +57,9 @@ export function hitTestInEditorScope(
if (!editor.graph.getNode(scopeId)) {
editor.state.enteredContainerId = null
} else {
- const { lx, ly } = canvasToLocal(cx, cy, scopeId)
return deep
- ? editor.graph.hitTestDeep(lx, ly, scopeId)
- : editor.graph.hitTest(lx, ly, scopeId)
+ ? editor.graph.hitTestDeep(cx, cy, scopeId)
+ : editor.graph.hitTest(cx, cy, scopeId)
}
}
return deep
diff --git a/tests/engine/editor/selection/hit-test.test.ts b/tests/engine/editor/selection/hit-test.test.ts
new file mode 100644
index 000000000..aa2461c41
--- /dev/null
+++ b/tests/engine/editor/selection/hit-test.test.ts
@@ -0,0 +1,28 @@
+import { describe, expect, test } from 'bun:test'
+
+import { createEditor } from '@open-pencil/core/editor'
+
+describe('editor scoped hit testing', () => {
+ test('uses world coordinates inside entered containers', () => {
+ const editor = createEditor()
+ const pageId = editor.state.currentPageId
+ const frame = editor.graph.createNode('FRAME', pageId, {
+ x: 100,
+ y: 100,
+ width: 200,
+ height: 200
+ })
+ const child = editor.graph.createNode('RECTANGLE', frame.id, {
+ x: 20,
+ y: 20,
+ width: 40,
+ height: 40,
+ fills: [{ type: 'SOLID', color: { r: 1, g: 0, b: 0, a: 1 }, opacity: 1, visible: true }]
+ })
+
+ editor.state.enteredContainerId = frame.id
+ editor.setCanvasKit({} as Parameters<typeof editor.setCanvasKit>[0], {} as Parameters<typeof editor.setCanvasKit>[1])
+
+ expect(editor.hitTestAtPoint(130, 130)?.id).toBe(child.id)
+ })
+})