@handlewithcare/react-prosemirror 3.1.0-tiptap.48 → 3.1.0-tiptap.49

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 (30) hide show
  1. package/dist/cjs/ReactEditorView.js +0 -2
  2. package/dist/cjs/components/ChildNodeViews.js +7 -12
  3. package/dist/cjs/components/CursorWrapper.js +9 -8
  4. package/dist/cjs/components/TextNodeView.js +38 -112
  5. package/dist/cjs/hooks/useEditor.js +5 -13
  6. package/dist/cjs/hooks/useNodeViewDescription.js +15 -38
  7. package/dist/cjs/plugins/beforeInputPlugin.js +41 -47
  8. package/dist/cjs/tiptap/hooks/useEditor.js +349 -0
  9. package/dist/cjs/tiptap/hooks/useTiptapEditor.js +2 -2
  10. package/dist/cjs/tiptap/utils/ssrJSDOMPatch.js +59 -0
  11. package/dist/cjs/viewdesc.js +3 -10
  12. package/dist/esm/ReactEditorView.js +0 -2
  13. package/dist/esm/components/ChildNodeViews.js +7 -12
  14. package/dist/esm/components/CursorWrapper.js +10 -9
  15. package/dist/esm/components/TextNodeView.js +38 -112
  16. package/dist/esm/hooks/useEditor.js +5 -13
  17. package/dist/esm/hooks/useNodeViewDescription.js +16 -39
  18. package/dist/esm/plugins/beforeInputPlugin.js +41 -47
  19. package/dist/esm/tiptap/hooks/useEditor.js +339 -0
  20. package/dist/esm/tiptap/hooks/useTiptapEditor.js +1 -1
  21. package/dist/esm/tiptap/utils/ssrJSDOMPatch.js +56 -0
  22. package/dist/esm/viewdesc.js +3 -10
  23. package/dist/tsconfig.tsbuildinfo +1 -1
  24. package/dist/types/ReactEditorView.d.ts +0 -4
  25. package/dist/types/components/TextNodeView.d.ts +4 -14
  26. package/dist/types/tiptap/hooks/useEditor.d.ts +38 -0
  27. package/dist/types/tiptap/hooks/useTiptapEditor.d.ts +1 -1
  28. package/dist/types/tiptap/utils/ssrJSDOMPatch.d.ts +1 -0
  29. package/dist/types/viewdesc.d.ts +1 -1
  30. package/package.json +1 -2
@@ -0,0 +1,349 @@
1
+ // Forked from Tiptap's useEditor hook, with modifications to support
2
+ // server-side rendering
3
+ "use strict";
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ Object.defineProperty(exports, "useEditor", {
8
+ enumerable: true,
9
+ get: function() {
10
+ return useEditor;
11
+ }
12
+ });
13
+ const _core = require("@tiptap/core");
14
+ const _react = require("@tiptap/react");
15
+ const _react1 = require("react");
16
+ const _index = require("use-sync-external-store/shim/index.js");
17
+ const isDev = process.env.NODE_ENV !== "production";
18
+ const isSSR = typeof window === "undefined";
19
+ const isNext = isSSR || Boolean(typeof window !== "undefined" && window.next);
20
+ /**
21
+ * This class handles the creation, destruction, and re-creation of the editor instance.
22
+ */ let EditorInstanceManager = class EditorInstanceManager {
23
+ /**
24
+ * The current editor instance.
25
+ */ editor = null;
26
+ /**
27
+ * The most recent options to apply to the editor.
28
+ */ options;
29
+ /**
30
+ * The subscriptions to notify when the editor instance
31
+ * has been created or destroyed.
32
+ */ subscriptions = new Set();
33
+ /**
34
+ * A timeout to destroy the editor if it was not mounted within a time frame.
35
+ */ scheduledDestructionTimeout;
36
+ /**
37
+ * Whether the editor has been mounted.
38
+ */ isComponentMounted = false;
39
+ /**
40
+ * The most recent dependencies array.
41
+ */ previousDeps = null;
42
+ /**
43
+ * The unique instance ID. This is used to identify the editor instance. And will be re-generated for each new instance.
44
+ */ instanceId = "";
45
+ constructor(options){
46
+ this.options = options;
47
+ this.subscriptions = new Set();
48
+ this.setEditor(this.getInitialEditor());
49
+ this.scheduleDestroy();
50
+ this.getEditor = this.getEditor.bind(this);
51
+ this.getServerSnapshot = this.getServerSnapshot.bind(this);
52
+ this.subscribe = this.subscribe.bind(this);
53
+ this.refreshEditorInstance = this.refreshEditorInstance.bind(this);
54
+ this.scheduleDestroy = this.scheduleDestroy.bind(this);
55
+ this.onRender = this.onRender.bind(this);
56
+ this.createEditor = this.createEditor.bind(this);
57
+ }
58
+ setEditor(editor) {
59
+ this.editor = editor;
60
+ this.instanceId = Math.random().toString(36).slice(2, 9);
61
+ // Notify all subscribers that the editor instance has been created
62
+ this.subscriptions.forEach((cb)=>cb());
63
+ }
64
+ getInitialEditor() {
65
+ if (this.options.current.immediatelyRender === undefined) {
66
+ if (isSSR || isNext) {
67
+ if (isDev) {
68
+ /**
69
+ * Throw an error in development, to make sure the developer is aware that tiptap cannot be SSR'd
70
+ * and that they need to set `immediatelyRender` to `false` to avoid hydration mismatches.
71
+ */ throw new Error("Tiptap Error: SSR has been detected, please set `immediatelyRender` explicitly to `false` to avoid hydration mismatches.");
72
+ }
73
+ // Best faith effort in production, run the code in the legacy mode to avoid hydration mismatches and errors in production
74
+ return null;
75
+ }
76
+ // Default to immediately rendering when client-side rendering
77
+ return this.createEditor();
78
+ }
79
+ // ----- FORKED CHANGES -----
80
+ // if (this.options.current.immediatelyRender && isSSR && isDev) {
81
+ // // Warn in development, to make sure the developer is aware that tiptap cannot be SSR'd, set `immediatelyRender` to `false` to avoid hydration mismatches.
82
+ // throw new Error(
83
+ // "Tiptap Error: SSR has been detected, and `immediatelyRender` has been set to `true` this is an unsupported configuration that may result in errors, explicitly set `immediatelyRender` to `false` to avoid hydration mismatches."
84
+ // );
85
+ // }
86
+ // ----- END FORKED CHANGES -----
87
+ if (this.options.current.immediatelyRender) {
88
+ return this.createEditor();
89
+ }
90
+ return null;
91
+ }
92
+ /**
93
+ * Create a new editor instance. And attach event listeners.
94
+ */ createEditor() {
95
+ var _this = this;
96
+ const optionsToApply = {
97
+ ...this.options.current,
98
+ // Always call the most recent version of the callback function by default
99
+ onBeforeCreate: function() {
100
+ for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
101
+ args[_key] = arguments[_key];
102
+ }
103
+ return _this.options.current.onBeforeCreate?.(...args);
104
+ },
105
+ onBlur: function() {
106
+ for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
107
+ args[_key] = arguments[_key];
108
+ }
109
+ return _this.options.current.onBlur?.(...args);
110
+ },
111
+ onCreate: function() {
112
+ for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
113
+ args[_key] = arguments[_key];
114
+ }
115
+ return _this.options.current.onCreate?.(...args);
116
+ },
117
+ onDestroy: function() {
118
+ for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
119
+ args[_key] = arguments[_key];
120
+ }
121
+ return _this.options.current.onDestroy?.(...args);
122
+ },
123
+ onFocus: function() {
124
+ for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
125
+ args[_key] = arguments[_key];
126
+ }
127
+ return _this.options.current.onFocus?.(...args);
128
+ },
129
+ onSelectionUpdate: function() {
130
+ for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
131
+ args[_key] = arguments[_key];
132
+ }
133
+ return _this.options.current.onSelectionUpdate?.(...args);
134
+ },
135
+ onTransaction: function() {
136
+ for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
137
+ args[_key] = arguments[_key];
138
+ }
139
+ return _this.options.current.onTransaction?.(...args);
140
+ },
141
+ onUpdate: function() {
142
+ for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
143
+ args[_key] = arguments[_key];
144
+ }
145
+ return _this.options.current.onUpdate?.(...args);
146
+ },
147
+ onContentError: function() {
148
+ for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
149
+ args[_key] = arguments[_key];
150
+ }
151
+ return _this.options.current.onContentError?.(...args);
152
+ },
153
+ onDrop: function() {
154
+ for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
155
+ args[_key] = arguments[_key];
156
+ }
157
+ return _this.options.current.onDrop?.(...args);
158
+ },
159
+ onPaste: function() {
160
+ for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
161
+ args[_key] = arguments[_key];
162
+ }
163
+ return _this.options.current.onPaste?.(...args);
164
+ },
165
+ onDelete: function() {
166
+ for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
167
+ args[_key] = arguments[_key];
168
+ }
169
+ return _this.options.current.onDelete?.(...args);
170
+ }
171
+ };
172
+ const editor = new _core.Editor(optionsToApply);
173
+ // no need to keep track of the event listeners, they will be removed when the editor is destroyed
174
+ return editor;
175
+ }
176
+ /**
177
+ * Get the current editor instance.
178
+ */ getEditor() {
179
+ return this.editor;
180
+ }
181
+ // ----- FORKED CHANGES -----
182
+ /**
183
+ * Always disable the editor on the server-side.
184
+ */ getServerSnapshot() {
185
+ // return null;
186
+ // Return editor in SSRm, contrary to Tiptap's implementation
187
+ return this.editor;
188
+ }
189
+ // ----- END FORKED CHANGES -----
190
+ /**
191
+ * Subscribe to the editor instance's changes.
192
+ */ subscribe(onStoreChange) {
193
+ this.subscriptions.add(onStoreChange);
194
+ return ()=>{
195
+ this.subscriptions.delete(onStoreChange);
196
+ };
197
+ }
198
+ static compareOptions(a, b) {
199
+ return Object.keys(a).every((key)=>{
200
+ if ([
201
+ "onCreate",
202
+ "onBeforeCreate",
203
+ "onDestroy",
204
+ "onUpdate",
205
+ "onTransaction",
206
+ "onFocus",
207
+ "onBlur",
208
+ "onSelectionUpdate",
209
+ "onContentError",
210
+ "onDrop",
211
+ "onPaste"
212
+ ].includes(key)) {
213
+ // we don't want to compare callbacks, they are always different and only registered once
214
+ return true;
215
+ }
216
+ // We often encourage putting extensions inlined in the options object, so we will do a slightly deeper comparison here
217
+ if (key === "extensions" && a.extensions && b.extensions) {
218
+ if (a.extensions.length !== b.extensions.length) {
219
+ return false;
220
+ }
221
+ return a.extensions.every((extension, index)=>{
222
+ if (extension !== b.extensions?.[index]) {
223
+ return false;
224
+ }
225
+ return true;
226
+ });
227
+ }
228
+ if (a[key] !== b[key]) {
229
+ // if any of the options have changed, we should update the editor options
230
+ return false;
231
+ }
232
+ return true;
233
+ });
234
+ }
235
+ /**
236
+ * On each render, we will create, update, or destroy the editor instance.
237
+ * @param deps The dependencies to watch for changes
238
+ * @returns A cleanup function
239
+ */ onRender(deps) {
240
+ // The returned callback will run on each render
241
+ return ()=>{
242
+ this.isComponentMounted = true;
243
+ // Cleanup any scheduled destructions, since we are currently rendering
244
+ clearTimeout(this.scheduledDestructionTimeout);
245
+ if (this.editor && !this.editor.isDestroyed && deps.length === 0) {
246
+ // if the editor does exist & deps are empty, we don't need to re-initialize the editor generally
247
+ if (!EditorInstanceManager.compareOptions(this.options.current, this.editor.options)) {
248
+ // But, the options are different, so we need to update the editor options
249
+ // Still, this is faster than re-creating the editor
250
+ this.editor.setOptions({
251
+ ...this.options.current,
252
+ editable: this.editor.isEditable
253
+ });
254
+ }
255
+ } else {
256
+ // When the editor:
257
+ // - does not yet exist
258
+ // - is destroyed
259
+ // - the deps array changes
260
+ // We need to destroy the editor instance and re-initialize it
261
+ this.refreshEditorInstance(deps);
262
+ }
263
+ return ()=>{
264
+ this.isComponentMounted = false;
265
+ this.scheduleDestroy();
266
+ };
267
+ };
268
+ }
269
+ /**
270
+ * Recreate the editor instance if the dependencies have changed.
271
+ */ refreshEditorInstance(deps) {
272
+ if (this.editor && !this.editor.isDestroyed) {
273
+ // Editor instance already exists
274
+ if (this.previousDeps === null) {
275
+ // If lastDeps has not yet been initialized, reuse the current editor instance
276
+ this.previousDeps = deps;
277
+ return;
278
+ }
279
+ const depsAreEqual = this.previousDeps.length === deps.length && this.previousDeps.every((dep, index)=>dep === deps[index]);
280
+ if (depsAreEqual) {
281
+ // deps exist and are equal, no need to recreate
282
+ return;
283
+ }
284
+ }
285
+ if (this.editor && !this.editor.isDestroyed) {
286
+ // Destroy the editor instance if it exists
287
+ this.editor.destroy();
288
+ }
289
+ this.setEditor(this.createEditor());
290
+ // Update the lastDeps to the current deps
291
+ this.previousDeps = deps;
292
+ }
293
+ /**
294
+ * Schedule the destruction of the editor instance.
295
+ * This will only destroy the editor if it was not mounted on the next tick.
296
+ * This is to avoid destroying the editor instance when it's actually still mounted.
297
+ */ scheduleDestroy() {
298
+ const currentInstanceId = this.instanceId;
299
+ const currentEditor = this.editor;
300
+ // Wait two ticks to see if the component is still mounted
301
+ this.scheduledDestructionTimeout = setTimeout(()=>{
302
+ if (this.isComponentMounted && this.instanceId === currentInstanceId) {
303
+ // If still mounted on the following tick, with the same instanceId, do not destroy the editor
304
+ if (currentEditor) {
305
+ // just re-apply options as they might have changed
306
+ currentEditor.setOptions(this.options.current);
307
+ }
308
+ return;
309
+ }
310
+ if (currentEditor && !currentEditor.isDestroyed) {
311
+ currentEditor.destroy();
312
+ if (this.instanceId === currentInstanceId) {
313
+ this.setEditor(null);
314
+ }
315
+ }
316
+ // This allows the effect to run again between ticks
317
+ // which may save us from having to re-create the editor
318
+ }, 1);
319
+ }
320
+ };
321
+ function useEditor() {
322
+ let options = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {}, deps = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : [];
323
+ const mostRecentOptions = (0, _react1.useRef)(options);
324
+ mostRecentOptions.current = options;
325
+ const [instanceManager] = (0, _react1.useState)(()=>new EditorInstanceManager(mostRecentOptions));
326
+ const editor = (0, _index.useSyncExternalStore)(instanceManager.subscribe, instanceManager.getEditor, instanceManager.getServerSnapshot);
327
+ (0, _react1.useDebugValue)(editor);
328
+ // This effect will handle creating/updating the editor instance
329
+ // eslint-disable-next-line react-hooks/exhaustive-deps
330
+ (0, _react1.useEffect)(instanceManager.onRender(deps));
331
+ // The default behavior is to re-render on each transaction
332
+ // This is legacy behavior that will be removed in future versions
333
+ (0, _react.useEditorState)({
334
+ editor,
335
+ selector: (param)=>{
336
+ let { transactionNumber } = param;
337
+ if (options.shouldRerenderOnTransaction === false || options.shouldRerenderOnTransaction === undefined) {
338
+ // This will prevent the editor from re-rendering on each transaction
339
+ return null;
340
+ }
341
+ // This will avoid re-rendering on the first transaction when `immediatelyRender` is set to `true`
342
+ if (options.immediatelyRender && transactionNumber === 0) {
343
+ return 0;
344
+ }
345
+ return transactionNumber + 1;
346
+ }
347
+ });
348
+ return editor;
349
+ }
@@ -8,10 +8,10 @@ Object.defineProperty(exports, "useTiptapEditor", {
8
8
  return useTiptapEditor;
9
9
  }
10
10
  });
11
- const _react = require("@tiptap/react");
12
11
  const _StaticEditorView = require("../../StaticEditorView.js");
13
12
  const _ReactProseMirror = require("../extensions/ReactProseMirror.js");
14
13
  const _ReactProseMirrorCommands = require("../extensions/ReactProseMirrorCommands.js");
14
+ const _useEditor = require("./useEditor.js");
15
15
  function useTiptapEditor(options, deps) {
16
16
  const extensions = [
17
17
  _ReactProseMirror.ReactProseMirror,
@@ -28,7 +28,7 @@ function useTiptapEditor(options, deps) {
28
28
  };
29
29
  extensions.push(_ReactProseMirrorCommands.ReactProseMirrorCommands);
30
30
  }
31
- const editor = (0, _react.useEditor)({
31
+ const editor = (0, _useEditor.useEditor)({
32
32
  ...options,
33
33
  extensions,
34
34
  element: null
@@ -0,0 +1,59 @@
1
+ /**
2
+ * This file is used to patch global DOM variables in a NodeJS environment.
3
+ * This is needed for ProseMirror to work in a NodeJS environment.
4
+ */ "use strict";
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ if (typeof window === "undefined") {
9
+ // Make sure to import JSDOM only in a NodeJS environment.
10
+ // The magic comments prevent bundlers from statically analyzing this require:
11
+ // - webpackIgnore: true → webpack / Next.js
12
+ // - @vite-ignore → Vite
13
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
14
+ const jsdom = require(/* webpackIgnore: true */ /* @vite-ignore */ "jsdom");
15
+ const html = `
16
+ <!DOCTYPE html>
17
+ <html>
18
+ <head>
19
+ <title>Testing</title>
20
+ </head>
21
+ <body></body>
22
+ </html>
23
+ `;
24
+ const { window: window1 } = new jsdom.JSDOM(html);
25
+ global.window = window1;
26
+ global.document = window1.document;
27
+ // Use Object.defineProperty for navigator since it's read-only in Node.js 22+
28
+ Object.defineProperty(global, "navigator", {
29
+ value: window1.navigator,
30
+ writable: true,
31
+ configurable: true
32
+ });
33
+ global.innerHeight = 0;
34
+ global.SVGElement = window1.SVGElement;
35
+ // @ts-expect-error stub getSelection for SSR
36
+ document.getSelection = ()=>({});
37
+ document.createRange = ()=>({
38
+ setStart () {},
39
+ setEnd () {},
40
+ // @ts-expect-error stub getBoundingClientRect for SSR
41
+ getClientRects () {
42
+ return {
43
+ left: 0,
44
+ top: 0,
45
+ right: 0,
46
+ bottom: 0
47
+ };
48
+ },
49
+ // @ts-expect-error stub getBoundingClientRect for SSR
50
+ getBoundingClientRect () {
51
+ return {
52
+ left: 0,
53
+ top: 0,
54
+ right: 0,
55
+ bottom: 0
56
+ };
57
+ }
58
+ });
59
+ }
@@ -266,9 +266,7 @@ let ViewDesc = class ViewDesc {
266
266
  prev = i ? this.children[i - 1] : null;
267
267
  if (!prev || prev.dom.parentNode == this.contentDOM) break;
268
268
  }
269
- if (prev && side && enter && !prev.border && !prev.domAtom) {
270
- return prev.domFromPos(prev.size, side);
271
- }
269
+ if (prev && side && enter && !prev.border && !prev.domAtom) return prev.domFromPos(prev.size, side);
272
270
  return {
273
271
  node: this.contentDOM,
274
272
  offset: prev ? (0, _dom.domIndex)(prev.dom) + 1 : 0
@@ -403,9 +401,7 @@ let ViewDesc = class ViewDesc {
403
401
  const after = selRange.focusNode.childNodes[selRange.focusOffset];
404
402
  if (after && after.contentEditable == "false") force = true;
405
403
  }
406
- if (!(force || brKludge && _browser.browser.safari) && (0, _dom.isEquivalentPosition)(anchorDOM.node, anchorDOM.offset, selRange.anchorNode, selRange.anchorOffset) && (0, _dom.isEquivalentPosition)(headDOM.node, headDOM.offset, selRange.focusNode, selRange.focusOffset)) {
407
- return;
408
- }
404
+ if (!(force || brKludge && _browser.browser.safari) && (0, _dom.isEquivalentPosition)(anchorDOM.node, anchorDOM.offset, selRange.anchorNode, selRange.anchorOffset) && (0, _dom.isEquivalentPosition)(headDOM.node, headDOM.offset, selRange.focusNode, selRange.focusOffset)) return;
409
405
  // Selection.extend can be used to create an 'inverted' selection
410
406
  // (one where the focus is before the anchor), but not all
411
407
  // browsers support it yet.
@@ -670,10 +666,7 @@ let TextViewDesc = class TextViewDesc extends NodeViewDesc {
670
666
  skip: skip || true
671
667
  };
672
668
  }
673
- update(node, outerDeco, _innerDeco, _view) {
674
- if (this.dirty == NODE_DIRTY || this.dirty != NOT_DIRTY && !this.inParent() || !node.sameMarkup(this.node)) return false;
675
- this.updateOuterDeco(outerDeco);
676
- this.node = node;
669
+ update(_node, _outerDeco, _innerDeco, _view) {
677
670
  this.dirty = NOT_DIRTY;
678
671
  return true;
679
672
  }
@@ -33,7 +33,6 @@ function changedNodeViews(a, b) {
33
33
  nextProps;
34
34
  prevState;
35
35
  _destroyed;
36
- deferPendingEffects;
37
36
  constructor(place, props){
38
37
  // Prevent the base class from destroying the React-managed nodes.
39
38
  // Restore them below after invoking the base class constructor.
@@ -86,7 +85,6 @@ function changedNodeViews(a, b) {
86
85
  // @ts-expect-error this violates the typing but class does it, too.
87
86
  this.docView = null;
88
87
  this._destroyed = false;
89
- this.deferPendingEffects = false;
90
88
  }
91
89
  get props() {
92
90
  return this.nextProps;
@@ -51,18 +51,13 @@ const ChildView = /*#__PURE__*/ memo(function ChildView(param) {
51
51
  key: child.key
52
52
  }, (param)=>{
53
53
  let { siblingsRef, parentRef } = param;
54
- return /*#__PURE__*/ React.createElement(EditorContext.Consumer, null, (param)=>{
55
- let { registerEventListener, unregisterEventListener } = param;
56
- return /*#__PURE__*/ React.createElement(TextNodeView, {
57
- view: view,
58
- node: child.node,
59
- getPos: getPos,
60
- siblingsRef: siblingsRef,
61
- parentRef: parentRef,
62
- decorations: child.outerDeco,
63
- registerEventListener: registerEventListener,
64
- unregisterEventListener: unregisterEventListener
65
- });
54
+ return /*#__PURE__*/ React.createElement(TextNodeView, {
55
+ view: view,
56
+ node: child.node,
57
+ getPos: getPos,
58
+ siblingsRef: siblingsRef,
59
+ parentRef: parentRef,
60
+ decorations: child.outerDeco
66
61
  });
67
62
  }) : /*#__PURE__*/ React.createElement(NodeView, {
68
63
  key: child.key,
@@ -1,9 +1,8 @@
1
- import React, { forwardRef, useImperativeHandle, useRef, useState } from "react";
1
+ import React, { forwardRef, useImperativeHandle, useRef } from "react";
2
2
  import { domIndex } from "../dom.js";
3
3
  import { useEditorEffect } from "../hooks/useEditorEffect.js";
4
4
  export const CursorWrapper = /*#__PURE__*/ forwardRef(function CursorWrapper(param, ref) {
5
5
  let { widget, getPos, ...props } = param;
6
- const [shouldRender, setShouldRender] = useState(true);
7
6
  const innerRef = useRef(null);
8
7
  useImperativeHandle(ref, ()=>{
9
8
  return innerRef.current;
@@ -15,20 +14,22 @@ export const CursorWrapper = /*#__PURE__*/ forwardRef(function CursorWrapper(par
15
14
  // @ts-expect-error Internal property - domSelection
16
15
  const domSel = view.domSelection();
17
16
  const node = innerRef.current;
18
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
19
- domSel.collapse(node.parentNode, domIndex(node) + 1);
17
+ const img = node.nodeName == "IMG";
18
+ if (img) {
19
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
20
+ domSel.collapse(node.parentNode, domIndex(node) + 1);
21
+ } else {
22
+ domSel.collapse(node, 0);
23
+ }
20
24
  // @ts-expect-error Internal property - domObserver
21
25
  view.domObserver.connectSelection();
22
- setTimeout(()=>{
23
- setShouldRender(false);
24
- });
25
26
  }, []);
26
- return shouldRender ? /*#__PURE__*/ React.createElement("img", {
27
+ return /*#__PURE__*/ React.createElement("img", {
27
28
  ref: innerRef,
28
29
  className: "ProseMirror-separator",
29
30
  // eslint-disable-next-line react/no-unknown-property
30
31
  "mark-placeholder": "true",
31
32
  alt: "",
32
33
  ...props
33
- }) : null;
34
+ });
34
35
  });