Skip to content

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

Fix all deferred code quality items

Danila Poyarkov35e78e276af0
diff --git a/packages/core/src/editor/structure.ts b/packages/core/src/editor/structure.ts
index 1fbac579d..17dd1301d 100644
--- a/packages/core/src/editor/structure.ts
+++ b/packages/core/src/editor/structure.ts
@@ -147,8 +147,7 @@ export function createStructureActions(ctx: EditorContext) {
})
const containerId = containerNode.id
- parent.childIds = parent.childIds.filter((id) => id !== containerId)
- parent.childIds.splice(firstIndex, 0, containerId)
+ ctx.graph.insertChildAt(containerId, parentId, firstIndex)
for (const n of selectedNodes) {
ctx.graph.reparentNode(n.id, containerId)
@@ -160,8 +159,7 @@ export function createStructureActions(ctx: EditorContext) {
label: `Create ${containerType.toLowerCase().replace('_', ' ')}`,
forward: () => {
const c = ctx.graph.createNode(containerType, parentId, { ...containerNode, ...extraProps })
- parent.childIds = parent.childIds.filter((id) => id !== c.id)
- parent.childIds.splice(firstIndex, 0, c.id)
+ ctx.graph.insertChildAt(c.id, parentId, firstIndex)
for (const n of origPositions) ctx.graph.reparentNode(n.id, c.id)
ctx.state.selectedIds = new Set([c.id])
},
@@ -271,8 +269,7 @@ export function createStructureActions(ctx: EditorContext) {
for (let i = 0; i < childIds.length; i++) {
ctx.graph.reparentNode(childIds[i], parentId)
- parent.childIds = parent.childIds.filter((id) => id !== childIds[i])
- parent.childIds.splice(groupIndex + i, 0, childIds[i])
+ ctx.graph.insertChildAt(childIds[i], parentId, groupIndex + i)
}
ctx.graph.deleteNode(node.id)
@@ -283,16 +280,14 @@ export function createStructureActions(ctx: EditorContext) {
forward: () => {
for (let i = 0; i < childIds.length; i++) {
ctx.graph.reparentNode(childIds[i], parentId)
- parent.childIds = parent.childIds.filter((id) => id !== childIds[i])
- parent.childIds.splice(groupIndex + i, 0, childIds[i])
+ ctx.graph.insertChildAt(childIds[i], parentId, groupIndex + i)
}
ctx.graph.deleteNode(node.id)
ctx.state.selectedIds = new Set(childIds)
},
inverse: () => {
const g = ctx.graph.createNode('GROUP', parentId, { ...groupSnapshot, childIds: [] })
- parent.childIds = parent.childIds.filter((id) => id !== g.id)
- parent.childIds.splice(groupIndex, 0, g.id)
+ ctx.graph.insertChildAt(g.id, parentId, groupIndex)
for (const orig of origPositions) {
ctx.graph.reparentNode(orig.id, g.id)
ctx.graph.updateNode(orig.id, { x: orig.x, y: orig.y })
@@ -310,8 +305,7 @@ export function createStructureActions(ctx: EditorContext) {
if (!parent) continue
const idx = parent.childIds.indexOf(id)
if (idx === parent.childIds.length - 1) continue
- parent.childIds = parent.childIds.filter((cid) => cid !== id)
- parent.childIds.push(id)
+ ctx.graph.insertChildAt(id, node.parentId, parent.childIds.length)
}
ctx.requestRender()
}
@@ -324,8 +318,7 @@ export function createStructureActions(ctx: EditorContext) {
if (!parent) continue
const idx = parent.childIds.indexOf(id)
if (idx === 0) continue
- parent.childIds = parent.childIds.filter((cid) => cid !== id)
- parent.childIds.unshift(id)
+ ctx.graph.insertChildAt(id, node.parentId, 0)
}
ctx.requestRender()
}
diff --git a/packages/core/src/scene-graph.ts b/packages/core/src/scene-graph.ts
index 0aec517dc..12d972015 100644
--- a/packages/core/src/scene-graph.ts
+++ b/packages/core/src/scene-graph.ts
@@ -841,6 +841,21 @@ export class SceneGraph {
this.emitter.emit('node:reordered', nodeId, parentId, idx)
}
+ insertChildAt(childId: string, parentId: string, index: number): void {
+ const oldParent = this.getNode(this.getNode(childId)?.parentId ?? '')
+ if (oldParent) {
+ oldParent.childIds = oldParent.childIds.filter((id) => id !== childId)
+ }
+ const newParent = this.getNode(parentId)
+ if (!newParent) return
+ newParent.childIds = newParent.childIds.filter((id) => id !== childId)
+ newParent.childIds.splice(index, 0, childId)
+ const node = this.getNode(childId)
+ if (node) node.parentId = parentId
+ this.clearAbsPosCache()
+ this.emitter.emit('node:reordered', childId, parentId, index)
+ }
+
deleteNode(id: string): void {
const node = this.nodes.get(id)
if (!node || id === this.rootId) return
diff --git a/packages/vue/src/PropertyList/PropertyListItem.vue b/packages/vue/src/PropertyList/PropertyListItem.vue
index 40229a3be..14234e822 100644
--- a/packages/vue/src/PropertyList/PropertyListItem.vue
+++ b/packages/vue/src/PropertyList/PropertyListItem.vue
@@ -5,13 +5,14 @@ const { index } = defineProps<{
index: number
}>()
-const { update, remove, toggleVisibility } = usePropertyList()
+const { update, patch, remove, toggleVisibility } = usePropertyList()
</script>
<template>
<slot
:index="index"
- :update="(patch: Record<string, unknown>) => update(index, patch)"
+ :update="(item: unknown) => update(index, item)"
+ :patch="(changes: Record<string, unknown>) => patch(index, changes)"
:remove="() => remove(index)"
:toggle-visibility="() => toggleVisibility(index)"
/>
diff --git a/packages/vue/src/PropertyList/PropertyListRoot.vue b/packages/vue/src/PropertyList/PropertyListRoot.vue
index c94850ae7..62c754fc6 100644
--- a/packages/vue/src/PropertyList/PropertyListRoot.vue
+++ b/packages/vue/src/PropertyList/PropertyListRoot.vue
@@ -84,13 +84,14 @@ function toggleVisibility(index: number) {
providePropertyList({
editor,
propKey,
- items: items as ReturnType<typeof computed<unknown[]>>,
+ items,
isMixed,
activeNode,
isMulti,
- add: add as (defaults: unknown) => void,
+ add,
remove,
- update: update as (index: number, item: unknown) => void,
+ update,
+ patch,
toggleVisibility
})
</script>
diff --git a/packages/vue/src/PropertyList/context.ts b/packages/vue/src/PropertyList/context.ts
index cacbd06d8..69843e76b 100644
--- a/packages/vue/src/PropertyList/context.ts
+++ b/packages/vue/src/PropertyList/context.ts
@@ -6,27 +6,28 @@ import type { ComputedRef } from 'vue'
type ArrayPropKey = 'fills' | 'strokes' | 'effects'
-export interface PropertyListContext {
+export interface PropertyListContext<T = unknown> {
editor: Editor
propKey: ArrayPropKey
- items: ComputedRef<unknown[]>
+ items: ComputedRef<T[]>
isMixed: ComputedRef<boolean>
activeNode: ComputedRef<SceneNode | null>
isMulti: ComputedRef<boolean>
- add: (defaults: unknown) => void
+ add: (defaults: T) => void
remove: (index: number) => void
- update: (index: number, item: unknown) => void
+ update: (index: number, item: T) => void
+ patch: (index: number, changes: Partial<T>) => void
toggleVisibility: (index: number) => void
}
-export const PROPERTY_LIST_KEY: InjectionKey<PropertyListContext> = Symbol('property-list')
+const PROPERTY_LIST_KEY: InjectionKey<PropertyListContext> = Symbol('property-list')
-export function providePropertyList(ctx: PropertyListContext) {
- provide(PROPERTY_LIST_KEY, ctx)
+export function providePropertyList<T>(ctx: PropertyListContext<T>) {
+ provide(PROPERTY_LIST_KEY, ctx as PropertyListContext)
}
-export function usePropertyList(): PropertyListContext {
+export function usePropertyList<T = unknown>(): PropertyListContext<T> {
const ctx = inject(PROPERTY_LIST_KEY)
if (!ctx) throw new Error('[open-pencil] usePropertyList() called outside <PropertyListRoot>')
- return ctx
+ return ctx as PropertyListContext<T>
}
diff --git a/packages/vue/src/composables/use-canvas-input.ts b/packages/vue/src/composables/use-canvas-input.ts
index 8c5569002..8982db434 100644
--- a/packages/vue/src/composables/use-canvas-input.ts
+++ b/packages/vue/src/composables/use-canvas-input.ts
@@ -2,30 +2,24 @@ import { useEventListener } from '@vueuse/core'
import { ref, type Ref } from 'vue'
import {
- AUTO_LAYOUT_BREAK_THRESHOLD,
PEN_CLOSE_THRESHOLD,
ROTATION_SNAP_DEGREES,
DEFAULT_TEXT_WIDTH,
DEFAULT_TEXT_HEIGHT,
- computeSelectionBounds,
- computeSnap,
degToRad
} from '@open-pencil/core'
import type { Editor } from '@open-pencil/core/editor'
import type { SceneNode } from '@open-pencil/core'
-import type { DragDraw, DragMarquee, DragMove, DragPan, DragRotate, DragState } from '../input/types'
+import type { DragMarquee, DragPan, DragRotate, DragState } from '../input/types'
import { TOOL_TO_NODE } from '../input/types'
-import {
- HANDLE_CURSORS,
- hitTestHandle,
- hitTestCornerRotation,
- cornerRotationCursor
-} from '../input/geometry'
+import { hitTestCornerRotation } from '../input/geometry'
import { setupPanZoom } from '../input/pan-zoom'
-import { applyResize, tryStartResize } from '../input/resize'
-import { computeAutoLayoutIndicator, computeAutoLayoutIndicatorForFrame } from '../input/auto-layout'
+import { applyResize } from '../input/resize'
+import { handleMoveMove, handleMoveUp } from '../input/move'
+import { handleSelectDown, updateHoverCursor, type HitTestFns } from '../input/select'
+import { handleDrawMove, handleDrawUp } from '../input/draw'
export function useCanvasInput(
canvasRef: Ref<HTMLCanvasElement | null>,
@@ -98,6 +92,18 @@ export function useCanvasInput(
return lx >= 0 && lx <= container.width && ly >= 0 && ly <= container.height
}
+ const hitFns: HitTestFns = {
+ hitTestInScope,
+ isInsideContainerBounds,
+ hitTestSectionTitle,
+ hitTestComponentLabel,
+ hitTestFrameTitle
+ }
+
+ function setDrag(d: DragState) {
+ drag.value = d
+ }
+
function startPanDrag(e: MouseEvent) {
drag.value = {
type: 'pan',
@@ -172,133 +178,67 @@ export function useCanvasInput(
return true
}
- function duplicateAndDrag(
- cx: number,
- cy: number
- ): Map<string, { x: number; y: number; parentId: string }> {
- const newIds: string[] = []
- const newOriginals = new Map<string, { x: number; y: number; parentId: string }>()
- for (const id of editor.state.selectedIds) {
- const src = editor.graph.getNode(id)
- if (!src) continue
- const newId = editor.createShape(src.type, src.x, src.y, src.width, src.height)
- editor.graph.updateNode(newId, {
- name: src.name + ' copy',
- fills: [...src.fills],
- strokes: [...src.strokes],
- effects: [...src.effects],
- cornerRadius: src.cornerRadius,
- opacity: src.opacity,
- rotation: src.rotation
- })
- newIds.push(newId)
- const newNode = editor.graph.getNode(newId)
- newOriginals.set(newId, {
- x: src.x,
- y: src.y,
- parentId: newNode?.parentId ?? editor.state.currentPageId
- })
- }
- editor.select(newIds)
- drag.value = {
- type: 'move',
- startX: cx,
- startY: cy,
- originals: newOriginals,
- duplicated: true
- }
- editor.requestRender()
- return newOriginals
- }
-
- function detectAutoLayoutParent(): string | undefined {
- if (editor.state.selectedIds.size !== 1) return undefined
- const selectedId = [...editor.state.selectedIds][0]
- const selectedNode = editor.graph.getNode(selectedId)
- if (!selectedNode?.parentId) return undefined
- const parent = editor.graph.getNode(selectedNode.parentId)
- if (parent && parent.layoutMode !== 'NONE' && selectedNode.layoutPositioning !== 'ABSOLUTE') {
- return parent.id
- }
- return undefined
+ function handlePanMove(d: DragPan, e: MouseEvent) {
+ const dx = e.clientX - d.startScreenX
+ const dy = e.clientY - d.startScreenY
+ editor.state.panX = d.startPanX + dx
+ editor.state.panY = d.startPanY + dy
+ editor.requestRepaint()
}
- 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 = editor.state.enteredContainerId
- if (!scopeId) return null
+ function handleRotateMove(d: DragRotate, sx: number, sy: number, shiftKey: boolean) {
+ const currentAngle = Math.atan2(sy - d.centerY, sx - d.centerX) * (180 / Math.PI)
+ let rotation = d.origRotation + (currentAngle - d.startAngle)
- if (isInsideContainerBounds(cx, cy, scopeId)) {
- editor.clearSelection()
- return null
+ if (shiftKey) {
+ rotation = Math.round(rotation / ROTATION_SNAP_DEGREES) * ROTATION_SNAP_DEGREES
}
- editor.exitContainer()
- const afterExit = hitTestInScope(cx, cy, false)
- if (afterExit) return afterExit
+ rotation = ((((rotation + 180) % 360) + 360) % 360) - 180
+ editor.setRotationPreview({ nodeId: d.nodeId, angle: rotation })
+ }
- if (editor.state.enteredContainerId) {
- editor.exitContainer()
+ function handleTextSelectMove(cx: number, cy: number) {
+ const textEd = editor.textEditor
+ const editNode = editor.state.editingTextId
+ ? editor.graph.getNode(editor.state.editingTextId)
+ : null
+ if (textEd && editNode) {
+ const abs = editor.graph.getAbsolutePosition(editNode.id)
+ textEd.setCursorAt(cx - abs.x, cy - abs.y, true)
+ editor.requestRender()
}
- return null
}
- function handleSelectDown(e: MouseEvent, sx: number, sy: number, cx: number, cy: number) {
- if (editor.state.editingTextId && handleTextEditClick(cx, cy, e.shiftKey)) return
-
- if (editor.state.editingTextId) editor.commitTextEdit()
-
- if (tryStartRotation(sx, sy)) return
+ function handleMarqueeMove(d: DragMarquee, cx: number, cy: number) {
+ const minX = Math.min(d.startX, cx)
+ const minY = Math.min(d.startY, cy)
+ const maxX = Math.max(d.startX, cx)
+ const maxY = Math.max(d.startY, cy)
- const resizeDrag = tryStartResize(sx, sy, cx, cy, editor)
- if (resizeDrag) {
- drag.value = resizeDrag
- return
- }
+ const scopeId = editor.state.enteredContainerId
+ const parentId = scopeId ?? editor.state.currentPageId
+ const localMin = scopeId ? canvasToLocal(minX, minY, scopeId) : { lx: minX, ly: minY }
+ const localMax = scopeId ? canvasToLocal(maxX, maxY, scopeId) : { lx: maxX, ly: maxY }
+ const localMinX = Math.min(localMin.lx, localMax.lx)
+ const localMinY = Math.min(localMin.ly, localMax.ly)
+ const localMaxX = Math.max(localMin.lx, localMax.lx)
+ const localMaxY = Math.max(localMin.ly, localMax.ly)
- const hit = resolveHit(cx, cy)
- if (!hit) {
- if (!editor.state.enteredContainerId) {
- editor.clearSelection()
- drag.value = { type: 'marquee', startX: cx, startY: cy }
+ const hits: string[] = []
+ for (const node of editor.graph.getChildren(parentId)) {
+ if (!node.visible || node.locked) continue
+ if (
+ node.x + node.width > localMinX &&
+ node.x < localMaxX &&
+ node.y + node.height > localMinY &&
+ node.y < localMaxY
+ ) {
+ hits.push(node.id)
}
- return
- }
-
- if (!editor.state.selectedIds.has(hit.id) && !e.shiftKey) {
- editor.select([hit.id])
- } else if (e.shiftKey) {
- editor.select([hit.id], true)
- }
-
- const allLocked = [...editor.state.selectedIds].every((id) => editor.graph.getNode(id)?.locked)
- if (allLocked) return
-
- const originals = new Map<string, { x: number; y: number; parentId: string }>()
- for (const id of editor.state.selectedIds) {
- const n = editor.graph.getNode(id)
- if (n)
- originals.set(id, { x: n.x, y: n.y, parentId: n.parentId ?? editor.state.currentPageId })
- }
-
- if (e.altKey && editor.state.selectedIds.size > 0) {
- duplicateAndDrag(cx, cy)
- return
- }
-
- drag.value = {
- type: 'move',
- startX: cx,
- startY: cy,
- originals,
- autoLayoutParentId: detectAutoLayoutParent()
}
+ editor.select(hits)
+ editor.setMarquee({ x: minX, y: minY, width: maxX - minX, height: maxY - minY })
}
function onMouseDown(e: MouseEvent) {
@@ -331,7 +271,10 @@ export function useCanvasInput(
}
if (tool === 'SELECT') {
- handleSelectDown(e, sx, sy, cx, cy)
+ handleSelectDown(
+ e, sx, sy, cx, cy, editor, hitFns,
+ tryStartRotation, handleTextEditClick, setDrag
+ )
return
}
@@ -360,248 +303,6 @@ export function useCanvasInput(
drag.value = { type: 'draw', startX: cx, startY: cy, nodeId }
}
- function updateHoverCursor(e: MouseEvent) {
- const { sx, sy, cx, cy } = getCoords(e)
- let cursor: string | null = null
-
- for (const id of editor.state.selectedIds) {
- const node = editor.graph.getNode(id)
- if (!node) continue
- const abs = editor.graph.getAbsolutePosition(id)
- const handle = hitTestHandle(
- sx,
- sy,
- abs.x,
- abs.y,
- node.width,
- node.height,
- editor.state.zoom,
- editor.state.panX,
- editor.state.panY,
- node.rotation
- )
- if (handle) {
- cursor = HANDLE_CURSORS[handle]
- break
- }
- }
-
- if (!cursor && editor.state.selectedIds.size === 1) {
- const id = [...editor.state.selectedIds][0]
- const node = editor.graph.getNode(id)
- if (node) {
- const abs = editor.graph.getAbsolutePosition(id)
- const corner = hitTestCornerRotation(
- sx,
- sy,
- abs.x,
- abs.y,
- node.width,
- node.height,
- editor.state.zoom,
- editor.state.panX,
- editor.state.panY,
- node.rotation
- )
- if (corner) {
- cursor = cornerRotationCursor(corner, node.rotation)
- }
- }
- }
-
- cursorOverride.value = cursor
-
- const hit =
- hitTestSectionTitle(cx, cy) ?? hitTestComponentLabel(cx, cy) ?? hitTestInScope(cx, cy, false)
- editor.setHoveredNode(hit && !editor.state.selectedIds.has(hit.id) ? hit.id : null)
- }
-
- function handlePanMove(d: DragPan, e: MouseEvent) {
- const dx = e.clientX - d.startScreenX
- const dy = e.clientY - d.startScreenY
- editor.state.panX = d.startPanX + dx
- editor.state.panY = d.startPanY + dy
- editor.requestRepaint()
- }
-
- function handleRotateMove(d: DragRotate, sx: number, sy: number, shiftKey: boolean) {
- const currentAngle = Math.atan2(sy - d.centerY, sx - d.centerX) * (180 / Math.PI)
- let rotation = d.origRotation + (currentAngle - d.startAngle)
-
- if (shiftKey) {
- rotation = Math.round(rotation / ROTATION_SNAP_DEGREES) * ROTATION_SNAP_DEGREES
- }
-
- rotation = ((((rotation + 180) % 360) + 360) % 360) - 180
- editor.setRotationPreview({ nodeId: d.nodeId, angle: rotation })
- }
-
- function findDropTarget(cx: number, cy: number) {
- let dropTarget = editor.graph.hitTestFrame(
- cx,
- cy,
- editor.state.selectedIds,
- editor.state.currentPageId
- )
- const movingSection = [...editor.state.selectedIds].some(
- (id) => editor.graph.getNode(id)?.type === 'SECTION'
- )
- if (
- movingSection &&
- dropTarget &&
- dropTarget.type !== 'SECTION' &&
- dropTarget.type !== 'CANVAS'
- ) {
- dropTarget = null
- }
- return dropTarget
- }
-
- function applyMoveSnap(d: DragMove, dx: number, dy: number): { dx: number; dy: number } {
- const selectedNodes: SceneNode[] = []
- for (const [id, orig] of d.originals) {
- const n = editor.graph.getNode(id)
- if (n) {
- const abs = editor.graph.getAbsolutePosition(id)
- const parentAbs = n.parentId ? editor.graph.getAbsolutePosition(n.parentId) : { x: 0, y: 0 }
- selectedNodes.push({
- ...n,
- x: abs.x - parentAbs.x - n.x + orig.x + dx,
- y: abs.y - parentAbs.y - n.y + orig.y + dy
- })
- }
- }
-
- const bounds = computeSelectionBounds(selectedNodes)
- if (!bounds) return { dx, dy }
-
- const firstId = [...d.originals.keys()][0]
- const firstNode = editor.graph.getNode(firstId)
- const parentId = firstNode?.parentId ?? editor.state.currentPageId
- const siblings = editor.graph.getChildren(parentId)
- const parentAbs = !editor.isTopLevel(parentId)
- ? editor.graph.getAbsolutePosition(parentId)
- : { x: 0, y: 0 }
- const absTargets = siblings.map((n) => ({
- ...n,
- x: n.x + parentAbs.x,
- y: n.y + parentAbs.y
- }))
- const absBounds = {
- x: bounds.x + parentAbs.x,
- y: bounds.y + parentAbs.y,
- width: bounds.width,
- height: bounds.height
- }
- const snap = computeSnap(editor.state.selectedIds, absBounds, absTargets)
- editor.setSnapGuides(snap.guides)
- return { dx: dx + snap.dx, dy: dy + snap.dy }
- }
-
- function handleMoveMove(d: DragMove, cx: number, cy: number) {
- let dx = cx - d.startX
- let dy = cy - d.startY
-
- if (d.autoLayoutParentId && !d.brokeFromAutoLayout) {
- const dist = Math.sqrt(dx * dx + dy * dy)
- if (dist < AUTO_LAYOUT_BREAK_THRESHOLD) {
- computeAutoLayoutIndicator(d, cx, cy, editor)
- return
- }
- d.brokeFromAutoLayout = true
- editor.setLayoutInsertIndicator(null)
- }
-
- const dropTarget = findDropTarget(cx, cy)
- const dropParent = dropTarget ? editor.graph.getNode(dropTarget.id) : null
-
- if (dropParent && dropParent.layoutMode !== 'NONE') {
- computeAutoLayoutIndicatorForFrame(dropParent, cx, cy, editor)
- editor.setDropTarget(dropParent.id)
- for (const [id, orig] of d.originals) {
- editor.graph.updateNode(id, {
- x: Math.round(orig.x + dx),
- y: Math.round(orig.y + dy)
- })
- }
- editor.requestRender()
- return
- }
-
- editor.setLayoutInsertIndicator(null)
-
- const snapped = applyMoveSnap(d, dx, dy)
- dx = snapped.dx
- dy = snapped.dy
-
- for (const [id, orig] of d.originals) {
- editor.updateNode(id, { x: Math.round(orig.x + dx), y: Math.round(orig.y + dy) })
- }
-
- editor.setDropTarget(dropTarget?.id ?? null)
- }
-
- function handleTextSelectMove(cx: number, cy: number) {
- const textEd = editor.textEditor
- const editNode = editor.state.editingTextId
- ? editor.graph.getNode(editor.state.editingTextId)
- : null
- if (textEd && editNode) {
- const abs = editor.graph.getAbsolutePosition(editNode.id)
- textEd.setCursorAt(cx - abs.x, cy - abs.y, true)
- editor.requestRender()
- }
- }
-
- function handleDrawMove(d: DragDraw, cx: number, cy: number, shiftKey: boolean) {
- let w = cx - d.startX
- let h = cy - d.startY
-
- if (shiftKey) {
- const size = Math.max(Math.abs(w), Math.abs(h))
- w = Math.sign(w) * size
- h = Math.sign(h) * size
- }
-
- editor.updateNode(d.nodeId, {
- x: w < 0 ? d.startX + w : d.startX,
- y: h < 0 ? d.startY + h : d.startY,
- width: Math.abs(w),
- height: Math.abs(h)
- })
- }
-
- function handleMarqueeMove(d: DragMarquee, cx: number, cy: number) {
- const minX = Math.min(d.startX, cx)
- const minY = Math.min(d.startY, cy)
- const maxX = Math.max(d.startX, cx)
- const maxY = Math.max(d.startY, cy)
-
- const scopeId = editor.state.enteredContainerId
- const parentId = scopeId ?? editor.state.currentPageId
- const localMin = scopeId ? canvasToLocal(minX, minY, scopeId) : { lx: minX, ly: minY }
- const localMax = scopeId ? canvasToLocal(maxX, maxY, scopeId) : { lx: maxX, ly: maxY }
- const localMinX = Math.min(localMin.lx, localMax.lx)
- const localMinY = Math.min(localMin.ly, localMax.ly)
- const localMaxX = Math.max(localMin.lx, localMax.lx)
- const localMaxY = Math.max(localMin.ly, localMax.ly)
-
- const hits: string[] = []
- for (const node of editor.graph.getChildren(parentId)) {
- if (!node.visible || node.locked) continue
- if (
- node.x + node.width > localMinX &&
- node.x < localMaxX &&
- node.y + node.height > localMinY &&
- node.y < localMaxY
- ) {
- hits.push(node.id)
- }
- }
- editor.select(hits)
- editor.setMarquee({ x: minX, y: minY, width: maxX - minX, height: maxY - minY })
- }
-
function onMouseMove(e: MouseEvent) {
if (onCursorMove) {
const { cx, cy } = getCoords(e)
@@ -622,7 +323,8 @@ export function useCanvasInput(
}
if (!drag.value && editor.state.activeTool === 'SELECT') {
- updateHoverCursor(e)
+ const { sx, sy, cx, cy } = getCoords(e)
+ cursorOverride.value = updateHoverCursor(sx, sy, cx, cy, editor, hitFns)
}
if (!drag.value) return
@@ -640,7 +342,7 @@ export function useCanvasInput(
return
}
if (d.type === 'move') {
- handleMoveMove(d, cx, cy)
+ handleMoveMove(d, cx, cy, editor)
return
}
if (d.type === 'text-select') {
@@ -662,74 +364,18 @@ export function useCanvasInput(
}
if (d.type === 'draw') {
- handleDrawMove(d, cx, cy, e.shiftKey)
+ handleDrawMove(d, cx, cy, e.shiftKey, editor)
return
}
handleMarqueeMove(d, cx, cy)
}
- function handleMoveUp(d: DragMove) {
- const indicator = editor.state.layoutInsertIndicator
- editor.setLayoutInsertIndicator(null)
- editor.setSnapGuides([])
-
- if (indicator) {
- for (const id of editor.state.selectedIds) {
- editor.reorderInAutoLayout(id, indicator.parentId, indicator.index)
- }
- editor.setDropTarget(null)
- return
- }
-
- const moved = [...d.originals].some(([id, orig]) => {
- const node = editor.graph.getNode(id)
- return node && (node.x !== orig.x || node.y !== orig.y)
- })
-
- if (moved) {
- const dropId = editor.state.dropTargetId
- if (dropId) {
- editor.reparentNodes([...editor.state.selectedIds], dropId)
- } else {
- reparentOutsideNodes()
- }
- editor.commitMoveWithReparent(d.originals)
- }
- editor.setDropTarget(null)
- }
-
- function reparentOutsideNodes() {
- for (const id of editor.state.selectedIds) {
- const node = editor.graph.getNode(id)
- if (!node?.parentId || editor.isTopLevel(node.parentId)) continue
- const parent = editor.graph.getNode(node.parentId)
- if (!parent || (parent.type !== 'FRAME' && parent.type !== 'SECTION')) continue
- const outsideX = node.x + node.width < 0 || node.x > parent.width
- const outsideY = node.y + node.height < 0 || node.y > parent.height
- if (outsideX || outsideY) {
- const grandparentId = parent.parentId ?? editor.state.currentPageId
- editor.graph.reparentNode(id, grandparentId)
- }
- }
- }
-
- function handleDrawUp(d: DragDraw) {
- const node = editor.graph.getNode(d.nodeId)
- if (node && node.width < 2 && node.height < 2) {
- editor.updateNode(d.nodeId, { width: 100, height: 100 })
- }
- if (node?.type === 'SECTION') {
- editor.adoptNodesIntoSection(node.id)
- }
- editor.setTool('SELECT')
- }
-
function onMouseUp() {
if (!drag.value) return
const d = drag.value
- if (d.type === 'move') handleMoveUp(d)
+ if (d.type === 'move') handleMoveUp(d, editor)
else if (d.type === 'text-select') {
drag.value = null
return
@@ -744,7 +390,7 @@ export function useCanvasInput(
editor.commitRotation(d.nodeId, d.origRotation)
}
editor.setRotationPreview(null)
- } else if (d.type === 'draw') handleDrawUp(d)
+ } else if (d.type === 'draw') handleDrawUp(d, editor)
else if (d.type === 'marquee') editor.setMarquee(null)
drag.value = null
diff --git a/packages/vue/src/input/draw.ts b/packages/vue/src/input/draw.ts
new file mode 100644
index 000000000..d078010c6
--- /dev/null
+++ b/packages/vue/src/input/draw.ts
@@ -0,0 +1,32 @@
+import type { Editor } from '@open-pencil/core/editor'
+
+import type { DragDraw } from './types'
+
+export function handleDrawMove(d: DragDraw, cx: number, cy: number, shiftKey: boolean, editor: Editor) {
+ let w = cx - d.startX
+ let h = cy - d.startY
+
+ if (shiftKey) {
+ const size = Math.max(Math.abs(w), Math.abs(h))
+ w = Math.sign(w) * size
+ h = Math.sign(h) * size
+ }
+
+ editor.updateNode(d.nodeId, {
+ x: w < 0 ? d.startX + w : d.startX,
+ y: h < 0 ? d.startY + h : d.startY,
+ width: Math.abs(w),
+ height: Math.abs(h)
+ })
+}
+
+export function handleDrawUp(d: DragDraw, editor: Editor) {
+ const node = editor.graph.getNode(d.nodeId)
+ if (node && node.width < 2 && node.height < 2) {
+ editor.updateNode(d.nodeId, { width: 100, height: 100 })
+ }
+ if (node?.type === 'SECTION') {
+ editor.adoptNodesIntoSection(node.id)
+ }
+ editor.setTool('SELECT')
+}
diff --git a/packages/vue/src/input/move.ts b/packages/vue/src/input/move.ts
new file mode 100644
index 000000000..365aa50a6
--- /dev/null
+++ b/packages/vue/src/input/move.ts
@@ -0,0 +1,215 @@
+import { AUTO_LAYOUT_BREAK_THRESHOLD, computeSelectionBounds, computeSnap } from '@open-pencil/core'
+import type { SceneNode } from '@open-pencil/core'
+import type { Editor } from '@open-pencil/core/editor'
+
+import type { DragMove, DragState } from './types'
+import { computeAutoLayoutIndicator, computeAutoLayoutIndicatorForFrame } from './auto-layout'
+
+export function detectAutoLayoutParent(editor: Editor): string | undefined {
+ if (editor.state.selectedIds.size !== 1) return undefined
+ const selectedId = [...editor.state.selectedIds][0]
+ const selectedNode = editor.graph.getNode(selectedId)
+ if (!selectedNode?.parentId) return undefined
+ const parent = editor.graph.getNode(selectedNode.parentId)
+ if (parent && parent.layoutMode !== 'NONE' && selectedNode.layoutPositioning !== 'ABSOLUTE') {
+ return parent.id
+ }
+ return undefined
+}
+
+export function duplicateAndDrag(
+ cx: number,
+ cy: number,
+ editor: Editor
+): { originals: Map<string, { x: number; y: number; parentId: string }>; drag: DragState } {
+ const newIds: string[] = []
+ const newOriginals = new Map<string, { x: number; y: number; parentId: string }>()
+ for (const id of editor.state.selectedIds) {
+ const src = editor.graph.getNode(id)
+ if (!src) continue
+ const newId = editor.createShape(src.type, src.x, src.y, src.width, src.height)
+ editor.graph.updateNode(newId, {
+ name: src.name + ' copy',
+ fills: [...src.fills],
+ strokes: [...src.strokes],
+ effects: [...src.effects],
+ cornerRadius: src.cornerRadius,
+ opacity: src.opacity,
+ rotation: src.rotation
+ })
+ newIds.push(newId)
+ const newNode = editor.graph.getNode(newId)
+ newOriginals.set(newId, {
+ x: src.x,
+ y: src.y,
+ parentId: newNode?.parentId ?? editor.state.currentPageId
+ })
+ }
+ editor.select(newIds)
+ editor.requestRender()
+ return {
+ originals: newOriginals,
+ drag: {
+ type: 'move',
+ startX: cx,
+ startY: cy,
+ originals: newOriginals,
+ duplicated: true
+ }
+ }
+}
+
+function findDropTarget(cx: number, cy: number, editor: Editor): SceneNode | null {
+ let dropTarget = editor.graph.hitTestFrame(
+ cx,
+ cy,
+ editor.state.selectedIds,
+ editor.state.currentPageId
+ )
+ const movingSection = [...editor.state.selectedIds].some(
+ (id) => editor.graph.getNode(id)?.type === 'SECTION'
+ )
+ if (
+ movingSection &&
+ dropTarget &&
+ dropTarget.type !== 'SECTION' &&
+ dropTarget.type !== 'CANVAS'
+ ) {
+ dropTarget = null
+ }
+ return dropTarget
+}
+
+function applyMoveSnap(
+ d: DragMove,
+ dx: number,
+ dy: number,
+ editor: Editor
+): { dx: number; dy: number } {
+ const selectedNodes: SceneNode[] = []
+ for (const [id, orig] of d.originals) {
+ const n = editor.graph.getNode(id)
+ if (n) {
+ const abs = editor.graph.getAbsolutePosition(id)
+ const parentAbs = n.parentId ? editor.graph.getAbsolutePosition(n.parentId) : { x: 0, y: 0 }
+ selectedNodes.push({
+ ...n,
+ x: abs.x - parentAbs.x - n.x + orig.x + dx,
+ y: abs.y - parentAbs.y - n.y + orig.y + dy
+ })
+ }
+ }
+
+ const bounds = computeSelectionBounds(selectedNodes)
+ if (!bounds) return { dx, dy }
+
+ const firstId = [...d.originals.keys()][0]
+ const firstNode = editor.graph.getNode(firstId)
+ const parentId = firstNode?.parentId ?? editor.state.currentPageId
+ const siblings = editor.graph.getChildren(parentId)
+ const parentAbs = !editor.isTopLevel(parentId)
+ ? editor.graph.getAbsolutePosition(parentId)
+ : { x: 0, y: 0 }
+ const absTargets = siblings.map((n) => ({
+ ...n,
+ x: n.x + parentAbs.x,
+ y: n.y + parentAbs.y
+ }))
+ const absBounds = {
+ x: bounds.x + parentAbs.x,
+ y: bounds.y + parentAbs.y,
+ width: bounds.width,
+ height: bounds.height
+ }
+ const snap = computeSnap(editor.state.selectedIds, absBounds, absTargets)
+ editor.setSnapGuides(snap.guides)
+ return { dx: dx + snap.dx, dy: dy + snap.dy }
+}
+
+export function handleMoveMove(d: DragMove, cx: number, cy: number, editor: Editor) {
+ let dx = cx - d.startX
+ let dy = cy - d.startY
+
+ if (d.autoLayoutParentId && !d.brokeFromAutoLayout) {
+ const dist = Math.sqrt(dx * dx + dy * dy)
+ if (dist < AUTO_LAYOUT_BREAK_THRESHOLD) {
+ computeAutoLayoutIndicator(d, cx, cy, editor)
+ return
+ }
+ d.brokeFromAutoLayout = true
+ editor.setLayoutInsertIndicator(null)
+ }
+
+ const dropTarget = findDropTarget(cx, cy, editor)
+ const dropParent = dropTarget ? editor.graph.getNode(dropTarget.id) : null
+
+ if (dropParent && dropParent.layoutMode !== 'NONE') {
+ computeAutoLayoutIndicatorForFrame(dropParent, cx, cy, editor)
+ editor.setDropTarget(dropParent.id)
+ for (const [id, orig] of d.originals) {
+ editor.graph.updateNode(id, {
+ x: Math.round(orig.x + dx),
+ y: Math.round(orig.y + dy)
+ })
+ }
+ editor.requestRender()
+ return
+ }
+
+ editor.setLayoutInsertIndicator(null)
+
+ const snapped = applyMoveSnap(d, dx, dy, editor)
+ dx = snapped.dx
+ dy = snapped.dy
+
+ for (const [id, orig] of d.originals) {
+ editor.updateNode(id, { x: Math.round(orig.x + dx), y: Math.round(orig.y + dy) })
+ }
+
+ editor.setDropTarget(dropTarget?.id ?? null)
+}
+
+function reparentOutsideNodes(editor: Editor) {
+ for (const id of editor.state.selectedIds) {
+ const node = editor.graph.getNode(id)
+ if (!node?.parentId || editor.isTopLevel(node.parentId)) continue
+ const parent = editor.graph.getNode(node.parentId)
+ if (!parent || (parent.type !== 'FRAME' && parent.type !== 'SECTION')) continue
+ const outsideX = node.x + node.width < 0 || node.x > parent.width
+ const outsideY = node.y + node.height < 0 || node.y > parent.height
+ if (outsideX || outsideY) {
+ const grandparentId = parent.parentId ?? editor.state.currentPageId
+ editor.graph.reparentNode(id, grandparentId)
+ }
+ }
+}
+
+export function handleMoveUp(d: DragMove, editor: Editor) {
+ const indicator = editor.state.layoutInsertIndicator
+ editor.setLayoutInsertIndicator(null)
+ editor.setSnapGuides([])
+
+ if (indicator) {
+ for (const id of editor.state.selectedIds) {
+ editor.reorderInAutoLayout(id, indicator.parentId, indicator.index)
+ }
+ editor.setDropTarget(null)
+ return
+ }
+
+ const moved = [...d.originals].some(([id, orig]) => {
+ const node = editor.graph.getNode(id)
+ return node && (node.x !== orig.x || node.y !== orig.y)
+ })
+
+ if (moved) {
+ const dropId = editor.state.dropTargetId
+ if (dropId) {
+ editor.reparentNodes([...editor.state.selectedIds], dropId)
+ } else {
+ reparentOutsideNodes(editor)
+ }
+ editor.commitMoveWithReparent(d.originals)
+ }
+ editor.setDropTarget(null)
+}
diff --git a/packages/vue/src/input/select.ts b/packages/vue/src/input/select.ts
new file mode 100644
index 000000000..c950e121b
--- /dev/null
+++ b/packages/vue/src/input/select.ts
@@ -0,0 +1,172 @@
+import type { SceneNode } from '@open-pencil/core'
+import type { Editor } from '@open-pencil/core/editor'
+
+import type { DragState } from './types'
+import {
+ HANDLE_CURSORS,
+ hitTestHandle,
+ hitTestCornerRotation,
+ cornerRotationCursor
+} from './geometry'
+import { tryStartResize } from './resize'
+import { duplicateAndDrag, detectAutoLayoutParent } from './move'
+
+export interface HitTestFns {
+ hitTestInScope: (cx: number, cy: number, deep: boolean) => SceneNode | null
+ isInsideContainerBounds: (cx: number, cy: number, containerId: string) => boolean
+ hitTestSectionTitle: (cx: number, cy: number) => SceneNode | null
+ hitTestComponentLabel: (cx: number, cy: number) => SceneNode | null
+ hitTestFrameTitle: (cx: number, cy: number) => SceneNode | null
+}
+
+export function resolveHit(cx: number, cy: number, editor: Editor, fns: HitTestFns): SceneNode | null {
+ const titleHit =
+ fns.hitTestFrameTitle(cx, cy) ?? fns.hitTestSectionTitle(cx, cy) ?? fns.hitTestComponentLabel(cx, cy)
+ if (titleHit) return titleHit
+
+ const hit = fns.hitTestInScope(cx, cy, false)
+ if (hit) return hit
+
+ const scopeId = editor.state.enteredContainerId
+ if (!scopeId) return null
+
+ if (fns.isInsideContainerBounds(cx, cy, scopeId)) {
+ editor.clearSelection()
+ return null
+ }
+
+ editor.exitContainer()
+ const afterExit = fns.hitTestInScope(cx, cy, false)
+ if (afterExit) return afterExit
+
+ if (editor.state.enteredContainerId) {
+ editor.exitContainer()
+ }
+ return null
+}
+
+export function handleSelectDown(
+ e: MouseEvent,
+ sx: number,
+ sy: number,
+ cx: number,
+ cy: number,
+ editor: Editor,
+ fns: HitTestFns,
+ tryStartRotation: (sx: number, sy: number) => boolean,
+ handleTextEditClick: (cx: number, cy: number, shiftKey: boolean) => boolean,
+ setDrag: (d: DragState) => void
+) {
+ if (editor.state.editingTextId && handleTextEditClick(cx, cy, e.shiftKey)) return
+
+ if (editor.state.editingTextId) editor.commitTextEdit()
+
+ if (tryStartRotation(sx, sy)) return
+
+ const resizeDrag = tryStartResize(sx, sy, cx, cy, editor)
+ if (resizeDrag) {
+ setDrag(resizeDrag)
+ return
+ }
+
+ const hit = resolveHit(cx, cy, editor, fns)
+ if (!hit) {
+ if (!editor.state.enteredContainerId) {
+ editor.clearSelection()
+ setDrag({ type: 'marquee', startX: cx, startY: cy })
+ }
+ return
+ }
+
+ if (!editor.state.selectedIds.has(hit.id) && !e.shiftKey) {
+ editor.select([hit.id])
+ } else if (e.shiftKey) {
+ editor.select([hit.id], true)
+ }
+
+ const allLocked = [...editor.state.selectedIds].every((id) => editor.graph.getNode(id)?.locked)
+ if (allLocked) return
+
+ const originals = new Map<string, { x: number; y: number; parentId: string }>()
+ for (const id of editor.state.selectedIds) {
+ const n = editor.graph.getNode(id)
+ if (n)
+ originals.set(id, { x: n.x, y: n.y, parentId: n.parentId ?? editor.state.currentPageId })
+ }
+
+ if (e.altKey && editor.state.selectedIds.size > 0) {
+ const result = duplicateAndDrag(cx, cy, editor)
+ setDrag(result.drag)
+ return
+ }
+
+ setDrag({
+ type: 'move',
+ startX: cx,
+ startY: cy,
+ originals,
+ autoLayoutParentId: detectAutoLayoutParent(editor)
+ })
+}
+
+export function updateHoverCursor(
+ sx: number,
+ sy: number,
+ cx: number,
+ cy: number,
+ editor: Editor,
+ fns: Pick<HitTestFns, 'hitTestInScope' | 'hitTestSectionTitle' | 'hitTestComponentLabel'>
+): string | null {
+ let cursor: string | null = null
+
+ for (const id of editor.state.selectedIds) {
+ const node = editor.graph.getNode(id)
+ if (!node) continue
+ const abs = editor.graph.getAbsolutePosition(id)
+ const handle = hitTestHandle(
+ sx,
+ sy,
+ abs.x,
+ abs.y,
+ node.width,
+ node.height,
+ editor.state.zoom,
+ editor.state.panX,
+ editor.state.panY,
+ node.rotation
+ )
+ if (handle) {
+ cursor = HANDLE_CURSORS[handle]
+ break
+ }
+ }
+
+ if (!cursor && editor.state.selectedIds.size === 1) {
+ const id = [...editor.state.selectedIds][0]
+ const node = editor.graph.getNode(id)
+ if (node) {
+ const abs = editor.graph.getAbsolutePosition(id)
+ const corner = hitTestCornerRotation(
+ sx,
+ sy,
+ abs.x,
+ abs.y,
+ node.width,
+ node.height,
+ editor.state.zoom,
+ editor.state.panX,
+ editor.state.panY,
+ node.rotation
+ )
+ if (corner) {
+ cursor = cornerRotationCursor(corner, node.rotation)
+ }
+ }
+ }
+
+ const hit =
+ fns.hitTestSectionTitle(cx, cy) ?? fns.hitTestComponentLabel(cx, cy) ?? fns.hitTestInScope(cx, cy, false)
+ editor.setHoveredNode(hit && !editor.state.selectedIds.has(hit.id) ? hit.id : null)
+
+ return cursor
+}