AI-native design editor. Open-source Figma alternative.
refactor(app): centralize canvas menu actions
Danila Poyarkovf37a26327990
diff --git a/src/app/editor/canvas/context-menu.ts b/src/app/editor/canvas/context-menu.tsindex ce9978adf..c67c7281f 100644--- a/src/app/editor/canvas/context-menu.ts+++ b/src/app/editor/canvas/context-menu.ts@@ -2,8 +2,12 @@ import { computed, type Ref } from 'vue'import { formatShortcut, type Editor, type MenuEntry } from '@open-pencil/vue'-import { COPY_AS_PNG_SHORTCUT } from '@/app/editor/canvas/menu-actions'-import type { createCanvasMenuActions } from '@/app/editor/canvas/menu-actions'+import type { createCanvasMenuActions } from '@/app/editor/canvas/menu/actions'+import {+ CANVAS_COPY_AS_ACTIONS,+ CANVAS_COPY_AS_GROUP_TEST_ID,+ type CanvasContextActionId+} from '@/app/editor/canvas/menu/registry'const STATIC_SELECTION_COMMAND_IDS = new Set(['selection.duplicate', 'selection.delete'])@@ -32,6 +36,31 @@ function runAsync(action: () => Promise<void>) {}}+function copyAction(+ id: CanvasContextActionId,+ editor: Editor,+ actions: CanvasMenuActions+): () => void {+ switch (id) {+ case 'copy-as-text':+ return runAsync(() => actions.clipboardWrite(editor.copySelectionAsText(actions.ids()), 'text'))+ case 'copy-as-svg':+ return runAsync(() => actions.clipboardWrite(editor.copySelectionAsSVG(actions.ids()), 'SVG'))+ case 'copy-as-png':+ return runAsync(actions.copyAsPNG)+ case 'copy-as-jsx':+ return runAsync(() => actions.clipboardWrite(editor.copySelectionAsJSX(actions.ids()), 'JSX'))+ case 'copy-node-id':+ return runAsync(actions.copyNodeId)+ case 'copy-xpath':+ return runAsync(actions.copyXPath)+ default: {+ const exhaustive: never = id+ return exhaustive+ }+ }+}+function copyPasteAsEntry(editor: Editor,actions: CanvasMenuActions,@@ -39,30 +68,13 @@ function copyPasteAsEntry(): MenuEntry {return {label: labels.copyPasteAs,- testId: 'context-copy-paste-as',- sub: [- {- label: labels.copyAsText,- action: runAsync(() => actions.clipboardWrite(editor.copySelectionAsText(actions.ids()), 'text'))- },- {- label: labels.copyAsSVG,- testId: 'context-copy-as-svg',- action: runAsync(() => actions.clipboardWrite(editor.copySelectionAsSVG(actions.ids()), 'SVG'))- },- {- label: labels.copyAsPNG,- shortcut: formatShortcut(COPY_AS_PNG_SHORTCUT),- action: runAsync(actions.copyAsPNG)- },- {- label: labels.copyAsJSX,- testId: 'context-copy-as-jsx',- action: runAsync(() => actions.clipboardWrite(editor.copySelectionAsJSX(actions.ids()), 'JSX'))- },- { label: labels.copyNodeId, action: runAsync(actions.copyNodeId) },- { label: labels.copyXPath, action: runAsync(actions.copyXPath) }- ]+ testId: CANVAS_COPY_AS_GROUP_TEST_ID,+ sub: CANVAS_COPY_AS_ACTIONS.map((meta) => ({+ label: labels[meta.labelKey],+ testId: meta.testId,+ shortcut: formatShortcut(meta.shortcut),+ action: copyAction(meta.id, editor, actions)+ }))}}diff --git a/src/app/editor/canvas/menu/actions.ts b/src/app/editor/canvas/menu/actions.tsnew file mode 100644index 000000000..e65789801--- /dev/null+++ b/src/app/editor/canvas/menu/actions.ts@@ -0,0 +1,69 @@+import { useClipboard } from '@vueuse/core'+import type { Ref } from 'vue'++import { nodeToXPath } from '@open-pencil/core/xpath'++import type { EditorStore } from '@/app/editor/active-store'+import { toast } from '@/app/shell/ui'++function toArrayBuffer(data: Uint8Array): ArrayBuffer {+ const bytes = new Uint8Array(data.length)+ bytes.set(data)+ return bytes.buffer+}++export function createCanvasMenuActions(store: EditorStore, selectedIds: Ref<Set<string>>) {+ const { copy } = useClipboard()++ function ids() {+ return [...selectedIds.value]+ }++ function execCommand(cmd: 'copy' | 'cut' | 'paste') {+ try {+ if (window.document.execCommand(cmd)) return+ } catch (error) {+ console.warn(`Clipboard command ${cmd} failed`, error)+ }++ toast.error('Clipboard access is blocked in this browser context')+ }++ async function clipboardWrite(text: string | null, label: string) {+ if (!text) return+ await copy(text)+ toast.info(`Copied as ${label}`)+ }++ async function copyNodeId() {+ const nodeIds = ids()+ if (nodeIds.length === 0) return+ await copy(nodeIds.join(', '))+ toast.info(`Copied node ID${nodeIds.length > 1 ? 's' : ''}`)+ }++ async function copyXPath() {+ const nodeIds = ids()+ if (nodeIds.length === 0) return+ const xpaths = nodeIds+ .map((id) => nodeToXPath(store.graph, id))+ .filter((xpath): xpath is string => xpath !== null)+ if (xpaths.length === 0) return+ await copy(xpaths.join('\n'))+ toast.info(`Copied XPath${xpaths.length > 1 ? 's' : ''}`)+ }++ async function copyAsPNG() {+ if (typeof ClipboardItem === 'undefined') {+ toast.error('PNG clipboard export is not available in this browser')+ return+ }+ const data = await store.renderExportImage(ids(), 2, 'PNG')+ if (!data) return+ const blob = new Blob([toArrayBuffer(data)], { type: 'image/png' })+ await navigator.clipboard.write([new ClipboardItem({ 'image/png': blob })])+ toast.info('Copied as PNG')+ }++ return { ids, execCommand, clipboardWrite, copyNodeId, copyXPath, copyAsPNG }+}diff --git a/src/app/editor/canvas/menu/model.ts b/src/app/editor/canvas/menu/model.tsnew file mode 100644index 000000000..d409343ad--- /dev/null+++ b/src/app/editor/canvas/menu/model.ts@@ -0,0 +1,14 @@+const COMPONENT_MENU_LABEL_PARTS = ['component', 'instance']++export function isComponentMenuItem(label: string) {+ const normalized = label.toLowerCase()+ return COMPONENT_MENU_LABEL_PARTS.some((part) => normalized.includes(part))+}++export function canvasMenuItemClass(label: string, classes: { item: string; component: string }) {+ return isComponentMenuItem(label) ? classes.component : classes.item+}++export function canvasMenuShortcutClass(label: string) {+ return isComponentMenuItem(label) ? 'text-component/60' : 'text-muted'+}diff --git a/src/app/editor/canvas/menu/registry.ts b/src/app/editor/canvas/menu/registry.tsnew file mode 100644index 000000000..9c0424f38--- /dev/null+++ b/src/app/editor/canvas/menu/registry.ts@@ -0,0 +1,36 @@+import type { TestId } from '@open-pencil/vue'++export type CanvasContextActionId =+ | 'copy-as-text'+ | 'copy-as-svg'+ | 'copy-as-png'+ | 'copy-as-jsx'+ | 'copy-node-id'+ | 'copy-xpath'++export type CanvasContextLabelKey =+ | 'copyAsText'+ | 'copyAsSVG'+ | 'copyAsPNG'+ | 'copyAsJSX'+ | 'copyNodeId'+ | 'copyXPath'++export type CanvasContextActionMeta = {+ id: CanvasContextActionId+ labelKey: CanvasContextLabelKey+ shortcut?: string+ testId?: TestId+}++export const CANVAS_COPY_AS_GROUP_TEST_ID = 'context-copy-paste-as' satisfies TestId+export const COPY_AS_PNG_SHORTCUT = 'MOD+SHIFT+C'++export const CANVAS_COPY_AS_ACTIONS = [+ { id: 'copy-as-text', labelKey: 'copyAsText' },+ { id: 'copy-as-svg', labelKey: 'copyAsSVG', testId: 'context-copy-as-svg' },+ { id: 'copy-as-png', labelKey: 'copyAsPNG', shortcut: COPY_AS_PNG_SHORTCUT },+ { id: 'copy-as-jsx', labelKey: 'copyAsJSX', testId: 'context-copy-as-jsx' },+ { id: 'copy-node-id', labelKey: 'copyNodeId' },+ { id: 'copy-xpath', labelKey: 'copyXPath' }+] satisfies CanvasContextActionMeta[]diff --git a/src/components/CanvasMenu.vue b/src/components/CanvasMenu.vueindex d3e64c34d..98a6cc4d1 100644--- a/src/components/CanvasMenu.vue+++ b/src/components/CanvasMenu.vue@@ -21,9 +21,9 @@ import type { EditorCommandId } from '@open-pencil/vue'import { useEditorStore } from '@/app/editor/active-store'import { appMenuShortcutLabel } from '@/app/shell/menu/shortcut'-import { createCanvasMenuActions } from '@/app/editor/canvas/menu-actions'+import { createCanvasMenuActions } from '@/app/editor/canvas/menu/actions'import { useCanvasContextMenu } from '@/app/editor/canvas/context-menu'-import { canvasMenuItemClass, canvasMenuShortcutClass } from '@/app/editor/canvas/menu-model'+import { canvasMenuItemClass, canvasMenuShortcutClass } from '@/app/editor/canvas/menu/model'import { menu, useMenuUI } from '@/components/ui/menu'const store = useEditorStore()