@handlewithcare/react-prosemirror 2.0.0

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 (209) hide show
  1. package/LICENSE.txt +12 -0
  2. package/README.md +705 -0
  3. package/dist/cjs/browser.js +53 -0
  4. package/dist/cjs/components/ChildNodeViews.js +376 -0
  5. package/dist/cjs/components/CursorWrapper.js +91 -0
  6. package/dist/cjs/components/CustomNodeView.js +79 -0
  7. package/dist/cjs/components/DocNodeView.js +104 -0
  8. package/dist/cjs/components/LayoutGroup.js +111 -0
  9. package/dist/cjs/components/MarkView.js +115 -0
  10. package/dist/cjs/components/NativeWidgetView.js +109 -0
  11. package/dist/cjs/components/NodeView.js +196 -0
  12. package/dist/cjs/components/NodeViewComponentProps.js +4 -0
  13. package/dist/cjs/components/OutputSpec.js +88 -0
  14. package/dist/cjs/components/ProseMirror.js +103 -0
  15. package/dist/cjs/components/ProseMirrorDoc.js +92 -0
  16. package/dist/cjs/components/SeparatorHackView.js +100 -0
  17. package/dist/cjs/components/TextNodeView.js +112 -0
  18. package/dist/cjs/components/TrailingHackView.js +90 -0
  19. package/dist/cjs/components/WidgetView.js +95 -0
  20. package/dist/cjs/components/WidgetViewComponentProps.js +4 -0
  21. package/dist/cjs/components/__tests__/ProseMirror.composition.test.js +398 -0
  22. package/dist/cjs/components/__tests__/ProseMirror.domchange.test.js +270 -0
  23. package/dist/cjs/components/__tests__/ProseMirror.draw-decoration.test.js +1010 -0
  24. package/dist/cjs/components/__tests__/ProseMirror.draw.test.js +337 -0
  25. package/dist/cjs/components/__tests__/ProseMirror.node-view.test.js +315 -0
  26. package/dist/cjs/components/__tests__/ProseMirror.selection.test.js +444 -0
  27. package/dist/cjs/components/__tests__/ProseMirror.test.js +382 -0
  28. package/dist/cjs/contexts/ChildDescriptorsContext.js +19 -0
  29. package/dist/cjs/contexts/EditorContext.js +12 -0
  30. package/dist/cjs/contexts/EditorStateContext.js +12 -0
  31. package/dist/cjs/contexts/LayoutGroupContext.js +12 -0
  32. package/dist/cjs/contexts/NodeViewContext.js +12 -0
  33. package/dist/cjs/contexts/SelectNodeContext.js +12 -0
  34. package/dist/cjs/contexts/StopEventContext.js +12 -0
  35. package/dist/cjs/contexts/__tests__/DeferredLayoutEffects.test.js +141 -0
  36. package/dist/cjs/decorations/ReactWidgetType.js +58 -0
  37. package/dist/cjs/decorations/computeDocDeco.js +44 -0
  38. package/dist/cjs/decorations/internalTypes.js +4 -0
  39. package/dist/cjs/decorations/iterDeco.js +79 -0
  40. package/dist/cjs/decorations/viewDecorations.js +163 -0
  41. package/dist/cjs/dom.js +142 -0
  42. package/dist/cjs/hooks/__tests__/useEditorViewLayoutEffect.test.js +108 -0
  43. package/dist/cjs/hooks/useClientOnly.js +18 -0
  44. package/dist/cjs/hooks/useComponentEventListeners.js +39 -0
  45. package/dist/cjs/hooks/useEditor.js +287 -0
  46. package/dist/cjs/hooks/useEditorEffect.js +35 -0
  47. package/dist/cjs/hooks/useEditorEventCallback.js +33 -0
  48. package/dist/cjs/hooks/useEditorEventListener.js +34 -0
  49. package/dist/cjs/hooks/useEditorState.js +16 -0
  50. package/dist/cjs/hooks/useForceUpdate.js +15 -0
  51. package/dist/cjs/hooks/useLayoutGroupEffect.js +19 -0
  52. package/dist/cjs/hooks/useNodeViewDescriptor.js +115 -0
  53. package/dist/cjs/hooks/useReactKeys.js +17 -0
  54. package/dist/cjs/hooks/useSelectNode.js +28 -0
  55. package/dist/cjs/hooks/useStopEvent.js +24 -0
  56. package/dist/cjs/index.js +53 -0
  57. package/dist/cjs/package.json +3 -0
  58. package/dist/cjs/plugins/__tests__/reactKeys.test.js +81 -0
  59. package/dist/cjs/plugins/beforeInputPlugin.js +143 -0
  60. package/dist/cjs/plugins/componentEventListeners.js +35 -0
  61. package/dist/cjs/plugins/componentEventListenersPlugin.js +35 -0
  62. package/dist/cjs/plugins/reactKeys.js +96 -0
  63. package/dist/cjs/props.js +269 -0
  64. package/dist/cjs/selection/SelectionDOMObserver.js +174 -0
  65. package/dist/cjs/selection/hasFocusAndSelection.js +35 -0
  66. package/dist/cjs/selection/selectionFromDOM.js +77 -0
  67. package/dist/cjs/selection/selectionToDOM.js +226 -0
  68. package/dist/cjs/ssr.js +85 -0
  69. package/dist/cjs/testing/editorViewTestHelpers.js +111 -0
  70. package/dist/cjs/testing/setupProseMirrorView.js +94 -0
  71. package/dist/cjs/viewdesc.js +664 -0
  72. package/dist/esm/browser.js +43 -0
  73. package/dist/esm/components/ChildNodeViews.js +318 -0
  74. package/dist/esm/components/CursorWrapper.js +40 -0
  75. package/dist/esm/components/CustomNodeView.js +28 -0
  76. package/dist/esm/components/DocNodeView.js +53 -0
  77. package/dist/esm/components/LayoutGroup.js +66 -0
  78. package/dist/esm/components/MarkView.js +64 -0
  79. package/dist/esm/components/NativeWidgetView.js +58 -0
  80. package/dist/esm/components/NodeView.js +145 -0
  81. package/dist/esm/components/NodeViewComponentProps.js +1 -0
  82. package/dist/esm/components/OutputSpec.js +38 -0
  83. package/dist/esm/components/ProseMirror.js +52 -0
  84. package/dist/esm/components/ProseMirrorDoc.js +34 -0
  85. package/dist/esm/components/SeparatorHackView.js +49 -0
  86. package/dist/esm/components/TextNodeView.js +102 -0
  87. package/dist/esm/components/TrailingHackView.js +39 -0
  88. package/dist/esm/components/WidgetView.js +44 -0
  89. package/dist/esm/components/WidgetViewComponentProps.js +1 -0
  90. package/dist/esm/components/__tests__/ProseMirror.composition.test.js +395 -0
  91. package/dist/esm/components/__tests__/ProseMirror.domchange.test.js +266 -0
  92. package/dist/esm/components/__tests__/ProseMirror.draw-decoration.test.js +967 -0
  93. package/dist/esm/components/__tests__/ProseMirror.draw.test.js +294 -0
  94. package/dist/esm/components/__tests__/ProseMirror.node-view.test.js +272 -0
  95. package/dist/esm/components/__tests__/ProseMirror.selection.test.js +440 -0
  96. package/dist/esm/components/__tests__/ProseMirror.test.js +339 -0
  97. package/dist/esm/contexts/ChildDescriptorsContext.js +9 -0
  98. package/dist/esm/contexts/EditorContext.js +7 -0
  99. package/dist/esm/contexts/EditorStateContext.js +2 -0
  100. package/dist/esm/contexts/LayoutGroupContext.js +2 -0
  101. package/dist/esm/contexts/NodeViewContext.js +2 -0
  102. package/dist/esm/contexts/SelectNodeContext.js +2 -0
  103. package/dist/esm/contexts/StopEventContext.js +2 -0
  104. package/dist/esm/contexts/__tests__/DeferredLayoutEffects.test.js +98 -0
  105. package/dist/esm/decorations/ReactWidgetType.js +40 -0
  106. package/dist/esm/decorations/computeDocDeco.js +44 -0
  107. package/dist/esm/decorations/internalTypes.js +1 -0
  108. package/dist/esm/decorations/iterDeco.js +73 -0
  109. package/dist/esm/decorations/viewDecorations.js +163 -0
  110. package/dist/esm/dom.js +105 -0
  111. package/dist/esm/hooks/__tests__/useEditorViewLayoutEffect.test.js +99 -0
  112. package/dist/esm/hooks/useClientOnly.js +8 -0
  113. package/dist/esm/hooks/useComponentEventListeners.js +54 -0
  114. package/dist/esm/hooks/useEditor.js +278 -0
  115. package/dist/esm/hooks/useEditorEffect.js +38 -0
  116. package/dist/esm/hooks/useEditorEventCallback.js +35 -0
  117. package/dist/esm/hooks/useEditorEventListener.js +28 -0
  118. package/dist/esm/hooks/useEditorState.js +8 -0
  119. package/dist/esm/hooks/useForceUpdate.js +8 -0
  120. package/dist/esm/hooks/useLayoutGroupEffect.js +9 -0
  121. package/dist/esm/hooks/useNodeViewDescriptor.js +105 -0
  122. package/dist/esm/hooks/useReactKeys.js +7 -0
  123. package/dist/esm/hooks/useSelectNode.js +18 -0
  124. package/dist/esm/hooks/useStopEvent.js +14 -0
  125. package/dist/esm/index.js +11 -0
  126. package/dist/esm/plugins/__tests__/reactKeys.test.js +77 -0
  127. package/dist/esm/plugins/beforeInputPlugin.js +133 -0
  128. package/dist/esm/plugins/componentEventListeners.js +25 -0
  129. package/dist/esm/plugins/componentEventListenersPlugin.js +25 -0
  130. package/dist/esm/plugins/reactKeys.js +81 -0
  131. package/dist/esm/props.js +251 -0
  132. package/dist/esm/selection/SelectionDOMObserver.js +164 -0
  133. package/dist/esm/selection/hasFocusAndSelection.js +17 -0
  134. package/dist/esm/selection/selectionFromDOM.js +59 -0
  135. package/dist/esm/selection/selectionToDOM.js +196 -0
  136. package/dist/esm/ssr.js +82 -0
  137. package/dist/esm/testing/editorViewTestHelpers.js +88 -0
  138. package/dist/esm/testing/setupProseMirrorView.js +76 -0
  139. package/dist/esm/viewdesc.js +654 -0
  140. package/dist/tsconfig.tsbuildinfo +1 -0
  141. package/dist/types/browser.d.ts +15 -0
  142. package/dist/types/components/ChildNodeViews.d.ts +9 -0
  143. package/dist/types/components/CursorWrapper.d.ts +5 -0
  144. package/dist/types/components/CustomNodeView.d.ts +21 -0
  145. package/dist/types/components/DocNodeView.d.ts +20 -0
  146. package/dist/types/components/LayoutGroup.d.ts +12 -0
  147. package/dist/types/components/MarkView.d.ts +9 -0
  148. package/dist/types/components/NativeWidgetView.d.ts +8 -0
  149. package/dist/types/components/NodeView.d.ts +11 -0
  150. package/dist/types/components/NodeViewComponentProps.d.ts +12 -0
  151. package/dist/types/components/OutputSpec.d.ts +8 -0
  152. package/dist/types/components/ProseMirror.d.ts +15 -0
  153. package/dist/types/components/ProseMirrorDoc.d.ts +10 -0
  154. package/dist/types/components/SeparatorHackView.d.ts +6 -0
  155. package/dist/types/components/TextNodeView.d.ts +23 -0
  156. package/dist/types/components/TrailingHackView.d.ts +6 -0
  157. package/dist/types/components/WidgetView.d.ts +8 -0
  158. package/dist/types/components/WidgetViewComponentProps.d.ts +6 -0
  159. package/dist/types/components/__tests__/ProseMirror.composition.test.d.ts +1 -0
  160. package/dist/types/components/__tests__/ProseMirror.domchange.test.d.ts +1 -0
  161. package/dist/types/components/__tests__/ProseMirror.draw-decoration.test.d.ts +1 -0
  162. package/dist/types/components/__tests__/ProseMirror.draw.test.d.ts +1 -0
  163. package/dist/types/components/__tests__/ProseMirror.node-view.test.d.ts +1 -0
  164. package/dist/types/components/__tests__/ProseMirror.selection.test.d.ts +1 -0
  165. package/dist/types/components/__tests__/ProseMirror.test.d.ts +1 -0
  166. package/dist/types/contexts/ChildDescriptorsContext.d.ts +6 -0
  167. package/dist/types/contexts/EditorContext.d.ts +14 -0
  168. package/dist/types/contexts/EditorStateContext.d.ts +2 -0
  169. package/dist/types/contexts/LayoutGroupContext.d.ts +5 -0
  170. package/dist/types/contexts/NodeViewContext.d.ts +6 -0
  171. package/dist/types/contexts/SelectNodeContext.d.ts +3 -0
  172. package/dist/types/contexts/StopEventContext.d.ts +3 -0
  173. package/dist/types/contexts/__tests__/DeferredLayoutEffects.test.d.ts +1 -0
  174. package/dist/types/decorations/ReactWidgetType.d.ts +39 -0
  175. package/dist/types/decorations/computeDocDeco.d.ts +13 -0
  176. package/dist/types/decorations/internalTypes.d.ts +16 -0
  177. package/dist/types/decorations/iterDeco.d.ts +3 -0
  178. package/dist/types/decorations/viewDecorations.d.ts +13 -0
  179. package/dist/types/dom.d.ts +22 -0
  180. package/dist/types/hooks/__tests__/useEditorViewLayoutEffect.test.d.ts +1 -0
  181. package/dist/types/hooks/useClientOnly.d.ts +1 -0
  182. package/dist/types/hooks/useComponentEventListeners.d.ts +33 -0
  183. package/dist/types/hooks/useEditor.d.ts +66 -0
  184. package/dist/types/hooks/useEditorEffect.d.ts +17 -0
  185. package/dist/types/hooks/useEditorEventCallback.d.ts +15 -0
  186. package/dist/types/hooks/useEditorEventListener.d.ts +8 -0
  187. package/dist/types/hooks/useEditorState.d.ts +5 -0
  188. package/dist/types/hooks/useForceUpdate.d.ts +5 -0
  189. package/dist/types/hooks/useLayoutGroupEffect.d.ts +3 -0
  190. package/dist/types/hooks/useNodeViewDescriptor.d.ts +11 -0
  191. package/dist/types/hooks/useReactKeys.d.ts +5 -0
  192. package/dist/types/hooks/useSelectNode.d.ts +1 -0
  193. package/dist/types/hooks/useStopEvent.d.ts +2 -0
  194. package/dist/types/index.d.ts +12 -0
  195. package/dist/types/plugins/__tests__/reactKeys.test.d.ts +1 -0
  196. package/dist/types/plugins/beforeInputPlugin.d.ts +3 -0
  197. package/dist/types/plugins/componentEventListeners.d.ts +4 -0
  198. package/dist/types/plugins/componentEventListenersPlugin.d.ts +4 -0
  199. package/dist/types/plugins/reactKeys.d.ts +19 -0
  200. package/dist/types/props.d.ts +1174 -0
  201. package/dist/types/selection/SelectionDOMObserver.d.ts +34 -0
  202. package/dist/types/selection/hasFocusAndSelection.d.ts +3 -0
  203. package/dist/types/selection/selectionFromDOM.d.ts +4 -0
  204. package/dist/types/selection/selectionToDOM.d.ts +9 -0
  205. package/dist/types/ssr.d.ts +19 -0
  206. package/dist/types/testing/editorViewTestHelpers.d.ts +23 -0
  207. package/dist/types/testing/setupProseMirrorView.d.ts +2 -0
  208. package/dist/types/viewdesc.d.ts +131 -0
  209. package/package.json +113 -0
@@ -0,0 +1,287 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
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
+ ReactEditorView: function() {
13
+ return ReactEditorView;
14
+ },
15
+ useEditor: function() {
16
+ return useEditor;
17
+ }
18
+ });
19
+ const _prosemirrormodel = require("prosemirror-model");
20
+ const _prosemirrorstate = require("prosemirror-state");
21
+ const _prosemirrorview = require("prosemirror-view");
22
+ const _react = require("react");
23
+ const _reactdom = require("react-dom");
24
+ const _beforeInputPlugin = require("../plugins/beforeInputPlugin.js");
25
+ const _SelectionDOMObserver = require("../selection/SelectionDOMObserver.js");
26
+ const _ssr = require("../ssr.js");
27
+ const _viewdesc = require("../viewdesc.js");
28
+ const _useComponentEventListeners = require("./useComponentEventListeners.js");
29
+ const _useForceUpdate = require("./useForceUpdate.js");
30
+ function buildNodeViews(view) {
31
+ const result = Object.create(null);
32
+ function add(obj) {
33
+ for(const prop in obj)if (!Object.prototype.hasOwnProperty.call(result, prop)) // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
34
+ result[prop] = obj[prop];
35
+ }
36
+ view.someProp("nodeViews", add);
37
+ view.someProp("markViews", add);
38
+ return result;
39
+ }
40
+ function changedNodeViews(a, b) {
41
+ let nA = 0, nB = 0;
42
+ for(const prop in a){
43
+ if (a[prop] != b[prop]) return true;
44
+ nA++;
45
+ }
46
+ for(const _ in b)nB++;
47
+ return nA != nB;
48
+ }
49
+ function changedProps(a, b) {
50
+ for (const prop of Object.keys(a)){
51
+ if (a[prop] !== b[prop]) return true;
52
+ }
53
+ return false;
54
+ }
55
+ function getEditable(view) {
56
+ return !view.someProp("editable", (value)=>value(view.state) === false);
57
+ }
58
+ let ReactEditorView = class ReactEditorView extends _prosemirrorview.EditorView {
59
+ shouldUpdatePluginViews = false;
60
+ oldProps;
61
+ _props;
62
+ constructor(place, props){
63
+ // Call the superclass constructor with an empty
64
+ // document and limited props. We'll set everything
65
+ // else ourselves.
66
+ const cleanup = (0, _ssr.setSsrStubs)();
67
+ super(place, {
68
+ state: _prosemirrorstate.EditorState.create({
69
+ schema: props.state.schema,
70
+ plugins: props.state.plugins
71
+ }),
72
+ plugins: props.plugins
73
+ });
74
+ cleanup();
75
+ this.shouldUpdatePluginViews = true;
76
+ this._props = props;
77
+ this.oldProps = {
78
+ state: props.state
79
+ };
80
+ this.state = props.state;
81
+ // @ts-expect-error We're making use of knowledge of internal attributes here
82
+ this.domObserver.stop();
83
+ // @ts-expect-error We're making use of knowledge of internal attributes here
84
+ this.domObserver = new _SelectionDOMObserver.SelectionDOMObserver(this);
85
+ // @ts-expect-error We're making use of knowledge of internal attributes here
86
+ this.domObserver.start();
87
+ this.editable = getEditable(this);
88
+ // Destroy the DOM created by the default
89
+ // ProseMirror ViewDesc implementation; we
90
+ // have a NodeViewDesc from React instead.
91
+ // @ts-expect-error We're making use of knowledge of internal attributes here
92
+ this.docView.dom.replaceChildren();
93
+ // @ts-expect-error We're making use of knowledge of internal attributes here
94
+ this.docView = props.docView;
95
+ }
96
+ /**
97
+ * Whether the EditorView's updateStateInner method thinks that the
98
+ * docView needs to be blown away and redrawn.
99
+ *
100
+ * @privateremarks
101
+ *
102
+ * When ProseMirror View detects that the EditorState has been reconfigured
103
+ * to provide new custom node views, it calls an internal function that
104
+ * we can't override in order to recreate the entire editor DOM.
105
+ *
106
+ * This property mimics that check, so that we can replace the EditorView
107
+ * with another of our own, preventing ProseMirror View from taking over
108
+ * DOM management responsibility.
109
+ */ get needsRedraw() {
110
+ if (this.oldProps.state.plugins === this._props.state.plugins && this._props.plugins === this.oldProps.plugins) {
111
+ return false;
112
+ }
113
+ const newNodeViews = buildNodeViews(this);
114
+ // @ts-expect-error Internal property
115
+ return changedNodeViews(this.nodeViews, newNodeViews);
116
+ }
117
+ /**
118
+ * Like setProps, but without executing any side effects.
119
+ * Safe to use in a component render method.
120
+ */ pureSetProps(props) {
121
+ // this.oldProps = this.props;
122
+ this._props = {
123
+ ...this._props,
124
+ ...props
125
+ };
126
+ this.state = this._props.state;
127
+ this.editable = getEditable(this);
128
+ }
129
+ /**
130
+ * Triggers any side effects that have been queued by previous
131
+ * calls to pureSetProps.
132
+ */ runPendingEffects() {
133
+ if (changedProps(this.props, this.oldProps)) {
134
+ const newProps = this.props;
135
+ this._props = this.oldProps;
136
+ this.state = this._props.state;
137
+ this.update(newProps);
138
+ }
139
+ }
140
+ update(props) {
141
+ super.update(props);
142
+ // Ensure that side effects aren't re-triggered until
143
+ // pureSetProps is called again
144
+ this.oldProps = props;
145
+ }
146
+ updatePluginViews(prevState) {
147
+ if (this.shouldUpdatePluginViews) {
148
+ // @ts-expect-error We're making use of knowledge of internal methods here
149
+ super.updatePluginViews(prevState);
150
+ }
151
+ }
152
+ // We want to trigger the default EditorView cleanup, but without
153
+ // the actual view.dom cleanup (which React will have already handled).
154
+ // So we give the EditorView a dummy DOM element and ask it to clean up
155
+ destroy() {
156
+ // @ts-expect-error we're intentionally overwriting this property
157
+ // to prevent side effects
158
+ this.dom = document.createElement("div");
159
+ super.destroy();
160
+ }
161
+ };
162
+ const EMPTY_SCHEMA = new _prosemirrormodel.Schema({
163
+ nodes: {
164
+ doc: {
165
+ content: "text*"
166
+ },
167
+ text: {
168
+ inline: true
169
+ }
170
+ }
171
+ });
172
+ const EMPTY_STATE = _prosemirrorstate.EditorState.create({
173
+ schema: EMPTY_SCHEMA
174
+ });
175
+ let didWarnValueDefaultValue = false;
176
+ function useEditor(mount, options) {
177
+ if (process.env.NODE_ENV !== "production") {
178
+ if (options.defaultState !== undefined && options.state !== undefined && !didWarnValueDefaultValue) {
179
+ console.error("A component contains a ProseMirror editor with both value and defaultValue props. " + "ProseMirror editors must be either controlled or uncontrolled " + "(specify either the state prop, or the defaultState prop, but not both). " + "Decide between using a controlled or uncontrolled ProseMirror editor " + "and remove one of these props. More info: " + "https://reactjs.org/link/controlled-components");
180
+ didWarnValueDefaultValue = true;
181
+ }
182
+ }
183
+ const [cursorWrapper, _setCursorWrapper] = (0, _react.useState)(null);
184
+ const forceUpdate = (0, _useForceUpdate.useForceUpdate)();
185
+ const defaultState = options.defaultState ?? EMPTY_STATE;
186
+ const [_state, setState] = (0, _react.useState)(defaultState);
187
+ const state = options.state ?? _state;
188
+ const { componentEventListenersPlugin, registerEventListener, unregisterEventListener } = (0, _useComponentEventListeners.useComponentEventListeners)();
189
+ const setCursorWrapper = (0, _react.useCallback)((deco)=>{
190
+ (0, _reactdom.flushSync)(()=>{
191
+ _setCursorWrapper(deco);
192
+ });
193
+ }, []);
194
+ const plugins = (0, _react.useMemo)(()=>[
195
+ ...options.plugins ?? [],
196
+ componentEventListenersPlugin,
197
+ (0, _beforeInputPlugin.beforeInputPlugin)(setCursorWrapper)
198
+ ], [
199
+ options.plugins,
200
+ componentEventListenersPlugin,
201
+ setCursorWrapper
202
+ ]);
203
+ const dispatchTransaction = (0, _react.useCallback)(function dispatchTransaction(tr) {
204
+ (0, _reactdom.flushSync)(()=>{
205
+ if (!options.state) {
206
+ setState((s)=>s.apply(tr));
207
+ }
208
+ if (options.dispatchTransaction) {
209
+ options.dispatchTransaction.call(this, tr);
210
+ }
211
+ });
212
+ }, [
213
+ options.dispatchTransaction,
214
+ options.state
215
+ ]);
216
+ const cleanup = (0, _ssr.setSsrStubs)();
217
+ const tempDom = document.createElement("div");
218
+ cleanup();
219
+ const docViewDescRef = (0, _react.useRef)(new _viewdesc.NodeViewDesc(undefined, [], ()=>-1, state.doc, [], _prosemirrorview.DecorationSet.empty, tempDom, null, tempDom, ()=>false, ()=>{
220
+ /* The doc node can't have a node selection*/ }, ()=>{
221
+ /* The doc node can't have a node selection*/ }));
222
+ const directEditorProps = {
223
+ ...options,
224
+ state,
225
+ plugins,
226
+ dispatchTransaction,
227
+ docView: docViewDescRef.current
228
+ };
229
+ const [view, setView] = (0, _react.useState)(// During the initial render, we create something of a dummy
230
+ // EditorView. This allows us to ensure that the first render actually
231
+ // renders the document, which is necessary for SSR.
232
+ ()=>new ReactEditorView(null, directEditorProps));
233
+ (0, _react.useLayoutEffect)(()=>{
234
+ return ()=>{
235
+ view?.destroy();
236
+ };
237
+ }, [
238
+ view
239
+ ]);
240
+ // This rule is concerned about infinite updates due to the
241
+ // call to setView. These calls are deliberately conditional,
242
+ // so this is not a concern.
243
+ // eslint-disable-next-line react-hooks/exhaustive-deps
244
+ (0, _react.useLayoutEffect)(()=>{
245
+ if (!mount) {
246
+ setView(null);
247
+ return;
248
+ }
249
+ if (!view || view.dom !== mount) {
250
+ const newView = new ReactEditorView({
251
+ mount
252
+ }, directEditorProps);
253
+ setView(newView);
254
+ newView.dom.addEventListener("compositionend", forceUpdate);
255
+ return;
256
+ }
257
+ // TODO: We should be able to put this in previous branch,
258
+ // but we need to convince EditorView's constructor not to
259
+ // clear out the DOM when passed a mount that already has
260
+ // content in it, otherwise React blows up when it tries
261
+ // to clean up.
262
+ if (view.needsRedraw) {
263
+ setView(null);
264
+ return;
265
+ }
266
+ // @ts-expect-error Internal property - domObserver
267
+ view?.domObserver.selectionToDOM();
268
+ view?.runPendingEffects();
269
+ });
270
+ view?.pureSetProps(directEditorProps);
271
+ const editor = (0, _react.useMemo)(()=>({
272
+ view: view,
273
+ registerEventListener,
274
+ unregisterEventListener,
275
+ cursorWrapper,
276
+ docViewDescRef
277
+ }), [
278
+ view,
279
+ registerEventListener,
280
+ unregisterEventListener,
281
+ cursorWrapper
282
+ ]);
283
+ return {
284
+ editor,
285
+ state
286
+ };
287
+ }
@@ -0,0 +1,35 @@
1
+ /* Copyright (c) The New York Times Company */ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "useEditorEffect", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return useEditorEffect;
9
+ }
10
+ });
11
+ const _react = require("react");
12
+ const _EditorContext = require("../contexts/EditorContext.js");
13
+ const _useLayoutGroupEffect = require("./useLayoutGroupEffect.js");
14
+ function useEditorEffect(effect, dependencies) {
15
+ const { view } = (0, _react.useContext)(_EditorContext.EditorContext);
16
+ // The rules of hooks want `effect` to be included in the
17
+ // dependency list, but dependency issues for `effect` will
18
+ // be caught by the linter at the call-site for
19
+ // `useEditorViewLayoutEffect`.
20
+ // Note: we specifically don't want to re-run the effect
21
+ // every time it changes, because it will most likely
22
+ // be defined inline and run on every re-render.
23
+ (0, _useLayoutGroupEffect.useLayoutGroupEffect)(()=>{
24
+ if (view) {
25
+ return effect(view);
26
+ }
27
+ }, // The rules of hooks want to be able to statically
28
+ // verify the dependencies for the effect, but this will
29
+ // have already happened at the call-site.
30
+ // eslint-disable-next-line react-hooks/exhaustive-deps
31
+ dependencies && [
32
+ view,
33
+ ...dependencies
34
+ ]);
35
+ }
@@ -0,0 +1,33 @@
1
+ /* Copyright (c) The New York Times Company */ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "useEditorEventCallback", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return useEditorEventCallback;
9
+ }
10
+ });
11
+ const _react = require("react");
12
+ const _EditorContext = require("../contexts/EditorContext.js");
13
+ const _useEditorEffect = require("./useEditorEffect.js");
14
+ function useEditorEventCallback(callback) {
15
+ const ref = (0, _react.useRef)(callback);
16
+ const { view } = (0, _react.useContext)(_EditorContext.EditorContext);
17
+ (0, _useEditorEffect.useEditorEffect)(()=>{
18
+ ref.current = callback;
19
+ }, [
20
+ callback
21
+ ]);
22
+ return (0, _react.useCallback)(function() {
23
+ for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
24
+ args[_key] = arguments[_key];
25
+ }
26
+ if (view) {
27
+ return ref.current(view, ...args);
28
+ }
29
+ return;
30
+ }, [
31
+ view
32
+ ]);
33
+ }
@@ -0,0 +1,34 @@
1
+ /* Copyright (c) The New York Times Company */ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "useEditorEventListener", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return useEditorEventListener;
9
+ }
10
+ });
11
+ const _react = require("react");
12
+ const _EditorContext = require("../contexts/EditorContext.js");
13
+ const _useEditorEffect = require("./useEditorEffect.js");
14
+ function useEditorEventListener(eventType, handler) {
15
+ const { registerEventListener, unregisterEventListener } = (0, _react.useContext)(_EditorContext.EditorContext);
16
+ const ref = (0, _react.useRef)(handler);
17
+ (0, _useEditorEffect.useEditorEffect)(()=>{
18
+ ref.current = handler;
19
+ }, [
20
+ handler
21
+ ]);
22
+ const eventHandler = (0, _react.useCallback)(function(view, event) {
23
+ return ref.current.call(this, view, event);
24
+ }, []);
25
+ (0, _useEditorEffect.useEditorEffect)(()=>{
26
+ registerEventListener(eventType, eventHandler);
27
+ return ()=>unregisterEventListener(eventType, eventHandler);
28
+ }, [
29
+ eventHandler,
30
+ eventType,
31
+ registerEventListener,
32
+ unregisterEventListener
33
+ ]);
34
+ }
@@ -0,0 +1,16 @@
1
+ /* Copyright (c) The New York Times Company */ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "useEditorState", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return useEditorState;
9
+ }
10
+ });
11
+ const _react = require("react");
12
+ const _EditorStateContext = require("../contexts/EditorStateContext.js");
13
+ function useEditorState() {
14
+ const editorState = (0, _react.useContext)(_EditorStateContext.EditorStateContext);
15
+ return editorState;
16
+ }
@@ -0,0 +1,15 @@
1
+ /* Copyright (c) The New York Times Company */ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "useForceUpdate", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return useForceUpdate;
9
+ }
10
+ });
11
+ const _react = require("react");
12
+ function useForceUpdate() {
13
+ const [, forceUpdate] = (0, _react.useReducer)((x)=>x + 1, 0);
14
+ return forceUpdate;
15
+ }
@@ -0,0 +1,19 @@
1
+ /* Copyright (c) The New York Times Company */ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "useLayoutGroupEffect", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return useLayoutGroupEffect;
9
+ }
10
+ });
11
+ const _react = require("react");
12
+ const _LayoutGroupContext = require("../contexts/LayoutGroupContext.js");
13
+ function useLayoutGroupEffect(effect, deps) {
14
+ const register = (0, _react.useContext)(_LayoutGroupContext.LayoutGroupContext);
15
+ // The rule for hooks wants to statically verify the deps,
16
+ // but the dependencies are up to the caller, not this implementation.
17
+ // eslint-disable-next-line react-hooks/exhaustive-deps
18
+ (0, _react.useLayoutEffect)(()=>register(effect), deps);
19
+ }
@@ -0,0 +1,115 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "useNodeViewDescriptor", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return useNodeViewDescriptor;
9
+ }
10
+ });
11
+ const _react = require("react");
12
+ const _ChildDescriptorsContext = require("../contexts/ChildDescriptorsContext.js");
13
+ const _EditorContext = require("../contexts/EditorContext.js");
14
+ const _viewdesc = require("../viewdesc.js");
15
+ function useNodeViewDescriptor(node, getPos, domRef, nodeDomRef, innerDecorations, outerDecorations, viewDesc, contentDOMRef) {
16
+ const { view } = (0, _react.useContext)(_EditorContext.EditorContext);
17
+ const [hasContentDOM, setHasContentDOM] = (0, _react.useState)(true);
18
+ const nodeViewDescRef = (0, _react.useRef)(viewDesc);
19
+ const stopEvent = (0, _react.useRef)(()=>false);
20
+ const setStopEvent = (0, _react.useCallback)((newStopEvent)=>{
21
+ stopEvent.current = newStopEvent;
22
+ }, []);
23
+ const selectNode = (0, _react.useRef)(()=>{
24
+ if (!nodeDomRef.current || !node) return;
25
+ if (nodeDomRef.current.nodeType == 1) nodeDomRef.current.classList.add("ProseMirror-selectednode");
26
+ if (contentDOMRef?.current || !node.type.spec.draggable) (domRef?.current ?? nodeDomRef.current).draggable = true;
27
+ });
28
+ const deselectNode = (0, _react.useRef)(()=>{
29
+ if (!nodeDomRef.current || !node) return;
30
+ if (nodeDomRef.current.nodeType == 1) {
31
+ nodeDomRef.current.classList.remove("ProseMirror-selectednode");
32
+ if (contentDOMRef?.current || !node.type.spec.draggable) (domRef?.current ?? nodeDomRef.current).removeAttribute("draggable");
33
+ }
34
+ });
35
+ const setSelectNode = (0, _react.useCallback)((newSelectNode, newDeselectNode)=>{
36
+ selectNode.current = newSelectNode;
37
+ deselectNode.current = newDeselectNode;
38
+ }, []);
39
+ const { siblingsRef, parentRef } = (0, _react.useContext)(_ChildDescriptorsContext.ChildDescriptorsContext);
40
+ const childDescriptors = (0, _react.useRef)([]);
41
+ (0, _react.useLayoutEffect)(()=>{
42
+ const siblings = siblingsRef.current;
43
+ return ()=>{
44
+ if (!nodeViewDescRef.current) return;
45
+ if (siblings.includes(nodeViewDescRef.current)) {
46
+ const index = siblings.indexOf(nodeViewDescRef.current);
47
+ siblings.splice(index, 1);
48
+ }
49
+ };
50
+ }, [
51
+ siblingsRef
52
+ ]);
53
+ // eslint-disable-next-line react-hooks/exhaustive-deps
54
+ (0, _react.useLayoutEffect)(()=>{
55
+ if (!node || !nodeDomRef.current) return;
56
+ const firstChildDesc = childDescriptors.current[0];
57
+ if (!nodeViewDescRef.current) {
58
+ nodeViewDescRef.current = new _viewdesc.NodeViewDesc(parentRef.current, childDescriptors.current, getPos, node, outerDecorations, innerDecorations, domRef?.current ?? nodeDomRef.current, firstChildDesc?.dom.parentElement ?? null, nodeDomRef.current, (event)=>!!stopEvent.current(event), ()=>selectNode.current(), ()=>deselectNode.current());
59
+ } else {
60
+ nodeViewDescRef.current.parent = parentRef.current;
61
+ nodeViewDescRef.current.children = childDescriptors.current;
62
+ nodeViewDescRef.current.node = node;
63
+ nodeViewDescRef.current.getPos = getPos;
64
+ nodeViewDescRef.current.outerDeco = outerDecorations;
65
+ nodeViewDescRef.current.innerDeco = innerDecorations;
66
+ nodeViewDescRef.current.dom = domRef?.current ?? nodeDomRef.current;
67
+ // @ts-expect-error We have our own ViewDesc implementations
68
+ nodeViewDescRef.current.dom.pmViewDesc = nodeViewDescRef.current;
69
+ nodeViewDescRef.current.contentDOM = // If there's already a contentDOM, we can just
70
+ // keep it; it won't have changed. This is especially
71
+ // important during compositions, where the
72
+ // firstChildDesc might not have a correct dom node set yet.
73
+ contentDOMRef?.current ?? nodeViewDescRef.current.contentDOM ?? firstChildDesc?.dom.parentElement ?? null;
74
+ nodeViewDescRef.current.nodeDOM = nodeDomRef.current;
75
+ }
76
+ setHasContentDOM(nodeViewDescRef.current.contentDOM !== null);
77
+ if (!siblingsRef.current.includes(nodeViewDescRef.current)) {
78
+ siblingsRef.current.push(nodeViewDescRef.current);
79
+ }
80
+ siblingsRef.current.sort(_viewdesc.sortViewDescs);
81
+ for (const childDesc of childDescriptors.current){
82
+ childDesc.parent = nodeViewDescRef.current;
83
+ // Because TextNodeViews can't locate the DOM nodes
84
+ // for compositions, we need to override them here
85
+ if (childDesc instanceof _viewdesc.CompositionViewDesc) {
86
+ const compositionTopDOM = nodeViewDescRef.current.contentDOM?.firstChild;
87
+ if (!compositionTopDOM) throw new Error(`Started a composition but couldn't find the text node it belongs to.`);
88
+ let textDOM = compositionTopDOM;
89
+ while(textDOM.firstChild){
90
+ textDOM = textDOM.firstChild;
91
+ }
92
+ if (!textDOM || !(textDOM instanceof Text)) throw new Error(`Started a composition but couldn't find the text node it belongs to.`);
93
+ childDesc.dom = compositionTopDOM;
94
+ childDesc.textDOM = textDOM;
95
+ childDesc.text = textDOM.data;
96
+ // @ts-expect-error ???
97
+ childDesc.textDOM.pmViewDesc = childDesc;
98
+ // @ts-expect-error ???
99
+ view?.input.compositionNodes.push(childDesc);
100
+ }
101
+ }
102
+ return ()=>{
103
+ if (nodeViewDescRef.current?.children[0] instanceof _viewdesc.CompositionViewDesc && !view?.composing) {
104
+ nodeViewDescRef.current?.children[0].dom.parentNode?.removeChild(nodeViewDescRef.current?.children[0].dom);
105
+ }
106
+ };
107
+ });
108
+ return {
109
+ hasContentDOM,
110
+ childDescriptors,
111
+ nodeViewDescRef,
112
+ setStopEvent,
113
+ setSelectNode
114
+ };
115
+ }
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "useReactKeys", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return useReactKeys;
9
+ }
10
+ });
11
+ const _react = require("react");
12
+ const _EditorContext = require("../contexts/EditorContext.js");
13
+ const _reactKeys = require("../plugins/reactKeys.js");
14
+ function useReactKeys() {
15
+ const { view } = (0, _react.useContext)(_EditorContext.EditorContext);
16
+ return view && _reactKeys.reactKeysPluginKey.getState(view.state);
17
+ }
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "useSelectNode", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return useSelectNode;
9
+ }
10
+ });
11
+ const _react = require("react");
12
+ const _SelectNodeContext = require("../contexts/SelectNodeContext.js");
13
+ const _useEditorEffect = require("./useEditorEffect.js");
14
+ const _useEditorEventCallback = require("./useEditorEventCallback.js");
15
+ function useSelectNode(selectNode, deselectNode) {
16
+ const register = (0, _react.useContext)(_SelectNodeContext.SelectNodeContext);
17
+ const selectNodeMemo = (0, _useEditorEventCallback.useEditorEventCallback)(selectNode);
18
+ const deselectNodeMemo = (0, _useEditorEventCallback.useEditorEventCallback)(deselectNode ?? (()=>{
19
+ // empty
20
+ }));
21
+ return (0, _useEditorEffect.useEditorEffect)(()=>{
22
+ register(selectNodeMemo, deselectNodeMemo);
23
+ }, [
24
+ deselectNodeMemo,
25
+ register,
26
+ selectNodeMemo
27
+ ]);
28
+ }
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "useStopEvent", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return useStopEvent;
9
+ }
10
+ });
11
+ const _react = require("react");
12
+ const _StopEventContext = require("../contexts/StopEventContext.js");
13
+ const _useEditorEffect = require("./useEditorEffect.js");
14
+ const _useEditorEventCallback = require("./useEditorEventCallback.js");
15
+ function useStopEvent(stopEvent) {
16
+ const register = (0, _react.useContext)(_StopEventContext.StopEventContext);
17
+ const stopEventMemo = (0, _useEditorEventCallback.useEditorEventCallback)(stopEvent);
18
+ (0, _useEditorEffect.useEditorEffect)(()=>{
19
+ register(stopEventMemo);
20
+ }, [
21
+ register,
22
+ stopEventMemo
23
+ ]);
24
+ }