@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.
@@ -9,9 +9,17 @@ Object.defineProperty(exports, "useComponentEventListeners", {
9
9
  }
10
10
  });
11
11
  const _react = require("react");
12
- const _componentEventListeners = require("../plugins/componentEventListeners.js");
13
- function useComponentEventListeners() {
14
- const [registry, setRegistry] = (0, _react.useState)(new Map());
12
+ const _reactdom = require("react-dom");
13
+ function useComponentEventListeners(existingHandlers) {
14
+ const [registry, setRegistry] = (0, _react.useState)(new Map(Object.entries(existingHandlers ?? {}).map((param)=>{
15
+ let [eventName, handler] = param;
16
+ return [
17
+ eventName,
18
+ handler ? [
19
+ handler
20
+ ] : []
21
+ ];
22
+ })));
15
23
  const registerEventListener = (0, _react.useCallback)((eventType, handler)=>{
16
24
  const handlers = registry.get(eventType) ?? [];
17
25
  handlers.unshift(handler);
@@ -28,12 +36,45 @@ function useComponentEventListeners() {
28
36
  }, [
29
37
  registry
30
38
  ]);
31
- const componentEventListenersPlugin = (0, _react.useMemo)(()=>(0, _componentEventListeners.componentEventListeners)(registry), [
39
+ (0, _react.useLayoutEffect)(()=>{
40
+ if (!existingHandlers) return;
41
+ for (const [eventType, handler] of Object.entries(existingHandlers)){
42
+ if (!handler) return;
43
+ registerEventListener(eventType, handler);
44
+ }
45
+ return ()=>{
46
+ for (const [eventType, handler] of Object.entries(existingHandlers)){
47
+ if (!handler) return;
48
+ unregisterEventListener(eventType, handler);
49
+ }
50
+ };
51
+ }, [
52
+ existingHandlers,
53
+ registerEventListener,
54
+ unregisterEventListener
55
+ ]);
56
+ const handleDOMEvents = (0, _react.useMemo)(()=>{
57
+ const domEventHandlers = {};
58
+ for (const [eventType, handlers] of registry.entries()){
59
+ function handleEvent(view, event) {
60
+ for (const handler of handlers){
61
+ let handled = false;
62
+ (0, _reactdom.unstable_batchedUpdates)(()=>{
63
+ handled = !!handler(view, event);
64
+ });
65
+ if (handled || event.defaultPrevented) return true;
66
+ }
67
+ return false;
68
+ }
69
+ domEventHandlers[eventType] = handleEvent;
70
+ }
71
+ return domEventHandlers;
72
+ }, [
32
73
  registry
33
74
  ]);
34
75
  return {
35
76
  registerEventListener,
36
77
  unregisterEventListener,
37
- componentEventListenersPlugin
78
+ handleDOMEvents
38
79
  };
39
80
  }
@@ -32,7 +32,7 @@ function useEditor(mount, options) {
32
32
  const defaultState = options.defaultState ?? _constants.EMPTY_STATE;
33
33
  const [_state, setState] = (0, _react.useState)(defaultState);
34
34
  const state = options.state ?? _state;
35
- const { componentEventListenersPlugin, registerEventListener, unregisterEventListener } = (0, _useComponentEventListeners.useComponentEventListeners)();
35
+ const { handleDOMEvents, registerEventListener, unregisterEventListener } = (0, _useComponentEventListeners.useComponentEventListeners)(options.handleDOMEvents);
36
36
  const setCursorWrapper = (0, _react.useCallback)((deco)=>{
37
37
  (0, _reactdom.flushSync)(()=>{
38
38
  _setCursorWrapper(deco);
@@ -40,11 +40,9 @@ function useEditor(mount, options) {
40
40
  }, []);
41
41
  const plugins = (0, _react.useMemo)(()=>[
42
42
  ...options.plugins ?? [],
43
- componentEventListenersPlugin,
44
43
  (0, _beforeInputPlugin.beforeInputPlugin)(setCursorWrapper)
45
44
  ], [
46
45
  options.plugins,
47
- componentEventListenersPlugin,
48
46
  setCursorWrapper
49
47
  ]);
50
48
  const dispatchTransaction = (0, _react.useCallback)(function dispatchTransaction(tr) {
@@ -73,7 +71,8 @@ function useEditor(mount, options) {
73
71
  ...options,
74
72
  state,
75
73
  plugins,
76
- dispatchTransaction
74
+ dispatchTransaction,
75
+ handleDOMEvents
77
76
  };
78
77
  const [view, setView] = (0, _react.useState)(()=>{
79
78
  return new _StaticEditorView.StaticEditorView(directEditorProps);
@@ -105,17 +104,18 @@ function useEditor(mount, options) {
105
104
  if (view instanceof _ReactEditorView.ReactEditorView && !view.isDestroyed) {
106
105
  if (view.deferPendingEffects) {
107
106
  setTimeout(()=>{
107
+ // Plugins might dispatch transactions from their
108
+ // view update lifecycle hooks
108
109
  flushSyncRef.current = false;
109
110
  view.commitPendingEffects();
110
111
  flushSyncRef.current = true;
111
112
  });
112
113
  } else {
113
- // Plugins might dispatch transactions from their
114
- // view update lifecycle hooks
115
114
  flushSyncRef.current = false;
116
115
  view.commitPendingEffects();
117
116
  flushSyncRef.current = true;
118
117
  }
118
+ view.commitPendingEffects();
119
119
  }
120
120
  });
121
121
  view.update(directEditorProps);
@@ -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
@@ -1,5 +1,5 @@
1
- /* Copyright (c) The New York Times Company */ import { useCallback, useMemo, useState } from "react";
2
- import { componentEventListeners } from "../plugins/componentEventListeners.js";
1
+ /* Copyright (c) The New York Times Company */ import { useCallback, useLayoutEffect, useMemo, useState } from "react";
2
+ import { unstable_batchedUpdates as batch } from "react-dom";
3
3
  /**
4
4
  * Produces a plugin that can be used with ProseMirror to handle DOM
5
5
  * events at the EditorView.dom element.
@@ -14,19 +14,27 @@ import { componentEventListeners } from "../plugins/componentEventListeners.js";
14
14
  * @privateRemarks
15
15
  *
16
16
  * This hook uses a combination of mutable and immutable updates to give
17
- * us precise control over when we re-create the ProseMirror plugin.
17
+ * us precise control over when we re-create the event listeners.
18
18
  *
19
- * The plugin has a mutable reference to the set of handlers for each
19
+ * The hook has a mutable reference to the set of handlers for each
20
20
  * event type, but the set of event types is static. This means that we
21
- * need to produce a new ProseMirror plugin whenever a new event type is
22
- * registered. We avoid producing a new ProseMirrer plugin in any other
23
- * scenario to avoid the performance overhead of reconfiguring the plugins
24
- * in the EditorView.
21
+ * need to produce a new handleDOMEVents record whenever a new event type is
22
+ * registered. We avoid producing a new record in any other
23
+ * scenario to avoid the performance overhead of re-registering the event
24
+ * listeners in the EditorView.
25
25
  *
26
26
  * To accomplish this, we shallowly clone the registry whenever a new event
27
27
  * type is registered.
28
- */ export function useComponentEventListeners() {
29
- const [registry, setRegistry] = useState(new Map());
28
+ */ export function useComponentEventListeners(existingHandlers) {
29
+ const [registry, setRegistry] = useState(new Map(Object.entries(existingHandlers ?? {}).map((param)=>{
30
+ let [eventName, handler] = param;
31
+ return [
32
+ eventName,
33
+ handler ? [
34
+ handler
35
+ ] : []
36
+ ];
37
+ })));
30
38
  const registerEventListener = useCallback((eventType, handler)=>{
31
39
  const handlers = registry.get(eventType) ?? [];
32
40
  handlers.unshift(handler);
@@ -43,12 +51,45 @@ import { componentEventListeners } from "../plugins/componentEventListeners.js";
43
51
  }, [
44
52
  registry
45
53
  ]);
46
- const componentEventListenersPlugin = useMemo(()=>componentEventListeners(registry), [
54
+ useLayoutEffect(()=>{
55
+ if (!existingHandlers) return;
56
+ for (const [eventType, handler] of Object.entries(existingHandlers)){
57
+ if (!handler) return;
58
+ registerEventListener(eventType, handler);
59
+ }
60
+ return ()=>{
61
+ for (const [eventType, handler] of Object.entries(existingHandlers)){
62
+ if (!handler) return;
63
+ unregisterEventListener(eventType, handler);
64
+ }
65
+ };
66
+ }, [
67
+ existingHandlers,
68
+ registerEventListener,
69
+ unregisterEventListener
70
+ ]);
71
+ const handleDOMEvents = useMemo(()=>{
72
+ const domEventHandlers = {};
73
+ for (const [eventType, handlers] of registry.entries()){
74
+ function handleEvent(view, event) {
75
+ for (const handler of handlers){
76
+ let handled = false;
77
+ batch(()=>{
78
+ handled = !!handler(view, event);
79
+ });
80
+ if (handled || event.defaultPrevented) return true;
81
+ }
82
+ return false;
83
+ }
84
+ domEventHandlers[eventType] = handleEvent;
85
+ }
86
+ return domEventHandlers;
87
+ }, [
47
88
  registry
48
89
  ]);
49
90
  return {
50
91
  registerEventListener,
51
92
  unregisterEventListener,
52
- componentEventListenersPlugin
93
+ handleDOMEvents
53
94
  };
54
95
  }
@@ -30,7 +30,7 @@ let didWarnValueDefaultValue = false;
30
30
  const defaultState = options.defaultState ?? EMPTY_STATE;
31
31
  const [_state, setState] = useState(defaultState);
32
32
  const state = options.state ?? _state;
33
- const { componentEventListenersPlugin, registerEventListener, unregisterEventListener } = useComponentEventListeners();
33
+ const { handleDOMEvents, registerEventListener, unregisterEventListener } = useComponentEventListeners(options.handleDOMEvents);
34
34
  const setCursorWrapper = useCallback((deco)=>{
35
35
  flushSync(()=>{
36
36
  _setCursorWrapper(deco);
@@ -38,11 +38,9 @@ let didWarnValueDefaultValue = false;
38
38
  }, []);
39
39
  const plugins = useMemo(()=>[
40
40
  ...options.plugins ?? [],
41
- componentEventListenersPlugin,
42
41
  beforeInputPlugin(setCursorWrapper)
43
42
  ], [
44
43
  options.plugins,
45
- componentEventListenersPlugin,
46
44
  setCursorWrapper
47
45
  ]);
48
46
  const dispatchTransaction = useCallback(function dispatchTransaction(tr) {
@@ -71,7 +69,8 @@ let didWarnValueDefaultValue = false;
71
69
  ...options,
72
70
  state,
73
71
  plugins,
74
- dispatchTransaction
72
+ dispatchTransaction,
73
+ handleDOMEvents
75
74
  };
76
75
  const [view, setView] = useState(()=>{
77
76
  return new StaticEditorView(directEditorProps);
@@ -103,17 +102,18 @@ let didWarnValueDefaultValue = false;
103
102
  if (view instanceof ReactEditorView && !view.isDestroyed) {
104
103
  if (view.deferPendingEffects) {
105
104
  setTimeout(()=>{
105
+ // Plugins might dispatch transactions from their
106
+ // view update lifecycle hooks
106
107
  flushSyncRef.current = false;
107
108
  view.commitPendingEffects();
108
109
  flushSyncRef.current = true;
109
110
  });
110
111
  } else {
111
- // Plugins might dispatch transactions from their
112
- // view update lifecycle hooks
113
112
  flushSyncRef.current = false;
114
113
  view.commitPendingEffects();
115
114
  flushSyncRef.current = true;
116
115
  }
116
+ view.commitPendingEffects();
117
117
  }
118
118
  });
119
119
  view.update(directEditorProps);