@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,226 @@
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
+ domIndex: function() {
13
+ return domIndex;
14
+ },
15
+ hasBlockDesc: function() {
16
+ return hasBlockDesc;
17
+ },
18
+ hasSelection: function() {
19
+ return hasSelection;
20
+ },
21
+ isEquivalentPosition: function() {
22
+ return isEquivalentPosition;
23
+ },
24
+ nodeSize: function() {
25
+ return nodeSize;
26
+ },
27
+ selectionToDOM: function() {
28
+ return selectionToDOM;
29
+ },
30
+ syncNodeSelection: function() {
31
+ return syncNodeSelection;
32
+ }
33
+ });
34
+ const _prosemirrorstate = require("prosemirror-state");
35
+ const _browser = require("../browser.js");
36
+ const isEquivalentPosition = function(node, off, targetNode, targetOff) {
37
+ return targetNode && (scanFor(node, off, targetNode, targetOff, -1) || scanFor(node, off, targetNode, targetOff, 1));
38
+ };
39
+ function hasBlockDesc(dom) {
40
+ let desc;
41
+ for(let cur = dom; cur; cur = cur.parentNode)if (desc = cur.pmViewDesc) break;
42
+ return desc && desc.node && desc.node.isBlock && (desc.dom == dom || desc.contentDOM == dom);
43
+ }
44
+ const atomElements = /^(img|br|input|textarea|hr)$/i;
45
+ function scanFor(node, off, targetNode, targetOff, dir) {
46
+ for(;;){
47
+ if (node == targetNode && off == targetOff) return true;
48
+ if (off == (dir < 0 ? 0 : nodeSize(node))) {
49
+ const parent = node.parentNode;
50
+ if (!parent || parent.nodeType != 1 || hasBlockDesc(node) || atomElements.test(node.nodeName) || node.contentEditable == "false") return false;
51
+ off = domIndex(node) + (dir < 0 ? 0 : 1);
52
+ node = parent;
53
+ } else if (node.nodeType == 1) {
54
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
55
+ node = node.childNodes[off + (dir < 0 ? -1 : 0)];
56
+ if (node.contentEditable == "false") return false;
57
+ off = dir < 0 ? nodeSize(node) : 0;
58
+ } else {
59
+ return false;
60
+ }
61
+ }
62
+ }
63
+ const domIndex = function(node) {
64
+ let n = node;
65
+ for(let index = 0;; index++){
66
+ n = n.previousSibling;
67
+ if (!n) return index;
68
+ }
69
+ };
70
+ function nodeSize(node) {
71
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
72
+ return node.nodeType == 3 ? node.nodeValue.length : node.childNodes.length;
73
+ }
74
+ function syncNodeSelection(view, sel) {
75
+ const v = view;
76
+ if (sel instanceof _prosemirrorstate.NodeSelection) {
77
+ const desc = v.docView.descAt(sel.from);
78
+ if (desc != v.lastSelectedViewDesc) {
79
+ clearNodeSelection(v);
80
+ if (desc) desc.selectNode();
81
+ v.lastSelectedViewDesc = desc;
82
+ }
83
+ } else {
84
+ clearNodeSelection(v);
85
+ }
86
+ }
87
+ // Clear all DOM statefulness of the last node selection.
88
+ function clearNodeSelection(view) {
89
+ const v = view;
90
+ if (v.lastSelectedViewDesc) {
91
+ if (v.lastSelectedViewDesc.parent) v.lastSelectedViewDesc.deselectNode();
92
+ v.lastSelectedViewDesc = undefined;
93
+ }
94
+ }
95
+ function hasSelection(view) {
96
+ const v = view;
97
+ const sel = v.domSelectionRange();
98
+ if (!sel.anchorNode) return false;
99
+ try {
100
+ // Firefox will raise 'permission denied' errors when accessing
101
+ // properties of `sel.anchorNode` when it's in a generated CSS
102
+ // element.
103
+ return v.dom.contains(sel.anchorNode.nodeType == 3 ? sel.anchorNode.parentNode : sel.anchorNode) && (v.editable || v.dom.contains(// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
104
+ sel.focusNode.nodeType == 3 ? sel.focusNode.parentNode : sel.focusNode));
105
+ } catch (_) {
106
+ return false;
107
+ }
108
+ }
109
+ function editorOwnsSelection(view) {
110
+ return view.editable ? view.hasFocus() : hasSelection(view) && document.activeElement && document.activeElement.contains(view.dom);
111
+ }
112
+ function selectCursorWrapper(view) {
113
+ const v = view;
114
+ const domSel = v.domSelection(), range = document.createRange();
115
+ if (!domSel) return;
116
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
117
+ const node = v.cursorWrapper.dom, img = node.nodeName == "IMG";
118
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
119
+ if (img) range.setStart(node.parentNode, domIndex(node) + 1);
120
+ else range.setStart(node, 0);
121
+ range.collapse(true);
122
+ domSel.removeAllRanges();
123
+ domSel.addRange(range);
124
+ // Kludge to kill 'control selection' in IE11 when selecting an
125
+ // invisible cursor wrapper, since that would result in those weird
126
+ // resize handles and a selection that considers the absolutely
127
+ // positioned wrapper, rather than the root editable node, the
128
+ // focused element.
129
+ if (!img && !v.state.selection.visible && _browser.browser.ie && _browser.browser.ie_version <= 11) {
130
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
131
+ node.disabled = true;
132
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
133
+ node.disabled = false;
134
+ }
135
+ }
136
+ function temporarilyEditableNear(view, pos) {
137
+ const v = view;
138
+ const { node, offset } = v.docView.domFromPos(pos, 0);
139
+ const after = offset < node.childNodes.length ? node.childNodes[offset] : null;
140
+ const before = offset ? node.childNodes[offset - 1] : null;
141
+ if (_browser.browser.safari && after && after.contentEditable == "false") return setEditable(after);
142
+ if ((!after || after.contentEditable == "false") && (!before || before.contentEditable == "false")) {
143
+ if (after) return setEditable(after);
144
+ else if (before) return setEditable(before);
145
+ }
146
+ return;
147
+ }
148
+ function setEditable(element) {
149
+ element.contentEditable = "true";
150
+ if (_browser.browser.safari && element.draggable) {
151
+ element.draggable = false;
152
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
153
+ element.wasDraggable = true;
154
+ }
155
+ return element;
156
+ }
157
+ function resetEditable(element) {
158
+ element.contentEditable = "false";
159
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
160
+ if (element.wasDraggable) {
161
+ element.draggable = true;
162
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
163
+ element.wasDraggable = null;
164
+ }
165
+ }
166
+ function removeClassOnSelectionChange(view) {
167
+ const v = view;
168
+ const doc = v.dom.ownerDocument;
169
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
170
+ doc.removeEventListener("selectionchange", v.input.hideSelectionGuard);
171
+ const domSel = v.domSelectionRange();
172
+ const node = domSel.anchorNode, offset = domSel.anchorOffset;
173
+ doc.addEventListener("selectionchange", v.input.hideSelectionGuard = ()=>{
174
+ if (domSel.anchorNode != node || domSel.anchorOffset != offset) {
175
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
176
+ doc.removeEventListener("selectionchange", v.input.hideSelectionGuard);
177
+ setTimeout(()=>{
178
+ if (!editorOwnsSelection(v) || v.state.selection.visible) v.dom.classList.remove("ProseMirror-hideselection");
179
+ }, 20);
180
+ }
181
+ });
182
+ }
183
+ const brokenSelectBetweenUneditable = _browser.browser.safari || _browser.browser.chrome && _browser.browser.chrome_version < 63;
184
+ function selectionToDOM(view) {
185
+ let force = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : false;
186
+ const v = view;
187
+ const sel = v.state.selection;
188
+ syncNodeSelection(v, sel);
189
+ if (!editorOwnsSelection(v)) return;
190
+ // The delayed drag selection causes issues with Cell Selections
191
+ // in Safari. And the drag selection delay is to workarond issues
192
+ // which only present in Chrome.
193
+ if (!force && v.input.mouseDown && v.input.mouseDown.allowDefault && _browser.browser.chrome) {
194
+ const domSel = v.domSelectionRange(), curSel = v.domObserver.currentSelection;
195
+ if (domSel.anchorNode && curSel.anchorNode && isEquivalentPosition(domSel.anchorNode, domSel.anchorOffset, curSel.anchorNode, curSel.anchorOffset)) {
196
+ v.input.mouseDown.delayedSelectionSync = true;
197
+ v.domObserver.setCurSelection();
198
+ return;
199
+ }
200
+ }
201
+ v.domObserver.disconnectSelection();
202
+ if (v.cursorWrapper) {
203
+ selectCursorWrapper(v);
204
+ } else {
205
+ const { anchor, head } = sel;
206
+ let resetEditableFrom;
207
+ let resetEditableTo;
208
+ if (brokenSelectBetweenUneditable && !(sel instanceof _prosemirrorstate.TextSelection)) {
209
+ if (!sel.$from.parent.inlineContent) resetEditableFrom = temporarilyEditableNear(v, sel.from);
210
+ if (!sel.empty && !sel.$from.parent.inlineContent) resetEditableTo = temporarilyEditableNear(v, sel.to);
211
+ }
212
+ v.docView.setSelection(anchor, head, v.root, force);
213
+ if (brokenSelectBetweenUneditable) {
214
+ if (resetEditableFrom) resetEditable(resetEditableFrom);
215
+ if (resetEditableTo) resetEditable(resetEditableTo);
216
+ }
217
+ if (sel.visible) {
218
+ v.dom.classList.remove("ProseMirror-hideselection");
219
+ } else {
220
+ v.dom.classList.add("ProseMirror-hideselection");
221
+ if ("onselectionchange" in document) removeClassOnSelectionChange(v);
222
+ }
223
+ }
224
+ v.domObserver.setCurSelection();
225
+ v.domObserver.connectSelection();
226
+ }
@@ -0,0 +1,85 @@
1
+ /* eslint-disable @typescript-eslint/no-empty-function */ /**
2
+ * @fileoverview
3
+ *
4
+ * Stubs for ProseMirror View during SSR. These are extremely
5
+ * barebones, because they _do not need to actually work_. They
6
+ * just need to prevent errors from being thrown when ProseMirror
7
+ * View attemps to access these APIs while constructing the
8
+ * initial EditorView. None of these APIs are necessary for SSR to
9
+ * work properly, so it's fine that they're all no-ops.
10
+ */ "use strict";
11
+ Object.defineProperty(exports, "__esModule", {
12
+ value: true
13
+ });
14
+ Object.defineProperty(exports, "setSsrStubs", {
15
+ enumerable: true,
16
+ get: function() {
17
+ return setSsrStubs;
18
+ }
19
+ });
20
+ let ClassList = class ClassList {
21
+ add() {}
22
+ remove() {}
23
+ };
24
+ let ElementStub = class ElementStub {
25
+ get parent() {
26
+ return new ElementStub();
27
+ }
28
+ get parentNode() {
29
+ return new ElementStub();
30
+ }
31
+ nodeName = "div";
32
+ appendChild() {
33
+ return new ElementStub();
34
+ }
35
+ setAttribute() {}
36
+ hasAttribute() {
37
+ return false;
38
+ }
39
+ insertBefore() {}
40
+ get classList() {
41
+ return new ClassList();
42
+ }
43
+ get ownerDocument() {
44
+ return new DocumentStub();
45
+ }
46
+ style = {};
47
+ addEventListener() {}
48
+ removeEventListener() {}
49
+ replaceChildren() {}
50
+ };
51
+ let DocumentStub = class DocumentStub {
52
+ createElement() {
53
+ return new ElementStub();
54
+ }
55
+ addEventListener() {}
56
+ removeEventListener() {}
57
+ get documentElement() {
58
+ return new ElementStub();
59
+ }
60
+ };
61
+ function setSsrStubs() {
62
+ const prevWindow = globalThis.window;
63
+ // @ts-expect-error HACK - EditorView checks for window.MutationObserver
64
+ // in its constructor, which breaks SSR. We temporarily set window
65
+ // to an empty object to prevent an error from being thrown, and then
66
+ // clean it up so that other isomorphic code doesn't get confused about
67
+ // whether there's a functioning global window object
68
+ globalThis.window ??= {
69
+ visualViewport: null
70
+ };
71
+ const prevDocument = globalThis.document;
72
+ // @ts-expect-error HACK: This is only used during SSR, and only
73
+ // to prevent outright errors when ProseMirror View attempts to
74
+ // access document properties either on import or when constructing
75
+ // the EditorView.
76
+ globalThis.document ??= new DocumentStub();
77
+ return function cleanupSsrStubs() {
78
+ if (globalThis.window !== prevWindow) {
79
+ globalThis.window = prevWindow;
80
+ }
81
+ if (globalThis.document !== prevDocument) {
82
+ globalThis.document = prevDocument;
83
+ }
84
+ };
85
+ }
@@ -0,0 +1,111 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */ "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
+ findTextNode: function() {
13
+ return findTextNode;
14
+ },
15
+ tempEditor: function() {
16
+ return tempEditor;
17
+ }
18
+ });
19
+ const _react = require("@testing-library/react");
20
+ const _expect = require("expect");
21
+ const _prosemirrormodel = require("prosemirror-model");
22
+ const _prosemirrorstate = require("prosemirror-state");
23
+ const _prosemirrortestbuilder = require("prosemirror-test-builder");
24
+ const _react1 = /*#__PURE__*/ _interop_require_default(require("react"));
25
+ const _ProseMirror = require("../components/ProseMirror.js");
26
+ const _ProseMirrorDoc = require("../components/ProseMirrorDoc.js");
27
+ const _useEditorEffect = require("../hooks/useEditorEffect.js");
28
+ const _reactKeys = require("../plugins/reactKeys.js");
29
+ function _interop_require_default(obj) {
30
+ return obj && obj.__esModule ? obj : {
31
+ default: obj
32
+ };
33
+ }
34
+ const toEqualNode = function(actual, expected) {
35
+ if (!(actual instanceof _prosemirrormodel.Node && expected instanceof _prosemirrormodel.Node)) {
36
+ throw new Error("Must be comparing nodes");
37
+ }
38
+ const pass = (0, _prosemirrortestbuilder.eq)(actual, expected);
39
+ return {
40
+ message: ()=>// `this` context will have correct typings
41
+ `expected ${this.utils.printReceived(actual)} ${pass ? "not " : ""}to equal ${this.utils.printExpected(expected)}`,
42
+ pass
43
+ };
44
+ };
45
+ _expect.expect.extend({
46
+ toEqualNode
47
+ });
48
+ function tempEditor(param) {
49
+ let { doc: startDoc, selection, controlled, plugins, ...props } = param;
50
+ startDoc = startDoc ?? (0, _prosemirrortestbuilder.doc)((0, _prosemirrortestbuilder.p)());
51
+ const state = _prosemirrorstate.EditorState.create({
52
+ doc: startDoc,
53
+ schema: _prosemirrortestbuilder.schema,
54
+ selection: selection ?? startDoc.tag?.a ? _prosemirrorstate.TextSelection.create(startDoc, startDoc.tag.a, startDoc.tag?.b) : undefined,
55
+ plugins: [
56
+ ...plugins ?? [],
57
+ (0, _reactKeys.reactKeys)()
58
+ ]
59
+ });
60
+ let view = null;
61
+ function Test() {
62
+ (0, _useEditorEffect.useEditorEffect)((v)=>{
63
+ view = v;
64
+ });
65
+ return null;
66
+ }
67
+ const { rerender, unmount } = (0, _react.render)(/*#__PURE__*/ _react1.default.createElement(_ProseMirror.ProseMirror, {
68
+ ...controlled ? {
69
+ state
70
+ } : {
71
+ defaultState: state
72
+ },
73
+ ...props
74
+ }, /*#__PURE__*/ _react1.default.createElement(Test, null), /*#__PURE__*/ _react1.default.createElement(_ProseMirrorDoc.ProseMirrorDoc, null)));
75
+ function rerenderEditor() {
76
+ let { ...newProps } = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
77
+ rerender(/*#__PURE__*/ _react1.default.createElement(_ProseMirror.ProseMirror, {
78
+ ...controlled ? {
79
+ state
80
+ } : {
81
+ defaultState: state
82
+ },
83
+ ...props,
84
+ ...newProps
85
+ }, /*#__PURE__*/ _react1.default.createElement(Test, null), /*#__PURE__*/ _react1.default.createElement(_ProseMirrorDoc.ProseMirrorDoc, null)));
86
+ return view;
87
+ }
88
+ // We need two renders for the hasContentDOM state to settle
89
+ rerenderEditor();
90
+ return {
91
+ view: view,
92
+ rerender: rerenderEditor,
93
+ unmount
94
+ };
95
+ }
96
+ function findTextNodeInner(node, text) {
97
+ if (node.nodeType == 3) {
98
+ if (node.nodeValue == text) return node;
99
+ } else if (node.nodeType == 1) {
100
+ for(let ch = node.firstChild; ch; ch = ch.nextSibling){
101
+ const found = findTextNodeInner(ch, text);
102
+ if (found) return found;
103
+ }
104
+ }
105
+ return undefined;
106
+ }
107
+ function findTextNode(node, text) {
108
+ const found = findTextNodeInner(node, text);
109
+ if (found) return found;
110
+ throw new Error("Unable to find matching text node");
111
+ }
@@ -0,0 +1,94 @@
1
+ /* Copyright (c) The New York Times Company */ "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
+ setupProseMirrorView: function() {
13
+ return setupProseMirrorView;
14
+ },
15
+ teardownProseMirrorView: function() {
16
+ return teardownProseMirrorView;
17
+ }
18
+ });
19
+ let oldElementFromPoint;
20
+ let oldGetClientRects;
21
+ let oldGetBoundingClientRect;
22
+ const mockElementFromPoint = ()=>globalThis.document.body;
23
+ const mockGetBoundingClientRect = ()=>{
24
+ return {
25
+ bottom: 0,
26
+ height: 0,
27
+ left: 0,
28
+ right: 0,
29
+ top: 0,
30
+ width: 0,
31
+ x: 0,
32
+ y: 0,
33
+ toJSON () {
34
+ return {
35
+ bottom: 0,
36
+ height: 0,
37
+ left: 0,
38
+ right: 0,
39
+ top: 0,
40
+ width: 0,
41
+ x: 0,
42
+ y: 0
43
+ };
44
+ }
45
+ };
46
+ };
47
+ const mockGetClientRects = ()=>{
48
+ const list = [
49
+ {
50
+ bottom: 0,
51
+ height: 0,
52
+ left: 0,
53
+ right: 0,
54
+ top: 0,
55
+ width: 0,
56
+ x: 0,
57
+ y: 0,
58
+ toJSON () {
59
+ return {
60
+ bottom: 0,
61
+ height: 0,
62
+ left: 0,
63
+ right: 0,
64
+ top: 0,
65
+ width: 0,
66
+ x: 0,
67
+ y: 0
68
+ };
69
+ }
70
+ }
71
+ ];
72
+ const domRectList = Object.assign(list, {
73
+ item (index) {
74
+ return list[index] ?? null;
75
+ }
76
+ });
77
+ return domRectList;
78
+ };
79
+ function setupProseMirrorView() {
80
+ oldElementFromPoint = Document.prototype.elementFromPoint;
81
+ Document.prototype.elementFromPoint = mockElementFromPoint;
82
+ oldGetClientRects = Range.prototype.getClientRects;
83
+ Range.prototype.getClientRects = mockGetClientRects;
84
+ oldGetBoundingClientRect = Range.prototype.getBoundingClientRect;
85
+ Range.prototype.getBoundingClientRect = mockGetBoundingClientRect;
86
+ }
87
+ function teardownProseMirrorView() {
88
+ // @ts-expect-error jsdom actually doesn't implement these, so they might be undefined
89
+ Document.prototype.elementFromPoint = oldElementFromPoint;
90
+ // @ts-expect-error jsdom actually doesn't implement these, so they might be undefined
91
+ Range.prototype.getClientRects = oldGetClientRects;
92
+ // @ts-expect-error jsdom actually doesn't implement these, so they might be undefined
93
+ Range.prototype.getBoundingClientRect = oldGetBoundingClientRect;
94
+ }