Skip to content

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

Move composables to @open-pencil/vue SDK, unify Editor type

Danila Poyarkov608be0ea2738
diff --git a/packages/core/src/editor/clipboard.ts b/packages/core/src/editor/clipboard.ts
index 68c195a1f..a6245941b 100644
--- a/packages/core/src/editor/clipboard.ts
+++ b/packages/core/src/editor/clipboard.ts
@@ -7,7 +7,7 @@ import {
import { collectFontKeys } from '../fonts'
import { computeImageHash } from '../figma-api'
-import type { SceneGraph, SceneNode } from '../scene-graph'
+import type { Fill, SceneGraph, SceneNode } from '../scene-graph'
import type { Vector } from '../types'
import type { EditorContext } from './types'
@@ -195,6 +195,99 @@ export function createClipboardActions(ctx: EditorContext) {
return hash
}
+ const IMAGE_MAX_DIMENSION = 4096
+ const IMAGE_GAP = 20
+
+ function decodeImageDimensions(bytes: Uint8Array): { w: number; h: number } | null {
+ const ck = ctx.getCk()
+ if (!ck) return null
+ const skImg = ck.MakeImageFromEncoded(bytes)
+ if (!skImg) return null
+ let w = skImg.width()
+ let h = skImg.height()
+ skImg.delete()
+ if (w > IMAGE_MAX_DIMENSION || h > IMAGE_MAX_DIMENSION) {
+ const ratio = Math.min(IMAGE_MAX_DIMENSION / w, IMAGE_MAX_DIMENSION / h)
+ w = Math.round(w * ratio)
+ h = Math.round(h * ratio)
+ }
+ return { w, h }
+ }
+
+ function placeImageNode(
+ bytes: Uint8Array,
+ x: number,
+ y: number,
+ w: number,
+ h: number,
+ name = 'Image'
+ ): string | null {
+ const hash = storeImage(bytes)
+ const displayName = name.replace(/\.[^.]+$/, '')
+ const pid = ctx.state.currentPageId
+ const fill: Fill = {
+ type: 'IMAGE',
+ imageHash: hash,
+ imageScaleMode: 'FILL',
+ color: { r: 0, g: 0, b: 0, a: 0 },
+ opacity: 1,
+ visible: true
+ }
+ const node = ctx.graph.createNode('RECTANGLE', pid, {
+ name: displayName,
+ x,
+ y,
+ width: w,
+ height: h,
+ fills: [fill]
+ })
+ const id = node.id
+ const snapshot = { ...node }
+ ctx.undo.push({
+ label: 'Place image',
+ forward: () => {
+ ctx.graph.images.set(hash, bytes)
+ ctx.graph.createNode(snapshot.type, pid, snapshot)
+ },
+ inverse: () => {
+ ctx.graph.deleteNode(id)
+ ctx.graph.images.delete(hash)
+ const next = new Set(ctx.state.selectedIds)
+ next.delete(id)
+ ctx.state.selectedIds = next
+ }
+ })
+ return id
+ }
+
+ async function placeImageFiles(files: File[], cx: number, cy: number) {
+ const prepared: Array<{ bytes: Uint8Array; name: string; w: number; h: number }> = []
+ for (const file of files) {
+ const bytes = new Uint8Array(await file.arrayBuffer())
+ const dims = decodeImageDimensions(bytes)
+ if (dims) prepared.push({ bytes, name: file.name, ...dims })
+ }
+ if (!prepared.length) return
+
+ let totalW = 0
+ for (const p of prepared) totalW += p.w
+ totalW += IMAGE_GAP * (prepared.length - 1)
+ const maxH = Math.max(...prepared.map((p) => p.h))
+
+ let curX = cx - totalW / 2
+ const topY = cy - maxH / 2
+ const ids: string[] = []
+ for (const p of prepared) {
+ const id = placeImageNode(p.bytes, curX, topY, p.w, p.h, p.name)
+ if (id) ids.push(id)
+ curX += p.w + IMAGE_GAP
+ }
+ if (ids.length) {
+ ctx.state.selectedIds = new Set(ids)
+ ctx.requestRender()
+ }
+ }
+
return {
collectSubtrees,
centerNodesAt,
@@ -204,6 +297,7 @@ export function createClipboardActions(ctx: EditorContext) {
pasteFromHTML,
warnMissingImages,
deleteSelected,
- storeImage
+ storeImage,
+ placeImageFiles
}
}
diff --git a/packages/core/src/editor/create.ts b/packages/core/src/editor/create.ts
index 56ce97173..735f145d4 100644
--- a/packages/core/src/editor/create.ts
+++ b/packages/core/src/editor/create.ts
@@ -242,6 +242,7 @@ export function createEditor(options?: EditorOptions) {
pasteFromHTML: clipboard.pasteFromHTML,
deleteSelected: clipboard.deleteSelected,
storeImage: clipboard.storeImage,
+ placeImageFiles: clipboard.placeImageFiles,
loadFontsForNodes: clipboard.loadFontsForNodes,
// Components — bridge functions
diff --git a/packages/vue/src/assets/rotate-cursor.svg b/packages/vue/src/assets/rotate-cursor.svg
new file mode 100644
index 000000000..0c7e32aac
--- /dev/null
+++ b/packages/vue/src/assets/rotate-cursor.svg
@@ -0,0 +1,3 @@
+<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="680 1800 640 620">
+ <path d="M1113.142,1956.331C1008.608,1982.71 887.611,2049.487 836.035,2213.487L891.955,2219.403L779,2396L705.496,2199.678L772.745,2206.792C832.051,1999.958 984.143,1921.272 1110.63,1892.641L1107.952,1824.711L1299,1911L1115.34,2012.065L1113.142,1956.331Z" fill="black" stroke="white" stroke-width="33.33"/>
+</svg>
diff --git a/packages/vue/src/composables/use-canvas-drop.ts b/packages/vue/src/composables/use-canvas-drop.ts
new file mode 100644
index 000000000..76f259bea
--- /dev/null
+++ b/packages/vue/src/composables/use-canvas-drop.ts
@@ -0,0 +1,68 @@
+import { useEventListener } from '@vueuse/core'
+import { ref, type Ref } from 'vue'
+
+import type { Editor } from '@open-pencil/core/editor'
+
+const ACCEPTED_TYPES = new Set(['image/png', 'image/jpeg', 'image/webp', 'image/gif', 'image/avif'])
+
+export function useCanvasDrop(canvasRef: Ref<HTMLCanvasElement | null>, editor: Editor) {
+ const isDraggingOver = ref(false)
+
+ useEventListener(canvasRef, 'dragover', (e: DragEvent) => {
+ if (!hasImageFiles(e)) return
+ e.preventDefault()
+ if (e.dataTransfer) e.dataTransfer.dropEffect = 'copy'
+ isDraggingOver.value = true
+ })
+
+ useEventListener(canvasRef, 'dragenter', (e: DragEvent) => {
+ if (!hasImageFiles(e)) return
+ e.preventDefault()
+ isDraggingOver.value = true
+ })
+
+ useEventListener(canvasRef, 'dragleave', () => {
+ isDraggingOver.value = false
+ })
+
+ useEventListener(canvasRef, 'drop', (e: DragEvent) => {
+ e.preventDefault()
+ isDraggingOver.value = false
+
+ const files = filterImageFiles(e.dataTransfer?.files ?? null)
+ if (!files.length) return
+
+ const canvas = canvasRef.value
+ if (!canvas) return
+
+ const rect = canvas.getBoundingClientRect()
+ const sx = e.clientX - rect.left
+ const sy = e.clientY - rect.top
+ const { x: cx, y: cy } = editor.screenToCanvas(sx, sy)
+
+ void editor.placeImageFiles(files, cx, cy)
+ })
+
+ return { isDraggingOver }
+}
+
+function hasImageFiles(e: DragEvent): boolean {
+ if (!e.dataTransfer?.types.includes('Files')) return false
+ for (const item of e.dataTransfer.items) {
+ if (item.kind === 'file' && ACCEPTED_TYPES.has(item.type)) return true
+ }
+ return false
+}
+
+function filterImageFiles(files: FileList | null): File[] {
+ if (!files) return []
+ const result: File[] = []
+ for (const file of files) {
+ if (ACCEPTED_TYPES.has(file.type)) result.push(file)
+ }
+ return result
+}
+
+export function extractImageFilesFromClipboard(e: ClipboardEvent): File[] {
+ return filterImageFiles(e.clipboardData?.files ?? null)
+}
diff --git a/packages/vue/src/composables/use-canvas-input.ts b/packages/vue/src/composables/use-canvas-input.ts
new file mode 100644
index 000000000..05c8d1924
--- /dev/null
+++ b/packages/vue/src/composables/use-canvas-input.ts
@@ -0,0 +1,1497 @@
+import { useEventListener } from '@vueuse/core'
+import { ref, type Ref } from 'vue'
+
+import {
+ AUTO_LAYOUT_BREAK_THRESHOLD,
+ CORNER_ROTATE_ZONE,
+ HANDLE_HIT_RADIUS,
+ PEN_CLOSE_THRESHOLD,
+ ROTATION_SNAP_DEGREES,
+ DEFAULT_TEXT_WIDTH,
+ DEFAULT_TEXT_HEIGHT,
+ computeSelectionBounds,
+ computeSnap,
+ degToRad
+} from '@open-pencil/core'
+
+import type { Editor, Tool } from '@open-pencil/core/editor'
+import type { NodeType, Rect, SceneNode, Vector } from '@open-pencil/core'
+
+type HandlePosition = 'nw' | 'n' | 'ne' | 'e' | 'se' | 's' | 'sw' | 'w'
+
+interface DragDraw {
+ type: 'draw'
+ startX: number
+ startY: number
+ nodeId: string
+}
+
+interface DragMove {
+ type: 'move'
+ startX: number
+ startY: number
+ originals: Map<string, { x: number; y: number; parentId: string }>
+ duplicated?: boolean
+ autoLayoutParentId?: string
+ brokeFromAutoLayout?: boolean
+}
+
+interface DragPan {
+ type: 'pan'
+ startScreenX: number
+ startScreenY: number
+ startPanX: number
+ startPanY: number
+}
+
+interface DragResize {
+ type: 'resize'
+ handle: HandlePosition
+ startX: number
+ startY: number
+ origRect: Rect
+ nodeId: string
+}
+
+interface DragMarquee {
+ type: 'marquee'
+ startX: number
+ startY: number
+}
+
+interface DragRotate {
+ type: 'rotate'
+ nodeId: string
+ centerX: number
+ centerY: number
+ startAngle: number
+ origRotation: number
+}
+
+interface DragPen {
+ type: 'pen-drag'
+ startX: number
+ startY: number
+}
+
+interface DragTextSelect {
+ type: 'text-select'
+ startX: number
+ startY: number
+}
+
+type DragState =
+ | DragDraw
+ | DragMove
+ | DragPan
+ | DragResize
+ | DragMarquee
+ | DragRotate
+ | DragPen
+ | DragTextSelect
+
+const TOOL_TO_NODE: Partial<Record<Tool, NodeType>> = {
+ FRAME: 'FRAME',
+ SECTION: 'SECTION',
+ RECTANGLE: 'RECTANGLE',
+ ELLIPSE: 'ELLIPSE',
+ LINE: 'LINE',
+ POLYGON: 'POLYGON',
+ STAR: 'STAR',
+ TEXT: 'TEXT'
+}
+
+const HANDLE_CURSORS: Record<HandlePosition, string> = {
+ nw: 'nwse-resize',
+ n: 'ns-resize',
+ ne: 'nesw-resize',
+ e: 'ew-resize',
+ se: 'nwse-resize',
+ s: 'ns-resize',
+ sw: 'nesw-resize',
+ w: 'ew-resize'
+}
+
+function getScreenRect(
+ absX: number,
+ absY: number,
+ w: number,
+ h: number,
+ zoom: number,
+ panX: number,
+ panY: number
+) {
+ return {
+ x1: absX * zoom + panX,
+ y1: absY * zoom + panY,
+ x2: (absX + w) * zoom + panX,
+ y2: (absY + h) * zoom + panY
+ }
+}
+
+function getHandlePositions(
+ absX: number,
+ absY: number,
+ w: number,
+ h: number,
+ zoom: number,
+ panX: number,
+ panY: number
+) {
+ const { x1, y1, x2, y2 } = getScreenRect(absX, absY, w, h, zoom, panX, panY)
+ const mx = (x1 + x2) / 2
+ const my = (y1 + y2) / 2
+
+ return {
+ nw: { x: x1, y: y1 },
+ n: { x: mx, y: y1 },
+ ne: { x: x2, y: y1 },
+ e: { x: x2, y: my },
+ se: { x: x2, y: y2 },
+ s: { x: mx, y: y2 },
+ sw: { x: x1, y: y2 },
+ w: { x: x1, y: my }
+ } satisfies Record<HandlePosition, Vector>
+}
+
+function unrotate(
+ sx: number,
+ sy: number,
+ centerX: number,
+ centerY: number,
+ rotation: number
+): { sx: number; sy: number } {
+ if (rotation === 0) return { sx, sy }
+ const rad = (-rotation * Math.PI) / 180
+ const cos = Math.cos(rad)
+ const sin = Math.sin(rad)
+ const dx = sx - centerX
+ const dy = sy - centerY
+ return {
+ sx: centerX + dx * cos - dy * sin,
+ sy: centerY + dx * sin + dy * cos
+ }
+}
+
+function hitTestHandle(
+ sx: number,
+ sy: number,
+ absX: number,
+ absY: number,
+ w: number,
+ h: number,
+ zoom: number,
+ panX: number,
+ panY: number,
+ rotation = 0
+): HandlePosition | null {
+ const { x1, y1, x2, y2 } = getScreenRect(absX, absY, w, h, zoom, panX, panY)
+ const cx = (x1 + x2) / 2
+ const cy = (y1 + y2) / 2
+ const ur = unrotate(sx, sy, cx, cy, rotation)
+
+ const handles = getHandlePositions(absX, absY, w, h, zoom, panX, panY)
+ for (const [pos, pt] of Object.entries(handles)) {
+ if (Math.abs(ur.sx - pt.x) < HANDLE_HIT_RADIUS && Math.abs(ur.sy - pt.y) < HANDLE_HIT_RADIUS) {
+ return pos as HandlePosition
+ }
+ }
+ return null
+}
+
+type CornerPosition = 'nw' | 'ne' | 'se' | 'sw'
+
+function hitTestCornerRotation(
+ sx: number,
+ sy: number,
+ absX: number,
+ absY: number,
+ w: number,
+ h: number,
+ zoom: number,
+ panX: number,
+ panY: number,
+ rotation = 0
+): CornerPosition | null {
+ const { x1, y1, x2, y2 } = getScreenRect(absX, absY, w, h, zoom, panX, panY)
+ const cx = (x1 + x2) / 2
+ const cy = (y1 + y2) / 2
+ const ur = unrotate(sx, sy, cx, cy, rotation)
+
+ const corners: Array<{ pos: CornerPosition; x: number; y: number }> = [
+ { pos: 'nw', x: x1, y: y1 },
+ { pos: 'ne', x: x2, y: y1 },
+ { pos: 'se', x: x2, y: y2 },
+ { pos: 'sw', x: x1, y: y2 }
+ ]
+
+ for (const { pos, x, y } of corners) {
+ const dx = Math.abs(ur.sx - x)
+ const dy = Math.abs(ur.sy - y)
+ if (
+ dx <= CORNER_ROTATE_ZONE &&
+ dy <= CORNER_ROTATE_ZONE &&
+ (dx > HANDLE_HIT_RADIUS || dy > HANDLE_HIT_RADIUS)
+ ) {
+ return pos
+ }
+ }
+ return null
+}
+
+const CORNER_BASE_ANGLES: Record<CornerPosition, number> = { nw: 0, ne: 90, se: 180, sw: 270 }
+
+import rotateCursorSvg from '../assets/rotate-cursor.svg?raw'
+
+const rotationCursorCache = new Map<number, string>()
+
+function buildRotationCursor(angleDeg: number): string {
+ const key = Math.round(angleDeg) % 360
+ let cached = rotationCursorCache.get(key)
+ if (cached) return cached
+ let svg: string
+ if (key === 0) {
+ svg = rotateCursorSvg
+ } else {
+ svg = rotateCursorSvg
+ .replace(
+ '<path',
+ `<g transform='translate(1002 2110) rotate(${key}) translate(-1002 -2110)'><path`
+ )
+ .replace('</svg>', '</g></svg>')
+ }
+ cached = `url("data:image/svg+xml,${encodeURIComponent(svg)}") 12 12, auto`
+ rotationCursorCache.set(key, cached)
+ return cached
+}
+
+function cornerRotationCursor(corner: CornerPosition, nodeRotation = 0): string {
+ return buildRotationCursor(CORNER_BASE_ANGLES[corner] + nodeRotation)
+}
+
+export function useCanvasInput(
+ canvasRef: Ref<HTMLCanvasElement | null>,
+ editor: Editor,
+ hitTestSectionTitle: (cx: number, cy: number) => SceneNode | null,
+ hitTestComponentLabel: (cx: number, cy: number) => SceneNode | null,
+ hitTestFrameTitle: (cx: number, cy: number) => SceneNode | null,
+ onCursorMove?: (cx: number, cy: number) => void
+) {
+ const drag = ref<DragState | null>(null)
+ const cursorOverride = ref<string | null>(null)
+ let lastClickTime = 0
+ let lastClickX = 0
+ let lastClickY = 0
+ let clickCount = 0
+ const MULTI_CLICK_DELAY = 500
+ const MULTI_CLICK_RADIUS = 5
+
+ function getCoords(e: MouseEvent) {
+ const canvas = canvasRef.value
+ if (!canvas) return { sx: 0, sy: 0, cx: 0, cy: 0 }
+ const rect = canvas.getBoundingClientRect()
+ const sx = e.clientX - rect.left
+ const sy = e.clientY - rect.top
+ const { x: cx, y: cy } = editor.screenToCanvas(sx, sy)
+ return { sx, sy, cx, cy }
+ }
+
+ function canvasToLocal(cx: number, cy: number, scopeId: string): { lx: number; ly: number } {
+ const node = editor.graph.getNode(scopeId)
+ if (!node) return { lx: cx, ly: cy }
+ const abs = editor.graph.getAbsolutePosition(scopeId)
+ let dx = cx - abs.x
+ let dy = cy - abs.y
+ if (node.rotation !== 0) {
+ const hw = node.width / 2
+ const hh = node.height / 2
+ const rad = degToRad(-node.rotation)
+ const cos = Math.cos(rad)
+ const sin = Math.sin(rad)
+ const rx = dx - hw
+ const ry = dy - hh
+ dx = rx * cos - ry * sin + hw
+ dy = rx * sin + ry * cos + hh
+ }
+ return { lx: dx, ly: dy }
+ }
+
+ function hitTestInScope(cx: number, cy: number, deep: boolean): SceneNode | null {
+ const scopeId = editor.state.enteredContainerId
+ if (scopeId) {
+ 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)
+ }
+ }
+ return deep
+ ? editor.graph.hitTestDeep(cx, cy, editor.state.currentPageId)
+ : editor.graph.hitTest(cx, cy, editor.state.currentPageId)
+ }
+
+ function isInsideContainerBounds(cx: number, cy: number, containerId: string): boolean {
+ const container = editor.graph.getNode(containerId)
+ if (!container) return false
+ const { lx, ly } = canvasToLocal(cx, cy, containerId)
+ return lx >= 0 && lx <= container.width && ly >= 0 && ly <= container.height
+ }
+
+ function startPanDrag(e: MouseEvent) {
+ drag.value = {
+ type: 'pan',
+ startScreenX: e.clientX,
+ startScreenY: e.clientY,
+ startPanX: editor.state.panX,
+ startPanY: editor.state.panY
+ }
+ }
+
+ function handleTextEditClick(cx: number, cy: number, shiftKey: boolean): boolean {
+ const editor = editor.textEditor
+ const editNode = editor.state.editingTextId
+ ? editor.graph.getNode(editor.state.editingTextId)
+ : null
+ if (!editor || !editNode) {
+ editor.commitTextEdit()
+ return false
+ }
+ const abs = editor.graph.getAbsolutePosition(editNode.id)
+ const localX = cx - abs.x
+ const localY = cy - abs.y
+ if (localX < 0 || localY < 0 || localX > editNode.width || localY > editNode.height) {
+ editor.commitTextEdit()
+ return false
+ }
+ if (clickCount >= 3) {
+ editor.selectAll()
+ } else if (clickCount === 2) {
+ editor.selectWordAt(localX, localY)
+ } else {
+ editor.setCursorAt(localX, localY, shiftKey)
+ drag.value = { type: 'text-select', startX: cx, startY: cy } as DragState
+ }
+ editor.requestRender()
+ return true
+ }
+
+ function tryStartRotation(sx: number, sy: number): boolean {
+ if (editor.state.selectedIds.size !== 1) return false
+ const id = [...editor.state.selectedIds][0]
+ const node = editor.graph.getNode(id)
+ if (!node || node.locked) return false
+ const abs = editor.graph.getAbsolutePosition(id)
+ if (
+ !hitTestCornerRotation(
+ sx,
+ sy,
+ abs.x,
+ abs.y,
+ node.width,
+ node.height,
+ editor.state.zoom,
+ editor.state.panX,
+ editor.state.panY,
+ node.rotation
+ )
+ )
+ return false
+
+ const screenCx = (abs.x + node.width / 2) * editor.state.zoom + editor.state.panX
+ const screenCy = (abs.y + node.height / 2) * editor.state.zoom + editor.state.panY
+ const startAngle = Math.atan2(sy - screenCy, sx - screenCx) * (180 / Math.PI)
+ drag.value = {
+ type: 'rotate',
+ nodeId: id,
+ centerX: screenCx,
+ centerY: screenCy,
+ startAngle,
+ origRotation: node.rotation
+ }
+ return true
+ }
+
+ function tryStartResize(sx: number, sy: number, cx: number, cy: number): boolean {
+ for (const id of editor.state.selectedIds) {
+ const node = editor.graph.getNode(id)
+ if (!node || node.locked) 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) {
+ drag.value = {
+ type: 'resize',
+ handle,
+ startX: cx,
+ startY: cy,
+ origRect: { x: node.x, y: node.y, width: node.width, height: node.height },
+ nodeId: id
+ }
+ return true
+ }
+ }
+ return false
+ }
+
+ 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 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
+
+ if (isInsideContainerBounds(cx, cy, scopeId)) {
+ editor.clearSelection()
+ return null
+ }
+
+ editor.exitContainer()
+ const afterExit = hitTestInScope(cx, cy, false)
+ if (afterExit) return afterExit
+
+ if (editor.state.enteredContainerId) {
+ editor.exitContainer()
+ }
+ 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
+ if (tryStartResize(sx, sy, cx, cy)) return
+
+ const hit = resolveHit(cx, cy)
+ if (!hit) {
+ if (!editor.state.enteredContainerId) {
+ editor.clearSelection()
+ drag.value = { 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) {
+ duplicateAndDrag(cx, cy)
+ return
+ }
+
+ drag.value = {
+ type: 'move',
+ startX: cx,
+ startY: cy,
+ originals,
+ autoLayoutParentId: detectAutoLayoutParent()
+ }
+ }
+
+ function onMouseDown(e: MouseEvent) {
+ editor.setHoveredNode(null)
+ const { sx, sy, cx, cy } = getCoords(e)
+
+ const now = performance.now()
+ if (
+ now - lastClickTime < MULTI_CLICK_DELAY &&
+ Math.abs(sx - lastClickX) < MULTI_CLICK_RADIUS &&
+ Math.abs(sy - lastClickY) < MULTI_CLICK_RADIUS
+ ) {
+ clickCount++
+ } else {
+ clickCount = 1
+ }
+ lastClickTime = now
+ lastClickX = sx
+ lastClickY = sy
+ const tool = editor.state.activeTool
+
+ if (e.button === 1 || tool === 'HAND') {
+ startPanDrag(e)
+ return
+ }
+
+ if (tool === 'SELECT' && e.altKey && !editor.state.selectedIds.size) {
+ startPanDrag(e)
+ return
+ }
+
+ if (tool === 'SELECT') {
+ handleSelectDown(e, sx, sy, cx, cy)
+ return
+ }
+
+ if (tool === 'PEN') {
+ editor.penAddVertex(cx, cy)
+ drag.value = { type: 'pen-drag', startX: cx, startY: cy } as DragState
+ return
+ }
+
+ if (tool === 'TEXT') {
+ const nodeId = editor.createShape('TEXT', cx, cy, DEFAULT_TEXT_WIDTH, DEFAULT_TEXT_HEIGHT)
+ editor.graph.updateNode(nodeId, { text: '' })
+ editor.select([nodeId])
+ editor.startTextEditing(nodeId)
+ editor.setTool('SELECT')
+ editor.requestRender()
+ return
+ }
+
+ const nodeType = TOOL_TO_NODE[tool]
+ if (!nodeType) return
+
+ const nodeId = editor.createShape(nodeType, cx, cy, 0, 0)
+ editor.select([nodeId])
+
+ 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)
+ 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.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 editor = editor.textEditor
+ const editNode = editor.state.editingTextId
+ ? editor.graph.getNode(editor.state.editingTextId)
+ : null
+ if (editor && editNode) {
+ const abs = editor.graph.getAbsolutePosition(editNode.id)
+ editor.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) {
+ {
+ const { cx, cy } = getCoords(e)
+ editor.state.cursorCanvasX = cx
+ editor.state.cursorCanvasY = cy
+ if (onCursorMove) onCursorMove(cx, cy)
+ }
+
+ if (editor.state.activeTool === 'PEN' && editor.state.penState && !drag.value) {
+ const { cx, cy } = getCoords(e)
+ editor.state.penCursorX = cx
+ editor.state.penCursorY = cy
+
+ const first = editor.state.penState.vertices[0]
+ if (editor.state.penState.vertices.length > 2) {
+ const dist = Math.hypot(cx - first.x, cy - first.y)
+ editor.penSetClosingToFirst(dist < PEN_CLOSE_THRESHOLD)
+ }
+ editor.requestRepaint()
+ }
+
+ if (!drag.value && editor.state.activeTool === 'SELECT') {
+ updateHoverCursor(e)
+ }
+
+ if (!drag.value) return
+ const d = drag.value
+
+ if (d.type === 'pan') {
+ handlePanMove(d, e)
+ return
+ }
+
+ const { cx, cy, sx, sy } = getCoords(e)
+
+ if (d.type === 'rotate') {
+ handleRotateMove(d, sx, sy, e.shiftKey)
+ return
+ }
+ if (d.type === 'move') {
+ handleMoveMove(d, cx, cy)
+ return
+ }
+ if (d.type === 'text-select') {
+ handleTextSelectMove(cx, cy)
+ return
+ }
+ if (d.type === 'resize') {
+ applyResize(d, cx, cy, e.shiftKey)
+ return
+ }
+
+ if (d.type === 'pen-drag') {
+ const tx = cx - d.startX
+ const ty = cy - d.startY
+ if (Math.hypot(tx, ty) > 2) {
+ editor.penSetDragTangent(tx, ty)
+ }
+ return
+ }
+
+ if (d.type === 'draw') {
+ handleDrawMove(d, cx, cy, e.shiftKey)
+ return
+ }
+
+ handleMarqueeMove(d, cx, cy)
+ }
+
+ function constrainToAspectRatio(
+ handle: HandlePosition,
+ origRect: Rect,
+ width: number,
+ height: number,
+ dx: number,
+ dy: number
+ ): Rect {
+ let x = handle.includes('w') ? origRect.x + origRect.width - Math.abs(width) : origRect.x
+ const isTop = handle === 'nw' || handle === 'n' || handle === 'ne'
+ let y = isTop ? origRect.y + origRect.height - Math.abs(height) : origRect.y
+ const aspect = origRect.width / origRect.height
+
+ if (handle === 'n' || handle === 's') {
+ width = Math.abs(height) * aspect
+ x = origRect.x + (origRect.width - width) / 2
+ } else if (handle === 'e' || handle === 'w') {
+ height = Math.abs(width) / aspect
+ y = origRect.y + (origRect.height - height) / 2
+ } else if (Math.abs(dx) > Math.abs(dy)) {
+ height = (Math.abs(width) / aspect) * Math.sign(height || 1)
+ if (isTop) y = origRect.y + origRect.height - Math.abs(height)
+ } else {
+ width = Math.abs(height) * aspect * Math.sign(width || 1)
+ if (handle.includes('w')) x = origRect.x + origRect.width - Math.abs(width)
+ }
+
+ return { x, y, width, height }
+ }
+
+ function applyResize(d: DragResize, cx: number, cy: number, constrain: boolean) {
+ const { handle, origRect } = d
+ let { x, y, width, height } = origRect
+ const dx = cx - d.startX
+ const dy = cy - d.startY
+
+ const moveLeft = handle.includes('w')
+ const moveRight = handle.includes('e')
+ const moveTop = handle === 'nw' || handle === 'n' || handle === 'ne'
+ const moveBottom = handle === 'sw' || handle === 's' || handle === 'se'
+
+ if (moveRight) width = origRect.width + dx
+ if (moveLeft) {
+ x = origRect.x + dx
+ width = origRect.width - dx
+ }
+ if (moveBottom) height = origRect.height + dy
+ if (moveTop) {
+ y = origRect.y + dy
+ height = origRect.height - dy
+ }
+
+ if (constrain && origRect.width > 0 && origRect.height > 0) {
+ ;({ x, y, width, height } = constrainToAspectRatio(handle, origRect, width, height, dx, dy))
+ }
+
+ if (width < 0) {
+ x += width
+ width = -width
+ }
+ if (height < 0) {
+ y += height
+ height = -height
+ }
+
+ editor.updateNode(d.nodeId, {
+ x: Math.round(x),
+ y: Math.round(y),
+ width: Math.round(Math.max(1, width)),
+ height: Math.round(Math.max(1, height))
+ })
+ }
+
+ 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)
+ else if (d.type === 'text-select') {
+ drag.value = null
+ return
+ } else if (d.type === 'resize') editor.commitResize(d.nodeId, d.origRect)
+ else if (d.type === 'pen-drag') {
+ drag.value = null
+ return
+ } else if (d.type === 'rotate') {
+ const preview = editor.state.rotationPreview
+ if (preview) {
+ editor.updateNode(d.nodeId, { rotation: preview.angle })
+ editor.commitRotation(d.nodeId, d.origRotation)
+ }
+ editor.setRotationPreview(null)
+ } else if (d.type === 'draw') handleDrawUp(d)
+ else if (d.type === 'marquee') editor.setMarquee(null)
+
+ drag.value = null
+ cursorOverride.value = null
+ }
+
+ const wheelAccum = {
+ deltaX: 0,
+ deltaY: 0,
+ zoomDelta: 0,
+ zoomCenterX: 0,
+ zoomCenterY: 0,
+ hasZoom: false,
+ rafId: 0
+ }
+
+ function flushWheel() {
+ wheelAccum.rafId = 0
+ editor.setHoveredNode(null)
+ if (wheelAccum.hasZoom) {
+ editor.applyZoom(wheelAccum.zoomDelta, wheelAccum.zoomCenterX, wheelAccum.zoomCenterY)
+ } else {
+ editor.pan(wheelAccum.deltaX, wheelAccum.deltaY)
+ }
+ wheelAccum.deltaX = 0
+ wheelAccum.deltaY = 0
+ wheelAccum.zoomDelta = 0
+ wheelAccum.hasZoom = false
+ }
+
+ // Normalize wheel deltaY across deltaMode variants (line/page/pixel).
+ // Trackpad pinch is always DOM_DELTA_PIXEL; external mice may use LINE or PAGE.
+ function normalizeWheelDelta(e: WheelEvent): { dx: number; dy: number } {
+ let { deltaX, deltaY } = e
+ if (e.deltaMode === WheelEvent.DOM_DELTA_LINE) {
+ deltaX *= 40
+ deltaY *= 40
+ } else if (e.deltaMode === WheelEvent.DOM_DELTA_PAGE) {
+ deltaX *= 800
+ deltaY *= 800
+ }
+ return { dx: deltaX, dy: deltaY }
+ }
+
+ function onWheel(e: WheelEvent) {
+ e.preventDefault()
+ const canvas = canvasRef.value
+ if (!canvas) return
+ const { dx, dy } = normalizeWheelDelta(e)
+
+ if (e.ctrlKey || e.metaKey) {
+ const rect = canvas.getBoundingClientRect()
+ wheelAccum.zoomCenterX = e.clientX - rect.left
+ wheelAccum.zoomCenterY = e.clientY - rect.top
+ wheelAccum.zoomDelta += dy
+ wheelAccum.hasZoom = true
+ } else {
+ wheelAccum.deltaX -= dx
+ wheelAccum.deltaY -= dy
+ }
+ if (!wheelAccum.rafId) {
+ wheelAccum.rafId = requestAnimationFrame(flushWheel)
+ }
+ }
+
+ function onDblClick(e: MouseEvent) {
+ if (editor.state.editingTextId) return
+
+ const { cx, cy } = getCoords(e)
+
+ const selectedId =
+ editor.state.selectedIds.size === 1 ? [...editor.state.selectedIds][0] : undefined
+ const selectedNode = selectedId ? editor.graph.getNode(selectedId) : undefined
+ const canEnter =
+ selectedNode && selectedId && editor.graph.isContainer(selectedId) && !selectedNode.locked
+
+ if (canEnter) {
+ editor.enterContainer(selectedId)
+ const useDeep = selectedNode.type === 'COMPONENT' || selectedNode.type === 'INSTANCE'
+ const hit = hitTestInScope(cx, cy, useDeep)
+ if (hit) {
+ editor.select([hit.id])
+ } else {
+ editor.clearSelection()
+ }
+ return
+ }
+
+ const hit =
+ hitTestSectionTitle(cx, cy) ?? hitTestComponentLabel(cx, cy) ?? hitTestInScope(cx, cy, true)
+ if (!hit) return
+
+ if (hit.type === 'TEXT') {
+ editor.select([hit.id])
+ editor.startTextEditing(hit.id)
+ const editor = editor.textEditor
+ if (editor) {
+ const abs = editor.graph.getAbsolutePosition(hit.id)
+ editor.selectWordAt(cx - abs.x, cy - abs.y)
+ editor.requestRender()
+ }
+ return
+ }
+
+ editor.select([hit.id])
+ }
+
+ function computeAutoLayoutIndicator(d: DragMove, cx: number, cy: number) {
+ if (!d.autoLayoutParentId) return
+ const parent = editor.graph.getNode(d.autoLayoutParentId)
+ if (!parent || parent.layoutMode === 'NONE') return
+ computeAutoLayoutIndicatorForFrame(parent, cx, cy)
+ }
+
+ function computeIndicatorPosition(
+ children: SceneNode[],
+ insertIndex: number,
+ parent: SceneNode,
+ parentAbs: Vector,
+ isRow: boolean
+ ): number {
+ if (children.length === 0) {
+ return isRow ? parentAbs.x + parent.paddingLeft : parentAbs.y + parent.paddingTop
+ }
+ if (insertIndex === 0) {
+ const firstAbs = editor.graph.getAbsolutePosition(children[0].id)
+ return (isRow ? firstAbs.x : firstAbs.y) - parent.itemSpacing / 2
+ }
+ if (insertIndex >= children.length) {
+ const last = children[children.length - 1]
+ const lastAbs = editor.graph.getAbsolutePosition(last.id)
+ return isRow
+ ? lastAbs.x + last.width + parent.itemSpacing / 2
+ : lastAbs.y + last.height + parent.itemSpacing / 2
+ }
+ const prev = children[insertIndex - 1]
+ const next = children[insertIndex]
+ const prevAbs = editor.graph.getAbsolutePosition(prev.id)
+ const nextAbs = editor.graph.getAbsolutePosition(next.id)
+ return isRow
+ ? (prevAbs.x + prev.width + nextAbs.x) / 2
+ : (prevAbs.y + prev.height + nextAbs.y) / 2
+ }
+
+ function filteredToRealIndex(parentId: string, insertIndex: number): number {
+ const allChildren = editor.graph.getChildren(parentId)
+ let realIndex = 0
+ let filteredCount = 0
+ for (const child of allChildren) {
+ if (editor.state.selectedIds.has(child.id)) continue
+ if (child.layoutPositioning === 'ABSOLUTE') {
+ realIndex++
+ continue
+ }
+ if (filteredCount === insertIndex) break
+ filteredCount++
+ realIndex++
+ }
+ return realIndex
+ }
+
+ function computeAutoLayoutIndicatorForFrame(parent: SceneNode, cx: number, cy: number) {
+ const children = editor.graph
+ .getChildren(parent.id)
+ .filter((c) => c.layoutPositioning !== 'ABSOLUTE' && !editor.state.selectedIds.has(c.id))
+
+ const parentAbs = editor.graph.getAbsolutePosition(parent.id)
+ const isRow = parent.layoutMode === 'HORIZONTAL'
+
+ let insertIndex = children.length
+ for (let i = 0; i < children.length; i++) {
+ const childAbs = editor.graph.getAbsolutePosition(children[i].id)
+ const mid = isRow ? childAbs.x + children[i].width / 2 : childAbs.y + children[i].height / 2
+ if ((isRow ? cx : cy) < mid) {
+ insertIndex = i
+ break
+ }
+ }
+
+ const indicatorPos = computeIndicatorPosition(children, insertIndex, parent, parentAbs, isRow)
+ const crossStart = isRow ? parentAbs.y + parent.paddingTop : parentAbs.x + parent.paddingLeft
+ const crossLength = isRow
+ ? parent.height - parent.paddingTop - parent.paddingBottom
+ : parent.width - parent.paddingLeft - parent.paddingRight
+
+ editor.setLayoutInsertIndicator({
+ parentId: parent.id,
+ index: filteredToRealIndex(parent.id, insertIndex),
+ x: isRow ? indicatorPos : crossStart,
+ y: isRow ? crossStart : indicatorPos,
+ length: crossLength,
+ direction: isRow ? 'VERTICAL' : 'HORIZONTAL'
+ })
+ }
+
+ let activeTouches: Touch[] = []
+ let pinchStartDist = 0
+ let pinchStartZoom = 0
+ let pinchMidX = 0
+ let pinchMidY = 0
+
+ function touchDist(a: Touch, b: Touch) {
+ return Math.hypot(a.clientX - b.clientX, a.clientY - b.clientY)
+ }
+
+ let touchAsMouse = false
+
+ function syntheticMouse(type: string, t: Touch): MouseEvent {
+ return new MouseEvent(type, {
+ clientX: t.clientX,
+ clientY: t.clientY,
+ screenX: t.screenX,
+ screenY: t.screenY,
+ button: 0,
+ buttons: 1,
+ bubbles: true
+ })
+ }
+
+ function onTouchStart(e: TouchEvent) {
+ e.preventDefault()
+ activeTouches = Array.from(e.touches)
+ const canvas = canvasRef.value
+ if (!canvas) return
+
+ if (activeTouches.length === 2) {
+ if (touchAsMouse) {
+ onMouseUp()
+ touchAsMouse = false
+ }
+ drag.value = null
+ const [a, b] = activeTouches
+ pinchStartDist = touchDist(a, b)
+ pinchStartZoom = editor.state.zoom
+ const rect = canvas.getBoundingClientRect()
+ pinchMidX = (a.clientX + b.clientX) / 2 - rect.left
+ pinchMidY = (a.clientY + b.clientY) / 2 - rect.top
+ } else if (activeTouches.length === 1) {
+ const t = activeTouches[0]
+ const tool = editor.state.activeTool
+ if (tool === 'HAND') {
+ touchAsMouse = false
+ drag.value = {
+ type: 'pan',
+ startScreenX: t.clientX,
+ startScreenY: t.clientY,
+ startPanX: editor.state.panX,
+ startPanY: editor.state.panY
+ }
+ } else {
+ touchAsMouse = true
+ onMouseDown(syntheticMouse('mousedown', t))
+ }
+ }
+ }
+
+ function onTouchMove(e: TouchEvent) {
+ e.preventDefault()
+ activeTouches = Array.from(e.touches)
+ const canvas = canvasRef.value
+ if (!canvas) return
+
+ if (activeTouches.length === 2) {
+ const [a, b] = activeTouches
+ const rect = canvas.getBoundingClientRect()
+ const newMidX = (a.clientX + b.clientX) / 2 - rect.left
+ const newMidY = (a.clientY + b.clientY) / 2 - rect.top
+
+ editor.setHoveredNode(null)
+ const newDist = touchDist(a, b)
+ if (pinchStartDist > 0) {
+ const scale = newDist / pinchStartDist
+ const newZoom = Math.max(0.02, Math.min(256, pinchStartZoom * scale))
+ const zoomRatio = newZoom / editor.state.zoom
+
+ const panDx = newMidX - pinchMidX
+ const panDy = newMidY - pinchMidY
+
+ editor.state.panX = pinchMidX - (pinchMidX - editor.state.panX) * zoomRatio + panDx
+ editor.state.panY = pinchMidY - (pinchMidY - editor.state.panY) * zoomRatio + panDy
+ editor.state.zoom = newZoom
+ }
+
+ pinchMidX = newMidX
+ pinchMidY = newMidY
+ editor.requestRepaint()
+ } else if (activeTouches.length === 1) {
+ const t = activeTouches[0]
+ if (touchAsMouse) {
+ onMouseMove(syntheticMouse('mousemove', t))
+ } else if (drag.value?.type === 'pan') {
+ const d = drag.value
+ editor.state.panX = d.startPanX + (t.clientX - d.startScreenX)
+ editor.state.panY = d.startPanY + (t.clientY - d.startScreenY)
+ editor.requestRepaint()
+ }
+ }
+ }
+
+ function onTouchEnd(e: TouchEvent) {
+ e.preventDefault()
+ activeTouches = Array.from(e.touches)
+
+ if (activeTouches.length === 0) {
+ if (touchAsMouse) {
+ onMouseUp()
+ touchAsMouse = false
+ } else {
+ drag.value = null
+ }
+ pinchStartDist = 0
+ } else if (activeTouches.length === 1) {
+ const t = activeTouches[0]
+ if (!touchAsMouse) {
+ drag.value = {
+ type: 'pan',
+ startScreenX: t.clientX,
+ startScreenY: t.clientY,
+ startPanX: editor.state.panX,
+ startPanY: editor.state.panY
+ }
+ }
+ pinchStartDist = 0
+ }
+ }
+
+ useEventListener(canvasRef, 'dblclick', onDblClick)
+ useEventListener(canvasRef, 'mousedown', onMouseDown)
+ useEventListener(canvasRef, 'mousemove', onMouseMove)
+ useEventListener(canvasRef, 'mouseup', onMouseUp)
+ useEventListener(canvasRef, 'mouseleave', () => {
+ if (!drag.value) {
+ editor.setHoveredNode(null)
+ }
+ })
+ useEventListener(window, 'mouseup', () => {
+ if (drag.value) onMouseUp()
+ })
+ useEventListener(canvasRef, 'wheel', onWheel, { passive: false })
+ useEventListener(canvasRef, 'touchstart', onTouchStart, { passive: false })
+ useEventListener(canvasRef, 'touchmove', onTouchMove, { passive: false })
+ useEventListener(canvasRef, 'touchend', onTouchEnd, { passive: false })
+ useEventListener(canvasRef, 'touchcancel', onTouchEnd, { passive: false })
+
+ // Safari macOS: trackpad pinch-to-zoom uses gesture events, not wheel+ctrlKey
+ let gestureStartZoom = 1
+ let gestureRafId = 0
+ let pendingGesture: { scale: number; sx: number; sy: number } | null = null
+
+ function flushGesture() {
+ gestureRafId = 0
+ if (!pendingGesture) return
+ editor.setHoveredNode(null)
+ const { scale, sx, sy } = pendingGesture
+ pendingGesture = null
+ const newZoom = Math.max(0.02, Math.min(256, gestureStartZoom * scale))
+ const zoomRatio = newZoom / editor.state.zoom
+ editor.state.panX = sx - (sx - editor.state.panX) * zoomRatio
+ editor.state.panY = sy - (sy - editor.state.panY) * zoomRatio
+ editor.state.zoom = newZoom
+ editor.requestRepaint()
+ }
+
+ useEventListener(
+ canvasRef,
+ 'gesturestart' as keyof HTMLElementEventMap,
+ (e: Event) => {
+ e.preventDefault()
+ gestureStartZoom = editor.state.zoom
+ },
+ { passive: false }
+ )
+ useEventListener(
+ canvasRef,
+ 'gesturechange' as keyof HTMLElementEventMap,
+ (e: Event) => {
+ e.preventDefault()
+ const ge = e as GestureEvent
+ const canvas = canvasRef.value
+ if (!canvas) return
+ const rect = canvas.getBoundingClientRect()
+ pendingGesture = {
+ scale: ge.scale,
+ sx: ge.clientX - rect.left,
+ sy: ge.clientY - rect.top
+ }
+ if (!gestureRafId) {
+ gestureRafId = requestAnimationFrame(flushGesture)
+ }
+ },
+ { passive: false }
+ )
+ useEventListener(
+ canvasRef,
+ 'gestureend' as keyof HTMLElementEventMap,
+ (e: Event) => {
+ e.preventDefault()
+ },
+ { passive: false }
+ )
+
+ return {
+ drag,
+ cursorOverride
+ }
+}
diff --git a/packages/vue/src/composables/use-font-status.ts b/packages/vue/src/composables/use-font-status.ts
new file mode 100644
index 000000000..2d2c90f98
--- /dev/null
+++ b/packages/vue/src/composables/use-font-status.ts
@@ -0,0 +1,24 @@
+import { computed } from 'vue'
+
+import { isFontLoaded, DEFAULT_FONT_FAMILY } from '@open-pencil/core'
+
+import type { SceneNode } from '@open-pencil/core'
+
+export function useNodeFontStatus(node: () => SceneNode) {
+ const missingFonts = computed(() => {
+ const n = node()
+ if (n.type !== 'TEXT') return []
+
+ const families = new Set<string>()
+ families.add(n.fontFamily || DEFAULT_FONT_FAMILY)
+ for (const run of n.styleRuns) {
+ if (run.style.fontFamily) families.add(run.style.fontFamily)
+ }
+
+ return [...families].filter((f) => !isFontLoaded(f))
+ })
+
+ const hasMissingFonts = computed(() => missingFonts.value.length > 0)
+
+ return { missingFonts, hasMissingFonts }
+}
diff --git a/packages/vue/src/composables/use-inline-rename.ts b/packages/vue/src/composables/use-inline-rename.ts
new file mode 100644
index 000000000..be8dc13b8
--- /dev/null
+++ b/packages/vue/src/composables/use-inline-rename.ts
@@ -0,0 +1,58 @@
+import { onClickOutside } from '@vueuse/core'
+import { nextTick, ref, type Ref } from 'vue'
+
+export function useInlineRename<T extends string>(onCommit: (id: T, newName: string) => void) {
+ const editingId = ref<T | null>(null)
+ const inputRef: Ref<HTMLInputElement | null> = ref(null)
+ let originalName = ''
+ let cleanupOutsideClick: (() => void) | undefined
+
+ function start(id: T, currentName: string) {
+ editingId.value = id
+ originalName = currentName
+ }
+
+ async function focusInput(input: HTMLInputElement | null) {
+ if (input === inputRef.value) return
+ inputRef.value = input
+ cleanupOutsideClick?.()
+ if (input) {
+ cleanupOutsideClick = onClickOutside(inputRef, () => input.blur())
+ }
+ await nextTick()
+ input?.focus()
+ input?.select()
+ }
+
+ function commit(id: T, input: HTMLInputElement) {
+ if (editingId.value !== id) return
+ const value = input.value.trim()
+ if (value && value !== originalName) {
+ onCommit(id, value)
+ }
+ editingId.value = null
+ inputRef.value = null
+ cleanupOutsideClick?.()
+ cleanupOutsideClick = undefined
+ }
+
+ function cancel() {
+ editingId.value = null
+ inputRef.value = null
+ cleanupOutsideClick?.()
+ cleanupOutsideClick = undefined
+ }
+
+ function onKeydown(e: KeyboardEvent) {
+ if (e.key === 'Enter') {
+ ;(e.target as HTMLInputElement).blur()
+ return
+ }
+
+ if (e.key === 'Escape') {
+ cancel()
+ }
+ }
+
+ return { editingId, start, focusInput, commit, cancel, onKeydown }
+}
diff --git a/packages/vue/src/composables/use-multi-props.ts b/packages/vue/src/composables/use-multi-props.ts
new file mode 100644
index 000000000..1c81a010c
--- /dev/null
+++ b/packages/vue/src/composables/use-multi-props.ts
@@ -0,0 +1,106 @@
+import { computed } from 'vue'
+
+import { useEditor } from '../context'
+
+import type { SceneNode } from '@open-pencil/core'
+
+export const MIXED = Symbol('mixed')
+export type MixedValue<T> = T | typeof MIXED
+
+export function useMultiProps() {
+ const store = useEditor()
+ const node = computed(() => store.getSelectedNode() ?? null)
+ const nodes = computed(() => store.getSelectedNodes())
+ const isMulti = computed(() => nodes.value.length > 1)
+ const active = computed(() => node.value || isMulti.value)
+ const activeNode = computed(() => node.value ?? (nodes.value[0] as SceneNode | undefined) ?? null)
+
+ function merged<K extends keyof SceneNode>(key: K): MixedValue<SceneNode[K]> {
+ const all = nodes.value
+ if (all.length === 0) return MIXED
+ const first = all[0][key]
+ for (let i = 1; i < all.length; i++) {
+ if (all[i][key] !== first) return MIXED
+ }
+ return first
+ }
+
+ function prop<K extends keyof SceneNode>(key: K) {
+ return computed(() => merged(key))
+ }
+
+ function updateAllWithUndo(patch: Partial<SceneNode>, label: string) {
+ for (const n of nodes.value) {
+ store.updateNodeWithUndo(n.id, patch, label)
+ }
+ store.requestRender()
+ }
+
+ function isArrayMixed(key: keyof SceneNode): boolean {
+ const all = nodes.value
+ if (all.length <= 1) return false
+ const first = JSON.stringify(all[0][key])
+ return all.some((n) => JSON.stringify(n[key]) !== first)
+ }
+
+ type ArrayPropKey = 'fills' | 'strokes' | 'effects'
+
+ function targetNodes(): SceneNode[] {
+ if (isMulti.value) return nodes.value
+ return activeNode.value ? [activeNode.value] : []
+ }
+
+ function updateArrayItem(
+ key: ArrayPropKey,
+ index: number,
+ patch: Record<string, unknown>,
+ label: string
+ ) {
+ for (const n of targetNodes()) {
+ const arr = [...n[key]]
+ arr[index] = { ...arr[index], ...patch } as (typeof arr)[number]
+ store.updateNodeWithUndo(n.id, { [key]: arr } as Partial<SceneNode>, label)
+ }
+ }
+
+ function removeArrayItem(key: ArrayPropKey, index: number, label: string) {
+ for (const n of targetNodes()) {
+ store.updateNodeWithUndo(
+ n.id,
+ { [key]: (n[key] as unknown[]).filter((_, i) => i !== index) } as Partial<SceneNode>,
+ label
+ )
+ }
+ }
+
+ function toggleArrayVisibility(key: ArrayPropKey, index: number) {
+ for (const n of targetNodes()) {
+ const items = n[key] as Array<{ visible: boolean }>
+ if (!items[index]) continue
+ const arr = [...n[key]]
+ arr[index] = { ...arr[index], visible: !items[index].visible }
+ store.updateNodeWithUndo(
+ n.id,
+ { [key]: arr } as Partial<SceneNode>,
+ `Toggle ${key} visibility`
+ )
+ }
+ }
+
+ return {
+ store,
+ node,
+ nodes,
+ isMulti,
+ active,
+ activeNode,
+ targetNodes,
+ prop,
+ merged,
+ updateAllWithUndo,
+ updateArrayItem,
+ removeArrayItem,
+ toggleArrayVisibility,
+ isArrayMixed
+ }
+}
diff --git a/packages/vue/src/composables/use-node-props.ts b/packages/vue/src/composables/use-node-props.ts
new file mode 100644
index 000000000..042bd5123
--- /dev/null
+++ b/packages/vue/src/composables/use-node-props.ts
@@ -0,0 +1,55 @@
+import { computed } from 'vue'
+
+import { useEditor } from '../context'
+
+import type { SceneNode } from '@open-pencil/core'
+
+export function useNodeProps() {
+ const store = useEditor()
+ const node = computed(() => store.getSelectedNode() ?? null)
+ const nodes = computed(() => store.getSelectedNodes())
+
+ function updateProp(key: string, value: number | string) {
+ if (store.getSelectedNodes().length > 1) {
+ storePreviousValues(key)
+ for (const n of store.getSelectedNodes()) {
+ store.updateNode(n.id, { [key]: value })
+ }
+ } else {
+ const node = store.getSelectedNode()
+ if (node) store.updateNode(node.id, { [key]: value })
+ }
+ }
+
+ const previousValues = new Map<string, Record<string, number | string>>()
+
+ function storePreviousValues(key: string) {
+ for (const n of store.getSelectedNodes()) {
+ let rec = previousValues.get(n.id)
+ if (!rec) {
+ rec = {}
+ previousValues.set(n.id, rec)
+ }
+ if (!(key in rec)) {
+ rec[key] = n[key as keyof SceneNode] as number | string
+ }
+ }
+ }
+
+ function commitProp(key: string, _value: number | string, previous: number | string) {
+ if (store.getSelectedNodes().length > 1) {
+ for (const n of store.getSelectedNodes()) {
+ const prev = previousValues.get(n.id)?.[key] ?? previous
+ store.commitNodeUpdate(n.id, { [key]: prev } as Partial<SceneNode>, `Change ${key}`)
+ }
+ previousValues.clear()
+ } else {
+ const node = store.getSelectedNode()
+ if (node) {
+ store.commitNodeUpdate(node.id, { [key]: previous } as Partial<SceneNode>, `Change ${key}`)
+ }
+ }
+ }
+
+ return { store, node, nodes, updateProp, commitProp }
+}
diff --git a/packages/vue/src/composables/use-text-edit.ts b/packages/vue/src/composables/use-text-edit.ts
new file mode 100644
index 000000000..ffc1af331
--- /dev/null
+++ b/packages/vue/src/composables/use-text-edit.ts
@@ -0,0 +1,340 @@
+import { useEventListener } from '@vueuse/core'
+import { shallowRef, watch, type Ref } from 'vue'
+
+import {
+ adjustRunsForDelete,
+ adjustRunsForInsert,
+ toggleBoldInRange,
+ toggleDecorationInRange,
+ toggleItalicInRange
+} from '@open-pencil/core'
+
+import type { Editor } from '@open-pencil/core/editor'
+import type { SceneNode } from '@open-pencil/core'
+
+const CARET_BLINK_MS = 530
+
+export function useTextEdit(canvasRef: Ref<HTMLCanvasElement | null>, editor: Editor) {
+ const textareaRef = shallowRef<HTMLTextAreaElement | null>(null)
+ let isComposing = false
+ let blinkTimer = 0
+
+ function getEditingNode() {
+ const id = editor.state.editingTextId
+ if (!id) return null
+ return editor.graph.getNode(id) ?? null
+ }
+
+ function resetBlink() {
+ if (editor.textEditor) editor.textEditor.caretVisible = true
+ clearInterval(blinkTimer)
+ blinkTimer = window.setInterval(() => {
+ if (!editor.textEditor) return
+ editor.textEditor.caretVisible = !editor.textEditor.caretVisible
+ editor.requestRepaint()
+ }, CARET_BLINK_MS)
+ editor.requestRepaint()
+ }
+
+ function syncText(nodeId: string, text: string, runs?: SceneNode['styleRuns']) {
+ const changes: Partial<SceneNode> = { text }
+ if (runs !== undefined) changes.styleRuns = runs
+ editor.graph.updateNode(nodeId, changes)
+ editor.requestRender()
+ }
+
+ function insertText(text: string, node: SceneNode) {
+ const editor = editor.textEditor
+ if (!editor) return
+ const range = editor.getSelectionRange()
+ let runs = node.styleRuns
+ if (range) {
+ runs = adjustRunsForDelete(runs, range[0], range[1] - range[0])
+ runs = adjustRunsForInsert(runs, range[0], text.length)
+ } else {
+ runs = adjustRunsForInsert(runs, editor.state?.cursor ?? 0, text.length)
+ }
+ editor.insert(text, node)
+ syncText(node.id, editor.state?.text ?? '', runs)
+ }
+
+ function deleteText(node: SceneNode, forward: boolean) {
+ const editor = editor.textEditor
+ if (!editor) return
+ const range = editor.getSelectionRange()
+ let runs = node.styleRuns
+ if (range) {
+ runs = adjustRunsForDelete(runs, range[0], range[1] - range[0])
+ } else if (forward && editor.state && editor.state.cursor < node.text.length) {
+ runs = adjustRunsForDelete(runs, editor.state.cursor, 1)
+ } else if (!forward && editor.state && editor.state.cursor > 0) {
+ runs = adjustRunsForDelete(runs, editor.state.cursor - 1, 1)
+ }
+ if (forward) {
+ editor.delete(node)
+ } else {
+ editor.backspace(node)
+ }
+ syncText(node.id, editor.state?.text ?? '', runs)
+ }
+
+ function onCompositionStart() {
+ isComposing = true
+ }
+
+ function onCompositionEnd(e: CompositionEvent) {
+ isComposing = false
+ if (!e.data) return
+ const node = getEditingNode()
+ if (!node) return
+ insertText(e.data, node)
+ if (textareaRef.value) textareaRef.value.value = ''
+ resetBlink()
+ }
+
+ function onInput() {
+ const el = textareaRef.value
+ if (isComposing || !el) return
+ const text = el.value
+ if (!text) return
+ el.value = ''
+
+ const node = getEditingNode()
+ if (!node) return
+ insertText(text, node)
+ resetBlink()
+ }
+
+ function handleHorizontalArrow(e: KeyboardEvent, editor: NonNullable<typeof editor.textEditor>) {
+ const select = e.shiftKey
+ const isMeta = e.metaKey || e.ctrlKey
+ if (e.key === 'ArrowLeft') {
+ if (isMeta) editor.moveToLineStart(select)
+ else if (e.altKey) editor.moveWordLeft(select)
+ else editor.moveLeft(select)
+ } else {
+ if (isMeta) editor.moveToLineEnd(select)
+ else if (e.altKey) editor.moveWordRight(select)
+ else editor.moveRight(select)
+ }
+ }
+
+ function handleDeletion(
+ e: KeyboardEvent,
+ editor: NonNullable<typeof editor.textEditor>,
+ node: SceneNode
+ ) {
+ const isMeta = e.metaKey || e.ctrlKey
+ const forward = e.key === 'Delete'
+ if (forward) {
+ if (isMeta) editor.moveToLineEnd(true)
+ else if (e.altKey) editor.moveWordRight(true)
+ } else {
+ if (isMeta) editor.moveToLineStart(true)
+ else if (e.altKey) editor.moveWordLeft(true)
+ }
+ deleteText(node, forward)
+ }
+
+ type MetaAction = (node: SceneNode) => void
+ const metaKeyActions: Partial<Record<string, MetaAction>> = {
+ a: () => editor.textEditor?.selectAll(),
+ c: () => handleCopy(),
+ x: (node) => handleCut(node),
+ v: (node) => void handlePaste(node),
+ b: (node) => toggleBold(node),
+ i: (node) => toggleItalic(node),
+ u: (node) => toggleUnderline(node)
+ }
+
+ function onKeyDown(e: KeyboardEvent) {
+ if (isComposing) return
+ const editor = editor.textEditor
+ const node = getEditingNode()
+ if (!editor || !node) return
+
+ const isMeta = e.metaKey || e.ctrlKey
+ let textChanged = false
+
+ switch (e.key) {
+ case 'Escape':
+ editor.commitTextEdit()
+ canvasRef.value?.focus()
+ e.preventDefault()
+ return
+ case 'Enter':
+ insertText('\n', node)
+ textChanged = true
+ break
+ case 'Backspace':
+ case 'Delete':
+ handleDeletion(e, editor, node)
+ textChanged = true
+ break
+ case 'ArrowLeft':
+ case 'ArrowRight':
+ handleHorizontalArrow(e, editor)
+ break
+ case 'ArrowUp':
+ editor.moveUp(e.shiftKey)
+ break
+ case 'ArrowDown':
+ editor.moveDown(e.shiftKey)
+ break
+ case 'Home':
+ editor.moveToLineStart(e.shiftKey)
+ break
+ case 'End':
+ editor.moveToLineEnd(e.shiftKey)
+ break
+ default: {
+ if (!isMeta) return
+ const action = metaKeyActions[e.key]
+ if (!action) return
+ action(node)
+ e.preventDefault()
+ return
+ }
+ }
+
+ if (!textChanged) {
+ editor.requestRender()
+ }
+ resetBlink()
+ e.preventDefault()
+ }
+
+ function applyFormatting(nodeId: string, changes: Partial<SceneNode>, label: string) {
+ editor.updateNodeWithUndo(nodeId, changes, label)
+ const updated = editor.graph.getNode(nodeId)
+ if (updated) editor.textEditor?.rebuildParagraph(updated)
+ editor.requestRender()
+ }
+
+ function toggleBold(node: SceneNode) {
+ const editor = editor.textEditor
+ const range = editor?.getSelectionRange()
+ if (range) {
+ const { runs } = toggleBoldInRange(
+ node.styleRuns,
+ range[0],
+ range[1],
+ node.fontWeight,
+ node.text.length
+ )
+ applyFormatting(node.id, { styleRuns: runs }, 'Toggle bold')
+ } else {
+ applyFormatting(node.id, { fontWeight: node.fontWeight >= 700 ? 400 : 700 }, 'Toggle bold')
+ }
+ }
+
+ function toggleItalic(node: SceneNode) {
+ const editor = editor.textEditor
+ const range = editor?.getSelectionRange()
+ if (range) {
+ const { runs } = toggleItalicInRange(
+ node.styleRuns,
+ range[0],
+ range[1],
+ node.italic,
+ node.text.length
+ )
+ applyFormatting(node.id, { styleRuns: runs }, 'Toggle italic')
+ } else {
+ applyFormatting(node.id, { italic: !node.italic }, 'Toggle italic')
+ }
+ }
+
+ function toggleUnderline(node: SceneNode) {
+ const editor = editor.textEditor
+ const range = editor?.getSelectionRange()
+ if (range) {
+ const { runs } = toggleDecorationInRange(
+ node.styleRuns,
+ range[0],
+ range[1],
+ 'UNDERLINE',
+ node.textDecoration,
+ node.text.length
+ )
+ applyFormatting(node.id, { styleRuns: runs }, 'Toggle underline')
+ } else {
+ applyFormatting(
+ node.id,
+ { textDecoration: node.textDecoration === 'UNDERLINE' ? 'NONE' : 'UNDERLINE' },
+ 'Toggle underline'
+ )
+ }
+ }
+
+ function handleCopy() {
+ const editor = editor.textEditor
+ if (!editor) return
+ const text = editor.getSelectedText()
+ if (text) void navigator.clipboard.writeText(text)
+ }
+
+ function handleCut(node: ReturnType<typeof getEditingNode>) {
+ const editor = editor.textEditor
+ if (!editor || !node) return
+ const text = editor.getSelectedText()
+ if (text) {
+ void navigator.clipboard.writeText(text)
+ deleteText(node, false)
+ resetBlink()
+ }
+ }
+
+ async function handlePaste(node: ReturnType<typeof getEditingNode>) {
+ const editor = editor.textEditor
+ if (!editor || !node) return
+ try {
+ const text = await navigator.clipboard.readText()
+ if (text) {
+ insertText(text, node)
+ resetBlink()
+ }
+ } catch (e) {
+ console.warn('Clipboard access denied:', e)
+ }
+ }
+
+ useEventListener(textareaRef, 'input', onInput)
+ useEventListener(textareaRef, 'compositionstart', onCompositionStart)
+ useEventListener(textareaRef, 'compositionend', onCompositionEnd)
+ useEventListener(textareaRef, 'keydown', onKeyDown)
+
+ useEventListener(canvasRef, 'mousedown', () => {
+ if (editor.state.editingTextId && textareaRef.value) {
+ requestAnimationFrame(() => textareaRef.value?.focus())
+ }
+ })
+
+ watch(
+ () => editor.state.editingTextId,
+ (id, _, onCleanup) => {
+ if (id) {
+ const el = document.createElement('textarea')
+ el.style.cssText =
+ 'position:fixed;opacity:0;width:1px;height:1px;padding:0;border:0;top:50%;left:50%;overflow:hidden;resize:none;'
+ el.autocomplete = 'off'
+ el.setAttribute('autocorrect', 'off')
+ el.setAttribute('autocapitalize', 'none')
+ el.spellcheck = false
+ el.tabIndex = -1
+ el.setAttribute('aria-hidden', 'true')
+ document.body.appendChild(el)
+ textareaRef.value = el
+ el.focus()
+ resetBlink()
+
+ onCleanup(() => {
+ clearInterval(blinkTimer)
+ el.remove()
+ textareaRef.value = null
+ isComposing = false
+ })
+ }
+ }
+ )
+}
diff --git a/packages/vue/src/composables/use-toast.ts b/packages/vue/src/composables/use-toast.ts
new file mode 100644
index 000000000..5e6ca8aca
--- /dev/null
+++ b/packages/vue/src/composables/use-toast.ts
@@ -0,0 +1,39 @@
+import { useEventListener } from '@vueuse/core'
+import { ref } from 'vue'
+
+export type ToastVariant = 'default' | 'warning' | 'error'
+
+export interface Toast {
+ id: number
+ message: string
+ variant: ToastVariant
+}
+
+const TOAST_DURATION = 3000
+
+const toasts = ref<Toast[]>([])
+let nextId = 0
+let errorHandlersInitialized = false
+
+function show(message: string, variant: ToastVariant = 'default') {
+ toasts.value.push({ id: ++nextId, message, variant })
+}
+
+function remove(id: number) {
+ toasts.value = toasts.value.filter((t) => t.id !== id)
+}
+
+function setupGlobalErrorHandler() {
+ if (errorHandlersInitialized) return
+ errorHandlersInitialized = true
+
+ useEventListener(window, 'error', (e) => {
+ show(e.message || 'An unexpected error occurred', 'error')
+ })
+ useEventListener(window, 'unhandledrejection', (e) => {
+ const msg = e.reason instanceof Error ? e.reason.message : String(e.reason)
+ show(msg || 'An unexpected error occurred', 'error')
+ })
+}
+
+export const toast = { show, remove, toasts, setupGlobalErrorHandler, TOAST_DURATION }
diff --git a/packages/vue/src/index.ts b/packages/vue/src/index.ts
index 277d00652..400d55345 100644
--- a/packages/vue/src/index.ts
+++ b/packages/vue/src/index.ts
@@ -6,6 +6,17 @@ export { EDITOR_KEY, provideEditor, useEditor } from './context'
export { useCanvas } from './composables/use-canvas'
export type { UseCanvasOptions } from './composables/use-canvas'
+export { useCanvasInput } from './composables/use-canvas-input'
+export { useTextEdit } from './composables/use-text-edit'
+export { useCanvasDrop, extractImageFilesFromClipboard } from './composables/use-canvas-drop'
+export { useNodeProps } from './composables/use-node-props'
+export { useMultiProps, MIXED } from './composables/use-multi-props'
+export type { MixedValue } from './composables/use-multi-props'
+export { useInlineRename } from './composables/use-inline-rename'
+export { useNodeFontStatus } from './composables/use-font-status'
+export { toast } from './composables/use-toast'
+export type { Toast, ToastVariant } from './composables/use-toast'
+
export { default as OpenPencilProvider } from './components/OpenPencilProvider.vue'
export { default as OpenPencilCanvas } from './components/OpenPencilCanvas.vue'
export { default as PageList } from './components/PageList.vue'
diff --git a/src/composables/use-canvas-drop.ts b/src/composables/use-canvas-drop.ts
index 49fa56209..f8792837a 100644
--- a/src/composables/use-canvas-drop.ts
+++ b/src/composables/use-canvas-drop.ts
@@ -1,68 +1 @@
-import { useEventListener } from '@vueuse/core'
-import { ref, type Ref } from 'vue'
-
-import type { EditorStore } from '@/stores/editor'
-
-const ACCEPTED_TYPES = new Set(['image/png', 'image/jpeg', 'image/webp', 'image/gif', 'image/avif'])
-
-export function useCanvasDrop(canvasRef: Ref<HTMLCanvasElement | null>, store: EditorStore) {
- const isDraggingOver = ref(false)
-
- useEventListener(canvasRef, 'dragover', (e: DragEvent) => {
- if (!hasImageFiles(e)) return
- e.preventDefault()
- if (e.dataTransfer) e.dataTransfer.dropEffect = 'copy'
- isDraggingOver.value = true
- })
-
- useEventListener(canvasRef, 'dragenter', (e: DragEvent) => {
- if (!hasImageFiles(e)) return
- e.preventDefault()
- isDraggingOver.value = true
- })
-
- useEventListener(canvasRef, 'dragleave', () => {
- isDraggingOver.value = false
- })
-
- useEventListener(canvasRef, 'drop', (e: DragEvent) => {
- e.preventDefault()
- isDraggingOver.value = false
-
- const files = filterImageFiles(e.dataTransfer?.files ?? null)
- if (!files.length) return
-
- const canvas = canvasRef.value
- 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)
-
- void store.placeImageFiles(files, cx, cy)
- })
-
- return { isDraggingOver }
-}
-
-function hasImageFiles(e: DragEvent): boolean {
- if (!e.dataTransfer?.types.includes('Files')) return false
- for (const item of e.dataTransfer.items) {
- if (item.kind === 'file' && ACCEPTED_TYPES.has(item.type)) return true
- }
- return false
-}
-
-function filterImageFiles(files: FileList | null): File[] {
- if (!files) return []
- const result: File[] = []
- for (const file of files) {
- if (ACCEPTED_TYPES.has(file.type)) result.push(file)
- }
- return result
-}
-
-export function extractImageFilesFromClipboard(e: ClipboardEvent): File[] {
- return filterImageFiles(e.clipboardData?.files ?? null)
-}
+export { useCanvasDrop, extractImageFilesFromClipboard } from '@open-pencil/vue'
diff --git a/src/composables/use-canvas-input.ts b/src/composables/use-canvas-input.ts
index a548607f7..05c419b25 100644
--- a/src/composables/use-canvas-input.ts
+++ b/src/composables/use-canvas-input.ts
@@ -1,1495 +1 @@
-import { useEventListener } from '@vueuse/core'
-import { ref, type Ref } from 'vue'
-
-import {
- AUTO_LAYOUT_BREAK_THRESHOLD,
- CORNER_ROTATE_ZONE,
- HANDLE_HIT_RADIUS,
- PEN_CLOSE_THRESHOLD,
- ROTATION_SNAP_DEGREES,
- DEFAULT_TEXT_WIDTH,
- DEFAULT_TEXT_HEIGHT
-} from '@/constants'
-import { computeSelectionBounds, computeSnap, degToRad } from '@open-pencil/core'
-
-import type { EditorStore, Tool } from '@/stores/editor'
-import type { NodeType, Rect, SceneNode, Vector } from '@open-pencil/core'
-
-type HandlePosition = 'nw' | 'n' | 'ne' | 'e' | 'se' | 's' | 'sw' | 'w'
-
-interface DragDraw {
- type: 'draw'
- startX: number
- startY: number
- nodeId: string
-}
-
-interface DragMove {
- type: 'move'
- startX: number
- startY: number
- originals: Map<string, { x: number; y: number; parentId: string }>
- duplicated?: boolean
- autoLayoutParentId?: string
- brokeFromAutoLayout?: boolean
-}
-
-interface DragPan {
- type: 'pan'
- startScreenX: number
- startScreenY: number
- startPanX: number
- startPanY: number
-}
-
-interface DragResize {
- type: 'resize'
- handle: HandlePosition
- startX: number
- startY: number
- origRect: Rect
- nodeId: string
-}
-
-interface DragMarquee {
- type: 'marquee'
- startX: number
- startY: number
-}
-
-interface DragRotate {
- type: 'rotate'
- nodeId: string
- centerX: number
- centerY: number
- startAngle: number
- origRotation: number
-}
-
-interface DragPen {
- type: 'pen-drag'
- startX: number
- startY: number
-}
-
-interface DragTextSelect {
- type: 'text-select'
- startX: number
- startY: number
-}
-
-type DragState =
- | DragDraw
- | DragMove
- | DragPan
- | DragResize
- | DragMarquee
- | DragRotate
- | DragPen
- | DragTextSelect
-
-const TOOL_TO_NODE: Partial<Record<Tool, NodeType>> = {
- FRAME: 'FRAME',
- SECTION: 'SECTION',
- RECTANGLE: 'RECTANGLE',
- ELLIPSE: 'ELLIPSE',
- LINE: 'LINE',
- POLYGON: 'POLYGON',
- STAR: 'STAR',
- TEXT: 'TEXT'
-}
-
-const HANDLE_CURSORS: Record<HandlePosition, string> = {
- nw: 'nwse-resize',
- n: 'ns-resize',
- ne: 'nesw-resize',
- e: 'ew-resize',
- se: 'nwse-resize',
- s: 'ns-resize',
- sw: 'nesw-resize',
- w: 'ew-resize'
-}
-
-function getScreenRect(
- absX: number,
- absY: number,
- w: number,
- h: number,
- zoom: number,
- panX: number,
- panY: number
-) {
- return {
- x1: absX * zoom + panX,
- y1: absY * zoom + panY,
- x2: (absX + w) * zoom + panX,
- y2: (absY + h) * zoom + panY
- }
-}
-
-function getHandlePositions(
- absX: number,
- absY: number,
- w: number,
- h: number,
- zoom: number,
- panX: number,
- panY: number
-) {
- const { x1, y1, x2, y2 } = getScreenRect(absX, absY, w, h, zoom, panX, panY)
- const mx = (x1 + x2) / 2
- const my = (y1 + y2) / 2
-
- return {
- nw: { x: x1, y: y1 },
- n: { x: mx, y: y1 },
- ne: { x: x2, y: y1 },
- e: { x: x2, y: my },
- se: { x: x2, y: y2 },
- s: { x: mx, y: y2 },
- sw: { x: x1, y: y2 },
- w: { x: x1, y: my }
- } satisfies Record<HandlePosition, Vector>
-}
-
-function unrotate(
- sx: number,
- sy: number,
- centerX: number,
- centerY: number,
- rotation: number
-): { sx: number; sy: number } {
- if (rotation === 0) return { sx, sy }
- const rad = (-rotation * Math.PI) / 180
- const cos = Math.cos(rad)
- const sin = Math.sin(rad)
- const dx = sx - centerX
- const dy = sy - centerY
- return {
- sx: centerX + dx * cos - dy * sin,
- sy: centerY + dx * sin + dy * cos
- }
-}
-
-function hitTestHandle(
- sx: number,
- sy: number,
- absX: number,
- absY: number,
- w: number,
- h: number,
- zoom: number,
- panX: number,
- panY: number,
- rotation = 0
-): HandlePosition | null {
- const { x1, y1, x2, y2 } = getScreenRect(absX, absY, w, h, zoom, panX, panY)
- const cx = (x1 + x2) / 2
- const cy = (y1 + y2) / 2
- const ur = unrotate(sx, sy, cx, cy, rotation)
-
- const handles = getHandlePositions(absX, absY, w, h, zoom, panX, panY)
- for (const [pos, pt] of Object.entries(handles)) {
- if (Math.abs(ur.sx - pt.x) < HANDLE_HIT_RADIUS && Math.abs(ur.sy - pt.y) < HANDLE_HIT_RADIUS) {
- return pos as HandlePosition
- }
- }
- return null
-}
-
-type CornerPosition = 'nw' | 'ne' | 'se' | 'sw'
-
-function hitTestCornerRotation(
- sx: number,
- sy: number,
- absX: number,
- absY: number,
- w: number,
- h: number,
- zoom: number,
- panX: number,
- panY: number,
- rotation = 0
-): CornerPosition | null {
- const { x1, y1, x2, y2 } = getScreenRect(absX, absY, w, h, zoom, panX, panY)
- const cx = (x1 + x2) / 2
- const cy = (y1 + y2) / 2
- const ur = unrotate(sx, sy, cx, cy, rotation)
-
- const corners: Array<{ pos: CornerPosition; x: number; y: number }> = [
- { pos: 'nw', x: x1, y: y1 },
- { pos: 'ne', x: x2, y: y1 },
- { pos: 'se', x: x2, y: y2 },
- { pos: 'sw', x: x1, y: y2 }
- ]
-
- for (const { pos, x, y } of corners) {
- const dx = Math.abs(ur.sx - x)
- const dy = Math.abs(ur.sy - y)
- if (
- dx <= CORNER_ROTATE_ZONE &&
- dy <= CORNER_ROTATE_ZONE &&
- (dx > HANDLE_HIT_RADIUS || dy > HANDLE_HIT_RADIUS)
- ) {
- return pos
- }
- }
- return null
-}
-
-const CORNER_BASE_ANGLES: Record<CornerPosition, number> = { nw: 0, ne: 90, se: 180, sw: 270 }
-
-import rotateCursorSvg from '@/assets/rotate-cursor.svg?raw'
-
-const rotationCursorCache = new Map<number, string>()
-
-function buildRotationCursor(angleDeg: number): string {
- const key = Math.round(angleDeg) % 360
- let cached = rotationCursorCache.get(key)
- if (cached) return cached
- let svg: string
- if (key === 0) {
- svg = rotateCursorSvg
- } else {
- svg = rotateCursorSvg
- .replace(
- '<path',
- `<g transform='translate(1002 2110) rotate(${key}) translate(-1002 -2110)'><path`
- )
- .replace('</svg>', '</g></svg>')
- }
- cached = `url("data:image/svg+xml,${encodeURIComponent(svg)}") 12 12, auto`
- rotationCursorCache.set(key, cached)
- return cached
-}
-
-function cornerRotationCursor(corner: CornerPosition, nodeRotation = 0): string {
- return buildRotationCursor(CORNER_BASE_ANGLES[corner] + nodeRotation)
-}
-
-export function useCanvasInput(
- canvasRef: Ref<HTMLCanvasElement | null>,
- store: EditorStore,
- hitTestSectionTitle: (cx: number, cy: number) => SceneNode | null,
- hitTestComponentLabel: (cx: number, cy: number) => SceneNode | null,
- hitTestFrameTitle: (cx: number, cy: number) => SceneNode | null,
- onCursorMove?: (cx: number, cy: number) => void
-) {
- const drag = ref<DragState | null>(null)
- const cursorOverride = ref<string | null>(null)
- let lastClickTime = 0
- let lastClickX = 0
- let lastClickY = 0
- let clickCount = 0
- const MULTI_CLICK_DELAY = 500
- const MULTI_CLICK_RADIUS = 5
-
- function getCoords(e: MouseEvent) {
- const canvas = canvasRef.value
- if (!canvas) return { sx: 0, sy: 0, cx: 0, cy: 0 }
- 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)
- return { sx, sy, cx, cy }
- }
-
- function canvasToLocal(cx: number, cy: number, scopeId: string): { lx: number; ly: number } {
- const node = store.graph.getNode(scopeId)
- if (!node) return { lx: cx, ly: cy }
- const abs = store.graph.getAbsolutePosition(scopeId)
- let dx = cx - abs.x
- let dy = cy - abs.y
- if (node.rotation !== 0) {
- const hw = node.width / 2
- const hh = node.height / 2
- const rad = degToRad(-node.rotation)
- const cos = Math.cos(rad)
- const sin = Math.sin(rad)
- const rx = dx - hw
- const ry = dy - hh
- dx = rx * cos - ry * sin + hw
- dy = rx * sin + ry * cos + hh
- }
- return { lx: dx, ly: dy }
- }
-
- 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 { lx, ly } = canvasToLocal(cx, cy, scopeId)
- return deep
- ? store.graph.hitTestDeep(lx, ly, scopeId)
- : store.graph.hitTest(lx, ly, 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 { lx, ly } = canvasToLocal(cx, cy, containerId)
- return lx >= 0 && lx <= container.width && ly >= 0 && ly <= container.height
- }
-
- function startPanDrag(e: MouseEvent) {
- drag.value = {
- type: 'pan',
- startScreenX: e.clientX,
- startScreenY: e.clientY,
- startPanX: store.state.panX,
- startPanY: store.state.panY
- }
- }
-
- function handleTextEditClick(cx: number, cy: number, shiftKey: boolean): boolean {
- const editor = store.textEditor
- const editNode = store.state.editingTextId
- ? store.graph.getNode(store.state.editingTextId)
- : null
- if (!editor || !editNode) {
- store.commitTextEdit()
- return false
- }
- const abs = store.graph.getAbsolutePosition(editNode.id)
- const localX = cx - abs.x
- const localY = cy - abs.y
- if (localX < 0 || localY < 0 || localX > editNode.width || localY > editNode.height) {
- store.commitTextEdit()
- return false
- }
- if (clickCount >= 3) {
- editor.selectAll()
- } else if (clickCount === 2) {
- editor.selectWordAt(localX, localY)
- } else {
- editor.setCursorAt(localX, localY, shiftKey)
- drag.value = { type: 'text-select', startX: cx, startY: cy } as DragState
- }
- store.requestRender()
- return true
- }
-
- function tryStartRotation(sx: number, sy: number): boolean {
- if (store.state.selectedIds.size !== 1) return false
- const id = [...store.state.selectedIds][0]
- const node = store.graph.getNode(id)
- if (!node || node.locked) return false
- const abs = store.graph.getAbsolutePosition(id)
- if (
- !hitTestCornerRotation(
- sx,
- sy,
- abs.x,
- abs.y,
- node.width,
- node.height,
- store.state.zoom,
- store.state.panX,
- store.state.panY,
- node.rotation
- )
- )
- return false
-
- const screenCx = (abs.x + node.width / 2) * store.state.zoom + store.state.panX
- const screenCy = (abs.y + node.height / 2) * store.state.zoom + store.state.panY
- const startAngle = Math.atan2(sy - screenCy, sx - screenCx) * (180 / Math.PI)
- drag.value = {
- type: 'rotate',
- nodeId: id,
- centerX: screenCx,
- centerY: screenCy,
- startAngle,
- origRotation: node.rotation
- }
- return true
- }
-
- function tryStartResize(sx: number, sy: number, cx: number, cy: number): boolean {
- for (const id of store.state.selectedIds) {
- const node = store.graph.getNode(id)
- if (!node || node.locked) continue
- const abs = store.graph.getAbsolutePosition(id)
- const handle = hitTestHandle(
- sx,
- sy,
- abs.x,
- abs.y,
- node.width,
- node.height,
- store.state.zoom,
- store.state.panX,
- store.state.panY,
- node.rotation
- )
- if (handle) {
- drag.value = {
- type: 'resize',
- handle,
- startX: cx,
- startY: cy,
- origRect: { x: node.x, y: node.y, width: node.width, height: node.height },
- nodeId: id
- }
- return true
- }
- }
- return false
- }
-
- 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 store.state.selectedIds) {
- const src = store.graph.getNode(id)
- if (!src) continue
- const newId = store.createShape(src.type, src.x, src.y, src.width, src.height)
- store.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 = store.graph.getNode(newId)
- newOriginals.set(newId, {
- x: src.x,
- y: src.y,
- parentId: newNode?.parentId ?? store.state.currentPageId
- })
- }
- store.select(newIds)
- drag.value = {
- type: 'move',
- startX: cx,
- startY: cy,
- originals: newOriginals,
- duplicated: true
- }
- store.requestRender()
- return newOriginals
- }
-
- function detectAutoLayoutParent(): string | undefined {
- if (store.state.selectedIds.size !== 1) return undefined
- const selectedId = [...store.state.selectedIds][0]
- const selectedNode = store.graph.getNode(selectedId)
- if (!selectedNode?.parentId) return undefined
- const parent = store.graph.getNode(selectedNode.parentId)
- if (parent && parent.layoutMode !== 'NONE' && selectedNode.layoutPositioning !== 'ABSOLUTE') {
- return parent.id
- }
- 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
-
- if (store.state.editingTextId) store.commitTextEdit()
-
- if (tryStartRotation(sx, sy)) return
- if (tryStartResize(sx, sy, cx, cy)) return
-
- const hit = resolveHit(cx, cy)
- if (!hit) {
- if (!store.state.enteredContainerId) {
- store.clearSelection()
- drag.value = { type: 'marquee', startX: cx, startY: cy }
- }
- return
- }
-
- if (!store.state.selectedIds.has(hit.id) && !e.shiftKey) {
- store.select([hit.id])
- } else if (e.shiftKey) {
- store.select([hit.id], true)
- }
-
- const allLocked = [...store.state.selectedIds].every((id) => store.graph.getNode(id)?.locked)
- if (allLocked) return
-
- const originals = new Map<string, { x: number; y: number; parentId: string }>()
- for (const id of store.state.selectedIds) {
- const n = store.graph.getNode(id)
- if (n)
- originals.set(id, { x: n.x, y: n.y, parentId: n.parentId ?? store.state.currentPageId })
- }
-
- if (e.altKey && store.state.selectedIds.size > 0) {
- duplicateAndDrag(cx, cy)
- return
- }
-
- drag.value = {
- type: 'move',
- startX: cx,
- startY: cy,
- originals,
- autoLayoutParentId: detectAutoLayoutParent()
- }
- }
-
- function onMouseDown(e: MouseEvent) {
- store.setHoveredNode(null)
- const { sx, sy, cx, cy } = getCoords(e)
-
- const now = performance.now()
- if (
- now - lastClickTime < MULTI_CLICK_DELAY &&
- Math.abs(sx - lastClickX) < MULTI_CLICK_RADIUS &&
- Math.abs(sy - lastClickY) < MULTI_CLICK_RADIUS
- ) {
- clickCount++
- } else {
- clickCount = 1
- }
- lastClickTime = now
- lastClickX = sx
- lastClickY = sy
- const tool = store.state.activeTool
-
- if (e.button === 1 || tool === 'HAND') {
- startPanDrag(e)
- return
- }
-
- if (tool === 'SELECT' && e.altKey && !store.state.selectedIds.size) {
- startPanDrag(e)
- return
- }
-
- if (tool === 'SELECT') {
- handleSelectDown(e, sx, sy, cx, cy)
- return
- }
-
- if (tool === 'PEN') {
- store.penAddVertex(cx, cy)
- drag.value = { type: 'pen-drag', startX: cx, startY: cy } as DragState
- return
- }
-
- if (tool === 'TEXT') {
- const nodeId = store.createShape('TEXT', cx, cy, DEFAULT_TEXT_WIDTH, DEFAULT_TEXT_HEIGHT)
- store.graph.updateNode(nodeId, { text: '' })
- store.select([nodeId])
- store.startTextEditing(nodeId)
- store.setTool('SELECT')
- store.requestRender()
- return
- }
-
- const nodeType = TOOL_TO_NODE[tool]
- if (!nodeType) return
-
- const nodeId = store.createShape(nodeType, cx, cy, 0, 0)
- store.select([nodeId])
-
- 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 store.state.selectedIds) {
- const node = store.graph.getNode(id)
- if (!node) continue
- const abs = store.graph.getAbsolutePosition(id)
- const handle = hitTestHandle(
- sx,
- sy,
- abs.x,
- abs.y,
- node.width,
- node.height,
- store.state.zoom,
- store.state.panX,
- store.state.panY,
- node.rotation
- )
- if (handle) {
- cursor = HANDLE_CURSORS[handle]
- break
- }
- }
-
- if (!cursor && store.state.selectedIds.size === 1) {
- const id = [...store.state.selectedIds][0]
- const node = store.graph.getNode(id)
- if (node) {
- const abs = store.graph.getAbsolutePosition(id)
- const corner = hitTestCornerRotation(
- sx,
- sy,
- abs.x,
- abs.y,
- node.width,
- node.height,
- store.state.zoom,
- store.state.panX,
- store.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)
- store.setHoveredNode(hit && !store.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
- store.state.panX = d.startPanX + dx
- store.state.panY = d.startPanY + dy
- store.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
- store.setRotationPreview({ nodeId: d.nodeId, angle: rotation })
- }
-
- function findDropTarget(cx: number, cy: number) {
- let dropTarget = store.graph.hitTestFrame(
- cx,
- cy,
- store.state.selectedIds,
- store.state.currentPageId
- )
- const movingSection = [...store.state.selectedIds].some(
- (id) => store.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 = store.graph.getNode(id)
- if (n) {
- const abs = store.graph.getAbsolutePosition(id)
- const parentAbs = n.parentId ? store.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 = store.graph.getNode(firstId)
- const parentId = firstNode?.parentId ?? store.state.currentPageId
- const siblings = store.graph.getChildren(parentId)
- const parentAbs = !store.isTopLevel(parentId)
- ? store.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(store.state.selectedIds, absBounds, absTargets)
- store.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)
- return
- }
- d.brokeFromAutoLayout = true
- store.setLayoutInsertIndicator(null)
- }
-
- const dropTarget = findDropTarget(cx, cy)
- const dropParent = dropTarget ? store.graph.getNode(dropTarget.id) : null
-
- if (dropParent && dropParent.layoutMode !== 'NONE') {
- computeAutoLayoutIndicatorForFrame(dropParent, cx, cy)
- store.setDropTarget(dropParent.id)
- for (const [id, orig] of d.originals) {
- store.graph.updateNode(id, {
- x: Math.round(orig.x + dx),
- y: Math.round(orig.y + dy)
- })
- }
- store.requestRender()
- return
- }
-
- store.setLayoutInsertIndicator(null)
-
- const snapped = applyMoveSnap(d, dx, dy)
- dx = snapped.dx
- dy = snapped.dy
-
- for (const [id, orig] of d.originals) {
- store.updateNode(id, { x: Math.round(orig.x + dx), y: Math.round(orig.y + dy) })
- }
-
- store.setDropTarget(dropTarget?.id ?? null)
- }
-
- function handleTextSelectMove(cx: number, cy: number) {
- const editor = store.textEditor
- const editNode = store.state.editingTextId
- ? store.graph.getNode(store.state.editingTextId)
- : null
- if (editor && editNode) {
- const abs = store.graph.getAbsolutePosition(editNode.id)
- editor.setCursorAt(cx - abs.x, cy - abs.y, true)
- store.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
- }
-
- store.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 = store.state.enteredContainerId
- const parentId = scopeId ?? store.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 store.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)
- }
- }
- store.select(hits)
- store.setMarquee({ x: minX, y: minY, width: maxX - minX, height: maxY - minY })
- }
-
- function onMouseMove(e: MouseEvent) {
- {
- const { cx, cy } = getCoords(e)
- store.state.cursorCanvasX = cx
- store.state.cursorCanvasY = cy
- if (onCursorMove) onCursorMove(cx, cy)
- }
-
- if (store.state.activeTool === 'PEN' && store.state.penState && !drag.value) {
- const { cx, cy } = getCoords(e)
- store.state.penCursorX = cx
- store.state.penCursorY = cy
-
- const first = store.state.penState.vertices[0]
- if (store.state.penState.vertices.length > 2) {
- const dist = Math.hypot(cx - first.x, cy - first.y)
- store.penSetClosingToFirst(dist < PEN_CLOSE_THRESHOLD)
- }
- store.requestRepaint()
- }
-
- if (!drag.value && store.state.activeTool === 'SELECT') {
- updateHoverCursor(e)
- }
-
- if (!drag.value) return
- const d = drag.value
-
- if (d.type === 'pan') {
- handlePanMove(d, e)
- return
- }
-
- const { cx, cy, sx, sy } = getCoords(e)
-
- if (d.type === 'rotate') {
- handleRotateMove(d, sx, sy, e.shiftKey)
- return
- }
- if (d.type === 'move') {
- handleMoveMove(d, cx, cy)
- return
- }
- if (d.type === 'text-select') {
- handleTextSelectMove(cx, cy)
- return
- }
- if (d.type === 'resize') {
- applyResize(d, cx, cy, e.shiftKey)
- return
- }
-
- if (d.type === 'pen-drag') {
- const tx = cx - d.startX
- const ty = cy - d.startY
- if (Math.hypot(tx, ty) > 2) {
- store.penSetDragTangent(tx, ty)
- }
- return
- }
-
- if (d.type === 'draw') {
- handleDrawMove(d, cx, cy, e.shiftKey)
- return
- }
-
- handleMarqueeMove(d, cx, cy)
- }
-
- function constrainToAspectRatio(
- handle: HandlePosition,
- origRect: Rect,
- width: number,
- height: number,
- dx: number,
- dy: number
- ): Rect {
- let x = handle.includes('w') ? origRect.x + origRect.width - Math.abs(width) : origRect.x
- const isTop = handle === 'nw' || handle === 'n' || handle === 'ne'
- let y = isTop ? origRect.y + origRect.height - Math.abs(height) : origRect.y
- const aspect = origRect.width / origRect.height
-
- if (handle === 'n' || handle === 's') {
- width = Math.abs(height) * aspect
- x = origRect.x + (origRect.width - width) / 2
- } else if (handle === 'e' || handle === 'w') {
- height = Math.abs(width) / aspect
- y = origRect.y + (origRect.height - height) / 2
- } else if (Math.abs(dx) > Math.abs(dy)) {
- height = (Math.abs(width) / aspect) * Math.sign(height || 1)
- if (isTop) y = origRect.y + origRect.height - Math.abs(height)
- } else {
- width = Math.abs(height) * aspect * Math.sign(width || 1)
- if (handle.includes('w')) x = origRect.x + origRect.width - Math.abs(width)
- }
-
- return { x, y, width, height }
- }
-
- function applyResize(d: DragResize, cx: number, cy: number, constrain: boolean) {
- const { handle, origRect } = d
- let { x, y, width, height } = origRect
- const dx = cx - d.startX
- const dy = cy - d.startY
-
- const moveLeft = handle.includes('w')
- const moveRight = handle.includes('e')
- const moveTop = handle === 'nw' || handle === 'n' || handle === 'ne'
- const moveBottom = handle === 'sw' || handle === 's' || handle === 'se'
-
- if (moveRight) width = origRect.width + dx
- if (moveLeft) {
- x = origRect.x + dx
- width = origRect.width - dx
- }
- if (moveBottom) height = origRect.height + dy
- if (moveTop) {
- y = origRect.y + dy
- height = origRect.height - dy
- }
-
- if (constrain && origRect.width > 0 && origRect.height > 0) {
- ;({ x, y, width, height } = constrainToAspectRatio(handle, origRect, width, height, dx, dy))
- }
-
- if (width < 0) {
- x += width
- width = -width
- }
- if (height < 0) {
- y += height
- height = -height
- }
-
- store.updateNode(d.nodeId, {
- x: Math.round(x),
- y: Math.round(y),
- width: Math.round(Math.max(1, width)),
- height: Math.round(Math.max(1, height))
- })
- }
-
- function handleMoveUp(d: DragMove) {
- const indicator = store.state.layoutInsertIndicator
- store.setLayoutInsertIndicator(null)
- store.setSnapGuides([])
-
- if (indicator) {
- for (const id of store.state.selectedIds) {
- store.reorderInAutoLayout(id, indicator.parentId, indicator.index)
- }
- store.setDropTarget(null)
- return
- }
-
- const moved = [...d.originals].some(([id, orig]) => {
- const node = store.graph.getNode(id)
- return node && (node.x !== orig.x || node.y !== orig.y)
- })
-
- if (moved) {
- const dropId = store.state.dropTargetId
- if (dropId) {
- store.reparentNodes([...store.state.selectedIds], dropId)
- } else {
- reparentOutsideNodes()
- }
- store.commitMoveWithReparent(d.originals)
- }
- store.setDropTarget(null)
- }
-
- function reparentOutsideNodes() {
- for (const id of store.state.selectedIds) {
- const node = store.graph.getNode(id)
- if (!node?.parentId || store.isTopLevel(node.parentId)) continue
- const parent = store.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 ?? store.state.currentPageId
- store.graph.reparentNode(id, grandparentId)
- }
- }
- }
-
- function handleDrawUp(d: DragDraw) {
- const node = store.graph.getNode(d.nodeId)
- if (node && node.width < 2 && node.height < 2) {
- store.updateNode(d.nodeId, { width: 100, height: 100 })
- }
- if (node?.type === 'SECTION') {
- store.adoptNodesIntoSection(node.id)
- }
- store.setTool('SELECT')
- }
-
- function onMouseUp() {
- if (!drag.value) return
- const d = drag.value
-
- if (d.type === 'move') handleMoveUp(d)
- else if (d.type === 'text-select') {
- drag.value = null
- return
- } else if (d.type === 'resize') store.commitResize(d.nodeId, d.origRect)
- else if (d.type === 'pen-drag') {
- drag.value = null
- return
- } else if (d.type === 'rotate') {
- const preview = store.state.rotationPreview
- if (preview) {
- store.updateNode(d.nodeId, { rotation: preview.angle })
- store.commitRotation(d.nodeId, d.origRotation)
- }
- store.setRotationPreview(null)
- } else if (d.type === 'draw') handleDrawUp(d)
- else if (d.type === 'marquee') store.setMarquee(null)
-
- drag.value = null
- cursorOverride.value = null
- }
-
- const wheelAccum = {
- deltaX: 0,
- deltaY: 0,
- zoomDelta: 0,
- zoomCenterX: 0,
- zoomCenterY: 0,
- hasZoom: false,
- rafId: 0
- }
-
- function flushWheel() {
- wheelAccum.rafId = 0
- store.setHoveredNode(null)
- if (wheelAccum.hasZoom) {
- store.applyZoom(wheelAccum.zoomDelta, wheelAccum.zoomCenterX, wheelAccum.zoomCenterY)
- } else {
- store.pan(wheelAccum.deltaX, wheelAccum.deltaY)
- }
- wheelAccum.deltaX = 0
- wheelAccum.deltaY = 0
- wheelAccum.zoomDelta = 0
- wheelAccum.hasZoom = false
- }
-
- // Normalize wheel deltaY across deltaMode variants (line/page/pixel).
- // Trackpad pinch is always DOM_DELTA_PIXEL; external mice may use LINE or PAGE.
- function normalizeWheelDelta(e: WheelEvent): { dx: number; dy: number } {
- let { deltaX, deltaY } = e
- if (e.deltaMode === WheelEvent.DOM_DELTA_LINE) {
- deltaX *= 40
- deltaY *= 40
- } else if (e.deltaMode === WheelEvent.DOM_DELTA_PAGE) {
- deltaX *= 800
- deltaY *= 800
- }
- return { dx: deltaX, dy: deltaY }
- }
-
- function onWheel(e: WheelEvent) {
- e.preventDefault()
- const canvas = canvasRef.value
- if (!canvas) return
- const { dx, dy } = normalizeWheelDelta(e)
-
- if (e.ctrlKey || e.metaKey) {
- const rect = canvas.getBoundingClientRect()
- wheelAccum.zoomCenterX = e.clientX - rect.left
- wheelAccum.zoomCenterY = e.clientY - rect.top
- wheelAccum.zoomDelta += dy
- wheelAccum.hasZoom = true
- } else {
- wheelAccum.deltaX -= dx
- wheelAccum.deltaY -= dy
- }
- if (!wheelAccum.rafId) {
- wheelAccum.rafId = requestAnimationFrame(flushWheel)
- }
- }
-
- function onDblClick(e: MouseEvent) {
- if (store.state.editingTextId) return
-
- const { cx, cy } = getCoords(e)
-
- const selectedId =
- store.state.selectedIds.size === 1 ? [...store.state.selectedIds][0] : undefined
- const selectedNode = selectedId ? store.graph.getNode(selectedId) : undefined
- const canEnter =
- selectedNode && selectedId && store.graph.isContainer(selectedId) && !selectedNode.locked
-
- if (canEnter) {
- store.enterContainer(selectedId)
- const useDeep = selectedNode.type === 'COMPONENT' || selectedNode.type === 'INSTANCE'
- const hit = hitTestInScope(cx, cy, useDeep)
- 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') {
- store.select([hit.id])
- store.startTextEditing(hit.id)
- const editor = store.textEditor
- if (editor) {
- const abs = store.graph.getAbsolutePosition(hit.id)
- editor.selectWordAt(cx - abs.x, cy - abs.y)
- store.requestRender()
- }
- return
- }
-
- store.select([hit.id])
- }
-
- function computeAutoLayoutIndicator(d: DragMove, cx: number, cy: number) {
- if (!d.autoLayoutParentId) return
- const parent = store.graph.getNode(d.autoLayoutParentId)
- if (!parent || parent.layoutMode === 'NONE') return
- computeAutoLayoutIndicatorForFrame(parent, cx, cy)
- }
-
- function computeIndicatorPosition(
- children: SceneNode[],
- insertIndex: number,
- parent: SceneNode,
- parentAbs: Vector,
- isRow: boolean
- ): number {
- if (children.length === 0) {
- return isRow ? parentAbs.x + parent.paddingLeft : parentAbs.y + parent.paddingTop
- }
- if (insertIndex === 0) {
- const firstAbs = store.graph.getAbsolutePosition(children[0].id)
- return (isRow ? firstAbs.x : firstAbs.y) - parent.itemSpacing / 2
- }
- if (insertIndex >= children.length) {
- const last = children[children.length - 1]
- const lastAbs = store.graph.getAbsolutePosition(last.id)
- return isRow
- ? lastAbs.x + last.width + parent.itemSpacing / 2
- : lastAbs.y + last.height + parent.itemSpacing / 2
- }
- const prev = children[insertIndex - 1]
- const next = children[insertIndex]
- const prevAbs = store.graph.getAbsolutePosition(prev.id)
- const nextAbs = store.graph.getAbsolutePosition(next.id)
- return isRow
- ? (prevAbs.x + prev.width + nextAbs.x) / 2
- : (prevAbs.y + prev.height + nextAbs.y) / 2
- }
-
- function filteredToRealIndex(parentId: string, insertIndex: number): number {
- const allChildren = store.graph.getChildren(parentId)
- let realIndex = 0
- let filteredCount = 0
- for (const child of allChildren) {
- if (store.state.selectedIds.has(child.id)) continue
- if (child.layoutPositioning === 'ABSOLUTE') {
- realIndex++
- continue
- }
- if (filteredCount === insertIndex) break
- filteredCount++
- realIndex++
- }
- return realIndex
- }
-
- function computeAutoLayoutIndicatorForFrame(parent: SceneNode, cx: number, cy: number) {
- const children = store.graph
- .getChildren(parent.id)
- .filter((c) => c.layoutPositioning !== 'ABSOLUTE' && !store.state.selectedIds.has(c.id))
-
- const parentAbs = store.graph.getAbsolutePosition(parent.id)
- const isRow = parent.layoutMode === 'HORIZONTAL'
-
- let insertIndex = children.length
- for (let i = 0; i < children.length; i++) {
- const childAbs = store.graph.getAbsolutePosition(children[i].id)
- const mid = isRow ? childAbs.x + children[i].width / 2 : childAbs.y + children[i].height / 2
- if ((isRow ? cx : cy) < mid) {
- insertIndex = i
- break
- }
- }
-
- const indicatorPos = computeIndicatorPosition(children, insertIndex, parent, parentAbs, isRow)
- const crossStart = isRow ? parentAbs.y + parent.paddingTop : parentAbs.x + parent.paddingLeft
- const crossLength = isRow
- ? parent.height - parent.paddingTop - parent.paddingBottom
- : parent.width - parent.paddingLeft - parent.paddingRight
-
- store.setLayoutInsertIndicator({
- parentId: parent.id,
- index: filteredToRealIndex(parent.id, insertIndex),
- x: isRow ? indicatorPos : crossStart,
- y: isRow ? crossStart : indicatorPos,
- length: crossLength,
- direction: isRow ? 'VERTICAL' : 'HORIZONTAL'
- })
- }
-
- let activeTouches: Touch[] = []
- let pinchStartDist = 0
- let pinchStartZoom = 0
- let pinchMidX = 0
- let pinchMidY = 0
-
- function touchDist(a: Touch, b: Touch) {
- return Math.hypot(a.clientX - b.clientX, a.clientY - b.clientY)
- }
-
- let touchAsMouse = false
-
- function syntheticMouse(type: string, t: Touch): MouseEvent {
- return new MouseEvent(type, {
- clientX: t.clientX,
- clientY: t.clientY,
- screenX: t.screenX,
- screenY: t.screenY,
- button: 0,
- buttons: 1,
- bubbles: true
- })
- }
-
- function onTouchStart(e: TouchEvent) {
- e.preventDefault()
- activeTouches = Array.from(e.touches)
- const canvas = canvasRef.value
- if (!canvas) return
-
- if (activeTouches.length === 2) {
- if (touchAsMouse) {
- onMouseUp()
- touchAsMouse = false
- }
- drag.value = null
- const [a, b] = activeTouches
- pinchStartDist = touchDist(a, b)
- pinchStartZoom = store.state.zoom
- const rect = canvas.getBoundingClientRect()
- pinchMidX = (a.clientX + b.clientX) / 2 - rect.left
- pinchMidY = (a.clientY + b.clientY) / 2 - rect.top
- } else if (activeTouches.length === 1) {
- const t = activeTouches[0]
- const tool = store.state.activeTool
- if (tool === 'HAND') {
- touchAsMouse = false
- drag.value = {
- type: 'pan',
- startScreenX: t.clientX,
- startScreenY: t.clientY,
- startPanX: store.state.panX,
- startPanY: store.state.panY
- }
- } else {
- touchAsMouse = true
- onMouseDown(syntheticMouse('mousedown', t))
- }
- }
- }
-
- function onTouchMove(e: TouchEvent) {
- e.preventDefault()
- activeTouches = Array.from(e.touches)
- const canvas = canvasRef.value
- if (!canvas) return
-
- if (activeTouches.length === 2) {
- const [a, b] = activeTouches
- const rect = canvas.getBoundingClientRect()
- const newMidX = (a.clientX + b.clientX) / 2 - rect.left
- const newMidY = (a.clientY + b.clientY) / 2 - rect.top
-
- store.setHoveredNode(null)
- const newDist = touchDist(a, b)
- if (pinchStartDist > 0) {
- const scale = newDist / pinchStartDist
- const newZoom = Math.max(0.02, Math.min(256, pinchStartZoom * scale))
- const zoomRatio = newZoom / store.state.zoom
-
- const panDx = newMidX - pinchMidX
- const panDy = newMidY - pinchMidY
-
- store.state.panX = pinchMidX - (pinchMidX - store.state.panX) * zoomRatio + panDx
- store.state.panY = pinchMidY - (pinchMidY - store.state.panY) * zoomRatio + panDy
- store.state.zoom = newZoom
- }
-
- pinchMidX = newMidX
- pinchMidY = newMidY
- store.requestRepaint()
- } else if (activeTouches.length === 1) {
- const t = activeTouches[0]
- if (touchAsMouse) {
- onMouseMove(syntheticMouse('mousemove', t))
- } else if (drag.value?.type === 'pan') {
- const d = drag.value
- store.state.panX = d.startPanX + (t.clientX - d.startScreenX)
- store.state.panY = d.startPanY + (t.clientY - d.startScreenY)
- store.requestRepaint()
- }
- }
- }
-
- function onTouchEnd(e: TouchEvent) {
- e.preventDefault()
- activeTouches = Array.from(e.touches)
-
- if (activeTouches.length === 0) {
- if (touchAsMouse) {
- onMouseUp()
- touchAsMouse = false
- } else {
- drag.value = null
- }
- pinchStartDist = 0
- } else if (activeTouches.length === 1) {
- const t = activeTouches[0]
- if (!touchAsMouse) {
- drag.value = {
- type: 'pan',
- startScreenX: t.clientX,
- startScreenY: t.clientY,
- startPanX: store.state.panX,
- startPanY: store.state.panY
- }
- }
- pinchStartDist = 0
- }
- }
-
- useEventListener(canvasRef, 'dblclick', onDblClick)
- useEventListener(canvasRef, 'mousedown', onMouseDown)
- useEventListener(canvasRef, 'mousemove', onMouseMove)
- useEventListener(canvasRef, 'mouseup', onMouseUp)
- useEventListener(canvasRef, 'mouseleave', () => {
- if (!drag.value) {
- store.setHoveredNode(null)
- }
- })
- useEventListener(window, 'mouseup', () => {
- if (drag.value) onMouseUp()
- })
- useEventListener(canvasRef, 'wheel', onWheel, { passive: false })
- useEventListener(canvasRef, 'touchstart', onTouchStart, { passive: false })
- useEventListener(canvasRef, 'touchmove', onTouchMove, { passive: false })
- useEventListener(canvasRef, 'touchend', onTouchEnd, { passive: false })
- useEventListener(canvasRef, 'touchcancel', onTouchEnd, { passive: false })
-
- // Safari macOS: trackpad pinch-to-zoom uses gesture events, not wheel+ctrlKey
- let gestureStartZoom = 1
- let gestureRafId = 0
- let pendingGesture: { scale: number; sx: number; sy: number } | null = null
-
- function flushGesture() {
- gestureRafId = 0
- if (!pendingGesture) return
- store.setHoveredNode(null)
- const { scale, sx, sy } = pendingGesture
- pendingGesture = null
- const newZoom = Math.max(0.02, Math.min(256, gestureStartZoom * scale))
- const zoomRatio = newZoom / store.state.zoom
- store.state.panX = sx - (sx - store.state.panX) * zoomRatio
- store.state.panY = sy - (sy - store.state.panY) * zoomRatio
- store.state.zoom = newZoom
- store.requestRepaint()
- }
-
- useEventListener(
- canvasRef,
- 'gesturestart' as keyof HTMLElementEventMap,
- (e: Event) => {
- e.preventDefault()
- gestureStartZoom = store.state.zoom
- },
- { passive: false }
- )
- useEventListener(
- canvasRef,
- 'gesturechange' as keyof HTMLElementEventMap,
- (e: Event) => {
- e.preventDefault()
- const ge = e as GestureEvent
- const canvas = canvasRef.value
- if (!canvas) return
- const rect = canvas.getBoundingClientRect()
- pendingGesture = {
- scale: ge.scale,
- sx: ge.clientX - rect.left,
- sy: ge.clientY - rect.top
- }
- if (!gestureRafId) {
- gestureRafId = requestAnimationFrame(flushGesture)
- }
- },
- { passive: false }
- )
- useEventListener(
- canvasRef,
- 'gestureend' as keyof HTMLElementEventMap,
- (e: Event) => {
- e.preventDefault()
- },
- { passive: false }
- )
-
- return {
- drag,
- cursorOverride
- }
-}
+export { useCanvasInput } from '@open-pencil/vue'
diff --git a/src/composables/use-font-status.ts b/src/composables/use-font-status.ts
index 2d2c90f98..8cfb51ff7 100644
--- a/src/composables/use-font-status.ts
+++ b/src/composables/use-font-status.ts
@@ -1,24 +1 @@
-import { computed } from 'vue'
-
-import { isFontLoaded, DEFAULT_FONT_FAMILY } from '@open-pencil/core'
-
-import type { SceneNode } from '@open-pencil/core'
-
-export function useNodeFontStatus(node: () => SceneNode) {
- const missingFonts = computed(() => {
- const n = node()
- if (n.type !== 'TEXT') return []
-
- const families = new Set<string>()
- families.add(n.fontFamily || DEFAULT_FONT_FAMILY)
- for (const run of n.styleRuns) {
- if (run.style.fontFamily) families.add(run.style.fontFamily)
- }
-
- return [...families].filter((f) => !isFontLoaded(f))
- })
-
- const hasMissingFonts = computed(() => missingFonts.value.length > 0)
-
- return { missingFonts, hasMissingFonts }
-}
+export { useNodeFontStatus } from '@open-pencil/vue'
diff --git a/src/composables/use-inline-rename.ts b/src/composables/use-inline-rename.ts
index be8dc13b8..23f2fd70b 100644
--- a/src/composables/use-inline-rename.ts
+++ b/src/composables/use-inline-rename.ts
@@ -1,58 +1 @@
-import { onClickOutside } from '@vueuse/core'
-import { nextTick, ref, type Ref } from 'vue'
-
-export function useInlineRename<T extends string>(onCommit: (id: T, newName: string) => void) {
- const editingId = ref<T | null>(null)
- const inputRef: Ref<HTMLInputElement | null> = ref(null)
- let originalName = ''
- let cleanupOutsideClick: (() => void) | undefined
-
- function start(id: T, currentName: string) {
- editingId.value = id
- originalName = currentName
- }
-
- async function focusInput(input: HTMLInputElement | null) {
- if (input === inputRef.value) return
- inputRef.value = input
- cleanupOutsideClick?.()
- if (input) {
- cleanupOutsideClick = onClickOutside(inputRef, () => input.blur())
- }
- await nextTick()
- input?.focus()
- input?.select()
- }
-
- function commit(id: T, input: HTMLInputElement) {
- if (editingId.value !== id) return
- const value = input.value.trim()
- if (value && value !== originalName) {
- onCommit(id, value)
- }
- editingId.value = null
- inputRef.value = null
- cleanupOutsideClick?.()
- cleanupOutsideClick = undefined
- }
-
- function cancel() {
- editingId.value = null
- inputRef.value = null
- cleanupOutsideClick?.()
- cleanupOutsideClick = undefined
- }
-
- function onKeydown(e: KeyboardEvent) {
- if (e.key === 'Enter') {
- ;(e.target as HTMLInputElement).blur()
- return
- }
-
- if (e.key === 'Escape') {
- cancel()
- }
- }
-
- return { editingId, start, focusInput, commit, cancel, onKeydown }
-}
+export { useInlineRename } from '@open-pencil/vue'
diff --git a/src/composables/use-multi-props.ts b/src/composables/use-multi-props.ts
index 3bfe0988f..2ba0c4357 100644
--- a/src/composables/use-multi-props.ts
+++ b/src/composables/use-multi-props.ts
@@ -1,106 +1,2 @@
-import { computed } from 'vue'
-
-import { useEditorStore } from '@/stores/editor'
-
-import type { SceneNode } from '@open-pencil/core'
-
-export const MIXED = Symbol('mixed')
-export type MixedValue<T> = T | typeof MIXED
-
-export function useMultiProps() {
- const store = useEditorStore()
- const node = computed(() => store.selectedNode.value ?? null)
- const nodes = computed(() => store.selectedNodes.value)
- const isMulti = computed(() => nodes.value.length > 1)
- const active = computed(() => node.value || isMulti.value)
- const activeNode = computed(() => node.value ?? (nodes.value[0] as SceneNode | undefined) ?? null)
-
- function merged<K extends keyof SceneNode>(key: K): MixedValue<SceneNode[K]> {
- const all = nodes.value
- if (all.length === 0) return MIXED
- const first = all[0][key]
- for (let i = 1; i < all.length; i++) {
- if (all[i][key] !== first) return MIXED
- }
- return first
- }
-
- function prop<K extends keyof SceneNode>(key: K) {
- return computed(() => merged(key))
- }
-
- function updateAllWithUndo(patch: Partial<SceneNode>, label: string) {
- for (const n of nodes.value) {
- store.updateNodeWithUndo(n.id, patch, label)
- }
- store.requestRender()
- }
-
- function isArrayMixed(key: keyof SceneNode): boolean {
- const all = nodes.value
- if (all.length <= 1) return false
- const first = JSON.stringify(all[0][key])
- return all.some((n) => JSON.stringify(n[key]) !== first)
- }
-
- type ArrayPropKey = 'fills' | 'strokes' | 'effects'
-
- function targetNodes(): SceneNode[] {
- if (isMulti.value) return nodes.value
- return activeNode.value ? [activeNode.value] : []
- }
-
- function updateArrayItem(
- key: ArrayPropKey,
- index: number,
- patch: Record<string, unknown>,
- label: string
- ) {
- for (const n of targetNodes()) {
- const arr = [...n[key]]
- arr[index] = { ...arr[index], ...patch } as (typeof arr)[number]
- store.updateNodeWithUndo(n.id, { [key]: arr } as Partial<SceneNode>, label)
- }
- }
-
- function removeArrayItem(key: ArrayPropKey, index: number, label: string) {
- for (const n of targetNodes()) {
- store.updateNodeWithUndo(
- n.id,
- { [key]: (n[key] as unknown[]).filter((_, i) => i !== index) } as Partial<SceneNode>,
- label
- )
- }
- }
-
- function toggleArrayVisibility(key: ArrayPropKey, index: number) {
- for (const n of targetNodes()) {
- const items = n[key] as Array<{ visible: boolean }>
- if (!items[index]) continue
- const arr = [...n[key]]
- arr[index] = { ...arr[index], visible: !items[index].visible }
- store.updateNodeWithUndo(
- n.id,
- { [key]: arr } as Partial<SceneNode>,
- `Toggle ${key} visibility`
- )
- }
- }
-
- return {
- store,
- node,
- nodes,
- isMulti,
- active,
- activeNode,
- targetNodes,
- prop,
- merged,
- updateAllWithUndo,
- updateArrayItem,
- removeArrayItem,
- toggleArrayVisibility,
- isArrayMixed
- }
-}
+export { useMultiProps, MIXED } from '@open-pencil/vue'
+export type { MixedValue } from '@open-pencil/vue'
diff --git a/src/composables/use-node-props.ts b/src/composables/use-node-props.ts
index dffdc4a55..361c0407b 100644
--- a/src/composables/use-node-props.ts
+++ b/src/composables/use-node-props.ts
@@ -1,55 +1 @@
-import { computed } from 'vue'
-
-import { useEditorStore } from '@/stores/editor'
-
-import type { SceneNode } from '@open-pencil/core'
-
-export function useNodeProps() {
- const store = useEditorStore()
- const node = computed(() => store.selectedNode.value ?? null)
- const nodes = computed(() => store.selectedNodes.value)
-
- function updateProp(key: string, value: number | string) {
- if (store.selectedNodes.value.length > 1) {
- storePreviousValues(key)
- for (const n of store.selectedNodes.value) {
- store.updateNode(n.id, { [key]: value })
- }
- } else {
- const node = store.selectedNode.value
- if (node) store.updateNode(node.id, { [key]: value })
- }
- }
-
- const previousValues = new Map<string, Record<string, number | string>>()
-
- function storePreviousValues(key: string) {
- for (const n of store.selectedNodes.value) {
- let rec = previousValues.get(n.id)
- if (!rec) {
- rec = {}
- previousValues.set(n.id, rec)
- }
- if (!(key in rec)) {
- rec[key] = n[key as keyof SceneNode] as number | string
- }
- }
- }
-
- function commitProp(key: string, _value: number | string, previous: number | string) {
- if (store.selectedNodes.value.length > 1) {
- for (const n of store.selectedNodes.value) {
- const prev = previousValues.get(n.id)?.[key] ?? previous
- store.commitNodeUpdate(n.id, { [key]: prev } as Partial<SceneNode>, `Change ${key}`)
- }
- previousValues.clear()
- } else {
- const node = store.selectedNode.value
- if (node) {
- store.commitNodeUpdate(node.id, { [key]: previous } as Partial<SceneNode>, `Change ${key}`)
- }
- }
- }
-
- return { store, node, nodes, updateProp, commitProp }
-}
+export { useNodeProps } from '@open-pencil/vue'
diff --git a/src/composables/use-text-edit.ts b/src/composables/use-text-edit.ts
index cb6802b01..63da07f2b 100644
--- a/src/composables/use-text-edit.ts
+++ b/src/composables/use-text-edit.ts
@@ -1,340 +1 @@
-import { useEventListener } from '@vueuse/core'
-import { shallowRef, watch, type Ref } from 'vue'
-
-import {
- adjustRunsForDelete,
- adjustRunsForInsert,
- toggleBoldInRange,
- toggleDecorationInRange,
- toggleItalicInRange
-} from '@open-pencil/core'
-
-import type { EditorStore } from '@/stores/editor'
-import type { SceneNode } from '@open-pencil/core'
-
-const CARET_BLINK_MS = 530
-
-export function useTextEdit(canvasRef: Ref<HTMLCanvasElement | null>, store: EditorStore) {
- const textareaRef = shallowRef<HTMLTextAreaElement | null>(null)
- let isComposing = false
- let blinkTimer = 0
-
- function getEditingNode() {
- const id = store.state.editingTextId
- if (!id) return null
- return store.graph.getNode(id) ?? null
- }
-
- function resetBlink() {
- if (store.textEditor) store.textEditor.caretVisible = true
- clearInterval(blinkTimer)
- blinkTimer = window.setInterval(() => {
- if (!store.textEditor) return
- store.textEditor.caretVisible = !store.textEditor.caretVisible
- store.requestRepaint()
- }, CARET_BLINK_MS)
- store.requestRepaint()
- }
-
- function syncText(nodeId: string, text: string, runs?: SceneNode['styleRuns']) {
- const changes: Partial<SceneNode> = { text }
- if (runs !== undefined) changes.styleRuns = runs
- store.graph.updateNode(nodeId, changes)
- store.requestRender()
- }
-
- function insertText(text: string, node: SceneNode) {
- const editor = store.textEditor
- if (!editor) return
- const range = editor.getSelectionRange()
- let runs = node.styleRuns
- if (range) {
- runs = adjustRunsForDelete(runs, range[0], range[1] - range[0])
- runs = adjustRunsForInsert(runs, range[0], text.length)
- } else {
- runs = adjustRunsForInsert(runs, editor.state?.cursor ?? 0, text.length)
- }
- editor.insert(text, node)
- syncText(node.id, editor.state?.text ?? '', runs)
- }
-
- function deleteText(node: SceneNode, forward: boolean) {
- const editor = store.textEditor
- if (!editor) return
- const range = editor.getSelectionRange()
- let runs = node.styleRuns
- if (range) {
- runs = adjustRunsForDelete(runs, range[0], range[1] - range[0])
- } else if (forward && editor.state && editor.state.cursor < node.text.length) {
- runs = adjustRunsForDelete(runs, editor.state.cursor, 1)
- } else if (!forward && editor.state && editor.state.cursor > 0) {
- runs = adjustRunsForDelete(runs, editor.state.cursor - 1, 1)
- }
- if (forward) {
- editor.delete(node)
- } else {
- editor.backspace(node)
- }
- syncText(node.id, editor.state?.text ?? '', runs)
- }
-
- function onCompositionStart() {
- isComposing = true
- }
-
- function onCompositionEnd(e: CompositionEvent) {
- isComposing = false
- if (!e.data) return
- const node = getEditingNode()
- if (!node) return
- insertText(e.data, node)
- if (textareaRef.value) textareaRef.value.value = ''
- resetBlink()
- }
-
- function onInput() {
- const el = textareaRef.value
- if (isComposing || !el) return
- const text = el.value
- if (!text) return
- el.value = ''
-
- const node = getEditingNode()
- if (!node) return
- insertText(text, node)
- resetBlink()
- }
-
- function handleHorizontalArrow(e: KeyboardEvent, editor: NonNullable<typeof store.textEditor>) {
- const select = e.shiftKey
- const isMeta = e.metaKey || e.ctrlKey
- if (e.key === 'ArrowLeft') {
- if (isMeta) editor.moveToLineStart(select)
- else if (e.altKey) editor.moveWordLeft(select)
- else editor.moveLeft(select)
- } else {
- if (isMeta) editor.moveToLineEnd(select)
- else if (e.altKey) editor.moveWordRight(select)
- else editor.moveRight(select)
- }
- }
-
- function handleDeletion(
- e: KeyboardEvent,
- editor: NonNullable<typeof store.textEditor>,
- node: SceneNode
- ) {
- const isMeta = e.metaKey || e.ctrlKey
- const forward = e.key === 'Delete'
- if (forward) {
- if (isMeta) editor.moveToLineEnd(true)
- else if (e.altKey) editor.moveWordRight(true)
- } else {
- if (isMeta) editor.moveToLineStart(true)
- else if (e.altKey) editor.moveWordLeft(true)
- }
- deleteText(node, forward)
- }
-
- type MetaAction = (node: SceneNode) => void
- const metaKeyActions: Partial<Record<string, MetaAction>> = {
- a: () => store.textEditor?.selectAll(),
- c: () => handleCopy(),
- x: (node) => handleCut(node),
- v: (node) => void handlePaste(node),
- b: (node) => toggleBold(node),
- i: (node) => toggleItalic(node),
- u: (node) => toggleUnderline(node)
- }
-
- function onKeyDown(e: KeyboardEvent) {
- if (isComposing) return
- const editor = store.textEditor
- const node = getEditingNode()
- if (!editor || !node) return
-
- const isMeta = e.metaKey || e.ctrlKey
- let textChanged = false
-
- switch (e.key) {
- case 'Escape':
- store.commitTextEdit()
- canvasRef.value?.focus()
- e.preventDefault()
- return
- case 'Enter':
- insertText('\n', node)
- textChanged = true
- break
- case 'Backspace':
- case 'Delete':
- handleDeletion(e, editor, node)
- textChanged = true
- break
- case 'ArrowLeft':
- case 'ArrowRight':
- handleHorizontalArrow(e, editor)
- break
- case 'ArrowUp':
- editor.moveUp(e.shiftKey)
- break
- case 'ArrowDown':
- editor.moveDown(e.shiftKey)
- break
- case 'Home':
- editor.moveToLineStart(e.shiftKey)
- break
- case 'End':
- editor.moveToLineEnd(e.shiftKey)
- break
- default: {
- if (!isMeta) return
- const action = metaKeyActions[e.key]
- if (!action) return
- action(node)
- e.preventDefault()
- return
- }
- }
-
- if (!textChanged) {
- store.requestRender()
- }
- resetBlink()
- e.preventDefault()
- }
-
- function applyFormatting(nodeId: string, changes: Partial<SceneNode>, label: string) {
- store.updateNodeWithUndo(nodeId, changes, label)
- const updated = store.graph.getNode(nodeId)
- if (updated) store.textEditor?.rebuildParagraph(updated)
- store.requestRender()
- }
-
- function toggleBold(node: SceneNode) {
- const editor = store.textEditor
- const range = editor?.getSelectionRange()
- if (range) {
- const { runs } = toggleBoldInRange(
- node.styleRuns,
- range[0],
- range[1],
- node.fontWeight,
- node.text.length
- )
- applyFormatting(node.id, { styleRuns: runs }, 'Toggle bold')
- } else {
- applyFormatting(node.id, { fontWeight: node.fontWeight >= 700 ? 400 : 700 }, 'Toggle bold')
- }
- }
-
- function toggleItalic(node: SceneNode) {
- const editor = store.textEditor
- const range = editor?.getSelectionRange()
- if (range) {
- const { runs } = toggleItalicInRange(
- node.styleRuns,
- range[0],
- range[1],
- node.italic,
- node.text.length
- )
- applyFormatting(node.id, { styleRuns: runs }, 'Toggle italic')
- } else {
- applyFormatting(node.id, { italic: !node.italic }, 'Toggle italic')
- }
- }
-
- function toggleUnderline(node: SceneNode) {
- const editor = store.textEditor
- const range = editor?.getSelectionRange()
- if (range) {
- const { runs } = toggleDecorationInRange(
- node.styleRuns,
- range[0],
- range[1],
- 'UNDERLINE',
- node.textDecoration,
- node.text.length
- )
- applyFormatting(node.id, { styleRuns: runs }, 'Toggle underline')
- } else {
- applyFormatting(
- node.id,
- { textDecoration: node.textDecoration === 'UNDERLINE' ? 'NONE' : 'UNDERLINE' },
- 'Toggle underline'
- )
- }
- }
-
- function handleCopy() {
- const editor = store.textEditor
- if (!editor) return
- const text = editor.getSelectedText()
- if (text) void navigator.clipboard.writeText(text)
- }
-
- function handleCut(node: ReturnType<typeof getEditingNode>) {
- const editor = store.textEditor
- if (!editor || !node) return
- const text = editor.getSelectedText()
- if (text) {
- void navigator.clipboard.writeText(text)
- deleteText(node, false)
- resetBlink()
- }
- }
-
- async function handlePaste(node: ReturnType<typeof getEditingNode>) {
- const editor = store.textEditor
- if (!editor || !node) return
- try {
- const text = await navigator.clipboard.readText()
- if (text) {
- insertText(text, node)
- resetBlink()
- }
- } catch (e) {
- console.warn('Clipboard access denied:', e)
- }
- }
-
- useEventListener(textareaRef, 'input', onInput)
- useEventListener(textareaRef, 'compositionstart', onCompositionStart)
- useEventListener(textareaRef, 'compositionend', onCompositionEnd)
- useEventListener(textareaRef, 'keydown', onKeyDown)
-
- useEventListener(canvasRef, 'mousedown', () => {
- if (store.state.editingTextId && textareaRef.value) {
- requestAnimationFrame(() => textareaRef.value?.focus())
- }
- })
-
- watch(
- () => store.state.editingTextId,
- (id, _, onCleanup) => {
- if (id) {
- const el = document.createElement('textarea')
- el.style.cssText =
- 'position:fixed;opacity:0;width:1px;height:1px;padding:0;border:0;top:50%;left:50%;overflow:hidden;resize:none;'
- el.autocomplete = 'off'
- el.setAttribute('autocorrect', 'off')
- el.setAttribute('autocapitalize', 'none')
- el.spellcheck = false
- el.tabIndex = -1
- el.setAttribute('aria-hidden', 'true')
- document.body.appendChild(el)
- textareaRef.value = el
- el.focus()
- resetBlink()
-
- onCleanup(() => {
- clearInterval(blinkTimer)
- el.remove()
- textareaRef.value = null
- isComposing = false
- })
- }
- }
- )
-}
+export { useTextEdit } from '@open-pencil/vue'
diff --git a/src/composables/use-toast.ts b/src/composables/use-toast.ts
index 5e6ca8aca..9124cdb4e 100644
--- a/src/composables/use-toast.ts
+++ b/src/composables/use-toast.ts
@@ -1,39 +1,2 @@
-import { useEventListener } from '@vueuse/core'
-import { ref } from 'vue'
-
-export type ToastVariant = 'default' | 'warning' | 'error'
-
-export interface Toast {
- id: number
- message: string
- variant: ToastVariant
-}
-
-const TOAST_DURATION = 3000
-
-const toasts = ref<Toast[]>([])
-let nextId = 0
-let errorHandlersInitialized = false
-
-function show(message: string, variant: ToastVariant = 'default') {
- toasts.value.push({ id: ++nextId, message, variant })
-}
-
-function remove(id: number) {
- toasts.value = toasts.value.filter((t) => t.id !== id)
-}
-
-function setupGlobalErrorHandler() {
- if (errorHandlersInitialized) return
- errorHandlersInitialized = true
-
- useEventListener(window, 'error', (e) => {
- show(e.message || 'An unexpected error occurred', 'error')
- })
- useEventListener(window, 'unhandledrejection', (e) => {
- const msg = e.reason instanceof Error ? e.reason.message : String(e.reason)
- show(msg || 'An unexpected error occurred', 'error')
- })
-}
-
-export const toast = { show, remove, toasts, setupGlobalErrorHandler, TOAST_DURATION }
+export { toast } from '@open-pencil/vue'
+export type { Toast, ToastVariant } from '@open-pencil/vue'
diff --git a/src/stores/editor.ts b/src/stores/editor.ts
index 256fb1c2a..00fba448e 100644
--- a/src/stores/editor.ts
+++ b/src/stores/editor.ts
@@ -492,101 +492,6 @@ export function createEditorStore() {
downloadBlob(data, fileName, mime)
}
- // ─── Images ───────────────────────────────────────────────────
-
- const IMAGE_MAX_DIMENSION = 4096
- const IMAGE_GAP = 20
-
- function decodeImageDimensions(bytes: Uint8Array): { w: number; h: number } | null {
- const renderer = editor.renderer
- if (!renderer) return null
- const skImg = renderer.ck.MakeImageFromEncoded(bytes)
- if (!skImg) return null
- let w = skImg.width()
- let h = skImg.height()
- skImg.delete()
- if (w > IMAGE_MAX_DIMENSION || h > IMAGE_MAX_DIMENSION) {
- const ratio = Math.min(IMAGE_MAX_DIMENSION / w, IMAGE_MAX_DIMENSION / h)
- w = Math.round(w * ratio)
- h = Math.round(h * ratio)
- }
- return { w, h }
- }
-
- async function placeImageFiles(files: File[], cx: number, cy: number) {
- const prepared: Array<{ bytes: Uint8Array; name: string; w: number; h: number }> = []
- for (const file of files) {
- const bytes = new Uint8Array(await file.arrayBuffer())
- const dims = decodeImageDimensions(bytes)
- if (dims) prepared.push({ bytes, name: file.name, ...dims })
- }
- if (!prepared.length) return
-
- let totalW = 0
- for (const p of prepared) totalW += p.w
- totalW += IMAGE_GAP * (prepared.length - 1)
- const maxH = Math.max(...prepared.map((p) => p.h))
-
- let curX = cx - totalW / 2
- const topY = cy - maxH / 2
- const ids: string[] = []
- for (const p of prepared) {
- const id = placeImageNode(p.bytes, curX, topY, p.w, p.h, p.name)
- if (id) ids.push(id)
- curX += p.w + IMAGE_GAP
- }
- if (ids.length) {
- editor.select(ids)
- editor.requestRender()
- }
- }
-
- function placeImageNode(
- bytes: Uint8Array,
- x: number,
- y: number,
- w: number,
- h: number,
- name = 'Image'
- ): string | null {
- const hash = editor.storeImage(bytes)
- const displayName = name.replace(/\.[^.]+$/, '')
- const pid = state.currentPageId
- const fill = {
- type: 'IMAGE' as const,
- imageHash: hash,
- imageScaleMode: 'FILL' as const,
- color: { r: 0, g: 0, b: 0, a: 0 },
- opacity: 1,
- visible: true
- }
- const node = editor.graph.createNode('RECTANGLE', pid, {
- name: displayName,
- x,
- y,
- width: w,
- height: h,
- fills: [fill]
- })
- const id = node.id
- const snapshot = { ...node }
- editor.undo.push({
- label: 'Place image',
- forward: () => {
- editor.graph.images.set(hash, bytes)
- editor.graph.createNode(snapshot.type, pid, snapshot)
- },
- inverse: () => {
- editor.graph.deleteNode(id)
- editor.graph.images.delete(hash)
- const next = new Set(state.selectedIds)
- next.delete(id)
- state.selectedIds = next
- }
- })
- return id
- }
-
// ─── Mobile clipboard ─────────────────────────────────────────
function mobileCopy() {
@@ -703,6 +608,7 @@ export function createEditorStore() {
zoomTo100: editor.zoomTo100,
zoomToSelection: editor.zoomToSelection,
storeImage: editor.storeImage,
+ placeImageFiles: editor.placeImageFiles,
// App-specific methods
flashNodes,
@@ -716,7 +622,6 @@ export function createEditorStore() {
saveFigFileAs,
renderExportImage,
exportSelection,
- placeImageFiles,
mobileCopy,
mobileCut,
mobilePaste