|
1 |
| -import type {Slice} from 'prosemirror-model'; |
| 1 | +import {DOMSerializer, type Slice} from 'prosemirror-model'; |
2 | 2 | import type {EditorView} from 'prosemirror-view';
|
3 |
| -import {__serializeForClipboard} from 'prosemirror-view'; |
4 | 3 |
|
5 |
| -declare module 'prosemirror-view' { |
6 |
| - type SerializeForClipboard = ( |
7 |
| - view: EditorView, |
8 |
| - slice: Slice, |
9 |
| - ) => {dom: HTMLElement; text: string}; |
| 4 | +// Trick from jQuery -- some elements must be wrapped in other |
| 5 | +// elements for innerHTML to work. I.e. if you do `div.innerHTML = |
| 6 | +// "<td>..</td>"` the table cells are ignored. |
| 7 | +const wrapMap: {[node: string]: string[]} = { |
| 8 | + thead: ['table'], |
| 9 | + tbody: ['table'], |
| 10 | + tfoot: ['table'], |
| 11 | + caption: ['table'], |
| 12 | + colgroup: ['table'], |
| 13 | + col: ['table', 'colgroup'], |
| 14 | + tr: ['table', 'tbody'], |
| 15 | + td: ['table', 'tbody', 'tr'], |
| 16 | + th: ['table', 'tbody', 'tr'], |
| 17 | +}; |
10 | 18 |
|
11 |
| - // internal export |
12 |
| - export const __serializeForClipboard: SerializeForClipboard; |
| 19 | +let _detachedDoc: Document | null = null; |
| 20 | +function detachedDoc() { |
| 21 | + // eslint-disable-next-line no-return-assign |
| 22 | + return _detachedDoc || (_detachedDoc = document.implementation.createHTMLDocument('title')); |
13 | 23 | }
|
14 | 24 |
|
15 |
| -if (!__serializeForClipboard) |
16 |
| - throw new Error('__serializeForClipboard not exported from prosemirror-view module.'); |
| 25 | +// MAJOR: remove serializeForClipboard |
| 26 | +/** |
| 27 | + * @deprecated |
| 28 | + * will be removed in next major version, use view.serializeForClipboard instead |
| 29 | + */ |
| 30 | +export function serializeForClipboard(view: EditorView, slice: Slice) { |
| 31 | + view.someProp('transformCopied', (f) => { |
| 32 | + slice = f(slice!, view); |
| 33 | + }); |
17 | 34 |
|
18 |
| -export {__serializeForClipboard as serializeForClipboard}; |
| 35 | + const context = []; |
| 36 | + let {content, openStart, openEnd} = slice; |
| 37 | + |
| 38 | + while ( |
| 39 | + openStart > 1 && |
| 40 | + openEnd > 1 && |
| 41 | + content.childCount == 1 && |
| 42 | + content.firstChild!.childCount == 1 |
| 43 | + ) { |
| 44 | + openStart--; |
| 45 | + openEnd--; |
| 46 | + const node = content.firstChild!; |
| 47 | + context.push( |
| 48 | + node.type.name, |
| 49 | + node.attrs != (node.type as any).defaultAttrs ? node.attrs : null, |
| 50 | + ); |
| 51 | + content = node.content; |
| 52 | + } |
| 53 | + |
| 54 | + const serializer = |
| 55 | + view.someProp('clipboardSerializer') || DOMSerializer.fromSchema(view.state.schema); |
| 56 | + const doc = detachedDoc(), |
| 57 | + wrap = doc.createElement('div'); |
| 58 | + wrap.appendChild(serializer.serializeFragment(content, {document: doc})); |
| 59 | + |
| 60 | + let firstChild = wrap.firstChild, |
| 61 | + needsWrap, |
| 62 | + wrappers = 0; |
| 63 | + while ( |
| 64 | + firstChild && |
| 65 | + firstChild.nodeType == 1 && |
| 66 | + (needsWrap = wrapMap[firstChild.nodeName.toLowerCase()]) |
| 67 | + ) { |
| 68 | + for (let i = needsWrap.length - 1; i >= 0; i--) { |
| 69 | + const wrapper = doc.createElement(needsWrap[i]); |
| 70 | + while (wrap.firstChild) wrapper.appendChild(wrap.firstChild); |
| 71 | + wrap.appendChild(wrapper); |
| 72 | + wrappers++; |
| 73 | + } |
| 74 | + firstChild = wrap.firstChild; |
| 75 | + } |
| 76 | + |
| 77 | + if (firstChild && firstChild.nodeType == 1) |
| 78 | + (firstChild as HTMLElement).setAttribute( |
| 79 | + 'data-pm-slice', |
| 80 | + `${openStart} ${openEnd}${wrappers ? ` -${wrappers}` : ''} ${JSON.stringify(context)}`, |
| 81 | + ); |
| 82 | + |
| 83 | + const text = |
| 84 | + view.someProp('clipboardTextSerializer', (f) => f(slice, view)) || |
| 85 | + slice.content.textBetween(0, slice.content.size, '\n\n'); |
| 86 | + |
| 87 | + return {dom: wrap, text, slice}; |
| 88 | +} |
0 commit comments