|
8 | 8 | TooltipTrigger, |
9 | 9 | } from '@/components/ui/tooltip'; |
10 | 10 | import { Compartment, EditorState, StateEffect, StateField, RangeSetBuilder } from '@codemirror/state'; |
11 | | -import { EditorView, keymap, lineNumbers, highlightActiveLine, Decoration, type DecorationSet } from '@codemirror/view'; |
| 11 | +import { EditorView, keymap, lineNumbers, highlightActiveLine, Decoration, type DecorationSet, ViewPlugin } from '@codemirror/view'; |
12 | 12 | import { defaultKeymap, history, historyKeymap, indentWithTab, undo, redo } from '@codemirror/commands'; |
13 | 13 | import { syntaxHighlighting, HighlightStyle, bracketMatching } from '@codemirror/language'; |
14 | 14 | import { search, highlightSelectionMatches } from '@codemirror/search'; |
@@ -94,6 +94,30 @@ const errorBaseTheme = EditorView.baseTheme({ |
94 | 94 | }, |
95 | 95 | }); |
96 | 96 |
|
| 97 | +const pastePlugin = ViewPlugin.define((view) => { |
| 98 | + const handlePaste = (event: ClipboardEvent) => { |
| 99 | + const text = event.clipboardData?.getData('text/plain'); |
| 100 | + if (text) { |
| 101 | + const { state } = view; |
| 102 | + const { from, to } = state.selection.main; |
| 103 | + view.dispatch({ |
| 104 | + changes: { from, to, insert: text }, |
| 105 | + selection: { anchor: from + text.length }, |
| 106 | + }); |
| 107 | + event.preventDefault(); |
| 108 | + } |
| 109 | + }; |
| 110 | + |
| 111 | + const dom = view.dom; |
| 112 | + dom.addEventListener('paste', handlePaste, { passive: false }); |
| 113 | + |
| 114 | + return { |
| 115 | + destroy() { |
| 116 | + dom.removeEventListener('paste', handlePaste); |
| 117 | + }, |
| 118 | + }; |
| 119 | +}); |
| 120 | + |
97 | 121 |
|
98 | 122 | const EDITOR_COLORS: Record<DaxEditorTheme, Record<'light' | 'dark', EditorColors>> = { |
99 | 123 | default: { |
@@ -353,6 +377,7 @@ export const DaxEditor = forwardRef<DaxEditorHandle, DaxEditorProps>(function Da |
353 | 377 | colorPickerExtension(swatchCallbackRef), |
354 | 378 | errorField, |
355 | 379 | errorBaseTheme, |
| 380 | + pastePlugin, |
356 | 381 | highlightCompartmentRef.current.of(syntaxHighlighting(buildHighlight(daxEditorTheme, theme))), |
357 | 382 | themeCompartmentRef.current.of(buildTheme(daxEditorTheme, theme)), |
358 | 383 | fontCompartmentRef.current.of(buildFontSizeTheme(fontSize)), |
|
0 commit comments