Skip to content

Commit 8a8aabf

Browse files
committed
fix: corrigir comportamento de paste no editor DAX
Adicionado um plugin ViewPlugin que intercepta eventos de paste e insere o texto do clipboard no editor CodeMirror. O handler de paste estava faltando, causando que o paste não funcionasse corretamente. O plugin: - Detecta quando o usuário tenta colar texto - Extrai o texto do clipboard - Insere no local da seleção atual - Previne o comportamento padrão para evitar duplicação https://claude.ai/code/session_011VUBgH8QA48BDse37N9SDd
1 parent 20f7a29 commit 8a8aabf

1 file changed

Lines changed: 26 additions & 1 deletion

File tree

src/components/DaxEditor.tsx

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
TooltipTrigger,
99
} from '@/components/ui/tooltip';
1010
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';
1212
import { defaultKeymap, history, historyKeymap, indentWithTab, undo, redo } from '@codemirror/commands';
1313
import { syntaxHighlighting, HighlightStyle, bracketMatching } from '@codemirror/language';
1414
import { search, highlightSelectionMatches } from '@codemirror/search';
@@ -94,6 +94,30 @@ const errorBaseTheme = EditorView.baseTheme({
9494
},
9595
});
9696

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+
97121

98122
const EDITOR_COLORS: Record<DaxEditorTheme, Record<'light' | 'dark', EditorColors>> = {
99123
default: {
@@ -353,6 +377,7 @@ export const DaxEditor = forwardRef<DaxEditorHandle, DaxEditorProps>(function Da
353377
colorPickerExtension(swatchCallbackRef),
354378
errorField,
355379
errorBaseTheme,
380+
pastePlugin,
356381
highlightCompartmentRef.current.of(syntaxHighlighting(buildHighlight(daxEditorTheme, theme))),
357382
themeCompartmentRef.current.of(buildTheme(daxEditorTheme, theme)),
358383
fontCompartmentRef.current.of(buildFontSizeTheme(fontSize)),

0 commit comments

Comments
 (0)