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

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 (39) hide show
  1. package/dist/cjs/ReactEditorView.js +0 -2
  2. package/dist/cjs/components/ChildNodeViews.js +9 -14
  3. package/dist/cjs/components/CursorWrapper.js +9 -6
  4. package/dist/cjs/components/TextNodeView.js +38 -109
  5. package/dist/cjs/components/TrailingHackView.js +0 -29
  6. package/dist/cjs/components/WidgetView.js +1 -0
  7. package/dist/cjs/hooks/useComponentEventListeners.js +6 -14
  8. package/dist/cjs/hooks/useNodeViewDescription.js +16 -37
  9. package/dist/cjs/plugins/beforeInputPlugin.js +43 -41
  10. package/dist/cjs/plugins/componentEventListeners.js +2 -9
  11. package/dist/cjs/tiptap/utils/ssrJSDOMPatch.js +59 -0
  12. package/dist/cjs/viewdesc.js +3 -10
  13. package/dist/esm/ReactEditorView.js +0 -2
  14. package/dist/esm/components/ChildNodeViews.js +9 -14
  15. package/dist/esm/components/CursorWrapper.js +10 -7
  16. package/dist/esm/components/TextNodeView.js +38 -109
  17. package/dist/esm/components/TrailingHackView.js +1 -30
  18. package/dist/esm/components/WidgetView.js +1 -0
  19. package/dist/esm/hooks/useComponentEventListeners.js +6 -14
  20. package/dist/esm/hooks/useNodeViewDescription.js +17 -38
  21. package/dist/esm/plugins/beforeInputPlugin.js +43 -41
  22. package/dist/esm/plugins/componentEventListeners.js +2 -9
  23. package/dist/esm/tiptap/hooks/useTiptapEditor.js +7 -1
  24. package/dist/esm/tiptap/utils/ssrJSDOMPatch.js +56 -0
  25. package/dist/esm/viewdesc.js +3 -10
  26. package/dist/tsconfig.tsbuildinfo +1 -1
  27. package/dist/types/ReactEditorView.d.ts +0 -4
  28. package/dist/types/components/CursorWrapper.d.ts +2 -4
  29. package/dist/types/components/TextNodeView.d.ts +4 -14
  30. package/dist/types/components/TrailingHackView.d.ts +1 -1
  31. package/dist/types/components/WidgetViewComponentProps.d.ts +4 -3
  32. package/dist/types/constants.d.ts +1 -1
  33. package/dist/types/hooks/useComponentEventListeners.d.ts +1 -1
  34. package/dist/types/plugins/componentEventListeners.d.ts +2 -3
  35. package/dist/types/props.d.ts +26 -26
  36. package/dist/types/tiptap/hooks/useTiptapEditor.d.ts +7 -0
  37. package/dist/types/tiptap/utils/ssrJSDOMPatch.d.ts +1 -0
  38. package/dist/types/viewdesc.d.ts +2 -2
  39. package/package.json +1 -2
@@ -33,7 +33,6 @@ let ReactEditorView = class ReactEditorView extends _prosemirrorview.EditorView
33
33
  nextProps;
34
34
  prevState;
35
35
  _destroyed;
36
- deferPendingEffects;
37
36
  constructor(place, props){
38
37
  // Prevent the base class from destroying the React-managed nodes.
39
38
  // Restore them below after invoking the base class constructor.
@@ -86,7 +85,6 @@ let ReactEditorView = class ReactEditorView extends _prosemirrorview.EditorView
86
85
  // @ts-expect-error this violates the typing but class does it, too.
87
86
  this.docView = null;
88
87
  this._destroyed = false;
89
- this.deferPendingEffects = false;
90
88
  }
91
89
  get props() {
92
90
  return this.nextProps;
@@ -110,18 +110,13 @@ 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(_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
- });
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
125
120
  });
126
121
  }) : /*#__PURE__*/ _react.default.createElement(_NodeView.NodeView, {
127
122
  key: child.key,
@@ -404,14 +399,14 @@ const ChildNodeViews = /*#__PURE__*/ (0, _react.memo)(function ChildNodeViews(pa
404
399
  component: _SeparatorHackView.SeparatorHackView,
405
400
  marks: [],
406
401
  offset: lastChild?.offset ?? 0,
407
- index: (lastChild?.index ?? 0) + 1,
402
+ index: (lastChild?.index ?? 0) + 2,
408
403
  key: "trailing-hack-img"
409
404
  }, {
410
405
  type: "hack",
411
406
  component: _TrailingHackView.TrailingHackView,
412
407
  marks: [],
413
408
  offset: lastChild?.offset ?? 0,
414
- index: (lastChild?.index ?? 0) + 2,
409
+ index: (lastChild?.index ?? 0) + 1,
415
410
  key: "trailing-hack-br"
416
411
  });
417
412
  }
@@ -54,7 +54,6 @@ 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);
58
57
  const innerRef = (0, _react.useRef)(null);
59
58
  (0, _react.useImperativeHandle)(ref, ()=>{
60
59
  return innerRef.current;
@@ -66,18 +65,22 @@ const CursorWrapper = /*#__PURE__*/ (0, _react.forwardRef)(function CursorWrappe
66
65
  // @ts-expect-error Internal property - domSelection
67
66
  const domSel = view.domSelection();
68
67
  const node = innerRef.current;
69
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
70
- domSel.collapse(node.parentNode, (0, _dom.domIndex)(node) + 1);
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
+ }
71
75
  // @ts-expect-error Internal property - domObserver
72
76
  view.domObserver.connectSelection();
73
- setShouldRender(false);
74
77
  }, []);
75
- return shouldRender ? /*#__PURE__*/ _react.default.createElement("img", {
78
+ return /*#__PURE__*/ _react.default.createElement("img", {
76
79
  ref: innerRef,
77
80
  className: "ProseMirror-separator",
78
81
  // eslint-disable-next-line react/no-unknown-property
79
82
  "mark-placeholder": "true",
80
83
  alt: "",
81
84
  ...props
82
- }) : null;
85
+ });
83
86
  });
@@ -8,10 +8,8 @@ Object.defineProperty(exports, "TextNodeView", {
8
8
  return TextNodeView;
9
9
  }
10
10
  });
11
- const _prosemirrorstate = require("prosemirror-state");
12
11
  const _prosemirrorview = require("prosemirror-view");
13
12
  const _react = require("react");
14
- const _ReactEditorView = require("../ReactEditorView.js");
15
13
  const _findDOMNode = require("../findDOMNode.js");
16
14
  const _viewdesc = require("../viewdesc.js");
17
15
  const _ChildNodeViews = require("./ChildNodeViews.js");
@@ -40,141 +38,72 @@ function shallowEqual(objA, objB) {
40
38
  let TextNodeView = class TextNodeView extends _react.Component {
41
39
  viewDescRef = null;
42
40
  renderRef = null;
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 || to > pos + node.nodeSize) {
59
- return false;
60
- }
61
- return this.containsCompositionNodeText;
62
- }
63
- handleCompositionEnd = ()=>{
64
- if (!this.wasProtecting) return;
65
- this.forceUpdate();
66
- return;
67
- };
68
- create() {
41
+ updateEffect() {
69
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
70
45
  const dom = (0, _findDOMNode.findDOMNode)(this);
71
- if (!dom && !view.composing) return null;
72
- let textNode = dom;
73
- while(textNode?.firstChild){
74
- textNode = textNode.firstChild;
75
- }
76
- if (!(textNode instanceof Text)) {
77
- textNode = null;
78
- }
79
- let viewDesc;
80
- if (this.shouldProtect(this.props)) {
81
- viewDesc = new _viewdesc.CompositionViewDesc(parentRef.current, getPos, // If we can't
82
- // actually find the correct DOM nodes from here (
83
- // which is the case in a composition in a newly
84
- // created text node), we let our parent do it.
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.
85
54
  // Passing a valid element here just so that the
86
55
  // ViewDesc constructor doesn't blow up.
87
- dom ?? document.createElement("div"), textNode ?? document.createTextNode(node.text ?? ""), node.text ?? "");
88
- } else {
89
- if (!dom || !textNode) return null;
90
- viewDesc = new _viewdesc.TextViewDesc(parentRef.current, [], getPos, node, decorations, _prosemirrorview.DecorationSet.empty, dom, textNode);
91
- }
92
- siblingsRef.current.push(viewDesc);
93
- siblingsRef.current.sort(_viewdesc.sortViewDescs);
94
- return viewDesc;
95
- }
96
- update() {
97
- const { view, node, decorations } = this.props;
98
- if (!(view instanceof _ReactEditorView.ReactEditorView)) return false;
99
- const viewDesc = this.viewDescRef;
100
- if (!viewDesc) return false;
101
- if (this.shouldProtect(this.props) !== viewDesc instanceof _viewdesc.CompositionViewDesc) {
102
- return false;
103
- }
104
- if (viewDesc instanceof _viewdesc.CompositionViewDesc) return false;
105
- const dom = (0, _findDOMNode.findDOMNode)(this);
106
- if (!dom || dom !== viewDesc.dom) return false;
107
- if (!dom.contains(viewDesc.nodeDOM)) return false;
108
- return viewDesc.matchesNode(node, decorations, _prosemirrorview.DecorationSet.empty) || viewDesc.update(node, decorations, _prosemirrorview.DecorationSet.empty, view);
109
- }
110
- destroy() {
111
- const viewDesc = this.viewDescRef;
112
- if (!viewDesc) return;
113
- viewDesc.destroy();
114
- const siblings = this.props.siblingsRef.current;
115
- if (siblings.includes(viewDesc)) {
116
- const index = siblings.indexOf(viewDesc);
117
- siblings.splice(index, 1);
56
+ document.createElement("div"), document.createTextNode(node.text ?? ""), node.text ?? "");
57
+ return;
118
58
  }
119
- }
120
- updateEffect() {
121
- if (!this.update()) {
122
- this.destroy();
123
- this.viewDescRef = this.create();
59
+ let textNode = dom;
60
+ while(textNode.firstChild){
61
+ textNode = textNode.firstChild;
124
62
  }
125
- const { view, node } = this.props;
126
- if (!(view instanceof _ReactEditorView.ReactEditorView)) {
127
- this.containsCompositionNodeText = true;
128
- return;
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;
129
74
  }
130
- const textNode = view.input.compositionNode;
131
- if (!textNode) {
132
- this.containsCompositionNodeText = true;
133
- return;
75
+ if (!siblingsRef.current.includes(this.viewDescRef)) {
76
+ siblingsRef.current.push(this.viewDescRef);
134
77
  }
135
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
136
- const text = textNode.nodeValue;
137
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
138
- this.containsCompositionNodeText = node.text === text;
78
+ siblingsRef.current.sort(_viewdesc.sortViewDescs);
139
79
  }
140
80
  shouldComponentUpdate(nextProps) {
141
- // When leaving the protected state, force a re-render so React's
142
- // virtual DOM resyncs with whatever the IME wrote into the real DOM
143
- // while we were returning a stale renderRef.
144
- if (this.wasProtecting && !this.shouldProtect(nextProps)) {
145
- return true;
146
- }
147
81
  return !shallowEqual(this.props, nextProps);
148
82
  }
149
83
  componentDidMount() {
150
- this.viewDescRef = null;
151
- // After a composition, force an update so that we re-check whether we need
152
- // to be protecting our rendered content and allow React to re-sync with the
153
- // DOM.
154
- const { registerEventListener } = this.props;
155
- registerEventListener("compositionend", this.handleCompositionEnd);
156
84
  this.updateEffect();
157
85
  }
158
86
  componentDidUpdate() {
159
87
  this.updateEffect();
160
88
  }
161
89
  componentWillUnmount() {
162
- const { unregisterEventListener } = this.props;
163
- unregisterEventListener("compositionend", this.handleCompositionEnd);
164
- this.destroy();
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
96
  }
166
97
  render() {
167
- const { node, decorations } = this.props;
98
+ const { view, getPos, node, decorations } = this.props;
168
99
  // During a composition, it's crucial that we don't try to
169
100
  // update the DOM that the user is working in. If there's
170
101
  // an active composition and the selection is in this node,
171
102
  // we freeze the DOM of this element so that it doesn't
172
103
  // interrupt the composition
173
- if (this.shouldProtect(this.props)) {
174
- this.wasProtecting = true;
104
+ if (view.composing && view.state.selection.from >= getPos() && view.state.selection.from <= getPos() + node.nodeSize) {
175
105
  return this.renderRef;
176
106
  }
177
- this.wasProtecting = false;
178
107
  this.renderRef = decorations.reduce(_ChildNodeViews.wrapInDeco, node.text);
179
108
  return this.renderRef;
180
109
  }
@@ -11,8 +11,6 @@ Object.defineProperty(exports, "TrailingHackView", {
11
11
  const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
12
12
  const _ChildDescriptionsContext = require("../contexts/ChildDescriptionsContext.js");
13
13
  const _useClientLayoutEffect = require("../hooks/useClientLayoutEffect.js");
14
- const _useEditorEffect = require("../hooks/useEditorEffect.js");
15
- const _useEditorEventListener = require("../hooks/useEditorEventListener.js");
16
14
  const _viewdesc = require("../viewdesc.js");
17
15
  function _getRequireWildcardCache(nodeInterop) {
18
16
  if (typeof WeakMap !== "function") return null;
@@ -57,7 +55,6 @@ function _interop_require_wildcard(obj, nodeInterop) {
57
55
  }
58
56
  function TrailingHackView(param) {
59
57
  let { getPos } = param;
60
- const [shouldRender, setShouldRender] = (0, _react.useState)(true);
61
58
  const { siblingsRef, parentRef } = (0, _react.useContext)(_ChildDescriptionsContext.ChildDescriptionsContext);
62
59
  const viewDescRef = (0, _react.useRef)(null);
63
60
  const ref = (0, _react.useRef)(null);
@@ -86,32 +83,6 @@ function TrailingHackView(param) {
86
83
  }
87
84
  siblingsRef.current.sort(_viewdesc.sortViewDescs);
88
85
  });
89
- // At the start of a composition, the browser will automatically delete
90
- // the trailing hack br element. We need to unmount ourselves _before_
91
- // that happens, so that React doesn't try to remove the already-removed
92
- // br node when this component gets unmounted
93
- (0, _useEditorEventListener.useEditorEventListener)("compositionstart", (view)=>{
94
- const { from } = view.state.selection;
95
- if (from === getPos()) {
96
- setShouldRender(false);
97
- }
98
- });
99
- // We need to run the same composition check when we first get mounted,
100
- // in case we got mounted in the same render batch as the beginning of
101
- // a composition
102
- (0, _useEditorEffect.useEditorEffect)((view)=>{
103
- if (!view.composing) return;
104
- const { from } = view.state.selection;
105
- if (from === getPos()) {
106
- setShouldRender(false);
107
- }
108
- }, [
109
- getPos
110
- ]);
111
- (0, _useEditorEventListener.useEditorEventListener)("compositionend", ()=>{
112
- setShouldRender(true);
113
- });
114
- if (!shouldRender) return null;
115
86
  return /*#__PURE__*/ _react.default.createElement("br", {
116
87
  ref: ref,
117
88
  className: "ProseMirror-trailingBreak"
@@ -78,6 +78,7 @@ function WidgetView(param) {
78
78
  viewDescRef.current.parent = parentRef.current;
79
79
  viewDescRef.current.widget = widget;
80
80
  viewDescRef.current.dom = domRef.current;
81
+ viewDescRef.current.dom.pmViewDesc = viewDescRef.current;
81
82
  }
82
83
  if (!siblingsRef.current.includes(viewDescRef.current)) {
83
84
  siblingsRef.current.push(viewDescRef.current);
@@ -10,16 +10,8 @@ Object.defineProperty(exports, "useComponentEventListeners", {
10
10
  });
11
11
  const _react = require("react");
12
12
  const _reactdom = require("react-dom");
13
- function useComponentEventListeners(existingHandlers) {
14
- const [registry, setRegistry] = (0, _react.useState)(new Map(Object.entries(existingHandlers ?? {}).map((param)=>{
15
- let [eventName, handler] = param;
16
- return [
17
- eventName,
18
- handler ? [
19
- handler
20
- ] : []
21
- ];
22
- })));
13
+ function useComponentEventListeners(handleDOMEventsProp) {
14
+ const [registry, setRegistry] = (0, _react.useState)(new Map());
23
15
  const registerEventListener = (0, _react.useCallback)((eventType, handler)=>{
24
16
  const handlers = registry.get(eventType) ?? [];
25
17
  handlers.unshift(handler);
@@ -37,19 +29,19 @@ function useComponentEventListeners(existingHandlers) {
37
29
  registry
38
30
  ]);
39
31
  (0, _react.useLayoutEffect)(()=>{
40
- if (!existingHandlers) return;
41
- for (const [eventType, handler] of Object.entries(existingHandlers)){
32
+ if (!handleDOMEventsProp) return;
33
+ for (const [eventType, handler] of Object.entries(handleDOMEventsProp)){
42
34
  if (!handler) return;
43
35
  registerEventListener(eventType, handler);
44
36
  }
45
37
  return ()=>{
46
- for (const [eventType, handler] of Object.entries(existingHandlers)){
38
+ for (const [eventType, handler] of Object.entries(handleDOMEventsProp)){
47
39
  if (!handler) return;
48
40
  unregisterEventListener(eventType, handler);
49
41
  }
50
42
  };
51
43
  }, [
52
- existingHandlers,
44
+ handleDOMEventsProp,
53
45
  registerEventListener,
54
46
  unregisterEventListener
55
47
  ]);
@@ -10,10 +10,8 @@ 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");
14
13
  const _ChildDescriptionsContext = require("../contexts/ChildDescriptionsContext.js");
15
14
  const _EditorContext = require("../contexts/EditorContext.js");
16
- const _ReactWidgetType = require("../decorations/ReactWidgetType.js");
17
15
  const _viewdesc = require("../viewdesc.js");
18
16
  const _useClientLayoutEffect = require("./useClientLayoutEffect.js");
19
17
  const _useEffectEvent = require("./useEffectEvent.js");
@@ -143,44 +141,25 @@ function useNodeViewDescription(getDOM, getContentDOM, constructor, props) {
143
141
  children.sort(_viewdesc.sortViewDescs);
144
142
  for (const child of children){
145
143
  child.parent = viewDesc;
146
- }
147
- if (!props.node.isTextblock) return;
148
- // Because TextNodeViews can't locate the DOM nodes
149
- // for compositions, we need to override them here
150
- if (!viewDescRef.current?.contentDOM) return;
151
- const compositionChildIndex = children.findIndex((child)=>child instanceof _viewdesc.CompositionViewDesc);
152
- if (compositionChildIndex === -1) return;
153
- const compositionViewDesc = children[compositionChildIndex];
154
- if (!(compositionViewDesc instanceof _viewdesc.CompositionViewDesc)) return;
155
- let compositionTopDOM = null;
156
- let search = children[compositionChildIndex - 1];
157
- while(search instanceof _viewdesc.MarkViewDesc){
158
- search = search.children[0];
159
- }
160
- if (search instanceof _viewdesc.WidgetViewDesc && search.widget.type instanceof _ReactWidgetType.ReactWidgetType && search.widget.type.Component === _CursorWrapper.CursorWrapper) {
161
- compositionTopDOM = search.dom.nextSibling;
162
- } else {
163
- for (const childNode of viewDescRef.current.contentDOM.childNodes){
164
- if (children.every((child)=>child.dom !== childNode)) {
165
- compositionTopDOM = childNode;
166
- break;
144
+ // Because TextNodeViews can't locate the DOM nodes
145
+ // 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;
167
152
  }
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);
168
161
  }
169
162
  }
170
- if (!compositionTopDOM) return;
171
- let textDOM = compositionTopDOM;
172
- while(textDOM.firstChild){
173
- textDOM = textDOM.firstChild;
174
- }
175
- if (!textDOM || !(textDOM instanceof Text)) {
176
- console.error(compositionTopDOM, textDOM);
177
- throw new Error(`Started a composition but couldn't find the text node it belongs to.`);
178
- }
179
- compositionViewDesc.dom = compositionTopDOM;
180
- compositionViewDesc.textDOM = textDOM;
181
- compositionViewDesc.text = textDOM.data;
182
- compositionViewDesc.textDOM.pmViewDesc = compositionViewDesc;
183
- view.input.compositionNodes.push(compositionViewDesc);
184
163
  });
185
164
  const childContextValue = (0, _react.useMemo)(()=>({
186
165
  parentRef: viewDescRef,
@@ -10,7 +10,6 @@ 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");
14
13
  const _CursorWrapper = require("../components/CursorWrapper.js");
15
14
  const _ReactWidgetType = require("../decorations/ReactWidgetType.js");
16
15
  function insertText(view, eventData) {
@@ -57,41 +56,68 @@ function handleGapCursorComposition(view) {
57
56
  }
58
57
  function beforeInputPlugin(setCursorWrapper) {
59
58
  let compositionMarks = null;
59
+ let precompositionSnapshot = null;
60
60
  return new _prosemirrorstate.Plugin({
61
61
  props: {
62
62
  handleDOMEvents: {
63
63
  compositionstart (view) {
64
- if (!(view instanceof _ReactEditorView.ReactEditorView)) return false;
65
- view.input.composing = true;
66
- compositionMarks = view.state.storedMarks;
67
- const tr = view.state.tr.deleteSelection().setStoredMarks(null);
68
- view.dispatch(tr);
64
+ compositionMarks = view.state.storedMarks ?? view.state.selection.$from.marks();
65
+ view.dispatch(view.state.tr.deleteSelection());
69
66
  handleGapCursorComposition(view);
70
67
  const { state } = view;
71
- if (compositionMarks?.length) {
68
+ const $pos = state.selection.$from;
69
+ if (compositionMarks) {
72
70
  setCursorWrapper((0, _ReactWidgetType.widget)(state.selection.from, _CursorWrapper.CursorWrapper, {
73
71
  key: "cursor-wrapper",
74
- marks: compositionMarks,
75
- side: 1
72
+ marks: compositionMarks
76
73
  }));
77
74
  }
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
+ view.input.composing = true;
78
85
  return true;
79
86
  },
80
87
  compositionupdate () {
81
88
  return true;
82
89
  },
83
90
  compositionend (view, event) {
84
- if (!(view instanceof _ReactEditorView.ReactEditorView)) return false;
85
- if (!view.composing) return false;
91
+ // @ts-expect-error Internal property - input
86
92
  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
+ }
87
118
  compositionMarks = null;
119
+ precompositionSnapshot = null;
88
120
  setCursorWrapper(null);
89
- if (view.input.compositionNode && !view.input.compositionNode.pmViewDesc) {
90
- view.input.compositionNode.remove();
91
- }
92
- view.input.compositionEndedAt = event.timeStamp;
93
- view.input.compositionNode = null;
94
- view.input.compositionID++;
95
121
  return true;
96
122
  },
97
123
  beforeinput (view, event) {
@@ -140,30 +166,6 @@ function beforeInputPlugin(setCursorWrapper) {
140
166
  insertText(view, event.data);
141
167
  break;
142
168
  }
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
- view.dom.addEventListener("input", ()=>{
161
- view.dispatch(tr);
162
- }, {
163
- once: true
164
- });
165
- break;
166
- }
167
169
  case "deleteWordBackward":
168
170
  case "deleteHardLineBackward":
169
171
  case "deleteSoftLineBackward":
@@ -8,7 +8,6 @@ Object.defineProperty(exports, "componentEventListeners", {
8
8
  return componentEventListeners;
9
9
  }
10
10
  });
11
- const _prosemirrorstate = require("prosemirror-state");
12
11
  const _reactdom = require("react-dom");
13
12
  function componentEventListeners(eventHandlerRegistry) {
14
13
  const domEventHandlers = {};
@@ -17,7 +16,7 @@ function componentEventListeners(eventHandlerRegistry) {
17
16
  for (const handler of handlers){
18
17
  let handled = false;
19
18
  (0, _reactdom.unstable_batchedUpdates)(()=>{
20
- handled = !!handler.call(this, view, event);
19
+ handled = !!handler(view, event);
21
20
  });
22
21
  if (handled || event.defaultPrevented) return true;
23
22
  }
@@ -25,11 +24,5 @@ function componentEventListeners(eventHandlerRegistry) {
25
24
  }
26
25
  domEventHandlers[eventType] = handleEvent;
27
26
  }
28
- const plugin = new _prosemirrorstate.Plugin({
29
- key: new _prosemirrorstate.PluginKey("@handlewithcare/react-prosemirror/componentEventListeners"),
30
- props: {
31
- handleDOMEvents: domEventHandlers
32
- }
33
- });
34
- return plugin;
27
+ return domEventHandlers;
35
28
  }