@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.
- package/dist/cjs/ReactEditorView.js +6 -0
- package/dist/cjs/components/ChildNodeViews.js +16 -10
- package/dist/cjs/components/CursorWrapper.js +3 -7
- package/dist/cjs/components/ProseMirror.js +5 -3
- package/dist/cjs/components/TextNodeView.js +260 -47
- package/dist/cjs/components/TrailingHackView.js +70 -0
- package/dist/cjs/components/WidgetView.js +3 -1
- package/dist/cjs/contexts/ChildDescriptionsContext.js +3 -1
- package/dist/cjs/decorations/viewDecorations.js +1 -6
- package/dist/cjs/hooks/useEditor.js +2 -10
- package/dist/cjs/hooks/useMarkViewDescription.js +61 -3
- package/dist/cjs/hooks/useNodeViewDescription.js +64 -21
- package/dist/cjs/plugins/beforeInputPlugin.js +147 -45
- package/dist/cjs/plugins/reactKeys.js +21 -14
- package/dist/cjs/viewdesc.js +52 -4
- package/dist/esm/ReactEditorView.js +6 -0
- package/dist/esm/components/ChildNodeViews.js +17 -11
- package/dist/esm/components/CursorWrapper.js +3 -7
- package/dist/esm/components/ProseMirror.js +5 -3
- package/dist/esm/components/TextNodeView.js +209 -45
- package/dist/esm/components/TrailingHackView.js +71 -1
- package/dist/esm/components/WidgetView.js +3 -1
- package/dist/esm/contexts/ChildDescriptionsContext.js +3 -1
- package/dist/esm/decorations/viewDecorations.js +1 -6
- package/dist/esm/hooks/useEditor.js +2 -10
- package/dist/esm/hooks/useMarkViewDescription.js +62 -4
- package/dist/esm/hooks/useNodeViewDescription.js +65 -22
- package/dist/esm/plugins/beforeInputPlugin.js +147 -45
- package/dist/esm/plugins/reactKeys.js +21 -14
- package/dist/esm/viewdesc.js +51 -4
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/types/ReactEditorView.d.ts +6 -1
- package/dist/types/components/TextNodeView.d.ts +20 -5
- package/dist/types/components/TrailingHackView.d.ts +1 -1
- package/dist/types/components/__tests__/ProseMirror.composition.test.d.ts +17 -1
- package/dist/types/contexts/ChildDescriptionsContext.d.ts +5 -3
- package/dist/types/decorations/viewDecorations.d.ts +2 -2
- package/dist/types/hooks/useEditor.d.ts +1 -2
- package/dist/types/hooks/useMarkViewDescription.d.ts +2 -1
- package/dist/types/hooks/useNodeViewDescription.d.ts +2 -1
- package/dist/types/plugins/beforeInputPlugin.d.ts +1 -2
- package/dist/types/plugins/reactKeys.d.ts +9 -8
- package/dist/types/viewdesc.d.ts +3 -2
- package/package.json +3 -1
|
@@ -13,14 +13,10 @@ export const CursorWrapper = /*#__PURE__*/ forwardRef(function CursorWrapper(par
|
|
|
13
13
|
view.domObserver.disconnectSelection();
|
|
14
14
|
// @ts-expect-error Internal property - domSelection
|
|
15
15
|
const domSel = view.domSelection();
|
|
16
|
+
if (!domSel.isCollapsed) return;
|
|
16
17
|
const node = innerRef.current;
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
20
|
-
domSel.collapse(node.parentNode, domIndex(node) + 1);
|
|
21
|
-
} else {
|
|
22
|
-
domSel.collapse(node, 0);
|
|
23
|
-
}
|
|
18
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
19
|
+
domSel.collapse(node.parentNode, domIndex(node) + 1);
|
|
24
20
|
// @ts-expect-error Internal property - domObserver
|
|
25
21
|
view.domObserver.connectSelection();
|
|
26
22
|
}, []);
|
|
@@ -17,12 +17,14 @@ const rootChildDescriptionsContextValue = {
|
|
|
17
17
|
},
|
|
18
18
|
siblingsRef: {
|
|
19
19
|
current: []
|
|
20
|
-
}
|
|
20
|
+
},
|
|
21
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
22
|
+
findCompositionDOM: ()=>{}
|
|
21
23
|
};
|
|
22
24
|
function ProseMirrorInner(param) {
|
|
23
25
|
let { children, nodeViewComponents, markViewComponents, ...props } = param;
|
|
24
26
|
const [mount, setMount] = useState(null);
|
|
25
|
-
const { editor,
|
|
27
|
+
const { editor, state } = useEditor(mount, props);
|
|
26
28
|
const nodeViewConstructors = editor.view.nodeViews;
|
|
27
29
|
const nodeViewContextValue = useMemo(()=>{
|
|
28
30
|
return {
|
|
@@ -39,7 +41,7 @@ function ProseMirrorInner(param) {
|
|
|
39
41
|
]);
|
|
40
42
|
const node = state.doc;
|
|
41
43
|
const decorations = computeDocDeco(editor.view);
|
|
42
|
-
const innerDecorations = viewDecorations(editor.view
|
|
44
|
+
const innerDecorations = viewDecorations(editor.view);
|
|
43
45
|
const docNodeViewContextValue = useMemo(()=>({
|
|
44
46
|
setMount,
|
|
45
47
|
node,
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
import { TextSelection } from "prosemirror-state";
|
|
1
2
|
import { DecorationSet } from "prosemirror-view";
|
|
2
|
-
import { Component } from "react";
|
|
3
|
+
import React, { Component, createRef, useReducer } from "react";
|
|
4
|
+
import { ReactEditorView } from "../ReactEditorView.js";
|
|
3
5
|
import { findDOMNode } from "../findDOMNode.js";
|
|
4
|
-
import { CompositionViewDesc, TextViewDesc, sortViewDescs } from "../viewdesc.js";
|
|
6
|
+
import { CompositionViewDesc, TextViewDesc, findTextInFragment, sortViewDescs } from "../viewdesc.js";
|
|
5
7
|
import { wrapInDeco } from "./ChildNodeViews.js";
|
|
6
8
|
function shallowEqual(objA, objB) {
|
|
7
9
|
if (objA === objB) {
|
|
@@ -26,75 +28,237 @@ function shallowEqual(objA, objB) {
|
|
|
26
28
|
return true;
|
|
27
29
|
}
|
|
28
30
|
export class TextNodeView extends Component {
|
|
29
|
-
viewDescRef =
|
|
30
|
-
renderRef =
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
31
|
+
viewDescRef = createMutRef();
|
|
32
|
+
renderRef = createMutRef();
|
|
33
|
+
wasProtecting = createMutRef();
|
|
34
|
+
containsCompositionNodeText = createMutRef();
|
|
35
|
+
// This is basically NodeViewDesc.localCompositionInfo
|
|
36
|
+
// from prosemirror-view. It's been slightly adjusted so that
|
|
37
|
+
// it can be used accurately during render, before we've
|
|
38
|
+
// necessarily found (or even let the browser create)
|
|
39
|
+
// view.input.compositionNode
|
|
40
|
+
shouldProtect(props) {
|
|
41
|
+
const { view, getPos, node } = props;
|
|
42
|
+
if (!(view instanceof ReactEditorView)) return false;
|
|
43
|
+
if (!view.composing) {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
const viewDesc = this.viewDescRef.current;
|
|
47
|
+
// If our DOM text node IS the IME's composition node, protect regardless
|
|
48
|
+
// of where the PM selection currently is. The IME may have replaced a
|
|
49
|
+
// selection that included us — moving the PM selection past us — but our
|
|
50
|
+
// DOM is still part of the in-progress composition. Until another
|
|
51
|
+
// TextNodeView's findCompositionDOM displaces us into a comp desc, only
|
|
52
|
+
// our own protect/no-update is preventing React from rewriting the IME's
|
|
53
|
+
// text. (When we *are* displaced, viewDesc is already a CompositionViewDesc
|
|
54
|
+
// and the existing position-based logic doesn't apply anyway.)
|
|
55
|
+
const ownsCompositionNode = viewDesc instanceof TextViewDesc && viewDesc.nodeDOM === view.input.compositionNode;
|
|
56
|
+
if (!ownsCompositionNode) {
|
|
57
|
+
const pos = getPos();
|
|
58
|
+
const { from, to } = view.state.selection;
|
|
59
|
+
if (!(view.state.selection instanceof TextSelection) || from <= pos || to > pos + node.nodeSize) {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return !!this.containsCompositionNodeText.current;
|
|
64
|
+
}
|
|
65
|
+
handleCompositionEnd = ()=>{
|
|
66
|
+
if (!this.wasProtecting.current) return;
|
|
67
|
+
const { view } = this.props;
|
|
68
|
+
if (!(view instanceof ReactEditorView)) return;
|
|
69
|
+
// If the IME detached our DOM during composition, React's fiber is now
|
|
70
|
+
// wired to a detached node and will silently send all subsequent updates
|
|
71
|
+
// into the void. Re-attach the orphan (so the upcoming unmount's
|
|
72
|
+
// removeChild has something to remove), then ask our wrapper to mint a
|
|
73
|
+
// new key — that forces React to drop this fiber and mount a fresh one
|
|
74
|
+
// whose stateNode it creates from the current render output.
|
|
35
75
|
const dom = findDOMNode(this);
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
this.viewDescRef = new CompositionViewDesc(parentRef.current, getPos, // These are just placeholders/dummies. We can't
|
|
42
|
-
// actually find the correct DOM nodes from here,
|
|
43
|
-
// so we let our parent do it.
|
|
44
|
-
// Passing a valid element here just so that the
|
|
45
|
-
// ViewDesc constructor doesn't blow up.
|
|
46
|
-
document.createElement("div"), document.createTextNode(node.text ?? ""), node.text ?? "");
|
|
47
|
-
return;
|
|
76
|
+
if (dom instanceof HTMLElement && !view.dom.contains(dom)) {
|
|
77
|
+
this.reattachAtCorrectPosition(dom);
|
|
78
|
+
this.props.forceRemount();
|
|
79
|
+
} else {
|
|
80
|
+
this.forceUpdate();
|
|
48
81
|
}
|
|
82
|
+
};
|
|
83
|
+
create() {
|
|
84
|
+
const { view, decorations, siblingsRef, parentRef, getPos, node } = this.props;
|
|
85
|
+
const dom = findDOMNode(this);
|
|
86
|
+
if (!dom && !view.composing) return null;
|
|
49
87
|
let textNode = dom;
|
|
50
|
-
while(textNode
|
|
88
|
+
while(textNode?.firstChild){
|
|
51
89
|
textNode = textNode.firstChild;
|
|
52
90
|
}
|
|
53
|
-
if (!
|
|
54
|
-
|
|
91
|
+
if (!(textNode instanceof Text)) {
|
|
92
|
+
textNode = null;
|
|
93
|
+
}
|
|
94
|
+
let viewDesc;
|
|
95
|
+
if (this.shouldProtect(this.props)) {
|
|
96
|
+
viewDesc = new CompositionViewDesc(parentRef.current, getPos, // If we can't
|
|
97
|
+
// actually find the correct DOM nodes from here (
|
|
98
|
+
// which is the case in a composition in a newly
|
|
99
|
+
// created text node), we let our parent do it.
|
|
100
|
+
// Passing a valid element here just so that the
|
|
101
|
+
// ViewDesc constructor doesn't blow up.
|
|
102
|
+
dom ?? document.createElement("div"), textNode ?? document.createTextNode(node.text ?? ""), node.text ?? "");
|
|
55
103
|
} else {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
this.viewDescRef.node = node;
|
|
59
|
-
this.viewDescRef.outerDeco = decorations;
|
|
60
|
-
this.viewDescRef.innerDeco = DecorationSet.empty;
|
|
61
|
-
this.viewDescRef.dom = dom;
|
|
62
|
-
this.viewDescRef.dom.pmViewDesc = this.viewDescRef;
|
|
63
|
-
this.viewDescRef.nodeDOM = textNode;
|
|
64
|
-
}
|
|
65
|
-
if (!siblingsRef.current.includes(this.viewDescRef)) {
|
|
66
|
-
siblingsRef.current.push(this.viewDescRef);
|
|
104
|
+
if (!dom || !textNode) return null;
|
|
105
|
+
viewDesc = new TextViewDesc(parentRef.current, [], getPos, node, decorations, DecorationSet.empty, dom, textNode);
|
|
67
106
|
}
|
|
107
|
+
siblingsRef.current.push(viewDesc);
|
|
68
108
|
siblingsRef.current.sort(sortViewDescs);
|
|
109
|
+
if (viewDesc instanceof CompositionViewDesc) {
|
|
110
|
+
this.props.findCompositionDOM(viewDesc);
|
|
111
|
+
}
|
|
112
|
+
return viewDesc;
|
|
113
|
+
}
|
|
114
|
+
update() {
|
|
115
|
+
const { view, node, decorations } = this.props;
|
|
116
|
+
if (!(view instanceof ReactEditorView)) return false;
|
|
117
|
+
const viewDesc = this.viewDescRef.current;
|
|
118
|
+
if (!viewDesc) return false;
|
|
119
|
+
// Don't force destroy/recreate just because we transitioned into protect
|
|
120
|
+
// mode. If our DOM text node is the IME's composition node, we want to
|
|
121
|
+
// keep the TextViewDesc alive so the new composition-text TextNodeView's
|
|
122
|
+
// findCompositionDOM second pass can find us, validate the size mismatch,
|
|
123
|
+
// and displace us into a properly-sized CompositionViewDesc. If we
|
|
124
|
+
// destroyed here, create() would put a wrong-size CompositionViewDesc on
|
|
125
|
+
// T and pre-empt that displacement.
|
|
126
|
+
const ownsCompositionNode = viewDesc instanceof TextViewDesc && viewDesc.nodeDOM === view.input.compositionNode;
|
|
127
|
+
if (!ownsCompositionNode && this.shouldProtect(this.props) !== viewDesc instanceof CompositionViewDesc) {
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
if (viewDesc instanceof CompositionViewDesc) return false;
|
|
131
|
+
const dom = findDOMNode(this);
|
|
132
|
+
if (!dom || dom !== viewDesc.dom) return false;
|
|
133
|
+
if (!dom.contains(viewDesc.nodeDOM)) return false;
|
|
134
|
+
return viewDesc.matchesNode(node, decorations, DecorationSet.empty) || viewDesc.update(node, decorations, DecorationSet.empty, view);
|
|
135
|
+
}
|
|
136
|
+
destroy() {
|
|
137
|
+
const viewDesc = this.viewDescRef.current;
|
|
138
|
+
if (!viewDesc) return;
|
|
139
|
+
viewDesc.destroy();
|
|
140
|
+
const siblings = this.props.siblingsRef.current;
|
|
141
|
+
if (siblings.includes(viewDesc)) {
|
|
142
|
+
const index = siblings.indexOf(viewDesc);
|
|
143
|
+
siblings.splice(index, 1);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
updateEffect() {
|
|
147
|
+
if (!this.update()) {
|
|
148
|
+
this.destroy();
|
|
149
|
+
this.viewDescRef.current = this.create();
|
|
150
|
+
}
|
|
151
|
+
const { view } = this.props;
|
|
152
|
+
if (!(view instanceof ReactEditorView)) {
|
|
153
|
+
this.containsCompositionNodeText.current = true;
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
const textNode = view.input.compositionNode;
|
|
157
|
+
if (!textNode) {
|
|
158
|
+
this.containsCompositionNodeText.current = true;
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
// Resolve the parent textblock containing this text node and ask
|
|
162
|
+
// findTextInFragment whether the IME text node's *current* content can be
|
|
163
|
+
// placed somewhere in the textblock's PM content overlapping the
|
|
164
|
+
// selection. If it can, the composition is still consistent with PM state
|
|
165
|
+
// and we should protect. If it can't (e.g. a remote change overwrote the
|
|
166
|
+
// composing region), PM and the DOM have diverged — abandon protection
|
|
167
|
+
// so the re-render can rewrite the DOM and cancel the composition.
|
|
168
|
+
const $pos = view.state.doc.resolve(this.props.getPos());
|
|
169
|
+
const parent = $pos.parent;
|
|
170
|
+
if (!parent.inlineContent) {
|
|
171
|
+
this.containsCompositionNodeText.current = false;
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
const parentStart = $pos.start();
|
|
175
|
+
const { from, to } = view.state.selection;
|
|
176
|
+
const textPos = findTextInFragment(parent.content, // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
177
|
+
textNode.nodeValue, from - parentStart, to - parentStart);
|
|
178
|
+
this.containsCompositionNodeText.current = textPos >= 0;
|
|
69
179
|
}
|
|
70
180
|
shouldComponentUpdate(nextProps) {
|
|
181
|
+
// When leaving the protected state, force a re-render so React's
|
|
182
|
+
// virtual DOM resyncs with whatever the IME wrote into the real DOM
|
|
183
|
+
// while we were returning a stale renderRef.
|
|
184
|
+
if (this.wasProtecting.current && !this.shouldProtect(nextProps)) {
|
|
185
|
+
return true;
|
|
186
|
+
}
|
|
71
187
|
return !shallowEqual(this.props, nextProps);
|
|
72
188
|
}
|
|
189
|
+
constructor(props){
|
|
190
|
+
super(props);
|
|
191
|
+
this.viewDescRef.current = null;
|
|
192
|
+
this.renderRef.current = null;
|
|
193
|
+
this.wasProtecting.current = false;
|
|
194
|
+
this.containsCompositionNodeText.current = true;
|
|
195
|
+
}
|
|
73
196
|
componentDidMount() {
|
|
197
|
+
this.containsCompositionNodeText.current = true;
|
|
198
|
+
// After a composition, force an update so that we re-check whether we need
|
|
199
|
+
// to be protecting our rendered content and allow React to re-sync with the
|
|
200
|
+
// DOM.
|
|
201
|
+
const { registerEventListener } = this.props;
|
|
202
|
+
registerEventListener("compositionend", this.handleCompositionEnd);
|
|
203
|
+
this.viewDescRef.current = this.create();
|
|
74
204
|
this.updateEffect();
|
|
75
205
|
}
|
|
76
206
|
componentDidUpdate() {
|
|
77
207
|
this.updateEffect();
|
|
208
|
+
const { view } = this.props;
|
|
209
|
+
if (!(view instanceof ReactEditorView)) return;
|
|
78
210
|
}
|
|
79
|
-
|
|
80
|
-
const
|
|
81
|
-
if (!
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
211
|
+
reattachAtCorrectPosition(dom) {
|
|
212
|
+
const viewDesc = this.viewDescRef.current;
|
|
213
|
+
if (!viewDesc) return;
|
|
214
|
+
let host = viewDesc.parent;
|
|
215
|
+
while(host && !host.contentDOM)host = host.parent;
|
|
216
|
+
if (!host?.contentDOM) return;
|
|
217
|
+
const siblings = viewDesc.parent?.children ?? [];
|
|
218
|
+
const idx = siblings.indexOf(viewDesc);
|
|
219
|
+
let nextDom = null;
|
|
220
|
+
for(let i = idx + 1; i < siblings.length; i++){
|
|
221
|
+
const sib = siblings[i];
|
|
222
|
+
if (sib?.dom && sib.dom.parentNode === host.contentDOM) {
|
|
223
|
+
nextDom = sib.dom;
|
|
224
|
+
break;
|
|
225
|
+
}
|
|
85
226
|
}
|
|
227
|
+
host.contentDOM.insertBefore(dom, nextDom);
|
|
228
|
+
}
|
|
229
|
+
componentWillUnmount() {
|
|
230
|
+
const { unregisterEventListener } = this.props;
|
|
231
|
+
unregisterEventListener("compositionend", this.handleCompositionEnd);
|
|
232
|
+
this.destroy();
|
|
86
233
|
}
|
|
87
234
|
render() {
|
|
88
|
-
const {
|
|
235
|
+
const { node, decorations } = this.props;
|
|
89
236
|
// During a composition, it's crucial that we don't try to
|
|
90
237
|
// update the DOM that the user is working in. If there's
|
|
91
238
|
// an active composition and the selection is in this node,
|
|
92
239
|
// we freeze the DOM of this element so that it doesn't
|
|
93
240
|
// interrupt the composition
|
|
94
|
-
if (
|
|
95
|
-
|
|
241
|
+
if (this.shouldProtect(this.props)) {
|
|
242
|
+
this.wasProtecting.current = true;
|
|
243
|
+
return this.renderRef.current;
|
|
96
244
|
}
|
|
97
|
-
this.
|
|
98
|
-
|
|
245
|
+
this.wasProtecting.current = false;
|
|
246
|
+
this.renderRef.current = decorations.reduce(wrapInDeco, node.text);
|
|
247
|
+
return this.renderRef.current;
|
|
99
248
|
}
|
|
100
249
|
}
|
|
250
|
+
/**
|
|
251
|
+
* createRef returns a RefObject, even though the docs
|
|
252
|
+
* say that it's acceptible to manage the ref's value
|
|
253
|
+
* yourself.
|
|
254
|
+
*/ function createMutRef() {
|
|
255
|
+
return /*#__PURE__*/ createRef();
|
|
256
|
+
}
|
|
257
|
+
export function RemountableTextNodeView(props) {
|
|
258
|
+
const [key, forceRemount] = useReducer((x)=>x + 1, 0);
|
|
259
|
+
return /*#__PURE__*/ React.createElement(TextNodeView, {
|
|
260
|
+
key: key,
|
|
261
|
+
forceRemount: forceRemount,
|
|
262
|
+
...props
|
|
263
|
+
});
|
|
264
|
+
}
|
|
@@ -1,12 +1,21 @@
|
|
|
1
|
-
import React, { useContext, useRef } from "react";
|
|
1
|
+
import React, { useContext, useRef, useState } from "react";
|
|
2
|
+
import { ReactEditorView } from "../ReactEditorView.js";
|
|
2
3
|
import { ChildDescriptionsContext } from "../contexts/ChildDescriptionsContext.js";
|
|
3
4
|
import { useClientLayoutEffect } from "../hooks/useClientLayoutEffect.js";
|
|
5
|
+
import { useEditorEffect } from "../hooks/useEditorEffect.js";
|
|
6
|
+
import { useEditorEventListener } from "../hooks/useEditorEventListener.js";
|
|
4
7
|
import { TrailingHackViewDesc, sortViewDescs } from "../viewdesc.js";
|
|
5
8
|
export function TrailingHackView(param) {
|
|
6
9
|
let { getPos } = param;
|
|
10
|
+
const [shouldRender, setShouldRender] = useState(true);
|
|
11
|
+
const [shouldReinsert, setShouldReinsert] = useState(false);
|
|
7
12
|
const { siblingsRef, parentRef } = useContext(ChildDescriptionsContext);
|
|
8
13
|
const viewDescRef = useRef(null);
|
|
9
14
|
const ref = useRef(null);
|
|
15
|
+
const preservedRef = useRef(ref.current);
|
|
16
|
+
if (ref.current) {
|
|
17
|
+
preservedRef.current = ref.current;
|
|
18
|
+
}
|
|
10
19
|
useClientLayoutEffect(()=>{
|
|
11
20
|
const siblings = siblingsRef.current;
|
|
12
21
|
return ()=>{
|
|
@@ -32,6 +41,67 @@ export function TrailingHackView(param) {
|
|
|
32
41
|
}
|
|
33
42
|
siblingsRef.current.sort(sortViewDescs);
|
|
34
43
|
});
|
|
44
|
+
// At the start of a composition, the browser will automatically delete
|
|
45
|
+
// the trailing hack br element. We need to unmount ourselves _before_
|
|
46
|
+
// that happens, so that React doesn't try to remove the already-removed
|
|
47
|
+
// br node when this component gets unmounted
|
|
48
|
+
useEditorEventListener("compositionstart", (view)=>{
|
|
49
|
+
const { from } = view.state.selection;
|
|
50
|
+
if (from === getPos()) {
|
|
51
|
+
setShouldRender(false);
|
|
52
|
+
setShouldReinsert(true);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
// Chrome and Safari will cancel/mangle the composition if the br element isn't
|
|
56
|
+
// still in the DOM after the compositionstart event. We manually add it
|
|
57
|
+
// back to the DOM, without React managing it, so that it can be removed
|
|
58
|
+
// again by the browser when it starts the composition.
|
|
59
|
+
useClientLayoutEffect(()=>{
|
|
60
|
+
if (!shouldReinsert) return;
|
|
61
|
+
const preservedHack = preservedRef.current;
|
|
62
|
+
if (!preservedHack) return;
|
|
63
|
+
if (!viewDescRef.current) return;
|
|
64
|
+
const { parent } = viewDescRef.current;
|
|
65
|
+
if (!parent) return;
|
|
66
|
+
const dom = parent.contentDOM;
|
|
67
|
+
if (!dom) return;
|
|
68
|
+
preservedHack.pmViewDesc = undefined;
|
|
69
|
+
const index = parent.children.indexOf(viewDescRef.current);
|
|
70
|
+
if (index === 0) {
|
|
71
|
+
dom.appendChild(preservedHack);
|
|
72
|
+
} else {
|
|
73
|
+
dom.insertBefore(preservedHack, dom.childNodes.item(index));
|
|
74
|
+
}
|
|
75
|
+
return ()=>{
|
|
76
|
+
try {
|
|
77
|
+
dom.removeChild(preservedHack);
|
|
78
|
+
} catch {
|
|
79
|
+
// It may have already been removed by the browser during
|
|
80
|
+
// the composition, but if we get unmounted before that happens,
|
|
81
|
+
// we need to remove it ourselves
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
}, [
|
|
85
|
+
shouldReinsert
|
|
86
|
+
]);
|
|
87
|
+
// We need to run the same composition check when we first get mounted,
|
|
88
|
+
// in case we got mounted in the same render batch as the beginning of
|
|
89
|
+
// a composition
|
|
90
|
+
useEditorEffect((view)=>{
|
|
91
|
+
if (!(view instanceof ReactEditorView)) return;
|
|
92
|
+
if (!view.compositionStarting) return;
|
|
93
|
+
const { from } = view.state.selection;
|
|
94
|
+
if (from === getPos()) {
|
|
95
|
+
setShouldRender(false);
|
|
96
|
+
}
|
|
97
|
+
}, [
|
|
98
|
+
getPos
|
|
99
|
+
]);
|
|
100
|
+
useEditorEventListener("compositionend", ()=>{
|
|
101
|
+
setShouldRender(true);
|
|
102
|
+
setShouldReinsert(false);
|
|
103
|
+
});
|
|
104
|
+
if (!shouldRender) return null;
|
|
35
105
|
return /*#__PURE__*/ React.createElement("br", {
|
|
36
106
|
ref: ref,
|
|
37
107
|
className: "ProseMirror-trailingBreak"
|
|
@@ -123,17 +123,12 @@ const ViewDecorationsCache = new WeakMap();
|
|
|
123
123
|
*
|
|
124
124
|
* This makes it safe to call in a React render function, even
|
|
125
125
|
* if its result is used in a dependencies array for a hook.
|
|
126
|
-
*/ export function viewDecorations(view
|
|
126
|
+
*/ export 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(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);
|
|
@@ -25,23 +25,16 @@ let didWarnValueDefaultValue = false;
|
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
27
|
const flushSyncRef = useRef(true);
|
|
28
|
-
const [cursorWrapper, _setCursorWrapper] = useState(null);
|
|
29
28
|
const forceUpdate = useForceUpdate();
|
|
30
29
|
const defaultState = options.defaultState ?? EMPTY_STATE;
|
|
31
30
|
const [_state, setState] = useState(defaultState);
|
|
32
31
|
const state = options.state ?? _state;
|
|
33
32
|
const { handleDOMEvents, registerEventListener, unregisterEventListener } = useComponentEventListeners(options.handleDOMEvents);
|
|
34
|
-
const setCursorWrapper = useCallback((deco)=>{
|
|
35
|
-
flushSync(()=>{
|
|
36
|
-
_setCursorWrapper(deco);
|
|
37
|
-
});
|
|
38
|
-
}, []);
|
|
39
33
|
const plugins = useMemo(()=>[
|
|
40
34
|
...options.plugins ?? [],
|
|
41
|
-
beforeInputPlugin(
|
|
35
|
+
beforeInputPlugin()
|
|
42
36
|
], [
|
|
43
|
-
options.plugins
|
|
44
|
-
setCursorWrapper
|
|
37
|
+
options.plugins
|
|
45
38
|
]);
|
|
46
39
|
const dispatchTransaction = useCallback(function dispatchTransaction(tr) {
|
|
47
40
|
if (flushSyncRef.current) {
|
|
@@ -120,7 +113,6 @@ let didWarnValueDefaultValue = false;
|
|
|
120
113
|
]);
|
|
121
114
|
return {
|
|
122
115
|
editor,
|
|
123
|
-
cursorWrapper,
|
|
124
116
|
state
|
|
125
117
|
};
|
|
126
118
|
}
|
|
@@ -2,7 +2,7 @@ import { useCallback, useContext, useMemo, useRef } from "react";
|
|
|
2
2
|
import { ReactEditorView } from "../ReactEditorView.js";
|
|
3
3
|
import { ChildDescriptionsContext } from "../contexts/ChildDescriptionsContext.js";
|
|
4
4
|
import { EditorContext } from "../contexts/EditorContext.js";
|
|
5
|
-
import { ReactMarkViewDesc, sortViewDescs } from "../viewdesc.js";
|
|
5
|
+
import { ReactMarkViewDesc, TextViewDesc, sortViewDescs } from "../viewdesc.js";
|
|
6
6
|
import { useClientLayoutEffect } from "./useClientLayoutEffect.js";
|
|
7
7
|
import { useEffectEvent } from "./useEffectEvent.js";
|
|
8
8
|
export function useMarkViewDescription(getDOM, getContentDOM, constructor, props) {
|
|
@@ -110,12 +110,70 @@ export function useMarkViewDescription(getDOM, getContentDOM, constructor, props
|
|
|
110
110
|
child.parent = viewDesc;
|
|
111
111
|
}
|
|
112
112
|
});
|
|
113
|
+
const findCompositionDOM = useCallback((compositionViewDesc)=>{
|
|
114
|
+
const children = childrenRef.current;
|
|
115
|
+
// Because TextNodeViews can't locate the DOM nodes
|
|
116
|
+
// for compositions, we need to override them here
|
|
117
|
+
if (!viewDescRef.current?.contentDOM) return;
|
|
118
|
+
let compositionTopDOM = null;
|
|
119
|
+
for (const childNode of viewDescRef.current.contentDOM.childNodes){
|
|
120
|
+
if (children.every((child)=>child.dom !== childNode)) {
|
|
121
|
+
compositionTopDOM = childNode;
|
|
122
|
+
break;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
if (!compositionTopDOM) {
|
|
126
|
+
// Otherwise the IME extended an existing tracked text node. Take it over.
|
|
127
|
+
const reactView = view;
|
|
128
|
+
const imeTextNode = reactView.input.compositionNode;
|
|
129
|
+
if (!imeTextNode || !viewDescRef.current.contentDOM.contains(imeTextNode.parentNode)) {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
const claimedDesc = imeTextNode.pmViewDesc;
|
|
133
|
+
if (!(claimedDesc instanceof TextViewDesc)) return;
|
|
134
|
+
if (claimedDesc.node.text === imeTextNode.nodeValue) return; // not extended
|
|
135
|
+
// Walk up to the direct child of contentDOM that contains the IME text node
|
|
136
|
+
// (could be the text node itself, could be wrapped in a mark span).
|
|
137
|
+
let topDOM = imeTextNode;
|
|
138
|
+
while(topDOM.parentNode !== viewDescRef.current.contentDOM){
|
|
139
|
+
const next = topDOM.parentNode;
|
|
140
|
+
if (!next) return;
|
|
141
|
+
topDOM = next;
|
|
142
|
+
}
|
|
143
|
+
// Detach the displaced TextViewDesc from the sibling list so sibling-size
|
|
144
|
+
// accounting (used by posBeforeChild) doesn't double-count this text node.
|
|
145
|
+
const displacedIdx = children.indexOf(claimedDesc);
|
|
146
|
+
if (displacedIdx >= 0) children.splice(displacedIdx, 1);
|
|
147
|
+
compositionViewDesc.dom = topDOM;
|
|
148
|
+
compositionViewDesc.textDOM = imeTextNode;
|
|
149
|
+
compositionViewDesc.text = imeTextNode.data;
|
|
150
|
+
imeTextNode.pmViewDesc = compositionViewDesc;
|
|
151
|
+
compositionViewDesc._displacedDesc = claimedDesc;
|
|
152
|
+
reactView.input.compositionNodes.push(compositionViewDesc);
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
let textDOM = compositionTopDOM;
|
|
156
|
+
while(textDOM.firstChild){
|
|
157
|
+
textDOM = textDOM.firstChild;
|
|
158
|
+
}
|
|
159
|
+
if (!textDOM || !(textDOM instanceof Text)) {
|
|
160
|
+
console.error(compositionTopDOM, textDOM);
|
|
161
|
+
throw new Error(`Started a composition but couldn't find the text node it belongs to.`);
|
|
162
|
+
}
|
|
163
|
+
compositionViewDesc.dom = compositionTopDOM;
|
|
164
|
+
compositionViewDesc.textDOM = textDOM;
|
|
165
|
+
compositionViewDesc.text = textDOM.data;
|
|
166
|
+
compositionViewDesc.textDOM.pmViewDesc = compositionViewDesc;
|
|
167
|
+
view.input.compositionNodes.push(compositionViewDesc);
|
|
168
|
+
}, [
|
|
169
|
+
view
|
|
170
|
+
]);
|
|
113
171
|
const childContextValue = useMemo(()=>({
|
|
114
172
|
parentRef: viewDescRef,
|
|
115
|
-
siblingsRef: childrenRef
|
|
173
|
+
siblingsRef: childrenRef,
|
|
174
|
+
findCompositionDOM
|
|
116
175
|
}), [
|
|
117
|
-
|
|
118
|
-
viewDescRef
|
|
176
|
+
findCompositionDOM
|
|
119
177
|
]);
|
|
120
178
|
return {
|
|
121
179
|
childContextValue,
|