@handlewithcare/react-prosemirror 3.1.0-tiptap.52 → 3.1.0-tiptap.53

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/dist/cjs/ReactEditorView.js +6 -0
  2. package/dist/cjs/components/ChildNodeViews.js +16 -10
  3. package/dist/cjs/components/CursorWrapper.js +3 -7
  4. package/dist/cjs/components/ProseMirror.js +5 -3
  5. package/dist/cjs/components/TextNodeView.js +260 -47
  6. package/dist/cjs/components/TrailingHackView.js +70 -0
  7. package/dist/cjs/components/WidgetView.js +3 -1
  8. package/dist/cjs/contexts/ChildDescriptionsContext.js +3 -1
  9. package/dist/cjs/decorations/viewDecorations.js +1 -6
  10. package/dist/cjs/hooks/useEditor.js +2 -10
  11. package/dist/cjs/hooks/useMarkViewDescription.js +61 -3
  12. package/dist/cjs/hooks/useNodeViewDescription.js +64 -21
  13. package/dist/cjs/plugins/beforeInputPlugin.js +147 -45
  14. package/dist/cjs/plugins/reactKeys.js +21 -14
  15. package/dist/cjs/viewdesc.js +52 -4
  16. package/dist/esm/ReactEditorView.js +6 -0
  17. package/dist/esm/components/ChildNodeViews.js +17 -11
  18. package/dist/esm/components/CursorWrapper.js +3 -7
  19. package/dist/esm/components/ProseMirror.js +5 -3
  20. package/dist/esm/components/TextNodeView.js +209 -45
  21. package/dist/esm/components/TrailingHackView.js +71 -1
  22. package/dist/esm/components/WidgetView.js +3 -1
  23. package/dist/esm/contexts/ChildDescriptionsContext.js +3 -1
  24. package/dist/esm/decorations/viewDecorations.js +1 -6
  25. package/dist/esm/hooks/useEditor.js +2 -10
  26. package/dist/esm/hooks/useMarkViewDescription.js +62 -4
  27. package/dist/esm/hooks/useNodeViewDescription.js +65 -22
  28. package/dist/esm/plugins/beforeInputPlugin.js +147 -45
  29. package/dist/esm/plugins/reactKeys.js +21 -14
  30. package/dist/esm/viewdesc.js +51 -4
  31. package/dist/tsconfig.tsbuildinfo +1 -1
  32. package/dist/types/ReactEditorView.d.ts +6 -1
  33. package/dist/types/components/TextNodeView.d.ts +20 -5
  34. package/dist/types/components/TrailingHackView.d.ts +1 -1
  35. package/dist/types/components/__tests__/ProseMirror.composition.test.d.ts +17 -1
  36. package/dist/types/contexts/ChildDescriptionsContext.d.ts +5 -3
  37. package/dist/types/decorations/viewDecorations.d.ts +2 -2
  38. package/dist/types/hooks/useEditor.d.ts +1 -2
  39. package/dist/types/hooks/useMarkViewDescription.d.ts +2 -1
  40. package/dist/types/hooks/useNodeViewDescription.d.ts +2 -1
  41. package/dist/types/plugins/beforeInputPlugin.d.ts +1 -2
  42. package/dist/types/plugins/reactKeys.d.ts +9 -8
  43. package/dist/types/viewdesc.d.ts +3 -2
  44. package/package.json +3 -1
@@ -33,6 +33,10 @@ let ReactEditorView = class ReactEditorView extends _prosemirrorview.EditorView
33
33
  nextProps;
34
34
  prevState;
35
35
  _destroyed;
36
+ // TODO: Probably refactor? It's used in TrailingHackView to detect
37
+ // whether it was mounted during a compositionstart event handler
38
+ compositionStarting;
39
+ displacedNodes;
36
40
  constructor(place, props){
37
41
  // Prevent the base class from destroying the React-managed nodes.
38
42
  // Restore them below after invoking the base class constructor.
@@ -85,6 +89,8 @@ let ReactEditorView = class ReactEditorView extends _prosemirrorview.EditorView
85
89
  // @ts-expect-error this violates the typing but class does it, too.
86
90
  this.docView = null;
87
91
  this._destroyed = false;
92
+ this.compositionStarting = false;
93
+ this.displacedNodes = [];
88
94
  }
89
95
  get props() {
90
96
  return this.nextProps;
@@ -109,14 +109,20 @@ const ChildView = /*#__PURE__*/ (0, _react.memo)(function ChildView(param) {
109
109
  }) : child.node.isText ? /*#__PURE__*/ _react.default.createElement(_ChildDescriptionsContext.ChildDescriptionsContext.Consumer, {
110
110
  key: child.key
111
111
  }, (param)=>{
112
- let { siblingsRef, parentRef } = param;
113
- return /*#__PURE__*/ _react.default.createElement(_TextNodeView.TextNodeView, {
114
- view: view,
115
- node: child.node,
116
- getPos: getPos,
117
- siblingsRef: siblingsRef,
118
- parentRef: parentRef,
119
- decorations: child.outerDeco
112
+ let { siblingsRef, parentRef, findCompositionDOM } = param;
113
+ return /*#__PURE__*/ _react.default.createElement(_EditorContext.EditorContext.Consumer, null, (param)=>{
114
+ let { registerEventListener, unregisterEventListener } = param;
115
+ return /*#__PURE__*/ _react.default.createElement(_TextNodeView.RemountableTextNodeView, {
116
+ view: view,
117
+ node: child.node,
118
+ getPos: getPos,
119
+ siblingsRef: siblingsRef,
120
+ parentRef: parentRef,
121
+ findCompositionDOM: findCompositionDOM,
122
+ decorations: child.outerDeco,
123
+ registerEventListener: registerEventListener,
124
+ unregisterEventListener: unregisterEventListener
125
+ });
120
126
  });
121
127
  }) : /*#__PURE__*/ _react.default.createElement(_NodeView.NodeView, {
122
128
  key: child.key,
@@ -399,14 +405,14 @@ const ChildNodeViews = /*#__PURE__*/ (0, _react.memo)(function ChildNodeViews(pa
399
405
  component: _SeparatorHackView.SeparatorHackView,
400
406
  marks: [],
401
407
  offset: lastChild?.offset ?? 0,
402
- index: (lastChild?.index ?? 0) + 2,
408
+ index: (lastChild?.index ?? 0) + 1,
403
409
  key: "trailing-hack-img"
404
410
  }, {
405
411
  type: "hack",
406
412
  component: _TrailingHackView.TrailingHackView,
407
413
  marks: [],
408
414
  offset: lastChild?.offset ?? 0,
409
- index: (lastChild?.index ?? 0) + 1,
415
+ index: (lastChild?.index ?? 0) + 2,
410
416
  key: "trailing-hack-br"
411
417
  });
412
418
  }
@@ -64,14 +64,10 @@ const CursorWrapper = /*#__PURE__*/ (0, _react.forwardRef)(function CursorWrappe
64
64
  view.domObserver.disconnectSelection();
65
65
  // @ts-expect-error Internal property - domSelection
66
66
  const domSel = view.domSelection();
67
+ if (!domSel.isCollapsed) return;
67
68
  const node = innerRef.current;
68
- const img = node.nodeName == "IMG";
69
- if (img) {
70
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
71
- domSel.collapse(node.parentNode, (0, _dom.domIndex)(node) + 1);
72
- } else {
73
- domSel.collapse(node, 0);
74
- }
69
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
70
+ domSel.collapse(node.parentNode, (0, _dom.domIndex)(node) + 1);
75
71
  // @ts-expect-error Internal property - domObserver
76
72
  view.domObserver.connectSelection();
77
73
  }, []);
@@ -68,12 +68,14 @@ const rootChildDescriptionsContextValue = {
68
68
  },
69
69
  siblingsRef: {
70
70
  current: []
71
- }
71
+ },
72
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
73
+ findCompositionDOM: ()=>{}
72
74
  };
73
75
  function ProseMirrorInner(param) {
74
76
  let { children, nodeViewComponents, markViewComponents, ...props } = param;
75
77
  const [mount, setMount] = (0, _react.useState)(null);
76
- const { editor, cursorWrapper, state } = (0, _useEditor.useEditor)(mount, props);
78
+ const { editor, state } = (0, _useEditor.useEditor)(mount, props);
77
79
  const nodeViewConstructors = editor.view.nodeViews;
78
80
  const nodeViewContextValue = (0, _react.useMemo)(()=>{
79
81
  return {
@@ -90,7 +92,7 @@ function ProseMirrorInner(param) {
90
92
  ]);
91
93
  const node = state.doc;
92
94
  const decorations = (0, _computeDocDeco.computeDocDeco)(editor.view);
93
- const innerDecorations = (0, _viewDecorations.viewDecorations)(editor.view, cursorWrapper);
95
+ const innerDecorations = (0, _viewDecorations.viewDecorations)(editor.view);
94
96
  const docNodeViewContextValue = (0, _react.useMemo)(()=>({
95
97
  setMount,
96
98
  node,
@@ -2,17 +2,68 @@
2
2
  Object.defineProperty(exports, "__esModule", {
3
3
  value: true
4
4
  });
5
- Object.defineProperty(exports, "TextNodeView", {
6
- enumerable: true,
7
- get: function() {
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: all[name]
9
+ });
10
+ }
11
+ _export(exports, {
12
+ RemountableTextNodeView: function() {
13
+ return RemountableTextNodeView;
14
+ },
15
+ TextNodeView: function() {
8
16
  return TextNodeView;
9
17
  }
10
18
  });
19
+ const _prosemirrorstate = require("prosemirror-state");
11
20
  const _prosemirrorview = require("prosemirror-view");
12
- const _react = require("react");
21
+ const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
22
+ const _ReactEditorView = require("../ReactEditorView.js");
13
23
  const _findDOMNode = require("../findDOMNode.js");
14
24
  const _viewdesc = require("../viewdesc.js");
15
25
  const _ChildNodeViews = require("./ChildNodeViews.js");
26
+ function _getRequireWildcardCache(nodeInterop) {
27
+ if (typeof WeakMap !== "function") return null;
28
+ var cacheBabelInterop = new WeakMap();
29
+ var cacheNodeInterop = new WeakMap();
30
+ return (_getRequireWildcardCache = function(nodeInterop) {
31
+ return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
32
+ })(nodeInterop);
33
+ }
34
+ function _interop_require_wildcard(obj, nodeInterop) {
35
+ if (!nodeInterop && obj && obj.__esModule) {
36
+ return obj;
37
+ }
38
+ if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
39
+ return {
40
+ default: obj
41
+ };
42
+ }
43
+ var cache = _getRequireWildcardCache(nodeInterop);
44
+ if (cache && cache.has(obj)) {
45
+ return cache.get(obj);
46
+ }
47
+ var newObj = {
48
+ __proto__: null
49
+ };
50
+ var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
51
+ for(var key in obj){
52
+ if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
53
+ var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
54
+ if (desc && (desc.get || desc.set)) {
55
+ Object.defineProperty(newObj, key, desc);
56
+ } else {
57
+ newObj[key] = obj[key];
58
+ }
59
+ }
60
+ }
61
+ newObj.default = obj;
62
+ if (cache) {
63
+ cache.set(obj, newObj);
64
+ }
65
+ return newObj;
66
+ }
16
67
  function shallowEqual(objA, objB) {
17
68
  if (objA === objB) {
18
69
  return true;
@@ -36,75 +87,237 @@ function shallowEqual(objA, objB) {
36
87
  return true;
37
88
  }
38
89
  let TextNodeView = class TextNodeView extends _react.Component {
39
- viewDescRef = null;
40
- renderRef = null;
41
- updateEffect() {
42
- const { view, decorations, siblingsRef, parentRef, getPos, node } = this.props;
43
- // There simply is no other way to ref a text node
44
- // eslint-disable-next-line react/no-find-dom-node
90
+ viewDescRef = createMutRef();
91
+ renderRef = createMutRef();
92
+ wasProtecting = createMutRef();
93
+ containsCompositionNodeText = createMutRef();
94
+ // This is basically NodeViewDesc.localCompositionInfo
95
+ // from prosemirror-view. It's been slightly adjusted so that
96
+ // it can be used accurately during render, before we've
97
+ // necessarily found (or even let the browser create)
98
+ // view.input.compositionNode
99
+ shouldProtect(props) {
100
+ const { view, getPos, node } = props;
101
+ if (!(view instanceof _ReactEditorView.ReactEditorView)) return false;
102
+ if (!view.composing) {
103
+ return false;
104
+ }
105
+ const viewDesc = this.viewDescRef.current;
106
+ // If our DOM text node IS the IME's composition node, protect regardless
107
+ // of where the PM selection currently is. The IME may have replaced a
108
+ // selection that included us — moving the PM selection past us — but our
109
+ // DOM is still part of the in-progress composition. Until another
110
+ // TextNodeView's findCompositionDOM displaces us into a comp desc, only
111
+ // our own protect/no-update is preventing React from rewriting the IME's
112
+ // text. (When we *are* displaced, viewDesc is already a CompositionViewDesc
113
+ // and the existing position-based logic doesn't apply anyway.)
114
+ const ownsCompositionNode = viewDesc instanceof _viewdesc.TextViewDesc && viewDesc.nodeDOM === view.input.compositionNode;
115
+ if (!ownsCompositionNode) {
116
+ const pos = getPos();
117
+ const { from, to } = view.state.selection;
118
+ if (!(view.state.selection instanceof _prosemirrorstate.TextSelection) || from <= pos || to > pos + node.nodeSize) {
119
+ return false;
120
+ }
121
+ }
122
+ return !!this.containsCompositionNodeText.current;
123
+ }
124
+ handleCompositionEnd = ()=>{
125
+ if (!this.wasProtecting.current) return;
126
+ const { view } = this.props;
127
+ if (!(view instanceof _ReactEditorView.ReactEditorView)) return;
128
+ // If the IME detached our DOM during composition, React's fiber is now
129
+ // wired to a detached node and will silently send all subsequent updates
130
+ // into the void. Re-attach the orphan (so the upcoming unmount's
131
+ // removeChild has something to remove), then ask our wrapper to mint a
132
+ // new key — that forces React to drop this fiber and mount a fresh one
133
+ // whose stateNode it creates from the current render output.
45
134
  const dom = (0, _findDOMNode.findDOMNode)(this);
46
- // We only need to explicitly create a CompositionViewDesc
47
- // when a composition was started that produces a new text node.
48
- // Otherwise we just rely on re-rendering the renderRef
49
- if (!dom) {
50
- if (!view.composing) return;
51
- this.viewDescRef = new _viewdesc.CompositionViewDesc(parentRef.current, getPos, // These are just placeholders/dummies. We can't
52
- // actually find the correct DOM nodes from here,
53
- // so we let our parent do it.
54
- // Passing a valid element here just so that the
55
- // ViewDesc constructor doesn't blow up.
56
- document.createElement("div"), document.createTextNode(node.text ?? ""), node.text ?? "");
57
- return;
135
+ if (dom instanceof HTMLElement && !view.dom.contains(dom)) {
136
+ this.reattachAtCorrectPosition(dom);
137
+ this.props.forceRemount();
138
+ } else {
139
+ this.forceUpdate();
58
140
  }
141
+ };
142
+ create() {
143
+ const { view, decorations, siblingsRef, parentRef, getPos, node } = this.props;
144
+ const dom = (0, _findDOMNode.findDOMNode)(this);
145
+ if (!dom && !view.composing) return null;
59
146
  let textNode = dom;
60
- while(textNode.firstChild){
147
+ while(textNode?.firstChild){
61
148
  textNode = textNode.firstChild;
62
149
  }
63
- if (!this.viewDescRef || this.viewDescRef instanceof _viewdesc.CompositionViewDesc) {
64
- this.viewDescRef = new _viewdesc.TextViewDesc(undefined, [], getPos, node, decorations, _prosemirrorview.DecorationSet.empty, dom, textNode);
150
+ if (!(textNode instanceof Text)) {
151
+ textNode = null;
152
+ }
153
+ let viewDesc;
154
+ if (this.shouldProtect(this.props)) {
155
+ viewDesc = new _viewdesc.CompositionViewDesc(parentRef.current, getPos, // If we can't
156
+ // actually find the correct DOM nodes from here (
157
+ // which is the case in a composition in a newly
158
+ // created text node), we let our parent do it.
159
+ // Passing a valid element here just so that the
160
+ // ViewDesc constructor doesn't blow up.
161
+ dom ?? document.createElement("div"), textNode ?? document.createTextNode(node.text ?? ""), node.text ?? "");
65
162
  } else {
66
- this.viewDescRef.parent = parentRef.current;
67
- this.viewDescRef.children = [];
68
- this.viewDescRef.node = node;
69
- this.viewDescRef.outerDeco = decorations;
70
- this.viewDescRef.innerDeco = _prosemirrorview.DecorationSet.empty;
71
- this.viewDescRef.dom = dom;
72
- this.viewDescRef.dom.pmViewDesc = this.viewDescRef;
73
- this.viewDescRef.nodeDOM = textNode;
74
- }
75
- if (!siblingsRef.current.includes(this.viewDescRef)) {
76
- siblingsRef.current.push(this.viewDescRef);
163
+ if (!dom || !textNode) return null;
164
+ viewDesc = new _viewdesc.TextViewDesc(parentRef.current, [], getPos, node, decorations, _prosemirrorview.DecorationSet.empty, dom, textNode);
77
165
  }
166
+ siblingsRef.current.push(viewDesc);
78
167
  siblingsRef.current.sort(_viewdesc.sortViewDescs);
168
+ if (viewDesc instanceof _viewdesc.CompositionViewDesc) {
169
+ this.props.findCompositionDOM(viewDesc);
170
+ }
171
+ return viewDesc;
172
+ }
173
+ update() {
174
+ const { view, node, decorations } = this.props;
175
+ if (!(view instanceof _ReactEditorView.ReactEditorView)) return false;
176
+ const viewDesc = this.viewDescRef.current;
177
+ if (!viewDesc) return false;
178
+ // Don't force destroy/recreate just because we transitioned into protect
179
+ // mode. If our DOM text node is the IME's composition node, we want to
180
+ // keep the TextViewDesc alive so the new composition-text TextNodeView's
181
+ // findCompositionDOM second pass can find us, validate the size mismatch,
182
+ // and displace us into a properly-sized CompositionViewDesc. If we
183
+ // destroyed here, create() would put a wrong-size CompositionViewDesc on
184
+ // T and pre-empt that displacement.
185
+ const ownsCompositionNode = viewDesc instanceof _viewdesc.TextViewDesc && viewDesc.nodeDOM === view.input.compositionNode;
186
+ if (!ownsCompositionNode && this.shouldProtect(this.props) !== viewDesc instanceof _viewdesc.CompositionViewDesc) {
187
+ return false;
188
+ }
189
+ if (viewDesc instanceof _viewdesc.CompositionViewDesc) return false;
190
+ const dom = (0, _findDOMNode.findDOMNode)(this);
191
+ if (!dom || dom !== viewDesc.dom) return false;
192
+ if (!dom.contains(viewDesc.nodeDOM)) return false;
193
+ return viewDesc.matchesNode(node, decorations, _prosemirrorview.DecorationSet.empty) || viewDesc.update(node, decorations, _prosemirrorview.DecorationSet.empty, view);
194
+ }
195
+ destroy() {
196
+ const viewDesc = this.viewDescRef.current;
197
+ if (!viewDesc) return;
198
+ viewDesc.destroy();
199
+ const siblings = this.props.siblingsRef.current;
200
+ if (siblings.includes(viewDesc)) {
201
+ const index = siblings.indexOf(viewDesc);
202
+ siblings.splice(index, 1);
203
+ }
204
+ }
205
+ updateEffect() {
206
+ if (!this.update()) {
207
+ this.destroy();
208
+ this.viewDescRef.current = this.create();
209
+ }
210
+ const { view } = this.props;
211
+ if (!(view instanceof _ReactEditorView.ReactEditorView)) {
212
+ this.containsCompositionNodeText.current = true;
213
+ return;
214
+ }
215
+ const textNode = view.input.compositionNode;
216
+ if (!textNode) {
217
+ this.containsCompositionNodeText.current = true;
218
+ return;
219
+ }
220
+ // Resolve the parent textblock containing this text node and ask
221
+ // findTextInFragment whether the IME text node's *current* content can be
222
+ // placed somewhere in the textblock's PM content overlapping the
223
+ // selection. If it can, the composition is still consistent with PM state
224
+ // and we should protect. If it can't (e.g. a remote change overwrote the
225
+ // composing region), PM and the DOM have diverged — abandon protection
226
+ // so the re-render can rewrite the DOM and cancel the composition.
227
+ const $pos = view.state.doc.resolve(this.props.getPos());
228
+ const parent = $pos.parent;
229
+ if (!parent.inlineContent) {
230
+ this.containsCompositionNodeText.current = false;
231
+ return;
232
+ }
233
+ const parentStart = $pos.start();
234
+ const { from, to } = view.state.selection;
235
+ const textPos = (0, _viewdesc.findTextInFragment)(parent.content, // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
236
+ textNode.nodeValue, from - parentStart, to - parentStart);
237
+ this.containsCompositionNodeText.current = textPos >= 0;
79
238
  }
80
239
  shouldComponentUpdate(nextProps) {
240
+ // When leaving the protected state, force a re-render so React's
241
+ // virtual DOM resyncs with whatever the IME wrote into the real DOM
242
+ // while we were returning a stale renderRef.
243
+ if (this.wasProtecting.current && !this.shouldProtect(nextProps)) {
244
+ return true;
245
+ }
81
246
  return !shallowEqual(this.props, nextProps);
82
247
  }
248
+ constructor(props){
249
+ super(props);
250
+ this.viewDescRef.current = null;
251
+ this.renderRef.current = null;
252
+ this.wasProtecting.current = false;
253
+ this.containsCompositionNodeText.current = true;
254
+ }
83
255
  componentDidMount() {
256
+ this.containsCompositionNodeText.current = true;
257
+ // After a composition, force an update so that we re-check whether we need
258
+ // to be protecting our rendered content and allow React to re-sync with the
259
+ // DOM.
260
+ const { registerEventListener } = this.props;
261
+ registerEventListener("compositionend", this.handleCompositionEnd);
262
+ this.viewDescRef.current = this.create();
84
263
  this.updateEffect();
85
264
  }
86
265
  componentDidUpdate() {
87
266
  this.updateEffect();
267
+ const { view } = this.props;
268
+ if (!(view instanceof _ReactEditorView.ReactEditorView)) return;
88
269
  }
89
- componentWillUnmount() {
90
- const { siblingsRef } = this.props;
91
- if (!this.viewDescRef) return;
92
- if (siblingsRef.current.includes(this.viewDescRef)) {
93
- const index = siblingsRef.current.indexOf(this.viewDescRef);
94
- siblingsRef.current.splice(index, 1);
270
+ reattachAtCorrectPosition(dom) {
271
+ const viewDesc = this.viewDescRef.current;
272
+ if (!viewDesc) return;
273
+ let host = viewDesc.parent;
274
+ while(host && !host.contentDOM)host = host.parent;
275
+ if (!host?.contentDOM) return;
276
+ const siblings = viewDesc.parent?.children ?? [];
277
+ const idx = siblings.indexOf(viewDesc);
278
+ let nextDom = null;
279
+ for(let i = idx + 1; i < siblings.length; i++){
280
+ const sib = siblings[i];
281
+ if (sib?.dom && sib.dom.parentNode === host.contentDOM) {
282
+ nextDom = sib.dom;
283
+ break;
284
+ }
95
285
  }
286
+ host.contentDOM.insertBefore(dom, nextDom);
287
+ }
288
+ componentWillUnmount() {
289
+ const { unregisterEventListener } = this.props;
290
+ unregisterEventListener("compositionend", this.handleCompositionEnd);
291
+ this.destroy();
96
292
  }
97
293
  render() {
98
- const { view, getPos, node, decorations } = this.props;
294
+ const { node, decorations } = this.props;
99
295
  // During a composition, it's crucial that we don't try to
100
296
  // update the DOM that the user is working in. If there's
101
297
  // an active composition and the selection is in this node,
102
298
  // we freeze the DOM of this element so that it doesn't
103
299
  // interrupt the composition
104
- if (view.composing && view.state.selection.from >= getPos() && view.state.selection.from <= getPos() + node.nodeSize) {
105
- return this.renderRef;
300
+ if (this.shouldProtect(this.props)) {
301
+ this.wasProtecting.current = true;
302
+ return this.renderRef.current;
106
303
  }
107
- this.renderRef = decorations.reduce(_ChildNodeViews.wrapInDeco, node.text);
108
- return this.renderRef;
304
+ this.wasProtecting.current = false;
305
+ this.renderRef.current = decorations.reduce(_ChildNodeViews.wrapInDeco, node.text);
306
+ return this.renderRef.current;
109
307
  }
110
308
  };
309
+ /**
310
+ * createRef returns a RefObject, even though the docs
311
+ * say that it's acceptible to manage the ref's value
312
+ * yourself.
313
+ */ function createMutRef() {
314
+ return /*#__PURE__*/ (0, _react.createRef)();
315
+ }
316
+ function RemountableTextNodeView(props) {
317
+ const [key, forceRemount] = (0, _react.useReducer)((x)=>x + 1, 0);
318
+ return /*#__PURE__*/ _react.default.createElement(TextNodeView, {
319
+ key: key,
320
+ forceRemount: forceRemount,
321
+ ...props
322
+ });
323
+ }
@@ -9,8 +9,11 @@ Object.defineProperty(exports, "TrailingHackView", {
9
9
  }
10
10
  });
11
11
  const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
12
+ const _ReactEditorView = require("../ReactEditorView.js");
12
13
  const _ChildDescriptionsContext = require("../contexts/ChildDescriptionsContext.js");
13
14
  const _useClientLayoutEffect = require("../hooks/useClientLayoutEffect.js");
15
+ const _useEditorEffect = require("../hooks/useEditorEffect.js");
16
+ const _useEditorEventListener = require("../hooks/useEditorEventListener.js");
14
17
  const _viewdesc = require("../viewdesc.js");
15
18
  function _getRequireWildcardCache(nodeInterop) {
16
19
  if (typeof WeakMap !== "function") return null;
@@ -55,9 +58,15 @@ function _interop_require_wildcard(obj, nodeInterop) {
55
58
  }
56
59
  function TrailingHackView(param) {
57
60
  let { getPos } = param;
61
+ const [shouldRender, setShouldRender] = (0, _react.useState)(true);
62
+ const [shouldReinsert, setShouldReinsert] = (0, _react.useState)(false);
58
63
  const { siblingsRef, parentRef } = (0, _react.useContext)(_ChildDescriptionsContext.ChildDescriptionsContext);
59
64
  const viewDescRef = (0, _react.useRef)(null);
60
65
  const ref = (0, _react.useRef)(null);
66
+ const preservedRef = (0, _react.useRef)(ref.current);
67
+ if (ref.current) {
68
+ preservedRef.current = ref.current;
69
+ }
61
70
  (0, _useClientLayoutEffect.useClientLayoutEffect)(()=>{
62
71
  const siblings = siblingsRef.current;
63
72
  return ()=>{
@@ -83,6 +92,67 @@ function TrailingHackView(param) {
83
92
  }
84
93
  siblingsRef.current.sort(_viewdesc.sortViewDescs);
85
94
  });
95
+ // At the start of a composition, the browser will automatically delete
96
+ // the trailing hack br element. We need to unmount ourselves _before_
97
+ // that happens, so that React doesn't try to remove the already-removed
98
+ // br node when this component gets unmounted
99
+ (0, _useEditorEventListener.useEditorEventListener)("compositionstart", (view)=>{
100
+ const { from } = view.state.selection;
101
+ if (from === getPos()) {
102
+ setShouldRender(false);
103
+ setShouldReinsert(true);
104
+ }
105
+ });
106
+ // Chrome and Safari will cancel/mangle the composition if the br element isn't
107
+ // still in the DOM after the compositionstart event. We manually add it
108
+ // back to the DOM, without React managing it, so that it can be removed
109
+ // again by the browser when it starts the composition.
110
+ (0, _useClientLayoutEffect.useClientLayoutEffect)(()=>{
111
+ if (!shouldReinsert) return;
112
+ const preservedHack = preservedRef.current;
113
+ if (!preservedHack) return;
114
+ if (!viewDescRef.current) return;
115
+ const { parent } = viewDescRef.current;
116
+ if (!parent) return;
117
+ const dom = parent.contentDOM;
118
+ if (!dom) return;
119
+ preservedHack.pmViewDesc = undefined;
120
+ const index = parent.children.indexOf(viewDescRef.current);
121
+ if (index === 0) {
122
+ dom.appendChild(preservedHack);
123
+ } else {
124
+ dom.insertBefore(preservedHack, dom.childNodes.item(index));
125
+ }
126
+ return ()=>{
127
+ try {
128
+ dom.removeChild(preservedHack);
129
+ } catch {
130
+ // It may have already been removed by the browser during
131
+ // the composition, but if we get unmounted before that happens,
132
+ // we need to remove it ourselves
133
+ }
134
+ };
135
+ }, [
136
+ shouldReinsert
137
+ ]);
138
+ // We need to run the same composition check when we first get mounted,
139
+ // in case we got mounted in the same render batch as the beginning of
140
+ // a composition
141
+ (0, _useEditorEffect.useEditorEffect)((view)=>{
142
+ if (!(view instanceof _ReactEditorView.ReactEditorView)) return;
143
+ if (!view.compositionStarting) return;
144
+ const { from } = view.state.selection;
145
+ if (from === getPos()) {
146
+ setShouldRender(false);
147
+ }
148
+ }, [
149
+ getPos
150
+ ]);
151
+ (0, _useEditorEventListener.useEditorEventListener)("compositionend", ()=>{
152
+ setShouldRender(true);
153
+ setShouldReinsert(false);
154
+ });
155
+ if (!shouldRender) return null;
86
156
  return /*#__PURE__*/ _react.default.createElement("br", {
87
157
  ref: ref,
88
158
  className: "ProseMirror-trailingBreak"
@@ -90,6 +90,8 @@ function WidgetView(param) {
90
90
  ref: domRef,
91
91
  widget: widget,
92
92
  getPos: getPos,
93
- contentEditable: false
93
+ ...!widget.type.spec.raw && {
94
+ contentEditable: false
95
+ }
94
96
  });
95
97
  }
@@ -15,5 +15,7 @@ const ChildDescriptionsContext = (0, _react.createContext)({
15
15
  },
16
16
  siblingsRef: {
17
17
  current: []
18
- }
18
+ },
19
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
20
+ findCompositionDOM: ()=>{}
19
21
  });
@@ -123,17 +123,12 @@ function insertAhead(array, i, deco) {
123
123
  array.splice(i, 0, deco);
124
124
  }
125
125
  const ViewDecorationsCache = new WeakMap();
126
- function viewDecorations(view, cursorWrapper) {
126
+ function viewDecorations(view) {
127
127
  const found = [];
128
128
  view.someProp("decorations", (f)=>{
129
129
  const result = f(view.state);
130
130
  if (result && result != empty) found.push(result);
131
131
  });
132
- if (cursorWrapper) {
133
- found.push(_prosemirrorview.DecorationSet.create(view.state.doc, [
134
- cursorWrapper
135
- ]));
136
- }
137
132
  const previous = ViewDecorationsCache.get(view);
138
133
  if (!previous) {
139
134
  const result = DecorationGroup.from(found);
@@ -27,23 +27,16 @@ function useEditor(mount, options) {
27
27
  }
28
28
  }
29
29
  const flushSyncRef = (0, _react.useRef)(true);
30
- const [cursorWrapper, _setCursorWrapper] = (0, _react.useState)(null);
31
30
  const forceUpdate = (0, _useForceUpdate.useForceUpdate)();
32
31
  const defaultState = options.defaultState ?? _constants.EMPTY_STATE;
33
32
  const [_state, setState] = (0, _react.useState)(defaultState);
34
33
  const state = options.state ?? _state;
35
34
  const { handleDOMEvents, registerEventListener, unregisterEventListener } = (0, _useComponentEventListeners.useComponentEventListeners)(options.handleDOMEvents);
36
- const setCursorWrapper = (0, _react.useCallback)((deco)=>{
37
- (0, _reactdom.flushSync)(()=>{
38
- _setCursorWrapper(deco);
39
- });
40
- }, []);
41
35
  const plugins = (0, _react.useMemo)(()=>[
42
36
  ...options.plugins ?? [],
43
- (0, _beforeInputPlugin.beforeInputPlugin)(setCursorWrapper)
37
+ (0, _beforeInputPlugin.beforeInputPlugin)()
44
38
  ], [
45
- options.plugins,
46
- setCursorWrapper
39
+ options.plugins
47
40
  ]);
48
41
  const dispatchTransaction = (0, _react.useCallback)(function dispatchTransaction(tr) {
49
42
  if (flushSyncRef.current) {
@@ -122,7 +115,6 @@ function useEditor(mount, options) {
122
115
  ]);
123
116
  return {
124
117
  editor,
125
- cursorWrapper,
126
118
  state
127
119
  };
128
120
  }