Skip to content

Commit dbae4a8

Browse files
authored
feat(deps): updated prosemirror-view to 1.38.0 (#717)
1 parent f48ac1c commit dbae4a8

File tree

4 files changed

+88
-19
lines changed

4 files changed

+88
-19
lines changed

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@
210210
"prosemirror-test-builder": "^1.1.1",
211211
"prosemirror-transform": "^1.10.2",
212212
"prosemirror-utils": "^1.2.2",
213-
"prosemirror-view": "^1.37.2",
213+
"prosemirror-view": "^1.38.0",
214214
"react-error-boundary": "^3.1.4",
215215
"react-hotkeys-hook": "4.5.0",
216216
"react-use": "^17.3.2",

src/extensions/behavior/Clipboard/clipboard.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {type Logger2, globalLogger} from '../../../logger';
77
import '../../../types/spec';
88
import {tryCatch} from '../../../utils/helpers';
99
import {isNodeSelection, isTextSelection, isWholeSelection} from '../../../utils/selection';
10-
import {serializeForClipboard} from '../../../utils/serialize-for-clipboard';
1110
import {BaseNode, pType} from '../../base/BaseSchema';
1211

1312
import {isInsideCode} from './code';
@@ -244,7 +243,7 @@ function serializeSelected(view: EditorView, serializer: Serializer): SerializeR
244243

245244
// FIXME: Verify and use Node instead of Fragment
246245
const markup = serializer.serialize(getCopyContent(view.state).content as any);
247-
const {dom, text} = serializeForClipboard(view, sel.content());
246+
const {dom, text} = view.serializeForClipboard(sel.content());
248247
return {markup, text, html: dom.innerHTML};
249248
}
250249

src/utils/serialize-for-clipboard.ts

Lines changed: 82 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,88 @@
1-
import type {Slice} from 'prosemirror-model';
1+
import {DOMSerializer, type Slice} from 'prosemirror-model';
22
import type {EditorView} from 'prosemirror-view';
3-
import {__serializeForClipboard} from 'prosemirror-view';
43

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+
};
1018

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'));
1323
}
1424

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+
});
1734

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

Comments
 (0)