fix: avoid unnecessary Slate clipboard reads

This commit is contained in:
Erwan Leboucher committed 2026-07-30 12:45:01 +02:00
1 parent fb87569e25
commit 8defe20691
2 files changed
+140 -1

No files matched your search

+98
View File
@@ -557,6 +557,104 @@ describe('CustomEditor', () => {
});
});
it('only reads clipboard formats advertised by the data transfer', () => {
render(<PasteFallbackHarness />);
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(<PasteFallbackHarness />);
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(<PasteFallbackHarness />);
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' ? `<span data-slate-fragment="${fragment}">fragment</span>` : ''
);
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(<PasteFallbackHarness />);
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' ? '<strong>pasted text</strong>' : '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<number, FrameRequestCallback>();
let nextFrameId = 1;
+42 -1
View File
@@ -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;
};