AI-native design editor. Open-source Figma alternative.
refactor(vue): dispatch canvas presses and hovers through a tool registry (#1621)
Danila Poyarkov320e4b43eadd
diff --git a/packages/vue/AGENTS.md b/packages/vue/AGENTS.mdindex 8116ce8f9..96e4a3b65 100644--- a/packages/vue/AGENTS.md+++ b/packages/vue/AGENTS.md@@ -15,6 +15,10 @@ Headless Vue 3 primitives and composables; the root app is one consumer. PrimitiThe app's `DesignPanel` retains at most one selection-property subtree through `RetainedPanel`. The app installs `createRetainedScopePlugin()`; opted-in descendants receive `provideRetainedActivity()`. Vue component scopes are detached, so pausing only a parent or bare `KeepAlive` does not suspend descendant work. Cancel drafts synchronously before DOM detachment can fire blur, close transient state and gate portals with `useRetainedPopup()`, and invalidate pending async results on deactivation or disposal. The plugin lets cleanup flush before pausing each component scope and resumes it on activation; it requires Vue's Options API. Unmounting the owning editor releases the retained subtree.+## Canvas tools++- What a tool does on the canvas lives in its entry in `CANVAS_TOOLS`: a press, and a hover while no drag is held. A new tool adds an entry, not a branch in `useCanvasInput`, and the editor profile's `tools` decides which are on offer; the `Record<Tool, CanvasTool>` type makes a missing entry a type error (`packages/vue/src/canvas/tools/registry.ts`).+## Preview islands- Preview runs controls as real components, never as canvas simulations: `packages/vue/src/canvas/islands/` renders each top-level layer that holds an instance with a behaviour as DOM in a shadow root over the canvas, projected through `@open-pencil/dom-css`, and mounts each behaviour's Reka UI primitives on its layers. Typing, focus, and keyboard handling belong to the browser and Reka.diff --git a/packages/vue/src/canvas/tools/input.ts b/packages/vue/src/canvas/tools/input.tsindex dba1a5be7..cb168c3ae 100644--- a/packages/vue/src/canvas/tools/input.ts+++ b/packages/vue/src/canvas/tools/input.ts@@ -1,98 +1,13 @@-import type { Ref } from 'vue'+import { CANVAS_TOOLS, type CanvasToolContext, type CanvasToolPointer } from './registry'-import type { Editor } from '@open-pencil/core'--import { startPenInput } from '#vue/canvas/pen/input'-import { startPencilInput } from '#vue/canvas/pencil/input'-import { HAND_GRAB_CURSOR } from '#vue/editor/tool-cursor'-import { attachModifiers, startConnectorDraw } from '#vue/shared/input/connector/draw'-import { startShapeDraw, startTextDraw } from '#vue/shared/input/draw'-import { startPanDrag } from '#vue/shared/input/pan/drag'-import { handleSelectDown } from '#vue/shared/input/select'-import type { HitTestFns } from '#vue/shared/input/select'-import { startStickyDraw } from '#vue/shared/input/sticky/draw'-import type { DragState } from '#vue/shared/input/types'--type ToolMouseDownOptions = {- event: MouseEvent- cx: number- cy: number- sx: number- sy: number- editor: Editor- hitFns: HitTestFns- cursorOverride: Ref<string | null>- setDrag: (d: DragState) => void- tryStartRotation: (cx: number, cy: number) => boolean- handleTextEditClick: (cx: number, cy: number, shiftKey: boolean) => boolean+/** Starts what a press does: the middle button pans with any tool, others go to the active tool. */+export function pressCanvasTool(pointer: CanvasToolPointer, context: CanvasToolContext) {+ const tool =+ pointer.event.button === 1 ? CANVAS_TOOLS.HAND : CANVAS_TOOLS[context.editor.state.activeTool]+ tool.press(pointer, context)}-export { startPanDrag }--export function handleToolMouseDown({- event,- cx,- cy,- sx,- sy,- editor,- hitFns,- cursorOverride,- setDrag,- tryStartRotation,- handleTextEditClick-}: ToolMouseDownOptions) {- const tool = editor.state.activeTool-- if (event.button === 1 || tool === 'HAND') {- startPanDrag(event, setDrag, editor)- cursorOverride.value = HAND_GRAB_CURSOR- return- }-- // Comments are placed by the app's comments layer above the canvas.- if (tool === 'COMMENT') return-- if (tool === 'SELECT') {- handleSelectDown(- event,- cx,- cy,- sx,- sy,- editor,- hitFns,- tryStartRotation,- handleTextEditClick,- setDrag- )- return- }-- if (tool === 'PEN') {- startPenInput(event, cx, cy, editor, setDrag, cursorOverride)- return- }-- if (tool === 'PENCIL') {- startPencilInput(cx, cy, editor, setDrag)- return- }-- if (tool === 'TEXT') {- startTextDraw(cx, cy, editor, setDrag)- return- }-- if (tool === 'CONNECTOR') {- startConnectorDraw(cx, cy, sx, sy, editor, setDrag, attachModifiers(event))- return- }-- if (tool === 'STICKY') {- startStickyDraw(cx, cy, editor, setDrag)- return- }-- startShapeDraw(cx, cy, editor, setDrag)+/** Lets the active tool show what the pointer is over while no drag is held. */+export function hoverCanvasTool(pointer: CanvasToolPointer, context: CanvasToolContext) {+ CANVAS_TOOLS[context.editor.state.activeTool].hover?.(pointer, context)}diff --git a/packages/vue/src/canvas/tools/registry.ts b/packages/vue/src/canvas/tools/registry.tsnew file mode 100644index 000000000..dcaa6dff6--- /dev/null+++ b/packages/vue/src/canvas/tools/registry.ts@@ -0,0 +1,102 @@+import type { Ref } from 'vue'++import type { Editor, Tool } from '@open-pencil/core'++import { startPenInput, updatePenHover } from '#vue/canvas/pen/input'+import { startPencilInput } from '#vue/canvas/pencil/input'+import { HAND_GRAB_CURSOR } from '#vue/editor/tool-cursor'+import { attachModifiers, startConnectorDraw } from '#vue/shared/input/connector/draw'+import { startShapeDraw, startTextDraw } from '#vue/shared/input/draw'+import { startPanDrag } from '#vue/shared/input/pan/drag'+import { handleSelectDown } from '#vue/shared/input/select'+import type { HitTestFns } from '#vue/shared/input/select'+import { startStickyDraw } from '#vue/shared/input/sticky/draw'+import type { DragState } from '#vue/shared/input/types'++/** Where the pointer is, in canvas (`cx`, `cy`) and screen (`sx`, `sy`) coordinates. */+export interface CanvasToolPointer {+ event: MouseEvent+ cx: number+ cy: number+ sx: number+ sy: number+}++/** What a canvas tool works with: the editor and the canvas input's own pieces. */+export interface CanvasToolContext {+ editor: Editor+ hitFns: HitTestFns+ cursorOverride: Ref<string | null>+ setDrag: (d: DragState) => void+ tryStartRotation: (cx: number, cy: number) => boolean+ handleTextEditClick: (cx: number, cy: number, shiftKey: boolean) => boolean+ /** Shows what the pointer is over while selecting: the cursor, guides, and auto layout. */+ hoverLayers: (pointer: CanvasToolPointer) => void+}++/** What a tool does on the canvas: on a press, and while the pointer moves with no drag. */+export interface CanvasTool {+ press: (pointer: CanvasToolPointer, context: CanvasToolContext) => void+ hover?: (pointer: CanvasToolPointer, context: CanvasToolContext) => void+}++const pan: CanvasTool = {+ press: ({ event }, { editor, setDrag, cursorOverride }) => {+ startPanDrag(event, setDrag, editor)+ cursorOverride.value = HAND_GRAB_CURSOR+ }+}++/** Draws the layer of the tool's own type, from the press to the release. */+const shape: CanvasTool = {+ press: ({ cx, cy }, { editor, setDrag }) => startShapeDraw(cx, cy, editor, setDrag)+}++/**+ * Every tool's canvas behaviour, by id. A tool added to `Tool` needs an entry here, and the+ * editor's profile decides which tools are on offer.+ */+export const CANVAS_TOOLS: Readonly<Record<Tool, CanvasTool>> = {+ SELECT: {+ press: ({ event, cx, cy, sx, sy }, context) =>+ handleSelectDown(+ event,+ cx,+ cy,+ sx,+ sy,+ context.editor,+ context.hitFns,+ context.tryStartRotation,+ context.handleTextEditClick,+ context.setDrag+ ),+ hover: (pointer, { hoverLayers }) => hoverLayers(pointer)+ },+ HAND: pan,+ // Comments are placed by the app's comments layer above the canvas.+ COMMENT: { press: () => undefined },+ PEN: {+ press: ({ event, cx, cy }, { editor, setDrag, cursorOverride }) =>+ startPenInput(event, cx, cy, editor, setDrag, cursorOverride),+ hover: ({ cx, cy }, { editor }) => {+ updatePenHover(cx, cy, editor)+ }+ },+ PENCIL: { press: ({ cx, cy }, { editor, setDrag }) => startPencilInput(cx, cy, editor, setDrag) },+ TEXT: { press: ({ cx, cy }, { editor, setDrag }) => startTextDraw(cx, cy, editor, setDrag) },+ CONNECTOR: {+ press: ({ event, cx, cy, sx, sy }, { editor, setDrag }) =>+ startConnectorDraw(cx, cy, sx, sy, editor, setDrag, attachModifiers(event)),+ hover: ({ event, cx, cy }, { editor }) => editor.connectorHover(cx, cy, attachModifiers(event))+ },+ STICKY: { press: ({ cx, cy }, { editor, setDrag }) => startStickyDraw(cx, cy, editor, setDrag) },+ FRAME: shape,+ SECTION: shape,+ RECTANGLE: shape,+ ELLIPSE: shape,+ LINE: shape,+ POLYGON: shape,+ STAR: shape,+ SHAPE_WITH_TEXT: shape+}diff --git a/packages/vue/src/canvas/useCanvasInput.ts b/packages/vue/src/canvas/useCanvasInput.tsindex 4a1e81f91..64d26f78a 100644--- a/packages/vue/src/canvas/useCanvasInput.ts+++ b/packages/vue/src/canvas/useCanvasInput.ts@@ -7,11 +7,12 @@ import type { SceneNode } from '@open-pencil/scene-graph'import { createAutoLayoutValueEdit } from '#vue/canvas/auto-layout/value-edit'import { createGuideInput, selectedTopLevelGuideFrameId } from '#vue/canvas/guides/input'import { createCanvasLabelEdit } from '#vue/canvas/labels/edit'-import { handlePenDragMove, updatePenHover } from '#vue/canvas/pen/input'+import { handlePenDragMove } from '#vue/canvas/pen/input'import { finishPencilDrag, handlePencilDragMove } from '#vue/canvas/pencil/input'import { createCanvasPointer } from '#vue/canvas/pointer/use'import { createTextEditInput } from '#vue/canvas/text-edit/input'-import { handleToolMouseDown } from '#vue/canvas/tools/input'+import { hoverCanvasTool, pressCanvasTool } from '#vue/canvas/tools/input'+import type { CanvasToolContext, CanvasToolPointer } from '#vue/canvas/tools/registry'import { createCanvasTransformInput } from '#vue/canvas/transform/input'import { finishMarquee } from '#vue/canvas/transform/marquee'import {@@ -204,6 +205,30 @@ export function useCanvasInput(onTextDblClick(e)}+ function hoverLayers({ event, cx, cy, sx, sy }: CanvasToolPointer) {+ const guideCursor = profile().edits ? guideInput.updateHover(sx, sy) : null+ cursorOverride.value =+ guideCursor ??+ updateHoverCursor(+ cx,+ cy,+ editor,+ hitFns,+ editor.state.measurementMode === 'deep' || hoverDeep(event)+ )+ editor.setAutoLayoutHover(autoLayoutHoverAt(cx, cy, editor.state.measurementMode))+ }++ const toolContext: CanvasToolContext = {+ editor,+ hitFns,+ cursorOverride,+ setDrag,+ tryStartRotation,+ handleTextEditClick,+ hoverLayers+ }+function onMouseDown(e: MouseEvent) {onActivate?.()if (!isEnabled()) return@@ -237,19 +262,7 @@ export function useCanvasInput(const selectedIdsBeforeMouseDown = new Set(editor.state.selectedIds)const clickCount = recordClick(sx, sy)if (clickCount === 1) selectedIdsBeforeClickSequence.value = selectedIdsBeforeMouseDown- handleToolMouseDown({- event: e,- cx,- cy,- sx,- sy,- editor,- hitFns,- cursorOverride,- setDrag,- tryStartRotation,- handleTextEditClick- })+ pressCanvasTool({ event: e, cx, cy, sx, sy }, toolContext)}// Dispatching the full drag union is intentionally centralized here.@@ -265,34 +278,11 @@ export function useCanvasInput(if (profile().pointer === 'pan' && !drag.value) return- if (!drag.value) {- const { cx, cy } = coords- updatePenHover(cx, cy, editor)- }-if (!drag.value) {const { cx, cy, sx, sy } = coordsupdateNodeEditHover(editor, cx, cy)editor.setShapeHandleHover(shapeHandleHoverAt(sx, sy, e.altKey))- }-- if (!drag.value && editor.state.activeTool === 'CONNECTOR') {- editor.connectorHover(coords.cx, coords.cy, attachModifiers(e))- }-- if (!drag.value && editor.state.activeTool === 'SELECT') {- const { sx, sy, cx, cy } = coords- const guideCursor = profile().edits ? guideInput.updateHover(sx, sy) : null- cursorOverride.value =- guideCursor ??- updateHoverCursor(- cx,- cy,- editor,- hitFns,- editor.state.measurementMode === 'deep' || hoverDeep(e)- )- editor.setAutoLayoutHover(autoLayoutHoverAt(cx, cy, editor.state.measurementMode))+ hoverCanvasTool({ event: e, ...coords }, toolContext)}if (!drag.value) return