Skip to content

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

test(canvas): cover selected frame guide scope

Danila Poyarkovdc8367921b20
diff --git a/packages/vue/src/canvas/guides/input.ts b/packages/vue/src/canvas/guides/input.ts
index d60ae0eb1..119d5ba5a 100644
--- a/packages/vue/src/canvas/guides/input.ts
+++ b/packages/vue/src/canvas/guides/input.ts
@@ -15,6 +15,13 @@ interface GuideInputOptions {
setCursor: (cursor: string | null) => void
}
+export function selectedTopLevelGuideFrameId(editor: Editor): string | null {
+ if (editor.state.selectedIds.size !== 1) return null
+ const id = [...editor.state.selectedIds][0]
+ const node = editor.graph.getNode(id)
+ return node?.type === 'FRAME' && node.parentId === editor.state.currentPageId ? node.id : null
+}
+
export function createGuideInput({
canvasRef,
editor,
diff --git a/packages/vue/src/canvas/useCanvasInput.ts b/packages/vue/src/canvas/useCanvasInput.ts
index 80dcf51ba..2f21f2dfe 100644
--- a/packages/vue/src/canvas/useCanvasInput.ts
+++ b/packages/vue/src/canvas/useCanvasInput.ts
@@ -4,7 +4,7 @@ import { onScopeDispose, ref, type Ref } from 'vue'
import type { Editor } from '@open-pencil/core/editor'
import type { SceneNode } from '@open-pencil/scene-graph'
-import { createGuideInput } from '#vue/canvas/guides/input'
+import { createGuideInput, selectedTopLevelGuideFrameId } from '#vue/canvas/guides/input'
import {
handleBendHandleMove,
handleNodeEditMouseUp,
@@ -208,13 +208,6 @@ export function useCanvasInput(
autoLayoutPaddingEdit.value = null
}
- function selectedTopLevelFrameId(): string | null {
- if (editor.state.selectedIds.size !== 1) return null
- const id = [...editor.state.selectedIds][0]
- const node = editor.graph.getNode(id)
- return node?.type === 'FRAME' && node.parentId === editor.state.currentPageId ? node.id : null
- }
-
function onDblClick(e: MouseEvent) {
if (startAutoLayoutPaddingEdit(e)) return
onTextDblClick(e)
@@ -302,7 +295,7 @@ export function useCanvasInput(
const { sx, sy, cx, cy } = getCoords(e)
if (d.type === 'guide') {
- const frameId = e.altKey && !d.guideId ? selectedTopLevelFrameId() : null
+ const frameId = e.altKey && !d.guideId ? selectedTopLevelGuideFrameId(editor) : null
guideInput.handleMove(
d,
sx,
diff --git a/tests/engine/vue/input/guides.test.ts b/tests/engine/vue/input/guides.test.ts
index 8ae8d0f85..8812d47ac 100644
--- a/tests/engine/vue/input/guides.test.ts
+++ b/tests/engine/vue/input/guides.test.ts
@@ -4,7 +4,7 @@ import { ref } from 'vue'
import { createEditor } from '@open-pencil/core/editor'
-import { createGuideInput } from '#vue/canvas/guides/input'
+import { createGuideInput, selectedTopLevelGuideFrameId } from '#vue/canvas/guides/input'
import type { DragState } from '#vue/shared/input/types'
function setup() {
@@ -14,7 +14,10 @@ function setup() {
const input = createGuideInput({
canvasRef: ref({ clientWidth: 500, clientHeight: 500 } as HTMLCanvasElement),
editor,
- canvasToLocal: (cx, cy) => ({ lx: cx, ly: cy }),
+ canvasToLocal: (cx, cy, scopeId) => {
+ const owner = editor.graph.getNode(scopeId)
+ return { lx: cx - (owner?.x ?? 0), ly: cy - (owner?.y ?? 0) }
+ },
setDrag: (next) => {
drag = next
},
@@ -99,12 +102,19 @@ describe('guide canvas input', () => {
height: 80
})
editor.graph.hitTestDeep = () => nested
+ editor.select([frame.id])
input.tryStartFromRuler(100, 5, 100, 5)
const drag = getDrag()
if (drag?.type !== 'guide') throw new Error('Expected guide drag')
+ const selectedFrameId = selectedTopLevelGuideFrameId(editor)
+ expect(selectedFrameId).toBe(frame.id)
- input.handleMove(drag, 200, 200, 200, 200, { frameId: frame.id, deep: false })
+ input.handleMove(drag, 200, 200, 200, 200, {
+ frameId: selectedFrameId ?? '',
+ deep: false
+ })
expect(drag.ownerId).toBe(frame.id)
+ expect(drag.position).toBe(100)
expect(editor.state.guides.preview?.ownerId).toBe(frame.id)
input.finish(drag)
expect(editor.graph.getNode(frame.id)?.guides).toHaveLength(1)