diff --git a/src/app/components/editor/Editor.test.tsx b/src/app/components/editor/Editor.test.tsx
index 711f724ca..cb76a0b5b 100644
--- a/src/app/components/editor/Editor.test.tsx
+++ b/src/app/components/editor/Editor.test.tsx
@@ -557,6 +557,104 @@ describe('CustomEditor', () => {
});
});
+ it('only reads clipboard formats advertised by the data transfer', () => {
+ render();
+ const editor = pasteFallbackEditor;
+ if (!editor) throw new Error('paste fallback harness did not mount');
+
+ const getData = vi.fn<(type: string) => string>((type) =>
+ type === 'text/plain' ? 'pasted text' : ''
+ );
+ const data = {
+ files: [],
+ getData,
+ types: ['text/plain'],
+ } as unknown as DataTransfer;
+
+ act(() => {
+ Transforms.select(editor, { path: [0, 0], offset: 0 });
+ editor.insertData(data);
+ });
+
+ expect(getData).toHaveBeenCalledTimes(1);
+ expect(getData).toHaveBeenCalledWith('text/plain');
+ expect(pastedLines()).toEqual(['pasted text']);
+ });
+
+ it('keeps Slate fragment pastes intact without reading unrelated formats', () => {
+ render();
+ const editor = pasteFallbackEditor;
+ if (!editor) throw new Error('paste fallback harness did not mount');
+
+ const fragment = btoa(encodeURIComponent(JSON.stringify([{ text: 'fragment text' }])));
+ const getData = vi.fn<(type: string) => string>((type) =>
+ type === 'application/x-slate-fragment' ? fragment : ''
+ );
+ const data = {
+ files: [],
+ getData,
+ types: ['application/x-slate-fragment', 'text/plain'],
+ } as unknown as DataTransfer;
+
+ act(() => {
+ Transforms.select(editor, { path: [0, 0], offset: 0 });
+ editor.insertData(data);
+ });
+
+ expect(getData).toHaveBeenCalledTimes(1);
+ expect(getData).toHaveBeenCalledWith('application/x-slate-fragment');
+ expect(pastedLines()).toEqual(['fragment text']);
+ });
+
+ it('keeps HTML-embedded Slate fragments intact without reading unrelated formats', () => {
+ render();
+ const editor = pasteFallbackEditor;
+ if (!editor) throw new Error('paste fallback harness did not mount');
+
+ const fragment = btoa(encodeURIComponent(JSON.stringify([{ text: 'HTML fragment text' }])));
+ const getData = vi.fn<(type: string) => string>((type) =>
+ type === 'text/html' ? `fragment` : ''
+ );
+ const data = {
+ files: [],
+ getData,
+ types: ['text/html', 'text/plain'],
+ } as unknown as DataTransfer;
+
+ act(() => {
+ Transforms.select(editor, { path: [0, 0], offset: 0 });
+ editor.insertData(data);
+ });
+
+ expect(getData).toHaveBeenCalledTimes(1);
+ expect(getData).toHaveBeenCalledWith('text/html');
+ expect(pastedLines()).toEqual(['HTML fragment text']);
+ });
+
+ it('falls back to plain text when HTML does not contain a Slate fragment', () => {
+ render();
+ const editor = pasteFallbackEditor;
+ if (!editor) throw new Error('paste fallback harness did not mount');
+
+ const getData = vi.fn<(type: string) => string>((type) =>
+ type === 'text/html' ? 'pasted text' : 'pasted text'
+ );
+ const data = {
+ files: [],
+ getData,
+ types: ['text/html', 'text/plain'],
+ } as unknown as DataTransfer;
+
+ act(() => {
+ Transforms.select(editor, { path: [0, 0], offset: 0 });
+ editor.insertData(data);
+ });
+
+ expect(getData).toHaveBeenNthCalledWith(1, 'text/html');
+ expect(getData).toHaveBeenNthCalledWith(2, 'text/plain');
+ expect(pastedLines()).toEqual(['pasted text']);
+ });
+
it('reuses the cached measurement when resize observer fires without changing the single-line width', async () => {
const queuedFrames = new Map();
let nextFrameId = 1;
diff --git a/src/app/components/editor/Editor.tsx b/src/app/components/editor/Editor.tsx
index ace613ace..413aba42d 100644
--- a/src/app/components/editor/Editor.tsx
+++ b/src/app/components/editor/Editor.tsx
@@ -43,8 +43,49 @@ const withVoid = (editor: Editor): Editor => {
return editor;
};
+const SLATE_FRAGMENT_TYPE = 'application/x-slate-fragment';
+const slateFragmentAttribute = /data-slate-fragment="(.+?)"/m;
+
+const insertSlateFragment = (editor: Editor, fragment: string): boolean => {
+ if (!fragment) return false;
+
+ const decoded = decodeURIComponent(window.atob(fragment));
+ editor.insertFragment(JSON.parse(decoded));
+ return true;
+};
+
+const withEfficientClipboardReads = (editor: Editor): Editor => {
+ const { insertData } = editor;
+
+ editor.insertData = (data) => {
+ const types = new Set(Array.from(data.types ?? [], (type) => type.toLowerCase()));
+ if (types.size === 0) {
+ insertData(data);
+ return;
+ }
+
+ if (
+ types.has(SLATE_FRAGMENT_TYPE) &&
+ insertSlateFragment(editor, data.getData(SLATE_FRAGMENT_TYPE))
+ ) {
+ return;
+ }
+
+ if (types.has('text/html')) {
+ const [, fragment] = data.getData('text/html').match(slateFragmentAttribute) || [];
+ if (fragment && insertSlateFragment(editor, fragment)) return;
+ }
+
+ if (types.has('text/plain')) editor.insertTextData(data);
+ };
+
+ return editor;
+};
+
export const useEditor = (): Editor => {
- const [editor] = useState(() => withInline(withVoid(withReact(withHistory(createEditor())))));
+ const [editor] = useState(() =>
+ withInline(withVoid(withEfficientClipboardReads(withReact(withHistory(createEditor())))))
+ );
return editor;
};