({
onPaste,
extensions,
}: {
onPaste?: PasteHandler;
extensions: Extensions;
})
| 10 | ) => boolean; |
| 11 | |
| 12 | export function createPasteHandler({ |
| 13 | onPaste, |
| 14 | extensions, |
| 15 | }: { |
| 16 | onPaste?: PasteHandler; |
| 17 | extensions: Extensions; |
| 18 | }) { |
| 19 | return (view: EditorView, event: ClipboardEvent, slice: Slice): boolean => { |
| 20 | const text = event.clipboardData?.getData('text/plain'); |
| 21 | |
| 22 | if (text && onPaste?.(text, view)) { |
| 23 | event.preventDefault(); |
| 24 | |
| 25 | return true; |
| 26 | } |
| 27 | |
| 28 | if (event.clipboardData?.files?.[0]) { |
| 29 | const file = event.clipboardData.files[0]; |
| 30 | if (onPaste?.(file, view)) { |
| 31 | event.preventDefault(); |
| 32 | |
| 33 | return true; |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | if (slice.content.childCount === 1) { |
| 38 | return false; |
| 39 | } |
| 40 | |
| 41 | if (event.clipboardData?.getData?.('text/html')) { |
| 42 | event.preventDefault(); |
| 43 | const html = event.clipboardData.getData('text/html'); |
| 44 | |
| 45 | const sanitizedHtml = sanitizePastedHtml(html); |
| 46 | |
| 47 | const jsonContent = generateJSON(sanitizedHtml, extensions); |
| 48 | const node = view.state.schema.nodeFromJSON(jsonContent); |
| 49 | |
| 50 | const transaction = view.state.tr.replaceSelectionWith(node, false); |
| 51 | view.dispatch(transaction); |
| 52 | |
| 53 | return true; |
| 54 | } |
| 55 | return false; |
| 56 | }; |
| 57 | } |
no test coverage detected