@handlewithcare/react-prosemirror 2.4.12 → 2.5.1
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/AbstractEditorView.js +4 -0
- package/dist/cjs/ReactEditorView.js +156 -0
- package/dist/cjs/StaticEditorView.js +86 -0
- package/dist/cjs/components/ChildNodeViews.js +58 -29
- package/dist/cjs/components/CustomNodeView.js +77 -137
- package/dist/cjs/{hooks/useNodePos.js → components/DefaultNodeView.js} +24 -26
- package/dist/cjs/components/DocNodeView.js +33 -41
- package/dist/cjs/components/MarkView.js +1 -2
- package/dist/cjs/components/NativeWidgetView.js +2 -3
- package/dist/cjs/components/NodeView.js +31 -21
- package/dist/cjs/components/ProseMirror.js +25 -17
- package/dist/cjs/components/ProseMirrorDoc.js +7 -27
- package/dist/cjs/components/ReactNodeView.js +98 -61
- package/dist/cjs/components/SeparatorHackView.js +1 -2
- package/dist/cjs/components/TextNodeView.js +4 -5
- package/dist/cjs/components/TrailingHackView.js +1 -2
- package/dist/cjs/components/WidgetView.js +2 -4
- package/dist/cjs/constants.js +33 -0
- package/dist/cjs/hooks/useEditor.js +33 -229
- package/dist/cjs/hooks/useEditorEffect.js +2 -2
- package/dist/cjs/hooks/useEditorEventCallback.js +8 -5
- package/dist/cjs/hooks/useIgnoreMutation.js +1 -1
- package/dist/cjs/hooks/useNodeViewDescriptor.js +123 -80
- package/dist/cjs/hooks/useReactKeys.js +1 -1
- package/dist/cjs/hooks/useSelectNode.js +9 -7
- package/dist/cjs/hooks/useStopEvent.js +1 -1
- package/dist/cjs/plugins/beforeInputPlugin.js +12 -0
- package/dist/cjs/testing/editorViewTestHelpers.js +0 -2
- package/dist/cjs/viewdesc.js +104 -25
- package/dist/esm/AbstractEditorView.js +1 -0
- package/dist/esm/ReactEditorView.js +156 -0
- package/dist/esm/StaticEditorView.js +76 -0
- package/dist/esm/components/ChildNodeViews.js +59 -31
- package/dist/esm/components/CustomNodeView.js +78 -138
- package/dist/esm/components/DefaultNodeView.js +16 -0
- package/dist/esm/components/DocNodeView.js +33 -41
- package/dist/esm/components/MarkView.js +1 -2
- package/dist/esm/components/NativeWidgetView.js +2 -3
- package/dist/esm/components/NodeView.js +32 -22
- package/dist/esm/components/ProseMirror.js +25 -17
- package/dist/esm/components/ProseMirrorDoc.js +7 -28
- package/dist/esm/components/ReactNodeView.js +99 -62
- package/dist/esm/components/SeparatorHackView.js +1 -2
- package/dist/esm/components/TextNodeView.js +4 -5
- package/dist/esm/components/TrailingHackView.js +1 -2
- package/dist/esm/components/WidgetView.js +2 -4
- package/dist/esm/constants.js +15 -0
- package/dist/esm/hooks/useEditor.js +29 -218
- package/dist/esm/hooks/useEditorEffect.js +2 -2
- package/dist/esm/hooks/useEditorEventCallback.js +8 -5
- package/dist/esm/hooks/useIgnoreMutation.js +1 -1
- package/dist/esm/hooks/useNodeViewDescriptor.js +125 -82
- package/dist/esm/hooks/useReactKeys.js +1 -1
- package/dist/esm/hooks/useSelectNode.js +9 -7
- package/dist/esm/hooks/useStopEvent.js +1 -1
- package/dist/esm/plugins/beforeInputPlugin.js +12 -0
- package/dist/esm/testing/editorViewTestHelpers.js +0 -2
- package/dist/esm/viewdesc.js +94 -18
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/types/AbstractEditorView.d.ts +27 -0
- package/dist/types/ReactEditorView.d.ts +80 -0
- package/dist/types/StaticEditorView.d.ts +24 -0
- package/dist/types/components/ChildNodeViews.d.ts +2 -2
- package/dist/types/components/CustomNodeView.d.ts +3 -3
- package/dist/types/components/DefaultNodeView.d.ts +3 -0
- package/dist/types/components/DocNodeView.d.ts +9 -17
- package/dist/types/components/MarkView.d.ts +2 -2
- package/dist/types/components/NativeWidgetView.d.ts +2 -2
- package/dist/types/components/NodeView.d.ts +5 -5
- package/dist/types/components/NodeViewComponentProps.d.ts +3 -4
- package/dist/types/components/ProseMirrorDoc.d.ts +14 -8
- package/dist/types/components/ReactNodeView.d.ts +4 -2
- package/dist/types/components/SeparatorHackView.d.ts +2 -2
- package/dist/types/components/TextNodeView.d.ts +4 -3
- package/dist/types/components/TrailingHackView.d.ts +2 -2
- package/dist/types/components/WidgetView.d.ts +2 -2
- package/dist/types/constants.d.ts +4 -0
- package/dist/types/contexts/EditorContext.d.ts +6 -4
- package/dist/types/contexts/IgnoreMutationContext.d.ts +2 -1
- package/dist/types/contexts/NodeViewContext.d.ts +3 -1
- package/dist/types/contexts/SelectNodeContext.d.ts +3 -1
- package/dist/types/contexts/StopEventContext.d.ts +2 -1
- package/dist/types/decorations/computeDocDeco.d.ts +3 -2
- package/dist/types/decorations/viewDecorations.d.ts +3 -2
- package/dist/types/hooks/useEditor.d.ts +5 -46
- package/dist/types/hooks/useNodeViewDescriptor.d.ts +18 -10
- package/dist/types/hooks/useReactKeys.d.ts +1 -1
- package/dist/types/hooks/useSelectNode.d.ts +2 -1
- package/dist/types/props.d.ts +3 -3
- package/dist/types/viewdesc.d.ts +29 -11
- package/package.json +7 -3
- package/dist/cjs/components/Editor.js +0 -28
- package/dist/cjs/components/NodeViews.js +0 -73
- package/dist/cjs/components/__tests__/LayoutGroup.test.js +0 -141
- package/dist/cjs/components/__tests__/ProseMirror.test.js +0 -255
- package/dist/cjs/contexts/NodeViewsContext.js +0 -10
- package/dist/cjs/hooks/__tests__/useEditorViewLayoutEffect.test.js +0 -107
- package/dist/cjs/hooks/__tests__/useNodeViews.test.js +0 -159
- package/dist/cjs/hooks/useClientOnly.js +0 -19
- package/dist/cjs/hooks/useEditorView.js +0 -100
- package/dist/cjs/hooks/useNodeViews.js +0 -100
- package/dist/cjs/nodeViews/createReactNodeViewConstructor.js +0 -244
- package/dist/cjs/nodeViews/phrasingContentTags.js +0 -57
- package/dist/cjs/plugins/__tests__/react.test.js +0 -139
- package/dist/cjs/plugins/react.js +0 -71
- package/dist/cjs/selection/SelectionDOMObserver.js +0 -171
- package/dist/cjs/selection/hasFocusAndSelection.js +0 -35
- package/dist/cjs/selection/selectionFromDOM.js +0 -77
- package/dist/cjs/selection/selectionToDOM.js +0 -226
- package/dist/cjs/ssr.js +0 -85
- package/dist/esm/components/Editor.js +0 -15
- package/dist/esm/components/NodeViews.js +0 -26
- package/dist/esm/components/__tests__/LayoutGroup.test.js +0 -98
- package/dist/esm/components/__tests__/ProseMirror.test.js +0 -207
- package/dist/esm/contexts/NodeViewsContext.js +0 -9
- package/dist/esm/hooks/__tests__/useEditorViewLayoutEffect.test.js +0 -98
- package/dist/esm/hooks/__tests__/useNodeViews.test.js +0 -116
- package/dist/esm/hooks/useClientOnly.js +0 -9
- package/dist/esm/hooks/useEditorView.js +0 -99
- package/dist/esm/hooks/useNodePos.js +0 -16
- package/dist/esm/hooks/useNodeViews.js +0 -53
- package/dist/esm/nodeViews/createReactNodeViewConstructor.js +0 -214
- package/dist/esm/nodeViews/phrasingContentTags.js +0 -49
- package/dist/esm/plugins/__tests__/react.test.js +0 -135
- package/dist/esm/plugins/react.js +0 -64
- package/dist/esm/selection/SelectionDOMObserver.js +0 -161
- package/dist/esm/selection/hasFocusAndSelection.js +0 -17
- package/dist/esm/selection/selectionFromDOM.js +0 -59
- package/dist/esm/selection/selectionToDOM.js +0 -196
- package/dist/esm/ssr.js +0 -82
- package/dist/types/components/Editor.d.ts +0 -7
- package/dist/types/components/NodeViews.d.ts +0 -6
- package/dist/types/components/__tests__/LayoutGroup.test.d.ts +0 -1
- package/dist/types/contexts/NodeViewsContext.d.ts +0 -19
- package/dist/types/hooks/__tests__/useEditorViewLayoutEffect.test.d.ts +0 -1
- package/dist/types/hooks/__tests__/useNodeViews.test.d.ts +0 -1
- package/dist/types/hooks/useClientOnly.d.ts +0 -1
- package/dist/types/hooks/useEditorView.d.ts +0 -23
- package/dist/types/hooks/useNodePos.d.ts +0 -9
- package/dist/types/hooks/useNodeViews.d.ts +0 -5
- package/dist/types/nodeViews/createReactNodeViewConstructor.d.ts +0 -48
- package/dist/types/nodeViews/phrasingContentTags.d.ts +0 -1
- package/dist/types/plugins/__tests__/react.test.d.ts +0 -1
- package/dist/types/plugins/react.d.ts +0 -21
- package/dist/types/selection/SelectionDOMObserver.d.ts +0 -33
- package/dist/types/selection/hasFocusAndSelection.d.ts +0 -3
- package/dist/types/selection/selectionFromDOM.d.ts +0 -4
- package/dist/types/selection/selectionToDOM.d.ts +0 -9
- package/dist/types/ssr.d.ts +0 -19
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { EditorView } from "prosemirror-view";
|
|
2
|
+
import { EMPTY_STATE } from "./constants.js";
|
|
3
|
+
function buildNodeViews(view) {
|
|
4
|
+
const result = Object.create(null);
|
|
5
|
+
function add(obj) {
|
|
6
|
+
for(const prop in obj)if (!Object.prototype.hasOwnProperty.call(result, prop)) // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
7
|
+
result[prop] = obj[prop];
|
|
8
|
+
}
|
|
9
|
+
view.someProp("nodeViews", add);
|
|
10
|
+
view.someProp("markViews", add);
|
|
11
|
+
return result;
|
|
12
|
+
}
|
|
13
|
+
function changedNodeViews(a, b) {
|
|
14
|
+
let nA = 0, nB = 0;
|
|
15
|
+
for(const prop in a){
|
|
16
|
+
if (a[prop] != b[prop]) return true;
|
|
17
|
+
nA++;
|
|
18
|
+
}
|
|
19
|
+
for(const _ in b)nB++;
|
|
20
|
+
return nA != nB;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Extends EditorView to make prop and state updates pure, remove the DOM
|
|
24
|
+
* Mutation Observer, and use a custom document view managed by React.
|
|
25
|
+
*
|
|
26
|
+
* @privateRemarks
|
|
27
|
+
*
|
|
28
|
+
* The implementation relies on the base class using a private member to store
|
|
29
|
+
* the committed props and having a public getter that we override to return the
|
|
30
|
+
* latest, uncommitted props. The base class can then be told to update when the
|
|
31
|
+
* React effects are commit an update, applying the pending, uncommitted props.
|
|
32
|
+
*/ export class ReactEditorView extends EditorView {
|
|
33
|
+
nextProps;
|
|
34
|
+
prevState;
|
|
35
|
+
constructor(place, props){
|
|
36
|
+
// Prevent the base class from destroying the React-managed nodes.
|
|
37
|
+
// Restore them below after invoking the base class constructor.
|
|
38
|
+
const reactContent = [
|
|
39
|
+
...place.mount.childNodes
|
|
40
|
+
];
|
|
41
|
+
// Prevent the base class from mutating the React-managed attributes.
|
|
42
|
+
// Restore them below after invoking the base class constructor.
|
|
43
|
+
const reactAttrs = [
|
|
44
|
+
...place.mount.attributes
|
|
45
|
+
];
|
|
46
|
+
for (const attr of reactAttrs){
|
|
47
|
+
place.mount.removeAttributeNode(attr);
|
|
48
|
+
}
|
|
49
|
+
try {
|
|
50
|
+
// Call the superclass constructor with only a state and no plugins.
|
|
51
|
+
// We'll set everything else ourselves and apply props during layout.
|
|
52
|
+
super(place, {
|
|
53
|
+
state: EMPTY_STATE
|
|
54
|
+
});
|
|
55
|
+
this.domObserver.stop();
|
|
56
|
+
this.domObserver.observer = null;
|
|
57
|
+
this.domObserver.queue = [];
|
|
58
|
+
} finally{
|
|
59
|
+
place.mount.replaceChildren(...reactContent);
|
|
60
|
+
for (const attr of place.mount.attributes){
|
|
61
|
+
place.mount.removeAttributeNode(attr);
|
|
62
|
+
}
|
|
63
|
+
for (const attr of reactAttrs){
|
|
64
|
+
place.mount.setAttributeNode(attr);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
this.prevState = EMPTY_STATE;
|
|
68
|
+
this.nextProps = props;
|
|
69
|
+
this.state = props.state;
|
|
70
|
+
this.nodeViews = buildNodeViews(this);
|
|
71
|
+
// Destroy the document view description that the base class makes.
|
|
72
|
+
// A React document view will assign itself to this attribute later.
|
|
73
|
+
this.docView.destroy();
|
|
74
|
+
// @ts-expect-error this violates the typing but class does it, too.
|
|
75
|
+
this.docView = null;
|
|
76
|
+
}
|
|
77
|
+
get props() {
|
|
78
|
+
return this.nextProps;
|
|
79
|
+
}
|
|
80
|
+
setProps(props) {
|
|
81
|
+
this.update({
|
|
82
|
+
...this.props,
|
|
83
|
+
...props
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
update(props) {
|
|
87
|
+
const prevProps = this.nextProps;
|
|
88
|
+
this.nextProps = props;
|
|
89
|
+
this.state = props.state;
|
|
90
|
+
if (prevProps.state.plugins !== props.state.plugins || prevProps.plugins !== props.plugins || prevProps.nodeViews !== props.nodeViews) {
|
|
91
|
+
const nodeViews = buildNodeViews(this);
|
|
92
|
+
if (changedNodeViews(this.nodeViews, nodeViews)) {
|
|
93
|
+
this.nodeViews = nodeViews;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
this.editable = !this.someProp("editable", (value)=>value(this.state) === false);
|
|
97
|
+
}
|
|
98
|
+
updateState(state) {
|
|
99
|
+
this.setProps({
|
|
100
|
+
state
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
someProp(propName, f) {
|
|
104
|
+
if (!this.props) {
|
|
105
|
+
// The base class constructor calls this method before props are set.
|
|
106
|
+
return undefined;
|
|
107
|
+
}
|
|
108
|
+
const prop = this.props[propName];
|
|
109
|
+
if (prop) {
|
|
110
|
+
const result = f ? f(prop) : prop;
|
|
111
|
+
if (result) {
|
|
112
|
+
return result;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
for (const plugin of this.props.plugins ?? []){
|
|
116
|
+
const prop = plugin.props[propName];
|
|
117
|
+
if (prop) {
|
|
118
|
+
const result = f ? f(prop) : prop;
|
|
119
|
+
if (result) {
|
|
120
|
+
return result;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
for (const plugin of this.state.plugins){
|
|
125
|
+
const prop = plugin.props[propName];
|
|
126
|
+
if (prop) {
|
|
127
|
+
const result = f ? f(prop) : prop;
|
|
128
|
+
if (result) {
|
|
129
|
+
return result;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return undefined;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Commit effects by appling the pending props and state.
|
|
137
|
+
*
|
|
138
|
+
* Ensures the DOM selection is correct and updates plugin views.
|
|
139
|
+
*
|
|
140
|
+
* @privateRemarks
|
|
141
|
+
*
|
|
142
|
+
* The correctness of this depends on the pure update function ensuring that
|
|
143
|
+
* the node view set is up to date so that it does not try to redraw.
|
|
144
|
+
*/ commitPendingEffects() {
|
|
145
|
+
// This class tracks state eagerly but the base class does it lazily.
|
|
146
|
+
// Temporarily roll it back so the base class can handle the updates.
|
|
147
|
+
this.state = this.prevState;
|
|
148
|
+
// Force the base class to try to update the document. React updated it, but
|
|
149
|
+
// this ensures that the base class validates the DOM selection and invokes
|
|
150
|
+
// node view selection callbacks.
|
|
151
|
+
this.docView.markDirty(-1, -1);
|
|
152
|
+
super.update(this.nextProps);
|
|
153
|
+
// Store the new previous state.
|
|
154
|
+
this.prevState = this.state;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
export class StaticEditorView {
|
|
2
|
+
props;
|
|
3
|
+
nodeViews;
|
|
4
|
+
constructor(props){
|
|
5
|
+
this.props = props;
|
|
6
|
+
this.nodeViews = {};
|
|
7
|
+
}
|
|
8
|
+
get composing() {
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
get dom() {
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
14
|
+
get editable() {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
get state() {
|
|
18
|
+
return this.props.state;
|
|
19
|
+
}
|
|
20
|
+
setProps(props) {
|
|
21
|
+
return this.update({
|
|
22
|
+
...this.props,
|
|
23
|
+
...props
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
update(props) {
|
|
27
|
+
this.props = props;
|
|
28
|
+
}
|
|
29
|
+
updateState(state) {
|
|
30
|
+
this.setProps({
|
|
31
|
+
state
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
someProp(propName, f) {
|
|
35
|
+
const prop = this.props[propName];
|
|
36
|
+
if (prop) {
|
|
37
|
+
const result = f ? f(prop) : prop;
|
|
38
|
+
if (result) {
|
|
39
|
+
return result;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
for (const plugin of this.props.plugins ?? []){
|
|
43
|
+
const prop = plugin.props[propName];
|
|
44
|
+
if (prop) {
|
|
45
|
+
const result = f ? f(prop) : prop;
|
|
46
|
+
if (result) {
|
|
47
|
+
return result;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
for (const plugin of this.state.plugins){
|
|
52
|
+
const prop = plugin.props[propName];
|
|
53
|
+
if (prop) {
|
|
54
|
+
const result = f ? f(prop) : prop;
|
|
55
|
+
if (result) {
|
|
56
|
+
return result;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return undefined;
|
|
61
|
+
}
|
|
62
|
+
destroy() {
|
|
63
|
+
// pass
|
|
64
|
+
}
|
|
65
|
+
domSelectionRange() {
|
|
66
|
+
return {
|
|
67
|
+
anchorNode: null,
|
|
68
|
+
anchorOffset: 0,
|
|
69
|
+
focusNode: null,
|
|
70
|
+
focusOffset: 0
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
domSelection() {
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import React, { cloneElement, createElement, memo, useContext, useRef } from "react";
|
|
1
|
+
import React, { cloneElement, createElement, memo, useCallback, useContext, useRef } from "react";
|
|
2
2
|
import { ChildDescriptorsContext } from "../contexts/ChildDescriptorsContext.js";
|
|
3
3
|
import { EditorContext } from "../contexts/EditorContext.js";
|
|
4
4
|
import { iterDeco } from "../decorations/iterDeco.js";
|
|
5
|
-
// import { useEditorState } from "../hooks/useEditorState.js";
|
|
6
5
|
import { useReactKeys } from "../hooks/useReactKeys.js";
|
|
7
6
|
import { htmlAttrsToReactProps, mergeReactProps } from "../props.js";
|
|
8
7
|
import { MarkView } from "./MarkView.js";
|
|
@@ -24,21 +23,30 @@ export function wrapInDeco(reactNode, deco) {
|
|
|
24
23
|
return /*#__PURE__*/ cloneElement(reactNode, mergeReactProps(reactNode.props, props));
|
|
25
24
|
}
|
|
26
25
|
function areChildrenEqual(a, b) {
|
|
27
|
-
return a.type === b.type && a.marks.every((mark)=>mark.isInSet(b.marks)) && b.marks.every((mark)=>mark.isInSet(a.marks)) && a.key === b.key && (a.type === "node" ? a.outerDeco?.length === b.outerDeco?.length && a.outerDeco?.every((prevDeco)=>b.outerDeco?.some((nextDeco)=>prevDeco.from === nextDeco.from && prevDeco.to && nextDeco.to &&
|
|
26
|
+
return a.type === b.type && a.marks.every((mark)=>mark.isInSet(b.marks)) && b.marks.every((mark)=>mark.isInSet(a.marks)) && a.key === b.key && (a.type === "node" ? a.outerDeco?.length === b.outerDeco?.length && a.outerDeco?.every((prevDeco)=>b.outerDeco?.some((nextDeco)=>prevDeco.from === nextDeco.from && prevDeco.to && nextDeco.to && // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
27
|
+
prevDeco.type.eq(nextDeco.type))) && a.innerDeco.eq(b.innerDeco) : true) && a.node === b.node && a.widget === b.widget;
|
|
28
28
|
}
|
|
29
29
|
const ChildView = /*#__PURE__*/ memo(function ChildView(param) {
|
|
30
30
|
let { child, getInnerPos } = param;
|
|
31
31
|
const { view } = useContext(EditorContext);
|
|
32
|
-
const
|
|
33
|
-
|
|
32
|
+
const childRef = useRef(child);
|
|
33
|
+
childRef.current = child;
|
|
34
|
+
const getPos = useCallback(()=>{
|
|
35
|
+
return getInnerPos() + childRef.current.offset;
|
|
36
|
+
}, [
|
|
37
|
+
getInnerPos
|
|
38
|
+
]);
|
|
34
39
|
return child.type === "widget" ? /*#__PURE__*/ React.createElement(WidgetView, {
|
|
35
40
|
key: child.key,
|
|
36
41
|
widget: child.widget,
|
|
37
|
-
getPos:
|
|
42
|
+
getPos: getPos
|
|
38
43
|
}) : child.type === "native-widget" ? /*#__PURE__*/ React.createElement(NativeWidgetView, {
|
|
39
44
|
key: child.key,
|
|
40
45
|
widget: child.widget,
|
|
41
|
-
getPos:
|
|
46
|
+
getPos: getPos
|
|
47
|
+
}) : child.type === "hack" ? /*#__PURE__*/ React.createElement(child.component, {
|
|
48
|
+
key: child.key,
|
|
49
|
+
getPos: getPos
|
|
42
50
|
}) : child.node.isText ? /*#__PURE__*/ React.createElement(ChildDescriptorsContext.Consumer, {
|
|
43
51
|
key: child.key
|
|
44
52
|
}, (param)=>{
|
|
@@ -46,7 +54,7 @@ const ChildView = /*#__PURE__*/ memo(function ChildView(param) {
|
|
|
46
54
|
return /*#__PURE__*/ React.createElement(TextNodeView, {
|
|
47
55
|
view: view,
|
|
48
56
|
node: child.node,
|
|
49
|
-
getPos:
|
|
57
|
+
getPos: getPos,
|
|
50
58
|
siblingsRef: siblingsRef,
|
|
51
59
|
parentRef: parentRef,
|
|
52
60
|
decorations: child.outerDeco
|
|
@@ -54,7 +62,7 @@ const ChildView = /*#__PURE__*/ memo(function ChildView(param) {
|
|
|
54
62
|
}) : /*#__PURE__*/ React.createElement(NodeView, {
|
|
55
63
|
key: child.key,
|
|
56
64
|
node: child.node,
|
|
57
|
-
getPos:
|
|
65
|
+
getPos: getPos,
|
|
58
66
|
outerDeco: child.outerDeco,
|
|
59
67
|
innerDeco: child.innerDeco
|
|
60
68
|
});
|
|
@@ -62,8 +70,13 @@ const ChildView = /*#__PURE__*/ memo(function ChildView(param) {
|
|
|
62
70
|
const InlinePartition = /*#__PURE__*/ memo(function InlinePartition(param) {
|
|
63
71
|
let { childViews, getInnerPos } = param;
|
|
64
72
|
const firstChild = childViews[0];
|
|
65
|
-
const
|
|
66
|
-
|
|
73
|
+
const firstChildRef = useRef(firstChild);
|
|
74
|
+
firstChildRef.current = firstChild;
|
|
75
|
+
const getPos = useCallback(()=>{
|
|
76
|
+
return getInnerPos() + firstChildRef.current.offset;
|
|
77
|
+
}, [
|
|
78
|
+
getInnerPos
|
|
79
|
+
]);
|
|
67
80
|
const firstMark = firstChild.marks[0];
|
|
68
81
|
if (!firstMark) {
|
|
69
82
|
return /*#__PURE__*/ React.createElement(React.Fragment, null, childViews.map((child)=>{
|
|
@@ -75,9 +88,9 @@ const InlinePartition = /*#__PURE__*/ memo(function InlinePartition(param) {
|
|
|
75
88
|
}));
|
|
76
89
|
}
|
|
77
90
|
return /*#__PURE__*/ React.createElement(MarkView, {
|
|
78
|
-
getPos: getFirstChildPos,
|
|
79
91
|
key: firstChild.key,
|
|
80
|
-
mark: firstMark
|
|
92
|
+
mark: firstMark,
|
|
93
|
+
getPos: getPos
|
|
81
94
|
}, /*#__PURE__*/ React.createElement(InlineView, {
|
|
82
95
|
key: firstChild.key,
|
|
83
96
|
getInnerPos: getInnerPos,
|
|
@@ -135,7 +148,7 @@ const InlineView = /*#__PURE__*/ memo(function InlineView(param) {
|
|
|
135
148
|
});
|
|
136
149
|
}));
|
|
137
150
|
});
|
|
138
|
-
function createKey(innerPos, offset, type, posToKey, widget
|
|
151
|
+
function createKey(innerPos, offset, index, type, posToKey, widget) {
|
|
139
152
|
const pos = innerPos + offset;
|
|
140
153
|
const key = posToKey?.get(pos);
|
|
141
154
|
if (type === "widget" || type === "native-widget") {
|
|
@@ -179,18 +192,23 @@ function adjustWidgetMarksBack(widgetChildren, nodeChild) {
|
|
|
179
192
|
}
|
|
180
193
|
const ChildElement = /*#__PURE__*/ memo(function ChildElement(param) {
|
|
181
194
|
let { child, getInnerPos } = param;
|
|
182
|
-
const
|
|
183
|
-
|
|
195
|
+
const childRef = useRef(child);
|
|
196
|
+
childRef.current = child;
|
|
197
|
+
const getPos = useCallback(()=>{
|
|
198
|
+
return getInnerPos() + childRef.current.offset;
|
|
199
|
+
}, [
|
|
200
|
+
getInnerPos
|
|
201
|
+
]);
|
|
184
202
|
if (child.type === "node") {
|
|
185
203
|
return child.marks.reduce((element, mark)=>/*#__PURE__*/ React.createElement(MarkView, {
|
|
186
|
-
|
|
187
|
-
|
|
204
|
+
mark: mark,
|
|
205
|
+
getPos: getPos
|
|
188
206
|
}, element), /*#__PURE__*/ React.createElement(NodeView, {
|
|
189
207
|
key: child.key,
|
|
190
208
|
outerDeco: child.outerDeco,
|
|
191
209
|
node: child.node,
|
|
192
210
|
innerDeco: child.innerDeco,
|
|
193
|
-
getPos:
|
|
211
|
+
getPos: getPos
|
|
194
212
|
}));
|
|
195
213
|
} else {
|
|
196
214
|
return /*#__PURE__*/ React.createElement(InlineView, {
|
|
@@ -224,9 +242,10 @@ function createChildElements(children, getInnerPos) {
|
|
|
224
242
|
}
|
|
225
243
|
export const ChildNodeViews = /*#__PURE__*/ memo(function ChildNodeViews(param) {
|
|
226
244
|
let { getPos, node, innerDecorations } = param;
|
|
227
|
-
// const editorState = useEditorState();
|
|
228
245
|
const reactKeys = useReactKeys();
|
|
229
|
-
const getInnerPos =
|
|
246
|
+
const getInnerPos = useCallback(()=>getPos() + 1, [
|
|
247
|
+
getPos
|
|
248
|
+
]);
|
|
230
249
|
const childMap = useRef(new Map()).current;
|
|
231
250
|
if (!node) return null;
|
|
232
251
|
const keysSeen = new Map();
|
|
@@ -237,7 +256,7 @@ export const ChildNodeViews = /*#__PURE__*/ memo(function ChildNodeViews(param)
|
|
|
237
256
|
const widgetMarks = widget.type.spec.marks ?? [];
|
|
238
257
|
let key;
|
|
239
258
|
if (isNative) {
|
|
240
|
-
key = createKey(getInnerPos
|
|
259
|
+
key = createKey(getInnerPos(), offset, index, "native-widget", reactKeys?.posToKey, widget);
|
|
241
260
|
const child = {
|
|
242
261
|
type: "native-widget",
|
|
243
262
|
widget,
|
|
@@ -254,7 +273,7 @@ export const ChildNodeViews = /*#__PURE__*/ memo(function ChildNodeViews(param)
|
|
|
254
273
|
}
|
|
255
274
|
keysSeen.set(key, keysSeen.size);
|
|
256
275
|
} else {
|
|
257
|
-
key = createKey(getInnerPos
|
|
276
|
+
key = createKey(getInnerPos(), offset, index, "widget", reactKeys?.posToKey, widget);
|
|
258
277
|
const child = {
|
|
259
278
|
type: "widget",
|
|
260
279
|
widget: widget,
|
|
@@ -274,8 +293,8 @@ export const ChildNodeViews = /*#__PURE__*/ memo(function ChildNodeViews(param)
|
|
|
274
293
|
const child = childMap.get(key);
|
|
275
294
|
widgetChildren.push(child);
|
|
276
295
|
adjustWidgetMarksForward(lastNodeChild, childMap.get(key));
|
|
277
|
-
}, (childNode, outerDeco, innerDeco, offset)=>{
|
|
278
|
-
const key = createKey(getInnerPos
|
|
296
|
+
}, (childNode, outerDeco, innerDeco, offset, index)=>{
|
|
297
|
+
const key = createKey(getInnerPos(), offset, index, "node", reactKeys?.posToKey);
|
|
279
298
|
const child = {
|
|
280
299
|
type: "node",
|
|
281
300
|
node: childNode,
|
|
@@ -283,6 +302,7 @@ export const ChildNodeViews = /*#__PURE__*/ memo(function ChildNodeViews(param)
|
|
|
283
302
|
innerDeco,
|
|
284
303
|
outerDeco,
|
|
285
304
|
offset,
|
|
305
|
+
index,
|
|
286
306
|
key
|
|
287
307
|
};
|
|
288
308
|
const prevChild = childMap.get(key);
|
|
@@ -306,20 +326,28 @@ export const ChildNodeViews = /*#__PURE__*/ memo(function ChildNodeViews(param)
|
|
|
306
326
|
// step
|
|
307
327
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
308
328
|
(a, b)=>keysSeen.get(a.key) - keysSeen.get(b.key));
|
|
309
|
-
const childElements = createChildElements(children, getInnerPos);
|
|
310
329
|
if (node.isTextblock) {
|
|
311
330
|
const lastChild = children[children.length - 1];
|
|
312
331
|
if (!lastChild || lastChild.type !== "node" || lastChild.node.isInline && !lastChild.node.isText || // RegExp.test actually handles undefined just fine
|
|
313
332
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
314
333
|
/\n$/.test(lastChild.node.text)) {
|
|
315
|
-
|
|
316
|
-
|
|
334
|
+
children.push({
|
|
335
|
+
type: "hack",
|
|
336
|
+
component: SeparatorHackView,
|
|
337
|
+
marks: [],
|
|
338
|
+
offset: lastChild?.offset ?? 0,
|
|
339
|
+
index: (lastChild?.index ?? 0) + 2,
|
|
317
340
|
key: "trailing-hack-img"
|
|
318
|
-
}
|
|
319
|
-
|
|
341
|
+
}, {
|
|
342
|
+
type: "hack",
|
|
343
|
+
component: TrailingHackView,
|
|
344
|
+
marks: [],
|
|
345
|
+
offset: lastChild?.offset ?? 0,
|
|
346
|
+
index: (lastChild?.index ?? 0) + 1,
|
|
320
347
|
key: "trailing-hack-br"
|
|
321
|
-
})
|
|
348
|
+
});
|
|
322
349
|
}
|
|
323
350
|
}
|
|
351
|
+
const childElements = createChildElements(children, getInnerPos);
|
|
324
352
|
return /*#__PURE__*/ React.createElement(React.Fragment, null, childElements);
|
|
325
353
|
});
|
|
@@ -1,153 +1,93 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import React, { cloneElement, createElement, memo,
|
|
1
|
+
import { DOMSerializer } from "prosemirror-model";
|
|
2
|
+
import React, { cloneElement, createElement, memo, useMemo, useRef, useState } from "react";
|
|
3
3
|
import { createPortal } from "react-dom";
|
|
4
4
|
import { ChildDescriptorsContext } from "../contexts/ChildDescriptorsContext.js";
|
|
5
|
-
import { EditorContext } from "../contexts/EditorContext.js";
|
|
6
|
-
import { useClientLayoutEffect } from "../hooks/useClientLayoutEffect.js";
|
|
7
|
-
import { useClientOnly } from "../hooks/useClientOnly.js";
|
|
8
|
-
import { useForceUpdate } from "../hooks/useForceUpdate.js";
|
|
9
5
|
import { useNodeViewDescriptor } from "../hooks/useNodeViewDescriptor.js";
|
|
10
6
|
import { ChildNodeViews, wrapInDeco } from "./ChildNodeViews.js";
|
|
11
7
|
export const CustomNodeView = /*#__PURE__*/ memo(function CustomNodeView(param) {
|
|
12
|
-
let {
|
|
13
|
-
const
|
|
14
|
-
const
|
|
15
|
-
const
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
const innerDecoRef = useRef(innerDeco);
|
|
23
|
-
innerDecoRef.current = innerDeco;
|
|
24
|
-
const customNodeViewRootRef = useRef(null);
|
|
25
|
-
const customNodeViewRef = useRef(null);
|
|
26
|
-
const forceUpdate = useForceUpdate();
|
|
27
|
-
const isOnClient = useClientOnly();
|
|
28
|
-
// In Strict/Concurrent mode, layout effects can be destroyed/re-run
|
|
29
|
-
// independently of renders. We need to ensure that if the
|
|
30
|
-
// destructor that destroys the node view is called, we then recreate
|
|
31
|
-
// the node view when the layout effect is re-run.
|
|
32
|
-
useClientLayoutEffect(()=>{
|
|
33
|
-
if (!customNodeViewRef.current) {
|
|
34
|
-
customNodeViewRef.current = customNodeView(nodeRef.current, // customNodeView will only be set if view is set, and we can only reach
|
|
35
|
-
// this line if customNodeView is set
|
|
36
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
37
|
-
view, getPosFunc, outerDecoRef.current, innerDecoRef.current);
|
|
38
|
-
if (customNodeViewRef.current.stopEvent) {
|
|
39
|
-
setStopEvent(customNodeViewRef.current.stopEvent.bind(customNodeViewRef.current));
|
|
40
|
-
}
|
|
41
|
-
if (customNodeViewRef.current.selectNode) {
|
|
42
|
-
setSelectNode(customNodeViewRef.current.selectNode.bind(customNodeViewRef.current), customNodeViewRef.current.deselectNode?.bind(customNodeViewRef.current) ?? (()=>{}));
|
|
43
|
-
}
|
|
44
|
-
if (customNodeViewRef.current.ignoreMutation) {
|
|
45
|
-
setIgnoreMutation(customNodeViewRef.current.ignoreMutation.bind(customNodeViewRef.current));
|
|
46
|
-
}
|
|
47
|
-
// If we've reconstructed the nodeview, then we need to
|
|
48
|
-
// recreate the portal into its contentDOM, which happens
|
|
49
|
-
// during the render. So we need to trigger a re-render!
|
|
50
|
-
forceUpdate();
|
|
51
|
-
}
|
|
52
|
-
if (!customNodeViewRootRef.current) return;
|
|
53
|
-
const { dom } = customNodeViewRef.current;
|
|
54
|
-
if (customNodeViewRootRef.current.firstChild === dom) {
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
|
-
nodeDomRef.current = customNodeViewRootRef.current;
|
|
58
|
-
customNodeViewRootRef.current.appendChild(dom);
|
|
59
|
-
// Layout effects can run multiple times — if this effect
|
|
60
|
-
// destroyed and recreated this node view, then we need to
|
|
61
|
-
// resync the selectNode state
|
|
62
|
-
if (view?.state.selection instanceof NodeSelection && view.state.selection.node === nodeRef.current) {
|
|
63
|
-
customNodeViewRef.current.selectNode?.();
|
|
64
|
-
}
|
|
65
|
-
const nodeView = customNodeViewRef.current;
|
|
66
|
-
return ()=>{
|
|
67
|
-
nodeView.destroy?.();
|
|
68
|
-
customNodeViewRef.current = null;
|
|
69
|
-
};
|
|
70
|
-
// setStopEvent, setSelectNodee, and setIgnoreMutation are all stable
|
|
71
|
-
// functions and don't need to be added to the dependencies. They also
|
|
72
|
-
// can't be, because they come from useNodeViewDescriptor, which
|
|
73
|
-
// _has_ to be called after this hook, so that the effects run
|
|
74
|
-
// in the correct order
|
|
75
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
76
|
-
}, [
|
|
77
|
-
customNodeView,
|
|
78
|
-
getPosFunc,
|
|
79
|
-
view
|
|
80
|
-
]);
|
|
81
|
-
useClientLayoutEffect(()=>{
|
|
82
|
-
if (!customNodeView || !customNodeViewRef.current) return;
|
|
83
|
-
const { destroy, update } = customNodeViewRef.current;
|
|
84
|
-
const updated = update?.call(customNodeViewRef.current, node, outerDeco, innerDeco) ?? true;
|
|
85
|
-
if (updated) return;
|
|
86
|
-
destroy?.call(customNodeViewRef.current);
|
|
87
|
-
if (!customNodeViewRootRef.current) return;
|
|
88
|
-
customNodeViewRef.current = customNodeView(nodeRef.current, // customNodeView will only be set if view is set, and we can only reach
|
|
89
|
-
// this line if customNodeView is set
|
|
90
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
91
|
-
view, getPosFunc, outerDecoRef.current, innerDecoRef.current);
|
|
92
|
-
const { dom } = customNodeViewRef.current;
|
|
93
|
-
nodeDomRef.current = customNodeViewRootRef.current;
|
|
94
|
-
customNodeViewRootRef.current.appendChild(dom);
|
|
95
|
-
}, [
|
|
96
|
-
customNodeView,
|
|
97
|
-
view,
|
|
98
|
-
innerDeco,
|
|
8
|
+
let { constructor, node, getPos, innerDeco, outerDeco } = param;
|
|
9
|
+
const ref = useRef(null);
|
|
10
|
+
const innerRef = useRef(null);
|
|
11
|
+
const [selected, setSelected] = useState(false);
|
|
12
|
+
const nodeProps = useMemo(()=>({
|
|
13
|
+
node,
|
|
14
|
+
getPos,
|
|
15
|
+
decorations: outerDeco,
|
|
16
|
+
innerDecorations: innerDeco
|
|
17
|
+
}), [
|
|
99
18
|
node,
|
|
100
|
-
outerDeco,
|
|
101
19
|
getPos,
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
const { childDescriptors, nodeViewDescRef, setStopEvent, setSelectNode, setIgnoreMutation } = useNodeViewDescriptor(node, getPosFunc, domRef, nodeDomRef, innerDeco, outerDeco, undefined, contentDomRef);
|
|
105
|
-
const childContextValue = useMemo(()=>({
|
|
106
|
-
parentRef: nodeViewDescRef,
|
|
107
|
-
siblingsRef: childDescriptors
|
|
108
|
-
}), [
|
|
109
|
-
childDescriptors,
|
|
110
|
-
nodeViewDescRef
|
|
20
|
+
outerDeco,
|
|
21
|
+
innerDeco
|
|
111
22
|
]);
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
// render function here. We only do this once, and the
|
|
116
|
-
// results are stored in a ref but not actually appended
|
|
117
|
-
// to the DOM until a client effect
|
|
118
|
-
if (!customNodeViewRef.current) {
|
|
119
|
-
customNodeViewRef.current = customNodeView(nodeRef.current, // customNodeView will only be set if view is set, and we can only reach
|
|
120
|
-
// this line if customNodeView is set
|
|
121
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
122
|
-
view, ()=>getPos.current(), outerDecoRef.current, innerDecoRef.current);
|
|
123
|
-
if (customNodeViewRef.current.stopEvent) {
|
|
124
|
-
setStopEvent(customNodeViewRef.current.stopEvent.bind(customNodeViewRef.current));
|
|
23
|
+
const createNodeView = function() {
|
|
24
|
+
for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
|
|
25
|
+
args[_key] = arguments[_key];
|
|
125
26
|
}
|
|
126
|
-
|
|
127
|
-
|
|
27
|
+
const nodeView = constructor(...args);
|
|
28
|
+
if (!nodeView || !nodeView.dom) {
|
|
29
|
+
const spec = node.type.spec.toDOM?.(node);
|
|
30
|
+
if (!spec) {
|
|
31
|
+
throw new Error(`Node spec for ${node.type.name} is missing toDOM`);
|
|
32
|
+
}
|
|
33
|
+
return DOMSerializer.renderSpec(document, spec, null);
|
|
128
34
|
}
|
|
129
|
-
|
|
130
|
-
|
|
35
|
+
return nodeView;
|
|
36
|
+
};
|
|
37
|
+
const { childContextValue, contentDOM } = useNodeViewDescriptor(ref, function() {
|
|
38
|
+
for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
|
|
39
|
+
args[_key] = arguments[_key];
|
|
131
40
|
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
ref
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
41
|
+
setSelected(false);
|
|
42
|
+
const nodeView = createNodeView(...args);
|
|
43
|
+
const contentDOM = nodeView.contentDOM;
|
|
44
|
+
const nodeDOM = nodeView.dom;
|
|
45
|
+
const wrapperDOM = innerRef.current ?? ref.current;
|
|
46
|
+
wrapperDOM.appendChild(nodeDOM);
|
|
47
|
+
if (!contentDOM && nodeDOM instanceof HTMLElement && nodeDOM.tagName !== "BR") {
|
|
48
|
+
if (!nodeDOM.hasAttribute("contenteditable")) {
|
|
49
|
+
nodeDOM.contentEditable = "false";
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return {
|
|
53
|
+
...nodeView,
|
|
54
|
+
destroy () {
|
|
55
|
+
if (nodeView.destroy) {
|
|
56
|
+
nodeView.destroy();
|
|
57
|
+
}
|
|
58
|
+
wrapperDOM.removeChild(nodeDOM);
|
|
59
|
+
},
|
|
60
|
+
selectNode: nodeView.selectNode?.bind(nodeView) ?? (()=>{
|
|
61
|
+
if (nodeDOM instanceof HTMLElement) {
|
|
62
|
+
nodeDOM.classList.add("ProseMirror-selectednode");
|
|
63
|
+
}
|
|
64
|
+
setSelected(true);
|
|
65
|
+
}),
|
|
66
|
+
deselectNode: nodeView.deselectNode?.bind(nodeView) ?? (()=>{
|
|
67
|
+
if (nodeDOM instanceof HTMLElement) {
|
|
68
|
+
nodeDOM.classList.remove("ProseMirror-selectednode");
|
|
69
|
+
}
|
|
70
|
+
setSelected(false);
|
|
71
|
+
}),
|
|
72
|
+
stopEvent: nodeView.stopEvent?.bind(nodeView),
|
|
73
|
+
ignoreMutation: nodeView.ignoreMutation?.bind(nodeView)
|
|
74
|
+
};
|
|
75
|
+
}, nodeProps);
|
|
76
|
+
const children = !node.isLeaf && contentDOM ? /*#__PURE__*/ createPortal(/*#__PURE__*/ React.createElement(ChildDescriptorsContext.Provider, {
|
|
77
|
+
value: childContextValue
|
|
78
|
+
}, /*#__PURE__*/ React.createElement(ChildNodeViews, {
|
|
140
79
|
getPos: getPos,
|
|
141
80
|
node: node,
|
|
142
81
|
innerDecorations: innerDeco
|
|
143
|
-
}), contentDOM)
|
|
144
|
-
const
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
}
|
|
82
|
+
})), contentDOM) : null;
|
|
83
|
+
const innerElement = /*#__PURE__*/ createElement(node.isInline ? "span" : "div", {
|
|
84
|
+
ref: innerRef
|
|
85
|
+
}, children);
|
|
86
|
+
const props = {
|
|
87
|
+
...selected || node.type.spec.draggable ? {
|
|
88
|
+
draggable: true
|
|
89
|
+
} : null,
|
|
90
|
+
ref
|
|
91
|
+
};
|
|
92
|
+
return /*#__PURE__*/ cloneElement(outerDeco.reduce(wrapInDeco, innerElement), props);
|
|
153
93
|
});
|