@handlewithcare/react-prosemirror 3.1.0-tiptap.47 → 3.1.0-tiptap.48

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.
package/README.md CHANGED
@@ -837,10 +837,40 @@ significant contributors help shape our roadmap priorities.
837
837
  [Become a Sponsor](https://handlewithcare.dev/pitter-patter/#become-a-sponsor)
838
838
 
839
839
  <h3>Sponsors</h3>
840
- <p>
841
- <a href="https://www.moment.dev/"><img src="https://media.githubusercontent.com/media/handlewithcarecollective/pitter-patter-sponsors/main/logos/Moment.png" alt="Moment" height="128"></a>
842
- <a href="https://www.lingco.io/"><img src="https://media.githubusercontent.com/media/handlewithcarecollective/pitter-patter-sponsors/main/logos/Lingco.png" alt="Lingco" height="128"></a>
843
- <a href="https://dskrpt.de/"><img src="https://media.githubusercontent.com/media/handlewithcarecollective/pitter-patter-sponsors/main/logos/Dskrpt.png" alt="Dskrpt" height="128"></a>
844
- <a href="https://github.com/fastrepl/"><img src="https://media.githubusercontent.com/media/handlewithcarecollective/pitter-patter-sponsors/main/logos/Fastrepl.png" alt="Fastrepl" height="128"></a>
845
- </p>
840
+
841
+ <table>
842
+ <tbody>
843
+ <tr>
844
+ <td align="center" valign="top" >
845
+ <a href="https://moment.dev/">
846
+ <img src="https://media.githubusercontent.com/media/handlewithcarecollective/pitter-patter-sponsors/main/logos/Moment.png" alt="Moment" height="128">
847
+ <br>
848
+ Moment
849
+ </a>
850
+ </td>
851
+ <td align="center" valign="top" >
852
+ <a href="https://www.lingco.io/">
853
+ <img src="https://media.githubusercontent.com/media/handlewithcarecollective/pitter-patter-sponsors/main/logos/Lingco.png" alt="Lingco" height="128">
854
+ <br>
855
+ Lingco
856
+ </a>
857
+ </td>
858
+ <td align="center" valign="top" >
859
+ <a href="https://dskrpt.de/">
860
+ <img src="https://media.githubusercontent.com/media/handlewithcarecollective/pitter-patter-sponsors/main/logos/Dskrpt.png" alt="dskrpt" height="128">
861
+ <br>
862
+ dskrpt
863
+ </a>
864
+ </td>
865
+ <td align="center" valign="top" >
866
+ <a href="https://char.com/">
867
+ <img src="https://media.githubusercontent.com/media/handlewithcarecollective/pitter-patter-sponsors/main/logos/Fastrepl.png" alt="Fastrepl" height="128">
868
+ <br>
869
+ Fastrepl
870
+ </a>
871
+ </td>
872
+ </tr>
873
+ </tbody>
874
+ </table>
875
+
846
876
  <!--sponsorsend-->
@@ -33,6 +33,7 @@ let ReactEditorView = class ReactEditorView extends _prosemirrorview.EditorView
33
33
  nextProps;
34
34
  prevState;
35
35
  _destroyed;
36
+ deferPendingEffects;
36
37
  constructor(place, props){
37
38
  // Prevent the base class from destroying the React-managed nodes.
38
39
  // Restore them below after invoking the base class constructor.
@@ -85,6 +86,7 @@ let ReactEditorView = class ReactEditorView extends _prosemirrorview.EditorView
85
86
  // @ts-expect-error this violates the typing but class does it, too.
86
87
  this.docView = null;
87
88
  this._destroyed = false;
89
+ this.deferPendingEffects = false;
88
90
  }
89
91
  get props() {
90
92
  return this.nextProps;
@@ -110,13 +110,18 @@ const ChildView = /*#__PURE__*/ (0, _react.memo)(function ChildView(param) {
110
110
  key: child.key
111
111
  }, (param)=>{
112
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
113
+ return /*#__PURE__*/ _react.default.createElement(_EditorContext.EditorContext.Consumer, null, (param)=>{
114
+ let { registerEventListener, unregisterEventListener } = param;
115
+ return /*#__PURE__*/ _react.default.createElement(_TextNodeView.TextNodeView, {
116
+ view: view,
117
+ node: child.node,
118
+ getPos: getPos,
119
+ siblingsRef: siblingsRef,
120
+ parentRef: parentRef,
121
+ decorations: child.outerDeco,
122
+ registerEventListener: registerEventListener,
123
+ unregisterEventListener: unregisterEventListener
124
+ });
120
125
  });
121
126
  }) : /*#__PURE__*/ _react.default.createElement(_NodeView.NodeView, {
122
127
  key: child.key,
@@ -54,6 +54,7 @@ function _interop_require_wildcard(obj, nodeInterop) {
54
54
  }
55
55
  const CursorWrapper = /*#__PURE__*/ (0, _react.forwardRef)(function CursorWrapper(param, ref) {
56
56
  let { widget, getPos, ...props } = param;
57
+ const [shouldRender, setShouldRender] = (0, _react.useState)(true);
57
58
  const innerRef = (0, _react.useRef)(null);
58
59
  (0, _react.useImperativeHandle)(ref, ()=>{
59
60
  return innerRef.current;
@@ -65,22 +66,20 @@ const CursorWrapper = /*#__PURE__*/ (0, _react.forwardRef)(function CursorWrappe
65
66
  // @ts-expect-error Internal property - domSelection
66
67
  const domSel = view.domSelection();
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();
73
+ setTimeout(()=>{
74
+ setShouldRender(false);
75
+ });
77
76
  }, []);
78
- return /*#__PURE__*/ _react.default.createElement("img", {
77
+ return shouldRender ? /*#__PURE__*/ _react.default.createElement("img", {
79
78
  ref: innerRef,
80
79
  className: "ProseMirror-separator",
81
80
  // eslint-disable-next-line react/no-unknown-property
82
81
  "mark-placeholder": "true",
83
82
  alt: "",
84
83
  ...props
85
- });
84
+ }) : null;
86
85
  });
@@ -8,8 +8,10 @@ Object.defineProperty(exports, "TextNodeView", {
8
8
  return TextNodeView;
9
9
  }
10
10
  });
11
+ const _prosemirrorstate = require("prosemirror-state");
11
12
  const _prosemirrorview = require("prosemirror-view");
12
13
  const _react = require("react");
14
+ const _ReactEditorView = require("../ReactEditorView.js");
13
15
  const _findDOMNode = require("../findDOMNode.js");
14
16
  const _viewdesc = require("../viewdesc.js");
15
17
  const _ChildNodeViews = require("./ChildNodeViews.js");
@@ -38,72 +40,144 @@ function shallowEqual(objA, objB) {
38
40
  let TextNodeView = class TextNodeView extends _react.Component {
39
41
  viewDescRef = null;
40
42
  renderRef = null;
41
- updateEffect() {
43
+ wasProtecting = false;
44
+ containsCompositionNodeText = true;
45
+ // This is basically NodeViewDesc.localCompositionInfo
46
+ // from prosemirror-view. It's been slightly adjusted so that
47
+ // it can be used accurately during render, before we've
48
+ // necessarily found (or even let the browser create)
49
+ // view.input.compositionNode
50
+ shouldProtect(props) {
51
+ const { view, getPos, node } = props;
52
+ if (!(view instanceof _ReactEditorView.ReactEditorView)) return false;
53
+ if (!view.composing) {
54
+ return false;
55
+ }
56
+ const pos = getPos();
57
+ const { from, to } = view.state.selection;
58
+ if (!(view.state.selection instanceof _prosemirrorstate.TextSelection) || from < pos || // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
59
+ to > pos + node.nodeSize) {
60
+ return false;
61
+ }
62
+ return this.containsCompositionNodeText;
63
+ }
64
+ handleCompositionEnd = ()=>{
65
+ if (!this.wasProtecting) return;
66
+ this.forceUpdate();
67
+ return;
68
+ };
69
+ create() {
42
70
  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
45
71
  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;
58
- }
72
+ if (!dom && !view.composing) return null;
59
73
  let textNode = dom;
60
- while(textNode.firstChild){
74
+ while(textNode?.firstChild){
61
75
  textNode = textNode.firstChild;
62
76
  }
63
- if (!this.viewDescRef || this.viewDescRef instanceof _viewdesc.CompositionViewDesc) {
64
- this.viewDescRef = new _viewdesc.TextViewDesc(undefined, [], getPos, node, decorations, _prosemirrorview.DecorationSet.empty, dom, textNode);
65
- } 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;
77
+ if (!(textNode instanceof Text)) {
78
+ textNode = null;
74
79
  }
75
- if (!siblingsRef.current.includes(this.viewDescRef)) {
76
- siblingsRef.current.push(this.viewDescRef);
80
+ let viewDesc;
81
+ if (this.shouldProtect(this.props)) {
82
+ viewDesc = new _viewdesc.CompositionViewDesc(parentRef.current, getPos, // If we can't
83
+ // actually find the correct DOM nodes from here (
84
+ // which is the case in a composition in a newly
85
+ // created text node), we let our parent do it.
86
+ // Passing a valid element here just so that the
87
+ // ViewDesc constructor doesn't blow up.
88
+ dom ?? document.createElement("div"), textNode ?? document.createTextNode(node.text ?? ""), node.text ?? "");
89
+ } else {
90
+ if (!dom || !textNode) return null;
91
+ viewDesc = new _viewdesc.TextViewDesc(parentRef.current, [], getPos, node, decorations, _prosemirrorview.DecorationSet.empty, dom, textNode);
77
92
  }
93
+ siblingsRef.current.push(viewDesc);
78
94
  siblingsRef.current.sort(_viewdesc.sortViewDescs);
95
+ return viewDesc;
96
+ }
97
+ update() {
98
+ const { view, node, decorations } = this.props;
99
+ if (!(view instanceof _ReactEditorView.ReactEditorView)) return false;
100
+ const viewDesc = this.viewDescRef;
101
+ if (!viewDesc) return false;
102
+ if (this.shouldProtect(this.props) !== viewDesc instanceof _viewdesc.CompositionViewDesc) {
103
+ return false;
104
+ }
105
+ if (viewDesc instanceof _viewdesc.CompositionViewDesc) return false;
106
+ const dom = (0, _findDOMNode.findDOMNode)(this);
107
+ if (!dom || dom !== viewDesc.dom) return false;
108
+ if (!dom.contains(viewDesc.nodeDOM)) return false;
109
+ return viewDesc.matchesNode(node, decorations, _prosemirrorview.DecorationSet.empty) || viewDesc.update(node, decorations, _prosemirrorview.DecorationSet.empty, view);
110
+ }
111
+ destroy() {
112
+ const viewDesc = this.viewDescRef;
113
+ if (!viewDesc) return;
114
+ viewDesc.destroy();
115
+ const siblings = this.props.siblingsRef.current;
116
+ if (siblings.includes(viewDesc)) {
117
+ const index = siblings.indexOf(viewDesc);
118
+ siblings.splice(index, 1);
119
+ }
120
+ }
121
+ updateEffect() {
122
+ if (!this.update()) {
123
+ this.destroy();
124
+ this.viewDescRef = this.create();
125
+ }
126
+ setTimeout(()=>{
127
+ const { view, node } = this.props;
128
+ if (!(view instanceof _ReactEditorView.ReactEditorView)) {
129
+ this.containsCompositionNodeText = true;
130
+ return;
131
+ }
132
+ const textNode = view.input.compositionNode;
133
+ if (!textNode) {
134
+ this.containsCompositionNodeText = true;
135
+ return;
136
+ }
137
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
138
+ const text = textNode.nodeValue;
139
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
140
+ this.containsCompositionNodeText = node.text === text;
141
+ });
79
142
  }
80
143
  shouldComponentUpdate(nextProps) {
144
+ // When leaving the protected state, force a re-render so React's
145
+ // virtual DOM resyncs with whatever the IME wrote into the real DOM
146
+ // while we were returning a stale renderRef.
147
+ if (this.wasProtecting && !this.shouldProtect(nextProps)) {
148
+ return true;
149
+ }
81
150
  return !shallowEqual(this.props, nextProps);
82
151
  }
83
152
  componentDidMount() {
153
+ this.viewDescRef = null;
154
+ // After a composition, force an update so that we re-check whether we need
155
+ // to be protecting our rendered content and allow React to re-sync with the
156
+ // DOM.
157
+ const { registerEventListener } = this.props;
158
+ registerEventListener("compositionend", this.handleCompositionEnd);
84
159
  this.updateEffect();
85
160
  }
86
161
  componentDidUpdate() {
87
162
  this.updateEffect();
88
163
  }
89
164
  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);
95
- }
165
+ const { unregisterEventListener } = this.props;
166
+ unregisterEventListener("compositionend", this.handleCompositionEnd);
167
+ this.destroy();
96
168
  }
97
169
  render() {
98
- const { view, getPos, node, decorations } = this.props;
170
+ const { node, decorations } = this.props;
99
171
  // During a composition, it's crucial that we don't try to
100
172
  // update the DOM that the user is working in. If there's
101
173
  // an active composition and the selection is in this node,
102
174
  // we freeze the DOM of this element so that it doesn't
103
175
  // interrupt the composition
104
- if (view.composing && view.state.selection.from >= getPos() && view.state.selection.from <= getPos() + node.nodeSize) {
176
+ if (this.shouldProtect(this.props)) {
177
+ this.wasProtecting = true;
105
178
  return this.renderRef;
106
179
  }
180
+ this.wasProtecting = false;
107
181
  this.renderRef = decorations.reduce(_ChildNodeViews.wrapInDeco, node.text);
108
182
  return this.renderRef;
109
183
  }
@@ -103,11 +103,19 @@ function useEditor(mount, options) {
103
103
  // running effects. Running effects will reattach selection
104
104
  // change listeners if the EditorView has been destroyed.
105
105
  if (view instanceof _ReactEditorView.ReactEditorView && !view.isDestroyed) {
106
- // Plugins might dispatch transactions from their
107
- // view update lifecycle hooks
108
- flushSyncRef.current = false;
109
- view.commitPendingEffects();
110
- flushSyncRef.current = true;
106
+ if (view.deferPendingEffects) {
107
+ setTimeout(()=>{
108
+ flushSyncRef.current = false;
109
+ view.commitPendingEffects();
110
+ flushSyncRef.current = true;
111
+ });
112
+ } else {
113
+ // Plugins might dispatch transactions from their
114
+ // view update lifecycle hooks
115
+ flushSyncRef.current = false;
116
+ view.commitPendingEffects();
117
+ flushSyncRef.current = true;
118
+ }
111
119
  }
112
120
  });
113
121
  view.update(directEditorProps);
@@ -10,8 +10,10 @@ Object.defineProperty(exports, "useNodeViewDescription", {
10
10
  });
11
11
  const _react = require("react");
12
12
  const _ReactEditorView = require("../ReactEditorView.js");
13
+ const _CursorWrapper = require("../components/CursorWrapper.js");
13
14
  const _ChildDescriptionsContext = require("../contexts/ChildDescriptionsContext.js");
14
15
  const _EditorContext = require("../contexts/EditorContext.js");
16
+ const _ReactWidgetType = require("../decorations/ReactWidgetType.js");
15
17
  const _viewdesc = require("../viewdesc.js");
16
18
  const _useClientLayoutEffect = require("./useClientLayoutEffect.js");
17
19
  const _useEffectEvent = require("./useEffectEvent.js");
@@ -141,25 +143,46 @@ function useNodeViewDescription(getDOM, getContentDOM, constructor, props) {
141
143
  children.sort(_viewdesc.sortViewDescs);
142
144
  for (const child of children){
143
145
  child.parent = viewDesc;
146
+ }
147
+ setTimeout(()=>{
144
148
  // Because TextNodeViews can't locate the DOM nodes
145
149
  // for compositions, we need to override them here
146
- if (child instanceof _viewdesc.CompositionViewDesc) {
147
- const compositionTopDOM = viewDesc?.contentDOM?.firstChild;
148
- if (!compositionTopDOM) throw new Error(`Started a composition but couldn't find the text node it belongs to.`);
149
- let textDOM = compositionTopDOM;
150
- while(textDOM.firstChild){
151
- textDOM = textDOM.firstChild;
150
+ if (!viewDescRef.current?.contentDOM) return;
151
+ const children = viewDescRef.current?.children;
152
+ const compositionChildIndex = children.findIndex((child)=>child instanceof _viewdesc.CompositionViewDesc);
153
+ if (compositionChildIndex === -1) return;
154
+ const compositionViewDesc = children[compositionChildIndex];
155
+ if (!(compositionViewDesc instanceof _viewdesc.CompositionViewDesc)) return;
156
+ let compositionTopDOM = null;
157
+ let search = children[compositionChildIndex - 1];
158
+ while(search instanceof _viewdesc.MarkViewDesc){
159
+ search = search.children[0];
160
+ }
161
+ if (search instanceof _viewdesc.WidgetViewDesc && search.widget.type instanceof _ReactWidgetType.ReactWidgetType && search.widget.type.Component === _CursorWrapper.CursorWrapper) {
162
+ compositionTopDOM = search.dom.nextSibling;
163
+ } else {
164
+ for (const childNode of viewDescRef.current.contentDOM.childNodes){
165
+ if (children.every((child)=>child.dom !== childNode)) {
166
+ compositionTopDOM = childNode;
167
+ break;
168
+ }
152
169
  }
153
- if (!textDOM || !(textDOM instanceof Text)) throw new Error(`Started a composition but couldn't find the text node it belongs to.`);
154
- child.dom = compositionTopDOM;
155
- child.textDOM = textDOM;
156
- child.text = textDOM.data;
157
- child.textDOM.pmViewDesc = child;
158
- // It should not be possible to be in a composition because one could
159
- // not start between the renders that switch the view type.
160
- view.input.compositionNodes.push(child);
161
170
  }
162
- }
171
+ if (!compositionTopDOM) return;
172
+ let textDOM = compositionTopDOM;
173
+ while(textDOM.firstChild){
174
+ textDOM = textDOM.firstChild;
175
+ }
176
+ if (!textDOM || !(textDOM instanceof Text)) {
177
+ console.error(compositionTopDOM, textDOM);
178
+ throw new Error(`Started a composition but couldn't find the text node it belongs to.`);
179
+ }
180
+ compositionViewDesc.dom = compositionTopDOM;
181
+ compositionViewDesc.textDOM = textDOM;
182
+ compositionViewDesc.text = textDOM.data;
183
+ compositionViewDesc.textDOM.pmViewDesc = compositionViewDesc;
184
+ view.input.compositionNodes.push(compositionViewDesc);
185
+ });
163
186
  });
164
187
  const childContextValue = (0, _react.useMemo)(()=>({
165
188
  parentRef: viewDescRef,
@@ -10,6 +10,7 @@ Object.defineProperty(exports, "beforeInputPlugin", {
10
10
  });
11
11
  const _prosemirrormodel = require("prosemirror-model");
12
12
  const _prosemirrorstate = require("prosemirror-state");
13
+ const _ReactEditorView = require("../ReactEditorView.js");
13
14
  const _CursorWrapper = require("../components/CursorWrapper.js");
14
15
  const _ReactWidgetType = require("../decorations/ReactWidgetType.js");
15
16
  function insertText(view, eventData) {
@@ -56,31 +57,23 @@ function handleGapCursorComposition(view) {
56
57
  }
57
58
  function beforeInputPlugin(setCursorWrapper) {
58
59
  let compositionMarks = null;
59
- let precompositionSnapshot = null;
60
60
  return new _prosemirrorstate.Plugin({
61
61
  props: {
62
62
  handleDOMEvents: {
63
63
  compositionstart (view) {
64
- compositionMarks = view.state.storedMarks ?? view.state.selection.$from.marks();
64
+ if (!(view instanceof _ReactEditorView.ReactEditorView)) return false;
65
+ compositionMarks = view.state.storedMarks;
65
66
  view.dispatch(view.state.tr.deleteSelection());
66
67
  handleGapCursorComposition(view);
67
68
  const { state } = view;
68
- const $pos = state.selection.$from;
69
- if (compositionMarks) {
69
+ // const $pos = state.selection.$from;
70
+ if (compositionMarks?.length) {
70
71
  setCursorWrapper((0, _ReactWidgetType.widget)(state.selection.from, _CursorWrapper.CursorWrapper, {
71
72
  key: "cursor-wrapper",
72
- marks: compositionMarks
73
+ marks: compositionMarks,
74
+ side: 1
73
75
  }));
74
76
  }
75
- // Snapshot the siblings of the node that contains the
76
- // current cursor. We'll restore this later, so that React
77
- // doesn't panic about unknown DOM nodes.
78
- const { node: parent } = view.domAtPos($pos.pos);
79
- precompositionSnapshot = [];
80
- for (const node of parent.childNodes){
81
- precompositionSnapshot.push(node);
82
- }
83
- // @ts-expect-error Internal property - input
84
77
  view.input.composing = true;
85
78
  return true;
86
79
  },
@@ -88,36 +81,17 @@ function beforeInputPlugin(setCursorWrapper) {
88
81
  return true;
89
82
  },
90
83
  compositionend (view, event) {
91
- // @ts-expect-error Internal property - input
84
+ if (!(view instanceof _ReactEditorView.ReactEditorView)) return false;
85
+ if (!view.composing) return false;
92
86
  view.input.composing = false;
93
- const { state } = view;
94
- const { node: parent } = view.domAtPos(state.selection.from);
95
- if (precompositionSnapshot) {
96
- // Restore the snapshot of the parent node's children
97
- // from before the composition started. This gives us a
98
- // clean slate from which to dispatch our transaction
99
- // and trigger a React update.
100
- precompositionSnapshot.forEach((prevNode, i)=>{
101
- if (parent.childNodes.length <= i) {
102
- parent.appendChild(prevNode);
103
- return;
104
- }
105
- parent.replaceChild(prevNode, parent.childNodes.item(i));
106
- });
107
- if (parent.childNodes.length > precompositionSnapshot.length) {
108
- for(let i = precompositionSnapshot.length; i < parent.childNodes.length; i++){
109
- parent.removeChild(parent.childNodes.item(i));
110
- }
111
- }
112
- }
113
- if (event.data) {
114
- insertText(view, event.data, {
115
- marks: compositionMarks
116
- });
117
- }
118
87
  compositionMarks = null;
119
- precompositionSnapshot = null;
120
88
  setCursorWrapper(null);
89
+ if (view.input.compositionNode && !view.input.compositionNode.pmViewDesc && (view.input.compositionNode instanceof Text || view.input.compositionNode instanceof Element)) {
90
+ view.input.compositionNode.remove();
91
+ }
92
+ view.input.compositionEndedAt = event.timeStamp;
93
+ view.input.compositionNode = null;
94
+ view.input.compositionID++;
121
95
  return true;
122
96
  },
123
97
  beforeinput (view, event) {
@@ -166,6 +140,38 @@ function beforeInputPlugin(setCursorWrapper) {
166
140
  insertText(view, event.data);
167
141
  break;
168
142
  }
143
+ case "insertCompositionText":
144
+ {
145
+ const { tr } = view.state;
146
+ // There's always a range on insertCompositionText beforeinput events
147
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
148
+ const range = event.getTargetRanges()[0];
149
+ const start = view.posAtDOM(range.startContainer, range.startOffset);
150
+ const end = view.posAtDOM(range.endContainer, range.endOffset, 1);
151
+ if (view.state.doc.textBetween(start, end, "**", "*") === event.data) {
152
+ return;
153
+ }
154
+ if (event.data) {
155
+ if (compositionMarks) tr.ensureMarks(compositionMarks);
156
+ tr.insertText(event.data, start, end);
157
+ } else {
158
+ tr.delete(start, end);
159
+ }
160
+ // When we insert the text that corresponds to an ongoing composition,
161
+ // the relevant TextNodeView will pause re-rendering so that React doesn't
162
+ // clobber the composition in the DOM. This means that we have to wait for
163
+ // the browser to update the DOM itself before attempting to reconcile
164
+ // the selection, so we specifically defer pending effects to the next
165
+ // macro task
166
+ if (view instanceof _ReactEditorView.ReactEditorView) {
167
+ view.deferPendingEffects = true;
168
+ }
169
+ view.dispatch(tr);
170
+ if (view instanceof _ReactEditorView.ReactEditorView) {
171
+ view.deferPendingEffects = false;
172
+ }
173
+ break;
174
+ }
169
175
  case "deleteWordBackward":
170
176
  case "deleteHardLineBackward":
171
177
  case "deleteSoftLineBackward":
@@ -266,7 +266,9 @@ let ViewDesc = class ViewDesc {
266
266
  prev = i ? this.children[i - 1] : null;
267
267
  if (!prev || prev.dom.parentNode == this.contentDOM) break;
268
268
  }
269
- if (prev && side && enter && !prev.border && !prev.domAtom) return prev.domFromPos(prev.size, side);
269
+ if (prev && side && enter && !prev.border && !prev.domAtom) {
270
+ return prev.domFromPos(prev.size, side);
271
+ }
270
272
  return {
271
273
  node: this.contentDOM,
272
274
  offset: prev ? (0, _dom.domIndex)(prev.dom) + 1 : 0
@@ -401,7 +403,9 @@ let ViewDesc = class ViewDesc {
401
403
  const after = selRange.focusNode.childNodes[selRange.focusOffset];
402
404
  if (after && after.contentEditable == "false") force = true;
403
405
  }
404
- if (!(force || brKludge && _browser.browser.safari) && (0, _dom.isEquivalentPosition)(anchorDOM.node, anchorDOM.offset, selRange.anchorNode, selRange.anchorOffset) && (0, _dom.isEquivalentPosition)(headDOM.node, headDOM.offset, selRange.focusNode, selRange.focusOffset)) return;
406
+ if (!(force || brKludge && _browser.browser.safari) && (0, _dom.isEquivalentPosition)(anchorDOM.node, anchorDOM.offset, selRange.anchorNode, selRange.anchorOffset) && (0, _dom.isEquivalentPosition)(headDOM.node, headDOM.offset, selRange.focusNode, selRange.focusOffset)) {
407
+ return;
408
+ }
405
409
  // Selection.extend can be used to create an 'inverted' selection
406
410
  // (one where the focus is before the anchor), but not all
407
411
  // browsers support it yet.
@@ -666,7 +670,10 @@ let TextViewDesc = class TextViewDesc extends NodeViewDesc {
666
670
  skip: skip || true
667
671
  };
668
672
  }
669
- update(_node, _outerDeco, _innerDeco, _view) {
673
+ update(node, outerDeco, _innerDeco, _view) {
674
+ if (this.dirty == NODE_DIRTY || this.dirty != NOT_DIRTY && !this.inParent() || !node.sameMarkup(this.node)) return false;
675
+ this.updateOuterDeco(outerDeco);
676
+ this.node = node;
670
677
  this.dirty = NOT_DIRTY;
671
678
  return true;
672
679
  }
@@ -33,6 +33,7 @@ function changedNodeViews(a, b) {
33
33
  nextProps;
34
34
  prevState;
35
35
  _destroyed;
36
+ deferPendingEffects;
36
37
  constructor(place, props){
37
38
  // Prevent the base class from destroying the React-managed nodes.
38
39
  // Restore them below after invoking the base class constructor.
@@ -85,6 +86,7 @@ function changedNodeViews(a, b) {
85
86
  // @ts-expect-error this violates the typing but class does it, too.
86
87
  this.docView = null;
87
88
  this._destroyed = false;
89
+ this.deferPendingEffects = false;
88
90
  }
89
91
  get props() {
90
92
  return this.nextProps;
@@ -51,13 +51,18 @@ const ChildView = /*#__PURE__*/ memo(function ChildView(param) {
51
51
  key: child.key
52
52
  }, (param)=>{
53
53
  let { siblingsRef, parentRef } = param;
54
- return /*#__PURE__*/ React.createElement(TextNodeView, {
55
- view: view,
56
- node: child.node,
57
- getPos: getPos,
58
- siblingsRef: siblingsRef,
59
- parentRef: parentRef,
60
- decorations: child.outerDeco
54
+ return /*#__PURE__*/ React.createElement(EditorContext.Consumer, null, (param)=>{
55
+ let { registerEventListener, unregisterEventListener } = param;
56
+ return /*#__PURE__*/ React.createElement(TextNodeView, {
57
+ view: view,
58
+ node: child.node,
59
+ getPos: getPos,
60
+ siblingsRef: siblingsRef,
61
+ parentRef: parentRef,
62
+ decorations: child.outerDeco,
63
+ registerEventListener: registerEventListener,
64
+ unregisterEventListener: unregisterEventListener
65
+ });
61
66
  });
62
67
  }) : /*#__PURE__*/ React.createElement(NodeView, {
63
68
  key: child.key,