|
1 | 1 | import { createNodeFromContent } from '../helpers/createNodeFromContent'; |
2 | 2 | import { selectionToInsertionEnd } from '../helpers/selectionToInsertionEnd'; |
3 | 3 |
|
| 4 | +/** |
| 5 | + * @typedef {import("prosemirror-model").Node} ProseMirrorNode |
| 6 | + * @typedef {import("prosemirror-model").Fragment} ProseMirrorFragment |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * Checks if the given node or fragment is a ProseMirror Fragment. |
| 11 | + * @param {ProseMirrorNode|ProseMirrorFragment} nodeOrFragment |
| 12 | + * @returns {boolean} |
| 13 | + */ |
4 | 14 | const isFragment = (nodeOrFragment) => { |
5 | 15 | return !('type' in nodeOrFragment); |
6 | 16 | }; |
7 | 17 |
|
8 | | -//prettier-ignore |
9 | | -export const insertContentAt = (position, value, options) => ({ tr, dispatch, editor }) => { |
10 | | - if (dispatch) { |
11 | | - options = { |
12 | | - parseOptions: {}, |
13 | | - updateSelection: true, |
14 | | - applyInputRules: false, |
15 | | - applyPasteRules: false, |
16 | | - ...options, |
17 | | - }; |
18 | | - |
19 | | - let content; |
20 | | - |
21 | | - try { |
22 | | - content = createNodeFromContent(value, editor.schema, { |
23 | | - parseOptions: { |
24 | | - preserveWhitespace: 'full', |
25 | | - ...options.parseOptions, |
26 | | - }, |
27 | | - errorOnInvalidContent: options.errorOnInvalidContent ?? editor.options.enableContentCheck, |
28 | | - }); |
29 | | - } catch (e) { |
30 | | - editor.emit('contentError', { |
31 | | - editor, |
32 | | - error: e, |
33 | | - disableCollaboration: () => { |
34 | | - console.error('[super-editor error]: Unable to disable collaboration at this point in time'); |
35 | | - }, |
36 | | - }); |
37 | | - return false; |
38 | | - } |
| 18 | +/** |
| 19 | + * Inserts content at the specified position. |
| 20 | + * @param {import("prosemirror-model").ResolvedPos} position |
| 21 | + * @param {string|Array<string|ProseMirrorNode>} value |
| 22 | + * @param {Object} options |
| 23 | + * @returns |
| 24 | + */ |
| 25 | +export const insertContentAt = |
| 26 | + (position, value, options) => |
| 27 | + ({ tr, dispatch, editor }) => { |
| 28 | + if (dispatch) { |
| 29 | + options = { |
| 30 | + parseOptions: {}, |
| 31 | + updateSelection: true, |
| 32 | + applyInputRules: false, |
| 33 | + applyPasteRules: false, |
| 34 | + ...options, |
| 35 | + }; |
| 36 | + |
| 37 | + let content; |
| 38 | + |
| 39 | + try { |
| 40 | + content = createNodeFromContent(value, editor.schema, { |
| 41 | + parseOptions: { |
| 42 | + preserveWhitespace: 'full', |
| 43 | + ...options.parseOptions, |
| 44 | + }, |
| 45 | + errorOnInvalidContent: options.errorOnInvalidContent ?? editor.options.enableContentCheck, |
| 46 | + }); |
| 47 | + } catch (e) { |
| 48 | + editor.emit('contentError', { |
| 49 | + editor, |
| 50 | + error: e, |
| 51 | + disableCollaboration: () => { |
| 52 | + console.error('[super-editor error]: Unable to disable collaboration at this point in time'); |
| 53 | + }, |
| 54 | + }); |
| 55 | + return false; |
| 56 | + } |
39 | 57 |
|
40 | | - let { from, to } = |
41 | | - typeof position === 'number' ? { from: position, to: position } : { from: position.from, to: position.to }; |
| 58 | + let { from, to } = |
| 59 | + typeof position === 'number' ? { from: position, to: position } : { from: position.from, to: position.to }; |
42 | 60 |
|
43 | | - let isOnlyTextContent = true; |
44 | | - let isOnlyBlockContent = true; |
45 | | - const nodes = isFragment(content) ? content : [content]; |
| 61 | + // If the original input is plainly textual, prefer insertText regardless of how parsing represents it. |
| 62 | + const forceTextInsert = |
| 63 | + typeof value === 'string' || |
| 64 | + (Array.isArray(value) && value.every((v) => typeof v === 'string' || (v && typeof v.text === 'string'))) || |
| 65 | + (value && typeof value === 'object' && typeof value.text === 'string'); |
46 | 66 |
|
47 | | - nodes.forEach((node) => { |
48 | | - // check if added node is valid |
49 | | - node.check(); |
| 67 | + let isOnlyTextContent = forceTextInsert; // start true for plain text inputs |
| 68 | + let isOnlyBlockContent = true; |
| 69 | + const nodes = isFragment(content) ? content : [content]; |
50 | 70 |
|
51 | | - isOnlyTextContent = isOnlyTextContent ? node.isText && node.marks.length === 0 : false; |
| 71 | + nodes.forEach((node) => { |
| 72 | + // check if added node is valid |
| 73 | + node.check(); |
52 | 74 |
|
53 | | - isOnlyBlockContent = isOnlyBlockContent ? node.isBlock : false; |
54 | | - }); |
| 75 | + // only refine text heuristic if we are NOT forcing text insertion based on the original value |
| 76 | + if (!forceTextInsert) { |
| 77 | + isOnlyTextContent = isOnlyTextContent ? node.isText && node.marks.length === 0 : false; |
| 78 | + } |
55 | 79 |
|
56 | | - // check if we can replace the wrapping node by |
57 | | - // the newly inserted content |
58 | | - // example: |
59 | | - // replace an empty paragraph by an inserted image |
60 | | - // instead of inserting the image below the paragraph |
61 | | - if (from === to && isOnlyBlockContent) { |
62 | | - const { parent } = tr.doc.resolve(from); |
63 | | - const isEmptyTextBlock = parent.isTextblock && !parent.type.spec.code && !parent.childCount; |
| 80 | + isOnlyBlockContent = isOnlyBlockContent ? node.isBlock : false; |
| 81 | + }); |
64 | 82 |
|
65 | | - if (isEmptyTextBlock) { |
66 | | - from -= 1; |
67 | | - to += 1; |
| 83 | + // check if we can replace the wrapping node by |
| 84 | + // the newly inserted content |
| 85 | + // example: |
| 86 | + // replace an empty paragraph by an inserted image |
| 87 | + // instead of inserting the image below the paragraph |
| 88 | + if (from === to && isOnlyBlockContent) { |
| 89 | + const { parent } = tr.doc.resolve(from); |
| 90 | + const isEmptyTextBlock = parent.isTextblock && !parent.type.spec.code && !parent.childCount; |
| 91 | + |
| 92 | + if (isEmptyTextBlock) { |
| 93 | + from -= 1; |
| 94 | + to += 1; |
| 95 | + } |
68 | 96 | } |
69 | | - } |
70 | 97 |
|
71 | | - let newContent; |
72 | | - |
73 | | - // if there is only plain text we have to use `insertText` |
74 | | - // because this will keep the current marks |
75 | | - if (isOnlyTextContent) { |
76 | | - // if value is string, we can use it directly |
77 | | - // otherwise if it is an array, we have to join it |
78 | | - if (Array.isArray(value)) { |
79 | | - newContent = value.map((v) => v.text || '').join(''); |
80 | | - } else if (typeof value === 'object' && !!value && !!value.text) { |
81 | | - newContent = value.text; |
| 98 | + let newContent; |
| 99 | + |
| 100 | + // if there is only plain text we have to use `insertText` |
| 101 | + // because this will keep the current marks |
| 102 | + if (isOnlyTextContent) { |
| 103 | + // if value is string, we can use it directly |
| 104 | + // otherwise if it is an array, we have to join it |
| 105 | + if (Array.isArray(value)) { |
| 106 | + newContent = value.map((v) => (typeof v === 'string' ? v : (v && v.text) || '')).join(''); |
| 107 | + } else if (typeof value === 'object' && !!value && !!value.text) { |
| 108 | + newContent = value.text; |
| 109 | + } else { |
| 110 | + newContent = value; |
| 111 | + } |
| 112 | + |
| 113 | + tr.insertText(newContent, from, to); |
82 | 114 | } else { |
83 | | - newContent = value; |
84 | | - } |
85 | | - |
86 | | - tr.insertText(newContent, from, to); |
87 | | - } else { |
88 | | - newContent = content; |
| 115 | + newContent = content; |
89 | 116 |
|
90 | | - tr.replaceWith(from, to, newContent); |
91 | | - } |
| 117 | + tr.replaceWith(from, to, newContent); |
| 118 | + } |
92 | 119 |
|
93 | | - // set cursor at end of inserted content |
94 | | - if (options.updateSelection) { |
95 | | - selectionToInsertionEnd(tr, tr.steps.length - 1, -1); |
96 | | - } |
| 120 | + // set cursor at end of inserted content |
| 121 | + if (options.updateSelection) { |
| 122 | + selectionToInsertionEnd(tr, tr.steps.length - 1, -1); |
| 123 | + } |
97 | 124 |
|
98 | | - if (options.applyInputRules) { |
99 | | - tr.setMeta('applyInputRules', { from, text: newContent }); |
100 | | - } |
| 125 | + if (options.applyInputRules) { |
| 126 | + tr.setMeta('applyInputRules', { from, text: newContent }); |
| 127 | + } |
101 | 128 |
|
102 | | - if (options.applyPasteRules) { |
103 | | - tr.setMeta('applyPasteRules', { from, text: newContent }); |
| 129 | + if (options.applyPasteRules) { |
| 130 | + tr.setMeta('applyPasteRules', { from, text: newContent }); |
| 131 | + } |
104 | 132 | } |
105 | | - } |
106 | 133 |
|
107 | | - return true; |
108 | | -}; |
| 134 | + return true; |
| 135 | + }; |
0 commit comments