@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.
Files changed (149) hide show
  1. package/dist/cjs/AbstractEditorView.js +4 -0
  2. package/dist/cjs/ReactEditorView.js +156 -0
  3. package/dist/cjs/StaticEditorView.js +86 -0
  4. package/dist/cjs/components/ChildNodeViews.js +58 -29
  5. package/dist/cjs/components/CustomNodeView.js +77 -137
  6. package/dist/cjs/{hooks/useNodePos.js → components/DefaultNodeView.js} +24 -26
  7. package/dist/cjs/components/DocNodeView.js +33 -41
  8. package/dist/cjs/components/MarkView.js +1 -2
  9. package/dist/cjs/components/NativeWidgetView.js +2 -3
  10. package/dist/cjs/components/NodeView.js +31 -21
  11. package/dist/cjs/components/ProseMirror.js +25 -17
  12. package/dist/cjs/components/ProseMirrorDoc.js +7 -27
  13. package/dist/cjs/components/ReactNodeView.js +98 -61
  14. package/dist/cjs/components/SeparatorHackView.js +1 -2
  15. package/dist/cjs/components/TextNodeView.js +4 -5
  16. package/dist/cjs/components/TrailingHackView.js +1 -2
  17. package/dist/cjs/components/WidgetView.js +2 -4
  18. package/dist/cjs/constants.js +33 -0
  19. package/dist/cjs/hooks/useEditor.js +33 -229
  20. package/dist/cjs/hooks/useEditorEffect.js +2 -2
  21. package/dist/cjs/hooks/useEditorEventCallback.js +8 -5
  22. package/dist/cjs/hooks/useIgnoreMutation.js +1 -1
  23. package/dist/cjs/hooks/useNodeViewDescriptor.js +123 -80
  24. package/dist/cjs/hooks/useReactKeys.js +1 -1
  25. package/dist/cjs/hooks/useSelectNode.js +9 -7
  26. package/dist/cjs/hooks/useStopEvent.js +1 -1
  27. package/dist/cjs/plugins/beforeInputPlugin.js +12 -0
  28. package/dist/cjs/testing/editorViewTestHelpers.js +0 -2
  29. package/dist/cjs/viewdesc.js +104 -25
  30. package/dist/esm/AbstractEditorView.js +1 -0
  31. package/dist/esm/ReactEditorView.js +156 -0
  32. package/dist/esm/StaticEditorView.js +76 -0
  33. package/dist/esm/components/ChildNodeViews.js +59 -31
  34. package/dist/esm/components/CustomNodeView.js +78 -138
  35. package/dist/esm/components/DefaultNodeView.js +16 -0
  36. package/dist/esm/components/DocNodeView.js +33 -41
  37. package/dist/esm/components/MarkView.js +1 -2
  38. package/dist/esm/components/NativeWidgetView.js +2 -3
  39. package/dist/esm/components/NodeView.js +32 -22
  40. package/dist/esm/components/ProseMirror.js +25 -17
  41. package/dist/esm/components/ProseMirrorDoc.js +7 -28
  42. package/dist/esm/components/ReactNodeView.js +99 -62
  43. package/dist/esm/components/SeparatorHackView.js +1 -2
  44. package/dist/esm/components/TextNodeView.js +4 -5
  45. package/dist/esm/components/TrailingHackView.js +1 -2
  46. package/dist/esm/components/WidgetView.js +2 -4
  47. package/dist/esm/constants.js +15 -0
  48. package/dist/esm/hooks/useEditor.js +29 -218
  49. package/dist/esm/hooks/useEditorEffect.js +2 -2
  50. package/dist/esm/hooks/useEditorEventCallback.js +8 -5
  51. package/dist/esm/hooks/useIgnoreMutation.js +1 -1
  52. package/dist/esm/hooks/useNodeViewDescriptor.js +125 -82
  53. package/dist/esm/hooks/useReactKeys.js +1 -1
  54. package/dist/esm/hooks/useSelectNode.js +9 -7
  55. package/dist/esm/hooks/useStopEvent.js +1 -1
  56. package/dist/esm/plugins/beforeInputPlugin.js +12 -0
  57. package/dist/esm/testing/editorViewTestHelpers.js +0 -2
  58. package/dist/esm/viewdesc.js +94 -18
  59. package/dist/tsconfig.tsbuildinfo +1 -1
  60. package/dist/types/AbstractEditorView.d.ts +27 -0
  61. package/dist/types/ReactEditorView.d.ts +80 -0
  62. package/dist/types/StaticEditorView.d.ts +24 -0
  63. package/dist/types/components/ChildNodeViews.d.ts +2 -2
  64. package/dist/types/components/CustomNodeView.d.ts +3 -3
  65. package/dist/types/components/DefaultNodeView.d.ts +3 -0
  66. package/dist/types/components/DocNodeView.d.ts +9 -17
  67. package/dist/types/components/MarkView.d.ts +2 -2
  68. package/dist/types/components/NativeWidgetView.d.ts +2 -2
  69. package/dist/types/components/NodeView.d.ts +5 -5
  70. package/dist/types/components/NodeViewComponentProps.d.ts +3 -4
  71. package/dist/types/components/ProseMirrorDoc.d.ts +14 -8
  72. package/dist/types/components/ReactNodeView.d.ts +4 -2
  73. package/dist/types/components/SeparatorHackView.d.ts +2 -2
  74. package/dist/types/components/TextNodeView.d.ts +4 -3
  75. package/dist/types/components/TrailingHackView.d.ts +2 -2
  76. package/dist/types/components/WidgetView.d.ts +2 -2
  77. package/dist/types/constants.d.ts +4 -0
  78. package/dist/types/contexts/EditorContext.d.ts +6 -4
  79. package/dist/types/contexts/IgnoreMutationContext.d.ts +2 -1
  80. package/dist/types/contexts/NodeViewContext.d.ts +3 -1
  81. package/dist/types/contexts/SelectNodeContext.d.ts +3 -1
  82. package/dist/types/contexts/StopEventContext.d.ts +2 -1
  83. package/dist/types/decorations/computeDocDeco.d.ts +3 -2
  84. package/dist/types/decorations/viewDecorations.d.ts +3 -2
  85. package/dist/types/hooks/useEditor.d.ts +5 -46
  86. package/dist/types/hooks/useNodeViewDescriptor.d.ts +18 -10
  87. package/dist/types/hooks/useReactKeys.d.ts +1 -1
  88. package/dist/types/hooks/useSelectNode.d.ts +2 -1
  89. package/dist/types/props.d.ts +3 -3
  90. package/dist/types/viewdesc.d.ts +29 -11
  91. package/package.json +7 -3
  92. package/dist/cjs/components/Editor.js +0 -28
  93. package/dist/cjs/components/NodeViews.js +0 -73
  94. package/dist/cjs/components/__tests__/LayoutGroup.test.js +0 -141
  95. package/dist/cjs/components/__tests__/ProseMirror.test.js +0 -255
  96. package/dist/cjs/contexts/NodeViewsContext.js +0 -10
  97. package/dist/cjs/hooks/__tests__/useEditorViewLayoutEffect.test.js +0 -107
  98. package/dist/cjs/hooks/__tests__/useNodeViews.test.js +0 -159
  99. package/dist/cjs/hooks/useClientOnly.js +0 -19
  100. package/dist/cjs/hooks/useEditorView.js +0 -100
  101. package/dist/cjs/hooks/useNodeViews.js +0 -100
  102. package/dist/cjs/nodeViews/createReactNodeViewConstructor.js +0 -244
  103. package/dist/cjs/nodeViews/phrasingContentTags.js +0 -57
  104. package/dist/cjs/plugins/__tests__/react.test.js +0 -139
  105. package/dist/cjs/plugins/react.js +0 -71
  106. package/dist/cjs/selection/SelectionDOMObserver.js +0 -171
  107. package/dist/cjs/selection/hasFocusAndSelection.js +0 -35
  108. package/dist/cjs/selection/selectionFromDOM.js +0 -77
  109. package/dist/cjs/selection/selectionToDOM.js +0 -226
  110. package/dist/cjs/ssr.js +0 -85
  111. package/dist/esm/components/Editor.js +0 -15
  112. package/dist/esm/components/NodeViews.js +0 -26
  113. package/dist/esm/components/__tests__/LayoutGroup.test.js +0 -98
  114. package/dist/esm/components/__tests__/ProseMirror.test.js +0 -207
  115. package/dist/esm/contexts/NodeViewsContext.js +0 -9
  116. package/dist/esm/hooks/__tests__/useEditorViewLayoutEffect.test.js +0 -98
  117. package/dist/esm/hooks/__tests__/useNodeViews.test.js +0 -116
  118. package/dist/esm/hooks/useClientOnly.js +0 -9
  119. package/dist/esm/hooks/useEditorView.js +0 -99
  120. package/dist/esm/hooks/useNodePos.js +0 -16
  121. package/dist/esm/hooks/useNodeViews.js +0 -53
  122. package/dist/esm/nodeViews/createReactNodeViewConstructor.js +0 -214
  123. package/dist/esm/nodeViews/phrasingContentTags.js +0 -49
  124. package/dist/esm/plugins/__tests__/react.test.js +0 -135
  125. package/dist/esm/plugins/react.js +0 -64
  126. package/dist/esm/selection/SelectionDOMObserver.js +0 -161
  127. package/dist/esm/selection/hasFocusAndSelection.js +0 -17
  128. package/dist/esm/selection/selectionFromDOM.js +0 -59
  129. package/dist/esm/selection/selectionToDOM.js +0 -196
  130. package/dist/esm/ssr.js +0 -82
  131. package/dist/types/components/Editor.d.ts +0 -7
  132. package/dist/types/components/NodeViews.d.ts +0 -6
  133. package/dist/types/components/__tests__/LayoutGroup.test.d.ts +0 -1
  134. package/dist/types/contexts/NodeViewsContext.d.ts +0 -19
  135. package/dist/types/hooks/__tests__/useEditorViewLayoutEffect.test.d.ts +0 -1
  136. package/dist/types/hooks/__tests__/useNodeViews.test.d.ts +0 -1
  137. package/dist/types/hooks/useClientOnly.d.ts +0 -1
  138. package/dist/types/hooks/useEditorView.d.ts +0 -23
  139. package/dist/types/hooks/useNodePos.d.ts +0 -9
  140. package/dist/types/hooks/useNodeViews.d.ts +0 -5
  141. package/dist/types/nodeViews/createReactNodeViewConstructor.d.ts +0 -48
  142. package/dist/types/nodeViews/phrasingContentTags.d.ts +0 -1
  143. package/dist/types/plugins/__tests__/react.test.d.ts +0 -1
  144. package/dist/types/plugins/react.d.ts +0 -21
  145. package/dist/types/selection/SelectionDOMObserver.d.ts +0 -33
  146. package/dist/types/selection/hasFocusAndSelection.d.ts +0 -3
  147. package/dist/types/selection/selectionFromDOM.d.ts +0 -4
  148. package/dist/types/selection/selectionToDOM.d.ts +0 -9
  149. package/dist/types/ssr.d.ts +0 -19
@@ -8,14 +8,10 @@ Object.defineProperty(exports, "CustomNodeView", {
8
8
  return CustomNodeView;
9
9
  }
10
10
  });
11
- const _prosemirrorstate = require("prosemirror-state");
11
+ const _prosemirrormodel = require("prosemirror-model");
12
12
  const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
13
13
  const _reactdom = require("react-dom");
14
14
  const _ChildDescriptorsContext = require("../contexts/ChildDescriptorsContext.js");
15
- const _EditorContext = require("../contexts/EditorContext.js");
16
- const _useClientLayoutEffect = require("../hooks/useClientLayoutEffect.js");
17
- const _useClientOnly = require("../hooks/useClientOnly.js");
18
- const _useForceUpdate = require("../hooks/useForceUpdate.js");
19
15
  const _useNodeViewDescriptor = require("../hooks/useNodeViewDescriptor.js");
20
16
  const _ChildNodeViews = require("./ChildNodeViews.js");
21
17
  function _getRequireWildcardCache(nodeInterop) {
@@ -60,145 +56,89 @@ function _interop_require_wildcard(obj, nodeInterop) {
60
56
  return newObj;
61
57
  }
62
58
  const CustomNodeView = /*#__PURE__*/ (0, _react.memo)(function CustomNodeView(param) {
63
- let { customNodeView, node, getPos, innerDeco, outerDeco } = param;
64
- const { view } = (0, _react.useContext)(_EditorContext.EditorContext);
65
- const domRef = (0, _react.useRef)(null);
66
- const nodeDomRef = (0, _react.useRef)(null);
67
- const contentDomRef = (0, _react.useRef)(null);
68
- const getPosFunc = (0, _react.useRef)(()=>getPos.current()).current;
69
- const nodeRef = (0, _react.useRef)(node);
70
- nodeRef.current = node;
71
- const outerDecoRef = (0, _react.useRef)(outerDeco);
72
- outerDecoRef.current = outerDeco;
73
- const innerDecoRef = (0, _react.useRef)(innerDeco);
74
- innerDecoRef.current = innerDeco;
75
- const customNodeViewRootRef = (0, _react.useRef)(null);
76
- const customNodeViewRef = (0, _react.useRef)(null);
77
- const forceUpdate = (0, _useForceUpdate.useForceUpdate)();
78
- const isOnClient = (0, _useClientOnly.useClientOnly)();
79
- // In Strict/Concurrent mode, layout effects can be destroyed/re-run
80
- // independently of renders. We need to ensure that if the
81
- // destructor that destroys the node view is called, we then recreate
82
- // the node view when the layout effect is re-run.
83
- (0, _useClientLayoutEffect.useClientLayoutEffect)(()=>{
84
- if (!customNodeViewRef.current) {
85
- customNodeViewRef.current = customNodeView(nodeRef.current, // customNodeView will only be set if view is set, and we can only reach
86
- // this line if customNodeView is set
87
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
88
- view, getPosFunc, outerDecoRef.current, innerDecoRef.current);
89
- if (customNodeViewRef.current.stopEvent) {
90
- setStopEvent(customNodeViewRef.current.stopEvent.bind(customNodeViewRef.current));
91
- }
92
- if (customNodeViewRef.current.selectNode) {
93
- setSelectNode(customNodeViewRef.current.selectNode.bind(customNodeViewRef.current), customNodeViewRef.current.deselectNode?.bind(customNodeViewRef.current) ?? (()=>{}));
94
- }
95
- if (customNodeViewRef.current.ignoreMutation) {
96
- setIgnoreMutation(customNodeViewRef.current.ignoreMutation.bind(customNodeViewRef.current));
97
- }
98
- // If we've reconstructed the nodeview, then we need to
99
- // recreate the portal into its contentDOM, which happens
100
- // during the render. So we need to trigger a re-render!
101
- forceUpdate();
102
- }
103
- if (!customNodeViewRootRef.current) return;
104
- const { dom } = customNodeViewRef.current;
105
- if (customNodeViewRootRef.current.firstChild === dom) {
106
- return;
107
- }
108
- nodeDomRef.current = customNodeViewRootRef.current;
109
- customNodeViewRootRef.current.appendChild(dom);
110
- // Layout effects can run multiple times — if this effect
111
- // destroyed and recreated this node view, then we need to
112
- // resync the selectNode state
113
- if (view?.state.selection instanceof _prosemirrorstate.NodeSelection && view.state.selection.node === nodeRef.current) {
114
- customNodeViewRef.current.selectNode?.();
115
- }
116
- const nodeView = customNodeViewRef.current;
117
- return ()=>{
118
- nodeView.destroy?.();
119
- customNodeViewRef.current = null;
120
- };
121
- // setStopEvent, setSelectNodee, and setIgnoreMutation are all stable
122
- // functions and don't need to be added to the dependencies. They also
123
- // can't be, because they come from useNodeViewDescriptor, which
124
- // _has_ to be called after this hook, so that the effects run
125
- // in the correct order
126
- // eslint-disable-next-line react-hooks/exhaustive-deps
127
- }, [
128
- customNodeView,
129
- getPosFunc,
130
- view
131
- ]);
132
- (0, _useClientLayoutEffect.useClientLayoutEffect)(()=>{
133
- if (!customNodeView || !customNodeViewRef.current) return;
134
- const { destroy, update } = customNodeViewRef.current;
135
- const updated = update?.call(customNodeViewRef.current, node, outerDeco, innerDeco) ?? true;
136
- if (updated) return;
137
- destroy?.call(customNodeViewRef.current);
138
- if (!customNodeViewRootRef.current) return;
139
- customNodeViewRef.current = customNodeView(nodeRef.current, // customNodeView will only be set if view is set, and we can only reach
140
- // this line if customNodeView is set
141
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
142
- view, getPosFunc, outerDecoRef.current, innerDecoRef.current);
143
- const { dom } = customNodeViewRef.current;
144
- nodeDomRef.current = customNodeViewRootRef.current;
145
- customNodeViewRootRef.current.appendChild(dom);
146
- }, [
147
- customNodeView,
148
- view,
149
- innerDeco,
59
+ let { constructor, node, getPos, innerDeco, outerDeco } = param;
60
+ const ref = (0, _react.useRef)(null);
61
+ const innerRef = (0, _react.useRef)(null);
62
+ const [selected, setSelected] = (0, _react.useState)(false);
63
+ const nodeProps = (0, _react.useMemo)(()=>({
64
+ node,
65
+ getPos,
66
+ decorations: outerDeco,
67
+ innerDecorations: innerDeco
68
+ }), [
150
69
  node,
151
- outerDeco,
152
70
  getPos,
153
- getPosFunc
154
- ]);
155
- const { childDescriptors, nodeViewDescRef, setStopEvent, setSelectNode, setIgnoreMutation } = (0, _useNodeViewDescriptor.useNodeViewDescriptor)(node, getPosFunc, domRef, nodeDomRef, innerDeco, outerDeco, undefined, contentDomRef);
156
- const childContextValue = (0, _react.useMemo)(()=>({
157
- parentRef: nodeViewDescRef,
158
- siblingsRef: childDescriptors
159
- }), [
160
- childDescriptors,
161
- nodeViewDescRef
71
+ outerDeco,
72
+ innerDeco
162
73
  ]);
163
- if (!isOnClient) return null;
164
- // In order to render the correct element with the correct
165
- // props below, we have to call the customNodeView in the
166
- // render function here. We only do this once, and the
167
- // results are stored in a ref but not actually appended
168
- // to the DOM until a client effect
169
- if (!customNodeViewRef.current) {
170
- customNodeViewRef.current = customNodeView(nodeRef.current, // customNodeView will only be set if view is set, and we can only reach
171
- // this line if customNodeView is set
172
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
173
- view, ()=>getPos.current(), outerDecoRef.current, innerDecoRef.current);
174
- if (customNodeViewRef.current.stopEvent) {
175
- setStopEvent(customNodeViewRef.current.stopEvent.bind(customNodeViewRef.current));
74
+ const createNodeView = function() {
75
+ for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
76
+ args[_key] = arguments[_key];
77
+ }
78
+ const nodeView = constructor(...args);
79
+ if (!nodeView || !nodeView.dom) {
80
+ const spec = node.type.spec.toDOM?.(node);
81
+ if (!spec) {
82
+ throw new Error(`Node spec for ${node.type.name} is missing toDOM`);
83
+ }
84
+ return _prosemirrormodel.DOMSerializer.renderSpec(document, spec, null);
176
85
  }
177
- if (customNodeViewRef.current.selectNode) {
178
- setSelectNode(customNodeViewRef.current.selectNode.bind(customNodeViewRef.current), customNodeViewRef.current.deselectNode?.bind(customNodeViewRef.current) ?? (()=>{}));
86
+ return nodeView;
87
+ };
88
+ const { childContextValue, contentDOM } = (0, _useNodeViewDescriptor.useNodeViewDescriptor)(ref, function() {
89
+ for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
90
+ args[_key] = arguments[_key];
179
91
  }
180
- if (customNodeViewRef.current.ignoreMutation) {
181
- setIgnoreMutation(customNodeViewRef.current.ignoreMutation.bind(customNodeViewRef.current));
92
+ setSelected(false);
93
+ const nodeView = createNodeView(...args);
94
+ const contentDOM = nodeView.contentDOM;
95
+ const nodeDOM = nodeView.dom;
96
+ const wrapperDOM = innerRef.current ?? ref.current;
97
+ wrapperDOM.appendChild(nodeDOM);
98
+ if (!contentDOM && nodeDOM instanceof HTMLElement && nodeDOM.tagName !== "BR") {
99
+ if (!nodeDOM.hasAttribute("contenteditable")) {
100
+ nodeDOM.contentEditable = "false";
101
+ }
182
102
  }
183
- }
184
- const { contentDOM } = customNodeViewRef.current;
185
- contentDomRef.current = contentDOM ?? null;
186
- const element = /*#__PURE__*/ (0, _react.createElement)(node.isInline ? "span" : "div", {
187
- ref: customNodeViewRootRef,
188
- contentEditable: !!contentDOM,
189
- suppressContentEditableWarning: true
190
- }, contentDOM && /*#__PURE__*/ (0, _reactdom.createPortal)(/*#__PURE__*/ _react.default.createElement(_ChildNodeViews.ChildNodeViews, {
103
+ return {
104
+ ...nodeView,
105
+ destroy () {
106
+ if (nodeView.destroy) {
107
+ nodeView.destroy();
108
+ }
109
+ wrapperDOM.removeChild(nodeDOM);
110
+ },
111
+ selectNode: nodeView.selectNode?.bind(nodeView) ?? (()=>{
112
+ if (nodeDOM instanceof HTMLElement) {
113
+ nodeDOM.classList.add("ProseMirror-selectednode");
114
+ }
115
+ setSelected(true);
116
+ }),
117
+ deselectNode: nodeView.deselectNode?.bind(nodeView) ?? (()=>{
118
+ if (nodeDOM instanceof HTMLElement) {
119
+ nodeDOM.classList.remove("ProseMirror-selectednode");
120
+ }
121
+ setSelected(false);
122
+ }),
123
+ stopEvent: nodeView.stopEvent?.bind(nodeView),
124
+ ignoreMutation: nodeView.ignoreMutation?.bind(nodeView)
125
+ };
126
+ }, nodeProps);
127
+ const children = !node.isLeaf && contentDOM ? /*#__PURE__*/ (0, _reactdom.createPortal)(/*#__PURE__*/ _react.default.createElement(_ChildDescriptorsContext.ChildDescriptorsContext.Provider, {
128
+ value: childContextValue
129
+ }, /*#__PURE__*/ _react.default.createElement(_ChildNodeViews.ChildNodeViews, {
191
130
  getPos: getPos,
192
131
  node: node,
193
132
  innerDecorations: innerDeco
194
- }), contentDOM));
195
- const decoratedElement = /*#__PURE__*/ (0, _react.cloneElement)(outerDeco.reduce(_ChildNodeViews.wrapInDeco, element), // eslint-disable-next-line @typescript-eslint/no-explicit-any
196
- outerDeco.some((d)=>d.type.attrs.nodeName) ? {
197
- ref: domRef
198
- } : // we've already passed the domRef to the NodeView component
199
- // as a prop
200
- undefined);
201
- return /*#__PURE__*/ _react.default.createElement(_ChildDescriptorsContext.ChildDescriptorsContext.Provider, {
202
- value: childContextValue
203
- }, decoratedElement);
133
+ })), contentDOM) : null;
134
+ const innerElement = /*#__PURE__*/ (0, _react.createElement)(node.isInline ? "span" : "div", {
135
+ ref: innerRef
136
+ }, children);
137
+ const props = {
138
+ ...selected || node.type.spec.draggable ? {
139
+ draggable: true
140
+ } : null,
141
+ ref
142
+ };
143
+ return /*#__PURE__*/ (0, _react.cloneElement)(outerDeco.reduce(_ChildNodeViews.wrapInDeco, innerElement), props);
204
144
  });
@@ -2,19 +2,14 @@
2
2
  Object.defineProperty(exports, "__esModule", {
3
3
  value: true
4
4
  });
5
- function _export(target, all) {
6
- for(var name in all)Object.defineProperty(target, name, {
7
- enumerable: true,
8
- get: all[name]
9
- });
10
- }
11
- _export(exports, {
12
- NodePosProvider: ()=>NodePosProvider,
13
- useNodePos: ()=>useNodePos
5
+ Object.defineProperty(exports, "DefaultNodeView", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return DefaultNodeView;
9
+ }
14
10
  });
15
- const _react = /*#__PURE__*/ _interopRequireWildcard(require("react"));
16
- const _reactJs = require("../plugins/react.js");
17
- const _useEditorStateJs = require("./useEditorState.js");
11
+ const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
12
+ const _OutputSpec = require("./OutputSpec.js");
18
13
  function _getRequireWildcardCache(nodeInterop) {
19
14
  if (typeof WeakMap !== "function") return null;
20
15
  var cacheBabelInterop = new WeakMap();
@@ -23,7 +18,7 @@ function _getRequireWildcardCache(nodeInterop) {
23
18
  return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
24
19
  })(nodeInterop);
25
20
  }
26
- function _interopRequireWildcard(obj, nodeInterop) {
21
+ function _interop_require_wildcard(obj, nodeInterop) {
27
22
  if (!nodeInterop && obj && obj.__esModule) {
28
23
  return obj;
29
24
  }
@@ -36,7 +31,9 @@ function _interopRequireWildcard(obj, nodeInterop) {
36
31
  if (cache && cache.has(obj)) {
37
32
  return cache.get(obj);
38
33
  }
39
- var newObj = {};
34
+ var newObj = {
35
+ __proto__: null
36
+ };
40
37
  var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
41
38
  for(var key in obj){
42
39
  if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
@@ -54,16 +51,17 @@ function _interopRequireWildcard(obj, nodeInterop) {
54
51
  }
55
52
  return newObj;
56
53
  }
57
- const NodePosContext = /*#__PURE__*/ (0, _react.createContext)(null);
58
- function NodePosProvider(param) {
59
- let { nodeKey , children } = param;
60
- const editorState = (0, _useEditorStateJs.useEditorState)();
61
- const pluginState = _reactJs.reactPluginKey.getState(editorState);
62
- if (!pluginState) return /*#__PURE__*/ _react.default.createElement(_react.default.Fragment, null, children);
63
- return /*#__PURE__*/ _react.default.createElement(NodePosContext.Provider, {
64
- value: pluginState.keyToPos.get(nodeKey) ?? 0
54
+ const DefaultNodeView = /*#__PURE__*/ (0, _react.forwardRef)(function DefaultNodeView(param, ref) {
55
+ let { nodeProps: { node }, children, ...props } = param;
56
+ const spec = (0, _react.useMemo)(()=>node.type.spec.toDOM?.(node), [
57
+ node
58
+ ]);
59
+ if (!spec) {
60
+ throw new Error(`Node spec for ${node.type.name} is missing toDOM`);
61
+ }
62
+ return /*#__PURE__*/ _react.default.createElement(_OutputSpec.OutputSpec, {
63
+ ...props,
64
+ outputSpec: spec,
65
+ ref: ref
65
66
  }, children);
66
- }
67
- function useNodePos() {
68
- return (0, _react.useContext)(NodePosContext);
69
- }
67
+ });
@@ -1,6 +1,4 @@
1
- // TODO: I must be missing something, but I do not know why
2
- // this linting rule is only broken in this file
3
- /* eslint-disable react/prop-types */ "use strict";
1
+ "use strict";
4
2
  Object.defineProperty(exports, "__esModule", {
5
3
  value: true
6
4
  });
@@ -55,50 +53,44 @@ function _interop_require_wildcard(obj, nodeInterop) {
55
53
  }
56
54
  return newObj;
57
55
  }
58
- const getPos = {
59
- current () {
60
- return -1;
61
- }
62
- };
63
56
  const DocNodeView = /*#__PURE__*/ (0, _react.memo)(/*#__PURE__*/ (0, _react.forwardRef)(function DocNodeView(param, ref) {
64
- let { className, node, innerDeco, outerDeco, as, viewDesc, ...elementProps } = param;
57
+ let { as, node, getPos, decorations, innerDecorations, setMount, ...elementProps } = param;
65
58
  const innerRef = (0, _react.useRef)(null);
66
- (0, _react.useImperativeHandle)(ref, ()=>{
67
- return innerRef.current;
68
- }, []);
69
- const { childDescriptors, nodeViewDescRef } = (0, _useNodeViewDescriptor.useNodeViewDescriptor)(node, ()=>getPos.current(), innerRef, innerRef, innerDeco, outerDeco, viewDesc);
70
- const childContextValue = (0, _react.useMemo)(()=>({
71
- parentRef: nodeViewDescRef,
72
- siblingsRef: childDescriptors
59
+ (0, _react.useImperativeHandle)(ref, ()=>innerRef.current);
60
+ (0, _react.useImperativeHandle)(setMount, ()=>innerRef.current);
61
+ const nodeProps = (0, _react.useMemo)(()=>({
62
+ node,
63
+ getPos,
64
+ decorations,
65
+ innerDecorations
73
66
  }), [
74
- childDescriptors,
75
- nodeViewDescRef
67
+ node,
68
+ getPos,
69
+ decorations,
70
+ innerDecorations
76
71
  ]);
77
- const props = {
78
- ...elementProps,
79
- ref: innerRef,
80
- className,
81
- suppressContentEditableWarning: true
82
- };
83
- const element = as ? /*#__PURE__*/ (0, _react.cloneElement)(as, props, /*#__PURE__*/ _react.default.createElement(_ChildDescriptorsContext.ChildDescriptorsContext.Provider, {
84
- value: childContextValue
85
- }, /*#__PURE__*/ _react.default.createElement(_ChildNodeViews.ChildNodeViews, {
86
- getPos: getPos,
87
- node: node,
88
- innerDecorations: innerDeco
89
- }))) : /*#__PURE__*/ (0, _react.createElement)("div", props, /*#__PURE__*/ _react.default.createElement(_ChildDescriptorsContext.ChildDescriptorsContext.Provider, {
72
+ const { childContextValue } = (0, _useNodeViewDescriptor.useNodeViewDescriptor)(innerRef, ()=>{
73
+ const dom = innerRef.current;
74
+ return {
75
+ dom,
76
+ contentDOM: dom,
77
+ update () {
78
+ return true;
79
+ }
80
+ };
81
+ }, nodeProps);
82
+ const children = /*#__PURE__*/ _react.default.createElement(_ChildDescriptorsContext.ChildDescriptorsContext.Provider, {
90
83
  value: childContextValue
91
84
  }, /*#__PURE__*/ _react.default.createElement(_ChildNodeViews.ChildNodeViews, {
92
85
  getPos: getPos,
93
86
  node: node,
94
- innerDecorations: innerDeco
95
- })));
96
- if (!node) return element;
97
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
98
- const nodeDecorations = outerDeco.filter((deco)=>!deco.inline);
99
- if (!nodeDecorations.length) {
100
- return element;
101
- }
102
- const wrapped = nodeDecorations.reduce(_ChildNodeViews.wrapInDeco, element);
103
- return wrapped;
87
+ innerDecorations: innerDecorations
88
+ }));
89
+ const props = {
90
+ ...elementProps,
91
+ suppressContentEditableWarning: true,
92
+ ref: innerRef
93
+ };
94
+ const element = as ? /*#__PURE__*/ (0, _react.cloneElement)(as, props, children) : /*#__PURE__*/ (0, _react.createElement)("div", props, children);
95
+ return nodeProps.decorations.reduce(_ChildNodeViews.wrapInDeco, element);
104
96
  }));
@@ -83,7 +83,7 @@ const MarkView = /*#__PURE__*/ (0, _react.memo)(/*#__PURE__*/ (0, _react.forward
83
83
  if (!domRef.current) return;
84
84
  const firstChildDesc = childDescriptors.current[0];
85
85
  if (!viewDescRef.current) {
86
- viewDescRef.current = new _viewdesc.MarkViewDesc(parentRef.current, childDescriptors.current, ()=>getPos.current(), mark, domRef.current, firstChildDesc?.dom.parentElement ?? domRef.current, {
86
+ viewDescRef.current = new _viewdesc.MarkViewDesc(parentRef.current, childDescriptors.current, getPos, mark, domRef.current, firstChildDesc?.dom.parentElement ?? domRef.current, {
87
87
  dom: domRef.current,
88
88
  contentDOM: firstChildDesc?.dom.parentElement ?? domRef.current
89
89
  });
@@ -93,7 +93,6 @@ const MarkView = /*#__PURE__*/ (0, _react.memo)(/*#__PURE__*/ (0, _react.forward
93
93
  viewDescRef.current.children = childDescriptors.current;
94
94
  viewDescRef.current.spec.contentDOM = viewDescRef.current.contentDOM = firstChildDesc?.dom.parentElement ?? domRef.current;
95
95
  viewDescRef.current.mark = mark;
96
- viewDescRef.current.getPos = ()=>getPos.current();
97
96
  }
98
97
  if (!siblingsRef.current.includes(viewDescRef.current)) {
99
98
  siblingsRef.current.push(viewDescRef.current);
@@ -75,7 +75,7 @@ function NativeWidgetView(param) {
75
75
  if (!rootDomRef.current) return;
76
76
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
77
77
  const toDOM = widget.type.toDOM;
78
- let dom = typeof toDOM === "function" ? toDOM(view, ()=>getPos.current()) : toDOM;
78
+ let dom = typeof toDOM === "function" ? toDOM(view, getPos) : toDOM;
79
79
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
80
80
  if (!widget.type.spec.raw) {
81
81
  if (dom.nodeType != 1) {
@@ -92,11 +92,10 @@ function NativeWidgetView(param) {
92
92
  (0, _useClientLayoutEffect.useClientLayoutEffect)(()=>{
93
93
  if (!rootDomRef.current) return;
94
94
  if (!viewDescRef.current) {
95
- viewDescRef.current = new _viewdesc.WidgetViewDesc(parentRef.current, ()=>getPos.current(), widget, rootDomRef.current);
95
+ viewDescRef.current = new _viewdesc.WidgetViewDesc(parentRef.current, getPos, widget, rootDomRef.current);
96
96
  } else {
97
97
  viewDescRef.current.parent = parentRef.current;
98
98
  viewDescRef.current.widget = widget;
99
- viewDescRef.current.getPos = ()=>getPos.current();
100
99
  viewDescRef.current.dom = rootDomRef.current;
101
100
  }
102
101
  if (!siblingsRef.current.includes(viewDescRef.current)) {
@@ -9,8 +9,9 @@ Object.defineProperty(exports, "NodeView", {
9
9
  }
10
10
  });
11
11
  const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
12
- const _EditorContext = require("../contexts/EditorContext.js");
12
+ const _NodeViewContext = require("../contexts/NodeViewContext.js");
13
13
  const _CustomNodeView = require("./CustomNodeView.js");
14
+ const _DefaultNodeView = require("./DefaultNodeView.js");
14
15
  const _ReactNodeView = require("./ReactNodeView.js");
15
16
  function _getRequireWildcardCache(nodeInterop) {
16
17
  if (typeof WeakMap !== "function") return null;
@@ -53,24 +54,33 @@ function _interop_require_wildcard(obj, nodeInterop) {
53
54
  }
54
55
  return newObj;
55
56
  }
56
- const NodeView = /*#__PURE__*/ (0, _react.memo)(function NodeView(param) {
57
- let { outerDeco, getPos, node, innerDeco, ...props } = param;
58
- const { view } = (0, _react.useContext)(_EditorContext.EditorContext);
59
- const customNodeView = view?.someProp("nodeViews", (nodeViews)=>nodeViews?.[node.type.name]);
60
- if (customNodeView) {
61
- return /*#__PURE__*/ _react.default.createElement(_CustomNodeView.CustomNodeView, {
62
- customNodeView: customNodeView,
63
- node: node,
64
- innerDeco: innerDeco,
65
- outerDeco: outerDeco,
66
- getPos: getPos
67
- });
68
- }
69
- return /*#__PURE__*/ _react.default.createElement(_ReactNodeView.ReactNodeView, {
70
- node: node,
71
- innerDeco: innerDeco,
72
- outerDeco: outerDeco,
73
- getPos: getPos,
74
- ...props
75
- });
57
+ const NodeView = /*#__PURE__*/ (0, _react.memo)(function NodeView(props) {
58
+ const { components, constructors } = (0, _react.useContext)(_NodeViewContext.NodeViewContext);
59
+ const component = components[props.node.type.name] ?? _DefaultNodeView.DefaultNodeView;
60
+ const constructor = constructors[props.node.type.name];
61
+ // Construct a wrapper component so that the node view remounts when either
62
+ // its component or constructor changes. A React node view would remount if
63
+ // its underlying component changed without this wrapper, but a custom node
64
+ // view otherwise uses the same React components for all custom node views.
65
+ const Component = (0, _react.useMemo)(()=>{
66
+ if (constructor) {
67
+ return function NodeView(props) {
68
+ return /*#__PURE__*/ _react.default.createElement(_CustomNodeView.CustomNodeView, {
69
+ constructor: constructor,
70
+ ...props
71
+ });
72
+ };
73
+ } else {
74
+ return function NodeView(props) {
75
+ return /*#__PURE__*/ _react.default.createElement(_ReactNodeView.ReactNodeView, {
76
+ component: component,
77
+ ...props
78
+ });
79
+ };
80
+ }
81
+ }, [
82
+ constructor,
83
+ component
84
+ ]);
85
+ return /*#__PURE__*/ _react.default.createElement(Component, props);
76
86
  });
@@ -8,7 +8,6 @@ Object.defineProperty(exports, "ProseMirror", {
8
8
  return ProseMirror;
9
9
  }
10
10
  });
11
- const _prosemirrorview = require("prosemirror-view");
12
11
  const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
13
12
  const _EditorContext = require("../contexts/EditorContext.js");
14
13
  const _EditorStateContext = require("../contexts/EditorStateContext.js");
@@ -59,7 +58,9 @@ function _interop_require_wildcard(obj, nodeInterop) {
59
58
  }
60
59
  return newObj;
61
60
  }
62
- const EMPTY_OUTER_DECOS = [];
61
+ function getPos() {
62
+ return -1;
63
+ }
63
64
  function ProseMirrorInner(param) {
64
65
  let { className, children, nodeViews, customNodeViews, ...props } = param;
65
66
  const [mount, setMount] = (0, _react.useState)(null);
@@ -67,26 +68,33 @@ function ProseMirrorInner(param) {
67
68
  ...props,
68
69
  nodeViews: customNodeViews
69
70
  });
70
- const innerDecos = editor.view ? (0, _viewDecorations.viewDecorations)(editor.view, editor.cursorWrapper) : _prosemirrorview.DecorationSet.empty;
71
- const outerDecos = editor.view ? (0, _computeDocDeco.computeDocDeco)(editor.view) : EMPTY_OUTER_DECOS;
72
- const nodeViewContextValue = (0, _react.useMemo)(()=>({
73
- nodeViews: nodeViews ?? {}
74
- }), [
71
+ const nodeViewConstructors = editor.view.nodeViews;
72
+ const nodeViewContextValue = (0, _react.useMemo)(()=>{
73
+ return {
74
+ components: {
75
+ ...nodeViews
76
+ },
77
+ constructors: nodeViewConstructors
78
+ };
79
+ }, [
80
+ nodeViewConstructors,
75
81
  nodeViews
76
82
  ]);
83
+ const node = state.doc;
84
+ const decorations = (0, _computeDocDeco.computeDocDeco)(editor.view);
85
+ const innerDecorations = (0, _viewDecorations.viewDecorations)(editor.view, editor.cursorWrapper);
77
86
  const docNodeViewContextValue = (0, _react.useMemo)(()=>({
78
- className: className,
79
- setMount: setMount,
80
- node: editor.view?.state.doc,
81
- innerDeco: innerDecos,
82
- outerDeco: outerDecos,
83
- viewDesc: editor.docViewDescRef.current
87
+ className,
88
+ setMount,
89
+ node,
90
+ getPos,
91
+ decorations,
92
+ innerDecorations
84
93
  }), [
85
94
  className,
86
- editor.docViewDescRef,
87
- editor.view?.state.doc,
88
- innerDecos,
89
- outerDecos
95
+ node,
96
+ decorations,
97
+ innerDecorations
90
98
  ]);
91
99
  return /*#__PURE__*/ _react.default.createElement(_EditorContext.EditorContext.Provider, {
92
100
  value: editor
@@ -13,11 +13,10 @@ _export(exports, {
13
13
  return DocNodeViewContext;
14
14
  },
15
15
  ProseMirrorDoc: function() {
16
- return ForwardedProseMirrorDoc;
16
+ return ProseMirrorDoc;
17
17
  }
18
18
  });
19
19
  const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
20
- const _ChildDescriptorsContext = require("../contexts/ChildDescriptorsContext.js");
21
20
  const _DocNodeView = require("./DocNodeView.js");
22
21
  function _getRequireWildcardCache(nodeInterop) {
23
22
  if (typeof WeakMap !== "function") return null;
@@ -61,32 +60,13 @@ function _interop_require_wildcard(obj, nodeInterop) {
61
60
  return newObj;
62
61
  }
63
62
  const DocNodeViewContext = /*#__PURE__*/ (0, _react.createContext)(null);
64
- function ProseMirrorDoc(param, ref) {
63
+ const ProseMirrorDoc = /*#__PURE__*/ (0, _react.forwardRef)(function ProseMirrorDoc(param, ref) {
65
64
  let { as, ...props } = param;
66
- const childDescriptors = (0, _react.useRef)([]);
67
- const innerRef = (0, _react.useRef)(null);
68
- const { setMount, ...docProps } = (0, _react.useContext)(DocNodeViewContext);
69
- const viewDescRef = (0, _react.useRef)(undefined);
70
- (0, _react.useImperativeHandle)(ref, ()=>{
71
- return innerRef.current;
72
- }, []);
73
- const childContextValue = (0, _react.useMemo)(()=>({
74
- parentRef: viewDescRef,
75
- siblingsRef: childDescriptors
76
- }), [
77
- childDescriptors,
78
- viewDescRef
79
- ]);
80
- return /*#__PURE__*/ _react.default.createElement(_ChildDescriptorsContext.ChildDescriptorsContext.Provider, {
81
- value: childContextValue
82
- }, /*#__PURE__*/ _react.default.createElement(_DocNodeView.DocNodeView, {
83
- ref: (el)=>{
84
- innerRef.current = el;
85
- setMount(el);
86
- },
65
+ const docProps = (0, _react.useContext)(DocNodeViewContext);
66
+ return /*#__PURE__*/ _react.default.createElement(_DocNodeView.DocNodeView, {
67
+ ref: ref,
87
68
  ...props,
88
69
  ...docProps,
89
70
  as: as
90
- }));
91
- }
92
- const ForwardedProseMirrorDoc = /*#__PURE__*/ (0, _react.forwardRef)(ProseMirrorDoc);
71
+ });
72
+ });