@tiptap/react 2.5.9 → 2.6.1

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/index.js CHANGED
@@ -1,131 +1,11 @@
1
1
  import { BubbleMenuPlugin } from '@tiptap/extension-bubble-menu';
2
2
  import React, { forwardRef, useState, useEffect, useDebugValue, useRef, createContext, useContext } from 'react';
3
3
  import ReactDOM, { flushSync } from 'react-dom';
4
- import { Editor as Editor$1, NodeView } from '@tiptap/core';
4
+ import { Editor, NodeView } from '@tiptap/core';
5
5
  export * from '@tiptap/core';
6
+ export { Editor } from '@tiptap/core';
6
7
  import { FloatingMenuPlugin } from '@tiptap/extension-floating-menu';
7
8
 
8
- const mergeRefs = (...refs) => {
9
- return (node) => {
10
- refs.forEach(ref => {
11
- if (typeof ref === 'function') {
12
- ref(node);
13
- }
14
- else if (ref) {
15
- ref.current = node;
16
- }
17
- });
18
- };
19
- };
20
- const Portals = ({ renderers }) => {
21
- return (React.createElement(React.Fragment, null, Object.entries(renderers).map(([key, renderer]) => {
22
- return ReactDOM.createPortal(renderer.reactElement, renderer.element, key);
23
- })));
24
- };
25
- class PureEditorContent extends React.Component {
26
- constructor(props) {
27
- super(props);
28
- this.editorContentRef = React.createRef();
29
- this.initialized = false;
30
- this.state = {
31
- renderers: {},
32
- };
33
- }
34
- componentDidMount() {
35
- this.init();
36
- }
37
- componentDidUpdate() {
38
- this.init();
39
- }
40
- init() {
41
- const { editor } = this.props;
42
- if (editor && !editor.isDestroyed && editor.options.element) {
43
- if (editor.contentComponent) {
44
- return;
45
- }
46
- const element = this.editorContentRef.current;
47
- element.append(...editor.options.element.childNodes);
48
- editor.setOptions({
49
- element,
50
- });
51
- editor.contentComponent = this;
52
- editor.createNodeViews();
53
- this.initialized = true;
54
- }
55
- }
56
- maybeFlushSync(fn) {
57
- // Avoid calling flushSync until the editor is initialized.
58
- // Initialization happens during the componentDidMount or componentDidUpdate
59
- // lifecycle methods, and React doesn't allow calling flushSync from inside
60
- // a lifecycle method.
61
- if (this.initialized) {
62
- flushSync(fn);
63
- }
64
- else {
65
- fn();
66
- }
67
- }
68
- setRenderer(id, renderer) {
69
- this.maybeFlushSync(() => {
70
- this.setState(({ renderers }) => ({
71
- renderers: {
72
- ...renderers,
73
- [id]: renderer,
74
- },
75
- }));
76
- });
77
- }
78
- removeRenderer(id) {
79
- this.maybeFlushSync(() => {
80
- this.setState(({ renderers }) => {
81
- const nextRenderers = { ...renderers };
82
- delete nextRenderers[id];
83
- return { renderers: nextRenderers };
84
- });
85
- });
86
- }
87
- componentWillUnmount() {
88
- const { editor } = this.props;
89
- if (!editor) {
90
- return;
91
- }
92
- this.initialized = false;
93
- if (!editor.isDestroyed) {
94
- editor.view.setProps({
95
- nodeViews: {},
96
- });
97
- }
98
- editor.contentComponent = null;
99
- if (!editor.options.element.firstChild) {
100
- return;
101
- }
102
- const newElement = document.createElement('div');
103
- newElement.append(...editor.options.element.childNodes);
104
- editor.setOptions({
105
- element: newElement,
106
- });
107
- }
108
- render() {
109
- const { editor, innerRef, ...rest } = this.props;
110
- return (React.createElement(React.Fragment, null,
111
- React.createElement("div", { ref: mergeRefs(innerRef, this.editorContentRef), ...rest }),
112
- React.createElement(Portals, { renderers: this.state.renderers })));
113
- }
114
- }
115
- // EditorContent should be re-created whenever the Editor instance changes
116
- const EditorContentWithKey = forwardRef((props, ref) => {
117
- const key = React.useMemo(() => {
118
- return Math.floor(Math.random() * 0xFFFFFFFF).toString();
119
- }, [props.editor]);
120
- // Can't use JSX here because it conflicts with the type definition of Vue's JSX, so use createElement
121
- return React.createElement(PureEditorContent, {
122
- key,
123
- innerRef: ref,
124
- ...props,
125
- });
126
- });
127
- const EditorContent = React.memo(EditorContentWithKey);
128
-
129
9
  var shim = {exports: {}};
130
10
 
131
11
  var useSyncExternalStoreShim_production_min = {};
@@ -404,12 +284,161 @@ if (process.env.NODE_ENV === 'production') {
404
284
 
405
285
  var shimExports = shim.exports;
406
286
 
407
- class Editor extends Editor$1 {
408
- constructor() {
409
- super(...arguments);
410
- this.contentComponent = null;
287
+ const mergeRefs = (...refs) => {
288
+ return (node) => {
289
+ refs.forEach(ref => {
290
+ if (typeof ref === 'function') {
291
+ ref(node);
292
+ }
293
+ else if (ref) {
294
+ ref.current = node;
295
+ }
296
+ });
297
+ };
298
+ };
299
+ /**
300
+ * This component renders all of the editor's node views.
301
+ */
302
+ const Portals = ({ contentComponent, }) => {
303
+ // For performance reasons, we render the node view portals on state changes only
304
+ const renderers = shimExports.useSyncExternalStore(contentComponent.subscribe, contentComponent.getSnapshot, contentComponent.getServerSnapshot);
305
+ // This allows us to directly render the portals without any additional wrapper
306
+ return (React.createElement(React.Fragment, null, Object.values(renderers)));
307
+ };
308
+ function getInstance() {
309
+ const subscribers = new Set();
310
+ let renderers = {};
311
+ return {
312
+ /**
313
+ * Subscribe to the editor instance's changes.
314
+ */
315
+ subscribe(callback) {
316
+ subscribers.add(callback);
317
+ return () => {
318
+ subscribers.delete(callback);
319
+ };
320
+ },
321
+ getSnapshot() {
322
+ return renderers;
323
+ },
324
+ getServerSnapshot() {
325
+ return renderers;
326
+ },
327
+ /**
328
+ * Adds a new NodeView Renderer to the editor.
329
+ */
330
+ setRenderer(id, renderer) {
331
+ renderers = {
332
+ ...renderers,
333
+ [id]: ReactDOM.createPortal(renderer.reactElement, renderer.element, id),
334
+ };
335
+ subscribers.forEach(subscriber => subscriber());
336
+ },
337
+ /**
338
+ * Removes a NodeView Renderer from the editor.
339
+ */
340
+ removeRenderer(id) {
341
+ const nextRenderers = { ...renderers };
342
+ delete nextRenderers[id];
343
+ renderers = nextRenderers;
344
+ subscribers.forEach(subscriber => subscriber());
345
+ },
346
+ };
347
+ }
348
+ class PureEditorContent extends React.Component {
349
+ constructor(props) {
350
+ super(props);
351
+ this.editorContentRef = React.createRef();
352
+ this.initialized = false;
353
+ this.state = {
354
+ hasContentComponentInitialized: Boolean(props.editor.contentComponent),
355
+ };
356
+ }
357
+ componentDidMount() {
358
+ this.init();
359
+ }
360
+ componentDidUpdate() {
361
+ this.init();
362
+ }
363
+ init() {
364
+ const editor = this.props.editor;
365
+ if (editor && !editor.isDestroyed && editor.options.element) {
366
+ if (editor.contentComponent) {
367
+ return;
368
+ }
369
+ const element = this.editorContentRef.current;
370
+ element.append(...editor.options.element.childNodes);
371
+ editor.setOptions({
372
+ element,
373
+ });
374
+ editor.contentComponent = getInstance();
375
+ // Has the content component been initialized?
376
+ if (!this.state.hasContentComponentInitialized) {
377
+ // Subscribe to the content component
378
+ this.unsubscribeToContentComponent = editor.contentComponent.subscribe(() => {
379
+ this.setState(prevState => {
380
+ if (!prevState.hasContentComponentInitialized) {
381
+ return {
382
+ hasContentComponentInitialized: true,
383
+ };
384
+ }
385
+ return prevState;
386
+ });
387
+ // Unsubscribe to previous content component
388
+ if (this.unsubscribeToContentComponent) {
389
+ this.unsubscribeToContentComponent();
390
+ }
391
+ });
392
+ }
393
+ editor.createNodeViews();
394
+ this.initialized = true;
395
+ }
396
+ }
397
+ componentWillUnmount() {
398
+ const editor = this.props.editor;
399
+ if (!editor) {
400
+ return;
401
+ }
402
+ this.initialized = false;
403
+ if (!editor.isDestroyed) {
404
+ editor.view.setProps({
405
+ nodeViews: {},
406
+ });
407
+ }
408
+ if (this.unsubscribeToContentComponent) {
409
+ this.unsubscribeToContentComponent();
410
+ }
411
+ editor.contentComponent = null;
412
+ if (!editor.options.element.firstChild) {
413
+ return;
414
+ }
415
+ const newElement = document.createElement('div');
416
+ newElement.append(...editor.options.element.childNodes);
417
+ editor.setOptions({
418
+ element: newElement,
419
+ });
420
+ }
421
+ render() {
422
+ const { editor, innerRef, ...rest } = this.props;
423
+ return (React.createElement(React.Fragment, null,
424
+ React.createElement("div", { ref: mergeRefs(innerRef, this.editorContentRef), ...rest }),
425
+ (editor === null || editor === void 0 ? void 0 : editor.contentComponent) && React.createElement(Portals, { contentComponent: editor.contentComponent })));
411
426
  }
412
427
  }
428
+ // EditorContent should be re-created whenever the Editor instance changes
429
+ const EditorContentWithKey = forwardRef((props, ref) => {
430
+ const key = React.useMemo(() => {
431
+ return Math.floor(Math.random() * 0xffffffff).toString();
432
+ // eslint-disable-next-line react-hooks/exhaustive-deps
433
+ }, [props.editor]);
434
+ // Can't use JSX here because it conflicts with the type definition of Vue's JSX, so use createElement
435
+ return React.createElement(PureEditorContent, {
436
+ key,
437
+ innerRef: ref,
438
+ ...props,
439
+ });
440
+ });
441
+ const EditorContent = React.memo(EditorContentWithKey);
413
442
 
414
443
  var withSelector = {exports: {}};
415
444
 
@@ -769,17 +798,20 @@ class EditorInstanceManager {
769
798
  * Create a new editor instance. And attach event listeners.
770
799
  */
771
800
  createEditor() {
772
- const editor = new Editor(this.options.current);
773
- // Always call the most recent version of the callback function by default
774
- editor.on('beforeCreate', (...args) => { var _a, _b; return (_b = (_a = this.options.current).onBeforeCreate) === null || _b === void 0 ? void 0 : _b.call(_a, ...args); });
775
- editor.on('blur', (...args) => { var _a, _b; return (_b = (_a = this.options.current).onBlur) === null || _b === void 0 ? void 0 : _b.call(_a, ...args); });
776
- editor.on('create', (...args) => { var _a, _b; return (_b = (_a = this.options.current).onCreate) === null || _b === void 0 ? void 0 : _b.call(_a, ...args); });
777
- editor.on('destroy', (...args) => { var _a, _b; return (_b = (_a = this.options.current).onDestroy) === null || _b === void 0 ? void 0 : _b.call(_a, ...args); });
778
- editor.on('focus', (...args) => { var _a, _b; return (_b = (_a = this.options.current).onFocus) === null || _b === void 0 ? void 0 : _b.call(_a, ...args); });
779
- editor.on('selectionUpdate', (...args) => { var _a, _b; return (_b = (_a = this.options.current).onSelectionUpdate) === null || _b === void 0 ? void 0 : _b.call(_a, ...args); });
780
- editor.on('transaction', (...args) => { var _a, _b; return (_b = (_a = this.options.current).onTransaction) === null || _b === void 0 ? void 0 : _b.call(_a, ...args); });
781
- editor.on('update', (...args) => { var _a, _b; return (_b = (_a = this.options.current).onUpdate) === null || _b === void 0 ? void 0 : _b.call(_a, ...args); });
782
- editor.on('contentError', (...args) => { var _a, _b; return (_b = (_a = this.options.current).onContentError) === null || _b === void 0 ? void 0 : _b.call(_a, ...args); });
801
+ const optionsToApply = {
802
+ ...this.options.current,
803
+ // Always call the most recent version of the callback function by default
804
+ onBeforeCreate: (...args) => { var _a, _b; return (_b = (_a = this.options.current).onBeforeCreate) === null || _b === void 0 ? void 0 : _b.call(_a, ...args); },
805
+ onBlur: (...args) => { var _a, _b; return (_b = (_a = this.options.current).onBlur) === null || _b === void 0 ? void 0 : _b.call(_a, ...args); },
806
+ onCreate: (...args) => { var _a, _b; return (_b = (_a = this.options.current).onCreate) === null || _b === void 0 ? void 0 : _b.call(_a, ...args); },
807
+ onDestroy: (...args) => { var _a, _b; return (_b = (_a = this.options.current).onDestroy) === null || _b === void 0 ? void 0 : _b.call(_a, ...args); },
808
+ onFocus: (...args) => { var _a, _b; return (_b = (_a = this.options.current).onFocus) === null || _b === void 0 ? void 0 : _b.call(_a, ...args); },
809
+ onSelectionUpdate: (...args) => { var _a, _b; return (_b = (_a = this.options.current).onSelectionUpdate) === null || _b === void 0 ? void 0 : _b.call(_a, ...args); },
810
+ onTransaction: (...args) => { var _a, _b; return (_b = (_a = this.options.current).onTransaction) === null || _b === void 0 ? void 0 : _b.call(_a, ...args); },
811
+ onUpdate: (...args) => { var _a, _b; return (_b = (_a = this.options.current).onUpdate) === null || _b === void 0 ? void 0 : _b.call(_a, ...args); },
812
+ onContentError: (...args) => { var _a, _b; return (_b = (_a = this.options.current).onContentError) === null || _b === void 0 ? void 0 : _b.call(_a, ...args); },
813
+ };
814
+ const editor = new Editor(optionsToApply);
783
815
  // no need to keep track of the event listeners, they will be removed when the editor is destroyed
784
816
  return editor;
785
817
  }
@@ -1013,7 +1045,9 @@ const useReactNodeView = () => useContext(ReactNodeViewContext);
1013
1045
  const NodeViewContent = props => {
1014
1046
  const Tag = props.as || 'div';
1015
1047
  const { nodeViewContentRef } = useReactNodeView();
1016
- return (React.createElement(Tag, { ...props, ref: nodeViewContentRef, "data-node-view-content": "", style: {
1048
+ return (
1049
+ // @ts-ignore
1050
+ React.createElement(Tag, { ...props, ref: nodeViewContentRef, "data-node-view-content": "", style: {
1017
1051
  whiteSpace: 'pre-wrap',
1018
1052
  ...props.style,
1019
1053
  } }));
@@ -1022,7 +1056,9 @@ const NodeViewContent = props => {
1022
1056
  const NodeViewWrapper = React.forwardRef((props, ref) => {
1023
1057
  const { onDragStart } = useReactNodeView();
1024
1058
  const Tag = props.as || 'div';
1025
- return (React.createElement(Tag, { ...props, ref: ref, "data-node-view-wrapper": "", onDragStart: onDragStart, style: {
1059
+ return (
1060
+ // @ts-ignore
1061
+ React.createElement(Tag, { ...props, ref: ref, "data-node-view-wrapper": "", onDragStart: onDragStart, style: {
1026
1062
  whiteSpace: 'normal',
1027
1063
  ...props.style,
1028
1064
  } }));
@@ -1076,19 +1112,29 @@ class ReactRenderer {
1076
1112
  this.element.setAttribute(key, attrs[key]);
1077
1113
  });
1078
1114
  }
1079
- this.render();
1115
+ if (this.editor.isInitialized) {
1116
+ // On first render, we need to flush the render synchronously
1117
+ // Renders afterwards can be async, but this fixes a cursor positioning issue
1118
+ flushSync(() => {
1119
+ this.render();
1120
+ });
1121
+ }
1122
+ else {
1123
+ this.render();
1124
+ }
1080
1125
  }
1081
1126
  render() {
1082
- var _a, _b;
1127
+ var _a;
1083
1128
  const Component = this.component;
1084
1129
  const props = this.props;
1130
+ const editor = this.editor;
1085
1131
  if (isClassComponent(Component) || isForwardRefComponent(Component)) {
1086
1132
  props.ref = (ref) => {
1087
1133
  this.ref = ref;
1088
1134
  };
1089
1135
  }
1090
- this.reactElement = React.createElement(Component, { ...props });
1091
- (_b = (_a = this.editor) === null || _a === void 0 ? void 0 : _a.contentComponent) === null || _b === void 0 ? void 0 : _b.setRenderer(this.id, this);
1136
+ this.reactElement = React.createElement(Component, props);
1137
+ (_a = editor === null || editor === void 0 ? void 0 : editor.contentComponent) === null || _a === void 0 ? void 0 : _a.setRenderer(this.id, this);
1092
1138
  }
1093
1139
  updateProps(props = {}) {
1094
1140
  this.props = {
@@ -1098,8 +1144,9 @@ class ReactRenderer {
1098
1144
  this.render();
1099
1145
  }
1100
1146
  destroy() {
1101
- var _a, _b;
1102
- (_b = (_a = this.editor) === null || _a === void 0 ? void 0 : _a.contentComponent) === null || _b === void 0 ? void 0 : _b.removeRenderer(this.id);
1147
+ var _a;
1148
+ const editor = this.editor;
1149
+ (_a = editor === null || editor === void 0 ? void 0 : editor.contentComponent) === null || _a === void 0 ? void 0 : _a.removeRenderer(this.id);
1103
1150
  }
1104
1151
  }
1105
1152
 
@@ -1121,18 +1168,19 @@ class ReactNodeView extends NodeView {
1121
1168
  };
1122
1169
  this.component.displayName = capitalizeFirstChar(this.extension.name);
1123
1170
  }
1124
- const ReactNodeViewProvider = componentProps => {
1125
- const Component = this.component;
1126
- const onDragStart = this.onDragStart.bind(this);
1127
- const nodeViewContentRef = element => {
1128
- if (element && this.contentDOMElement && element.firstChild !== this.contentDOMElement) {
1129
- element.appendChild(this.contentDOMElement);
1130
- }
1131
- };
1132
- return (React.createElement(React.Fragment, null,
1133
- React.createElement(ReactNodeViewContext.Provider, { value: { onDragStart, nodeViewContentRef } },
1134
- React.createElement(Component, { ...componentProps }))));
1171
+ const onDragStart = this.onDragStart.bind(this);
1172
+ const nodeViewContentRef = element => {
1173
+ if (element && this.contentDOMElement && element.firstChild !== this.contentDOMElement) {
1174
+ element.appendChild(this.contentDOMElement);
1175
+ }
1135
1176
  };
1177
+ const context = { onDragStart, nodeViewContentRef };
1178
+ const Component = this.component;
1179
+ // For performance reasons, we memoize the provider component
1180
+ // And all of the things it requires are declared outside of the component, so it doesn't need to re-render
1181
+ const ReactNodeViewProvider = React.memo(componentProps => {
1182
+ return (React.createElement(ReactNodeViewContext.Provider, { value: context }, React.createElement(Component, componentProps)));
1183
+ });
1136
1184
  ReactNodeViewProvider.displayName = 'ReactNodeView';
1137
1185
  if (this.node.isLeaf) {
1138
1186
  this.contentDOMElement = null;
@@ -1251,5 +1299,5 @@ function ReactNodeViewRenderer(component, options) {
1251
1299
  };
1252
1300
  }
1253
1301
 
1254
- export { BubbleMenu, Editor, EditorConsumer, EditorContent, EditorContext, EditorProvider, FloatingMenu, NodeViewContent, NodeViewWrapper, PureEditorContent, ReactNodeViewContext, ReactNodeViewRenderer, ReactRenderer, useCurrentEditor, useEditor, useEditorState, useReactNodeView };
1302
+ export { BubbleMenu, EditorConsumer, EditorContent, EditorContext, EditorProvider, FloatingMenu, NodeViewContent, NodeViewWrapper, PureEditorContent, ReactNodeViewContext, ReactNodeViewRenderer, ReactRenderer, useCurrentEditor, useEditor, useEditorState, useReactNodeView };
1255
1303
  //# sourceMappingURL=index.js.map