AI-native design editor. Open-source Figma alternative.
Fix clipboard: use native copy/paste events instead of async Clipboard API
Danila Poyarkovb1808acac8c7
diff --git a/src/composables/use-keyboard.ts b/src/composables/use-keyboard.tsindex d77ea4c21..e81966b52 100644--- a/src/composables/use-keyboard.ts+++ b/src/composables/use-keyboard.ts@@ -15,9 +15,33 @@ function openFileDialog(store: EditorStore) {input.click()}+function isEditing(e: Event) {+ return e.target instanceof HTMLInputElement || e.target instanceof HTMLTextAreaElement+}+export function useKeyboard(store: EditorStore) {+ useEventListener(window, 'copy', (e: ClipboardEvent) => {+ if (isEditing(e)) return+ e.preventDefault()+ store.writeCopyData(e.clipboardData!)+ })++ useEventListener(window, 'cut', (e: ClipboardEvent) => {+ if (isEditing(e)) return+ e.preventDefault()+ store.writeCopyData(e.clipboardData!)+ store.deleteSelected()+ })++ useEventListener(window, 'paste', (e: ClipboardEvent) => {+ if (isEditing(e)) return+ e.preventDefault()+ const html = e.clipboardData?.getData('text/html') ?? ''+ if (html) store.pasteFromHTML(html)+ })+useEventListener(window, 'keydown', (e: KeyboardEvent) => {- if (e.target instanceof HTMLInputElement || e.target instanceof HTMLTextAreaElement) return+ if (isEditing(e)) returnconst tool = TOOL_SHORTCUTS[e.key.toLowerCase()]if (tool) {@@ -44,15 +68,6 @@ export function useKeyboard(store: EditorStore) {} else if (e.key === 'o') {e.preventDefault()openFileDialog(store)- } else if (e.key === 'c') {- e.preventDefault()- store.copySelected()- } else if (e.key === 'x') {- e.preventDefault()- store.cutSelected()- } else if (e.key === 'v') {- e.preventDefault()- store.pasteFromClipboard()}}diff --git a/src/stores/editor.ts b/src/stores/editor.tsindex 58412069b..44c255a0f 100644--- a/src/stores/editor.ts+++ b/src/stores/editor.ts@@ -229,53 +229,33 @@ export function createEditorStore() {}}- async function copySelected() {+ function writeCopyData(clipboardData: DataTransfer) {const nodes = selectedNodes.valueif (nodes.length === 0) returnconst html = buildFigmaClipboardHTML(nodes, graph)const names = nodes.map((n) => n.name).join('\n')- const clipboardItem = new ClipboardItem({- 'text/html': new Blob([html], { type: 'text/html' }),- 'text/plain': new Blob([names], { type: 'text/plain' })- })- await navigator.clipboard.write([clipboardItem])- }-- async function cutSelected() {- await copySelected()- deleteSelected()+ clipboardData.setData('text/html', html)+ clipboardData.setData('text/plain', names)}- async function pasteFromClipboard() {- const items = await navigator.clipboard.read()- let html = ''- for (const item of items) {- if (item.types.includes('text/html')) {- const blob = await item.getType('text/html')- html = await blob.text()- break- }- }- if (!html) return-- // Try our own format first+ function pasteFromHTML(html: string) {const ownNodes = parseOpenPencilClipboard(html)if (ownNodes) {pasteOpenPencilNodes(ownNodes)return}- // Try Figma format- const figma = await parseFigmaClipboard(html)- if (figma) {- const created = importClipboardNodes(figma.nodes, graph, graph.rootId, 20, 20)- if (created.length > 0) {- state.selectedIds = new Set(created)- requestRender()+ parseFigmaClipboard(html).then((figma) => {+ if (figma) {+ const created = importClipboardNodes(figma.nodes, graph, graph.rootId, 20, 20)+ if (created.length > 0) {+ state.selectedIds = new Set(created)+ requestRender()+ }}- }+ })}function pasteOpenPencilNodes(@@ -432,9 +412,8 @@ export function createEditorStore() {updateNode,createShape,duplicateSelected,- copySelected,- cutSelected,- pasteFromClipboard,+ writeCopyData,+ pasteFromHTML,deleteSelected,commitMove,undoAction,