@handlewithcare/react-prosemirror 3.1.0-tiptap.48 → 3.1.0-tiptap.50
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/hooks/useComponentEventListeners.js +46 -5
- package/dist/cjs/hooks/useEditor.js +6 -6
- package/dist/cjs/tiptap/hooks/useEditor.js +349 -0
- package/dist/cjs/tiptap/hooks/useTiptapEditor.js +2 -2
- package/dist/esm/hooks/useComponentEventListeners.js +53 -12
- package/dist/esm/hooks/useEditor.js +6 -6
- package/dist/esm/tiptap/hooks/useEditor.js +339 -0
- package/dist/esm/tiptap/hooks/useTiptapEditor.js +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/types/components/TextNodeView.d.ts +2 -2
- package/dist/types/constants.d.ts +1 -1
- package/dist/types/contexts/EditorContext.d.ts +1 -1
- package/dist/types/hooks/useComponentEventListeners.d.ts +11 -10
- package/dist/types/hooks/useEditor.d.ts +2 -2
- package/dist/types/hooks/useEditorEventListener.d.ts +1 -1
- package/dist/types/props.d.ts +26 -26
- package/dist/types/tiptap/hooks/useEditor.d.ts +38 -0
- package/dist/types/tiptap/hooks/useTiptapEditor.d.ts +1 -1
- package/dist/types/viewdesc.d.ts +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
// Forked from Tiptap's useEditor hook, with modifications to support
|
|
2
|
+
// server-side rendering
|
|
3
|
+
import { Editor } from "@tiptap/core";
|
|
4
|
+
import { useEditorState } from "@tiptap/react";
|
|
5
|
+
import { useDebugValue, useEffect, useRef, useState } from "react";
|
|
6
|
+
import { useSyncExternalStore } from "use-sync-external-store/shim/index.js";
|
|
7
|
+
const isDev = process.env.NODE_ENV !== "production";
|
|
8
|
+
const isSSR = typeof window === "undefined";
|
|
9
|
+
const isNext = isSSR || Boolean(typeof window !== "undefined" && window.next);
|
|
10
|
+
/**
|
|
11
|
+
* This class handles the creation, destruction, and re-creation of the editor instance.
|
|
12
|
+
*/ let EditorInstanceManager = class EditorInstanceManager {
|
|
13
|
+
/**
|
|
14
|
+
* The current editor instance.
|
|
15
|
+
*/ editor = null;
|
|
16
|
+
/**
|
|
17
|
+
* The most recent options to apply to the editor.
|
|
18
|
+
*/ options;
|
|
19
|
+
/**
|
|
20
|
+
* The subscriptions to notify when the editor instance
|
|
21
|
+
* has been created or destroyed.
|
|
22
|
+
*/ subscriptions = new Set();
|
|
23
|
+
/**
|
|
24
|
+
* A timeout to destroy the editor if it was not mounted within a time frame.
|
|
25
|
+
*/ scheduledDestructionTimeout;
|
|
26
|
+
/**
|
|
27
|
+
* Whether the editor has been mounted.
|
|
28
|
+
*/ isComponentMounted = false;
|
|
29
|
+
/**
|
|
30
|
+
* The most recent dependencies array.
|
|
31
|
+
*/ previousDeps = null;
|
|
32
|
+
/**
|
|
33
|
+
* The unique instance ID. This is used to identify the editor instance. And will be re-generated for each new instance.
|
|
34
|
+
*/ instanceId = "";
|
|
35
|
+
constructor(options){
|
|
36
|
+
this.options = options;
|
|
37
|
+
this.subscriptions = new Set();
|
|
38
|
+
this.setEditor(this.getInitialEditor());
|
|
39
|
+
this.scheduleDestroy();
|
|
40
|
+
this.getEditor = this.getEditor.bind(this);
|
|
41
|
+
this.getServerSnapshot = this.getServerSnapshot.bind(this);
|
|
42
|
+
this.subscribe = this.subscribe.bind(this);
|
|
43
|
+
this.refreshEditorInstance = this.refreshEditorInstance.bind(this);
|
|
44
|
+
this.scheduleDestroy = this.scheduleDestroy.bind(this);
|
|
45
|
+
this.onRender = this.onRender.bind(this);
|
|
46
|
+
this.createEditor = this.createEditor.bind(this);
|
|
47
|
+
}
|
|
48
|
+
setEditor(editor) {
|
|
49
|
+
this.editor = editor;
|
|
50
|
+
this.instanceId = Math.random().toString(36).slice(2, 9);
|
|
51
|
+
// Notify all subscribers that the editor instance has been created
|
|
52
|
+
this.subscriptions.forEach((cb)=>cb());
|
|
53
|
+
}
|
|
54
|
+
getInitialEditor() {
|
|
55
|
+
if (this.options.current.immediatelyRender === undefined) {
|
|
56
|
+
if (isSSR || isNext) {
|
|
57
|
+
if (isDev) {
|
|
58
|
+
/**
|
|
59
|
+
* Throw an error in development, to make sure the developer is aware that tiptap cannot be SSR'd
|
|
60
|
+
* and that they need to set `immediatelyRender` to `false` to avoid hydration mismatches.
|
|
61
|
+
*/ throw new Error("Tiptap Error: SSR has been detected, please set `immediatelyRender` explicitly to `false` to avoid hydration mismatches.");
|
|
62
|
+
}
|
|
63
|
+
// Best faith effort in production, run the code in the legacy mode to avoid hydration mismatches and errors in production
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
// Default to immediately rendering when client-side rendering
|
|
67
|
+
return this.createEditor();
|
|
68
|
+
}
|
|
69
|
+
// ----- FORKED CHANGES -----
|
|
70
|
+
// if (this.options.current.immediatelyRender && isSSR && isDev) {
|
|
71
|
+
// // Warn in development, to make sure the developer is aware that tiptap cannot be SSR'd, set `immediatelyRender` to `false` to avoid hydration mismatches.
|
|
72
|
+
// throw new Error(
|
|
73
|
+
// "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."
|
|
74
|
+
// );
|
|
75
|
+
// }
|
|
76
|
+
// ----- END FORKED CHANGES -----
|
|
77
|
+
if (this.options.current.immediatelyRender) {
|
|
78
|
+
return this.createEditor();
|
|
79
|
+
}
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Create a new editor instance. And attach event listeners.
|
|
84
|
+
*/ createEditor() {
|
|
85
|
+
var _this = this;
|
|
86
|
+
const optionsToApply = {
|
|
87
|
+
...this.options.current,
|
|
88
|
+
// Always call the most recent version of the callback function by default
|
|
89
|
+
onBeforeCreate: function() {
|
|
90
|
+
for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
|
|
91
|
+
args[_key] = arguments[_key];
|
|
92
|
+
}
|
|
93
|
+
return _this.options.current.onBeforeCreate?.(...args);
|
|
94
|
+
},
|
|
95
|
+
onBlur: function() {
|
|
96
|
+
for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
|
|
97
|
+
args[_key] = arguments[_key];
|
|
98
|
+
}
|
|
99
|
+
return _this.options.current.onBlur?.(...args);
|
|
100
|
+
},
|
|
101
|
+
onCreate: function() {
|
|
102
|
+
for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
|
|
103
|
+
args[_key] = arguments[_key];
|
|
104
|
+
}
|
|
105
|
+
return _this.options.current.onCreate?.(...args);
|
|
106
|
+
},
|
|
107
|
+
onDestroy: function() {
|
|
108
|
+
for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
|
|
109
|
+
args[_key] = arguments[_key];
|
|
110
|
+
}
|
|
111
|
+
return _this.options.current.onDestroy?.(...args);
|
|
112
|
+
},
|
|
113
|
+
onFocus: function() {
|
|
114
|
+
for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
|
|
115
|
+
args[_key] = arguments[_key];
|
|
116
|
+
}
|
|
117
|
+
return _this.options.current.onFocus?.(...args);
|
|
118
|
+
},
|
|
119
|
+
onSelectionUpdate: function() {
|
|
120
|
+
for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
|
|
121
|
+
args[_key] = arguments[_key];
|
|
122
|
+
}
|
|
123
|
+
return _this.options.current.onSelectionUpdate?.(...args);
|
|
124
|
+
},
|
|
125
|
+
onTransaction: function() {
|
|
126
|
+
for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
|
|
127
|
+
args[_key] = arguments[_key];
|
|
128
|
+
}
|
|
129
|
+
return _this.options.current.onTransaction?.(...args);
|
|
130
|
+
},
|
|
131
|
+
onUpdate: function() {
|
|
132
|
+
for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
|
|
133
|
+
args[_key] = arguments[_key];
|
|
134
|
+
}
|
|
135
|
+
return _this.options.current.onUpdate?.(...args);
|
|
136
|
+
},
|
|
137
|
+
onContentError: function() {
|
|
138
|
+
for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
|
|
139
|
+
args[_key] = arguments[_key];
|
|
140
|
+
}
|
|
141
|
+
return _this.options.current.onContentError?.(...args);
|
|
142
|
+
},
|
|
143
|
+
onDrop: function() {
|
|
144
|
+
for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
|
|
145
|
+
args[_key] = arguments[_key];
|
|
146
|
+
}
|
|
147
|
+
return _this.options.current.onDrop?.(...args);
|
|
148
|
+
},
|
|
149
|
+
onPaste: function() {
|
|
150
|
+
for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
|
|
151
|
+
args[_key] = arguments[_key];
|
|
152
|
+
}
|
|
153
|
+
return _this.options.current.onPaste?.(...args);
|
|
154
|
+
},
|
|
155
|
+
onDelete: function() {
|
|
156
|
+
for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
|
|
157
|
+
args[_key] = arguments[_key];
|
|
158
|
+
}
|
|
159
|
+
return _this.options.current.onDelete?.(...args);
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
const editor = new Editor(optionsToApply);
|
|
163
|
+
// no need to keep track of the event listeners, they will be removed when the editor is destroyed
|
|
164
|
+
return editor;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Get the current editor instance.
|
|
168
|
+
*/ getEditor() {
|
|
169
|
+
return this.editor;
|
|
170
|
+
}
|
|
171
|
+
// ----- FORKED CHANGES -----
|
|
172
|
+
/**
|
|
173
|
+
* Always disable the editor on the server-side.
|
|
174
|
+
*/ getServerSnapshot() {
|
|
175
|
+
// return null;
|
|
176
|
+
// Return editor in SSRm, contrary to Tiptap's implementation
|
|
177
|
+
return this.editor;
|
|
178
|
+
}
|
|
179
|
+
// ----- END FORKED CHANGES -----
|
|
180
|
+
/**
|
|
181
|
+
* Subscribe to the editor instance's changes.
|
|
182
|
+
*/ subscribe(onStoreChange) {
|
|
183
|
+
this.subscriptions.add(onStoreChange);
|
|
184
|
+
return ()=>{
|
|
185
|
+
this.subscriptions.delete(onStoreChange);
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
static compareOptions(a, b) {
|
|
189
|
+
return Object.keys(a).every((key)=>{
|
|
190
|
+
if ([
|
|
191
|
+
"onCreate",
|
|
192
|
+
"onBeforeCreate",
|
|
193
|
+
"onDestroy",
|
|
194
|
+
"onUpdate",
|
|
195
|
+
"onTransaction",
|
|
196
|
+
"onFocus",
|
|
197
|
+
"onBlur",
|
|
198
|
+
"onSelectionUpdate",
|
|
199
|
+
"onContentError",
|
|
200
|
+
"onDrop",
|
|
201
|
+
"onPaste"
|
|
202
|
+
].includes(key)) {
|
|
203
|
+
// we don't want to compare callbacks, they are always different and only registered once
|
|
204
|
+
return true;
|
|
205
|
+
}
|
|
206
|
+
// We often encourage putting extensions inlined in the options object, so we will do a slightly deeper comparison here
|
|
207
|
+
if (key === "extensions" && a.extensions && b.extensions) {
|
|
208
|
+
if (a.extensions.length !== b.extensions.length) {
|
|
209
|
+
return false;
|
|
210
|
+
}
|
|
211
|
+
return a.extensions.every((extension, index)=>{
|
|
212
|
+
if (extension !== b.extensions?.[index]) {
|
|
213
|
+
return false;
|
|
214
|
+
}
|
|
215
|
+
return true;
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
if (a[key] !== b[key]) {
|
|
219
|
+
// if any of the options have changed, we should update the editor options
|
|
220
|
+
return false;
|
|
221
|
+
}
|
|
222
|
+
return true;
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* On each render, we will create, update, or destroy the editor instance.
|
|
227
|
+
* @param deps The dependencies to watch for changes
|
|
228
|
+
* @returns A cleanup function
|
|
229
|
+
*/ onRender(deps) {
|
|
230
|
+
// The returned callback will run on each render
|
|
231
|
+
return ()=>{
|
|
232
|
+
this.isComponentMounted = true;
|
|
233
|
+
// Cleanup any scheduled destructions, since we are currently rendering
|
|
234
|
+
clearTimeout(this.scheduledDestructionTimeout);
|
|
235
|
+
if (this.editor && !this.editor.isDestroyed && deps.length === 0) {
|
|
236
|
+
// if the editor does exist & deps are empty, we don't need to re-initialize the editor generally
|
|
237
|
+
if (!EditorInstanceManager.compareOptions(this.options.current, this.editor.options)) {
|
|
238
|
+
// But, the options are different, so we need to update the editor options
|
|
239
|
+
// Still, this is faster than re-creating the editor
|
|
240
|
+
this.editor.setOptions({
|
|
241
|
+
...this.options.current,
|
|
242
|
+
editable: this.editor.isEditable
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
} else {
|
|
246
|
+
// When the editor:
|
|
247
|
+
// - does not yet exist
|
|
248
|
+
// - is destroyed
|
|
249
|
+
// - the deps array changes
|
|
250
|
+
// We need to destroy the editor instance and re-initialize it
|
|
251
|
+
this.refreshEditorInstance(deps);
|
|
252
|
+
}
|
|
253
|
+
return ()=>{
|
|
254
|
+
this.isComponentMounted = false;
|
|
255
|
+
this.scheduleDestroy();
|
|
256
|
+
};
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Recreate the editor instance if the dependencies have changed.
|
|
261
|
+
*/ refreshEditorInstance(deps) {
|
|
262
|
+
if (this.editor && !this.editor.isDestroyed) {
|
|
263
|
+
// Editor instance already exists
|
|
264
|
+
if (this.previousDeps === null) {
|
|
265
|
+
// If lastDeps has not yet been initialized, reuse the current editor instance
|
|
266
|
+
this.previousDeps = deps;
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
const depsAreEqual = this.previousDeps.length === deps.length && this.previousDeps.every((dep, index)=>dep === deps[index]);
|
|
270
|
+
if (depsAreEqual) {
|
|
271
|
+
// deps exist and are equal, no need to recreate
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
if (this.editor && !this.editor.isDestroyed) {
|
|
276
|
+
// Destroy the editor instance if it exists
|
|
277
|
+
this.editor.destroy();
|
|
278
|
+
}
|
|
279
|
+
this.setEditor(this.createEditor());
|
|
280
|
+
// Update the lastDeps to the current deps
|
|
281
|
+
this.previousDeps = deps;
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Schedule the destruction of the editor instance.
|
|
285
|
+
* This will only destroy the editor if it was not mounted on the next tick.
|
|
286
|
+
* This is to avoid destroying the editor instance when it's actually still mounted.
|
|
287
|
+
*/ scheduleDestroy() {
|
|
288
|
+
const currentInstanceId = this.instanceId;
|
|
289
|
+
const currentEditor = this.editor;
|
|
290
|
+
// Wait two ticks to see if the component is still mounted
|
|
291
|
+
this.scheduledDestructionTimeout = setTimeout(()=>{
|
|
292
|
+
if (this.isComponentMounted && this.instanceId === currentInstanceId) {
|
|
293
|
+
// If still mounted on the following tick, with the same instanceId, do not destroy the editor
|
|
294
|
+
if (currentEditor) {
|
|
295
|
+
// just re-apply options as they might have changed
|
|
296
|
+
currentEditor.setOptions(this.options.current);
|
|
297
|
+
}
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
if (currentEditor && !currentEditor.isDestroyed) {
|
|
301
|
+
currentEditor.destroy();
|
|
302
|
+
if (this.instanceId === currentInstanceId) {
|
|
303
|
+
this.setEditor(null);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
// This allows the effect to run again between ticks
|
|
307
|
+
// which may save us from having to re-create the editor
|
|
308
|
+
}, 1);
|
|
309
|
+
}
|
|
310
|
+
};
|
|
311
|
+
export function useEditor() {
|
|
312
|
+
let options = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {}, deps = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : [];
|
|
313
|
+
const mostRecentOptions = useRef(options);
|
|
314
|
+
mostRecentOptions.current = options;
|
|
315
|
+
const [instanceManager] = useState(()=>new EditorInstanceManager(mostRecentOptions));
|
|
316
|
+
const editor = useSyncExternalStore(instanceManager.subscribe, instanceManager.getEditor, instanceManager.getServerSnapshot);
|
|
317
|
+
useDebugValue(editor);
|
|
318
|
+
// This effect will handle creating/updating the editor instance
|
|
319
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
320
|
+
useEffect(instanceManager.onRender(deps));
|
|
321
|
+
// The default behavior is to re-render on each transaction
|
|
322
|
+
// This is legacy behavior that will be removed in future versions
|
|
323
|
+
useEditorState({
|
|
324
|
+
editor,
|
|
325
|
+
selector: (param)=>{
|
|
326
|
+
let { transactionNumber } = param;
|
|
327
|
+
if (options.shouldRerenderOnTransaction === false || options.shouldRerenderOnTransaction === undefined) {
|
|
328
|
+
// This will prevent the editor from re-rendering on each transaction
|
|
329
|
+
return null;
|
|
330
|
+
}
|
|
331
|
+
// This will avoid re-rendering on the first transaction when `immediatelyRender` is set to `true`
|
|
332
|
+
if (options.immediatelyRender && transactionNumber === 0) {
|
|
333
|
+
return 0;
|
|
334
|
+
}
|
|
335
|
+
return transactionNumber + 1;
|
|
336
|
+
}
|
|
337
|
+
});
|
|
338
|
+
return editor;
|
|
339
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { useEditor } from "@tiptap/react";
|
|
2
1
|
import { StaticEditorView } from "../../StaticEditorView.js";
|
|
3
2
|
import { ReactProseMirror } from "../extensions/ReactProseMirror.js";
|
|
4
3
|
import { ReactProseMirrorCommands } from "../extensions/ReactProseMirrorCommands.js";
|
|
4
|
+
import { useEditor } from "./useEditor.js";
|
|
5
5
|
export function useTiptapEditor(options, deps) {
|
|
6
6
|
const extensions = [
|
|
7
7
|
ReactProseMirror,
|