@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.cjs CHANGED
@@ -11,127 +11,6 @@ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'defau
11
11
  var React__default = /*#__PURE__*/_interopDefaultCompat(React);
12
12
  var ReactDOM__default = /*#__PURE__*/_interopDefaultCompat(ReactDOM);
13
13
 
14
- const mergeRefs = (...refs) => {
15
- return (node) => {
16
- refs.forEach(ref => {
17
- if (typeof ref === 'function') {
18
- ref(node);
19
- }
20
- else if (ref) {
21
- ref.current = node;
22
- }
23
- });
24
- };
25
- };
26
- const Portals = ({ renderers }) => {
27
- return (React__default.default.createElement(React__default.default.Fragment, null, Object.entries(renderers).map(([key, renderer]) => {
28
- return ReactDOM__default.default.createPortal(renderer.reactElement, renderer.element, key);
29
- })));
30
- };
31
- class PureEditorContent extends React__default.default.Component {
32
- constructor(props) {
33
- super(props);
34
- this.editorContentRef = React__default.default.createRef();
35
- this.initialized = false;
36
- this.state = {
37
- renderers: {},
38
- };
39
- }
40
- componentDidMount() {
41
- this.init();
42
- }
43
- componentDidUpdate() {
44
- this.init();
45
- }
46
- init() {
47
- const { editor } = this.props;
48
- if (editor && !editor.isDestroyed && editor.options.element) {
49
- if (editor.contentComponent) {
50
- return;
51
- }
52
- const element = this.editorContentRef.current;
53
- element.append(...editor.options.element.childNodes);
54
- editor.setOptions({
55
- element,
56
- });
57
- editor.contentComponent = this;
58
- editor.createNodeViews();
59
- this.initialized = true;
60
- }
61
- }
62
- maybeFlushSync(fn) {
63
- // Avoid calling flushSync until the editor is initialized.
64
- // Initialization happens during the componentDidMount or componentDidUpdate
65
- // lifecycle methods, and React doesn't allow calling flushSync from inside
66
- // a lifecycle method.
67
- if (this.initialized) {
68
- ReactDOM.flushSync(fn);
69
- }
70
- else {
71
- fn();
72
- }
73
- }
74
- setRenderer(id, renderer) {
75
- this.maybeFlushSync(() => {
76
- this.setState(({ renderers }) => ({
77
- renderers: {
78
- ...renderers,
79
- [id]: renderer,
80
- },
81
- }));
82
- });
83
- }
84
- removeRenderer(id) {
85
- this.maybeFlushSync(() => {
86
- this.setState(({ renderers }) => {
87
- const nextRenderers = { ...renderers };
88
- delete nextRenderers[id];
89
- return { renderers: nextRenderers };
90
- });
91
- });
92
- }
93
- componentWillUnmount() {
94
- const { editor } = this.props;
95
- if (!editor) {
96
- return;
97
- }
98
- this.initialized = false;
99
- if (!editor.isDestroyed) {
100
- editor.view.setProps({
101
- nodeViews: {},
102
- });
103
- }
104
- editor.contentComponent = null;
105
- if (!editor.options.element.firstChild) {
106
- return;
107
- }
108
- const newElement = document.createElement('div');
109
- newElement.append(...editor.options.element.childNodes);
110
- editor.setOptions({
111
- element: newElement,
112
- });
113
- }
114
- render() {
115
- const { editor, innerRef, ...rest } = this.props;
116
- return (React__default.default.createElement(React__default.default.Fragment, null,
117
- React__default.default.createElement("div", { ref: mergeRefs(innerRef, this.editorContentRef), ...rest }),
118
- React__default.default.createElement(Portals, { renderers: this.state.renderers })));
119
- }
120
- }
121
- // EditorContent should be re-created whenever the Editor instance changes
122
- const EditorContentWithKey = React.forwardRef((props, ref) => {
123
- const key = React__default.default.useMemo(() => {
124
- return Math.floor(Math.random() * 0xFFFFFFFF).toString();
125
- }, [props.editor]);
126
- // Can't use JSX here because it conflicts with the type definition of Vue's JSX, so use createElement
127
- return React__default.default.createElement(PureEditorContent, {
128
- key,
129
- innerRef: ref,
130
- ...props,
131
- });
132
- });
133
- const EditorContent = React__default.default.memo(EditorContentWithKey);
134
-
135
14
  var shim = {exports: {}};
136
15
 
137
16
  var useSyncExternalStoreShim_production_min = {};
@@ -410,12 +289,161 @@ if (process.env.NODE_ENV === 'production') {
410
289
 
411
290
  var shimExports = shim.exports;
412
291
 
413
- class Editor extends core.Editor {
414
- constructor() {
415
- super(...arguments);
416
- this.contentComponent = null;
292
+ const mergeRefs = (...refs) => {
293
+ return (node) => {
294
+ refs.forEach(ref => {
295
+ if (typeof ref === 'function') {
296
+ ref(node);
297
+ }
298
+ else if (ref) {
299
+ ref.current = node;
300
+ }
301
+ });
302
+ };
303
+ };
304
+ /**
305
+ * This component renders all of the editor's node views.
306
+ */
307
+ const Portals = ({ contentComponent, }) => {
308
+ // For performance reasons, we render the node view portals on state changes only
309
+ const renderers = shimExports.useSyncExternalStore(contentComponent.subscribe, contentComponent.getSnapshot, contentComponent.getServerSnapshot);
310
+ // This allows us to directly render the portals without any additional wrapper
311
+ return (React__default.default.createElement(React__default.default.Fragment, null, Object.values(renderers)));
312
+ };
313
+ function getInstance() {
314
+ const subscribers = new Set();
315
+ let renderers = {};
316
+ return {
317
+ /**
318
+ * Subscribe to the editor instance's changes.
319
+ */
320
+ subscribe(callback) {
321
+ subscribers.add(callback);
322
+ return () => {
323
+ subscribers.delete(callback);
324
+ };
325
+ },
326
+ getSnapshot() {
327
+ return renderers;
328
+ },
329
+ getServerSnapshot() {
330
+ return renderers;
331
+ },
332
+ /**
333
+ * Adds a new NodeView Renderer to the editor.
334
+ */
335
+ setRenderer(id, renderer) {
336
+ renderers = {
337
+ ...renderers,
338
+ [id]: ReactDOM__default.default.createPortal(renderer.reactElement, renderer.element, id),
339
+ };
340
+ subscribers.forEach(subscriber => subscriber());
341
+ },
342
+ /**
343
+ * Removes a NodeView Renderer from the editor.
344
+ */
345
+ removeRenderer(id) {
346
+ const nextRenderers = { ...renderers };
347
+ delete nextRenderers[id];
348
+ renderers = nextRenderers;
349
+ subscribers.forEach(subscriber => subscriber());
350
+ },
351
+ };
352
+ }
353
+ class PureEditorContent extends React__default.default.Component {
354
+ constructor(props) {
355
+ super(props);
356
+ this.editorContentRef = React__default.default.createRef();
357
+ this.initialized = false;
358
+ this.state = {
359
+ hasContentComponentInitialized: Boolean(props.editor.contentComponent),
360
+ };
361
+ }
362
+ componentDidMount() {
363
+ this.init();
364
+ }
365
+ componentDidUpdate() {
366
+ this.init();
367
+ }
368
+ init() {
369
+ const editor = this.props.editor;
370
+ if (editor && !editor.isDestroyed && editor.options.element) {
371
+ if (editor.contentComponent) {
372
+ return;
373
+ }
374
+ const element = this.editorContentRef.current;
375
+ element.append(...editor.options.element.childNodes);
376
+ editor.setOptions({
377
+ element,
378
+ });
379
+ editor.contentComponent = getInstance();
380
+ // Has the content component been initialized?
381
+ if (!this.state.hasContentComponentInitialized) {
382
+ // Subscribe to the content component
383
+ this.unsubscribeToContentComponent = editor.contentComponent.subscribe(() => {
384
+ this.setState(prevState => {
385
+ if (!prevState.hasContentComponentInitialized) {
386
+ return {
387
+ hasContentComponentInitialized: true,
388
+ };
389
+ }
390
+ return prevState;
391
+ });
392
+ // Unsubscribe to previous content component
393
+ if (this.unsubscribeToContentComponent) {
394
+ this.unsubscribeToContentComponent();
395
+ }
396
+ });
397
+ }
398
+ editor.createNodeViews();
399
+ this.initialized = true;
400
+ }
401
+ }
402
+ componentWillUnmount() {
403
+ const editor = this.props.editor;
404
+ if (!editor) {
405
+ return;
406
+ }
407
+ this.initialized = false;
408
+ if (!editor.isDestroyed) {
409
+ editor.view.setProps({
410
+ nodeViews: {},
411
+ });
412
+ }
413
+ if (this.unsubscribeToContentComponent) {
414
+ this.unsubscribeToContentComponent();
415
+ }
416
+ editor.contentComponent = null;
417
+ if (!editor.options.element.firstChild) {
418
+ return;
419
+ }
420
+ const newElement = document.createElement('div');
421
+ newElement.append(...editor.options.element.childNodes);
422
+ editor.setOptions({
423
+ element: newElement,
424
+ });
425
+ }
426
+ render() {
427
+ const { editor, innerRef, ...rest } = this.props;
428
+ return (React__default.default.createElement(React__default.default.Fragment, null,
429
+ React__default.default.createElement("div", { ref: mergeRefs(innerRef, this.editorContentRef), ...rest }),
430
+ (editor === null || editor === void 0 ? void 0 : editor.contentComponent) && React__default.default.createElement(Portals, { contentComponent: editor.contentComponent })));
417
431
  }
418
432
  }
433
+ // EditorContent should be re-created whenever the Editor instance changes
434
+ const EditorContentWithKey = React.forwardRef((props, ref) => {
435
+ const key = React__default.default.useMemo(() => {
436
+ return Math.floor(Math.random() * 0xffffffff).toString();
437
+ // eslint-disable-next-line react-hooks/exhaustive-deps
438
+ }, [props.editor]);
439
+ // Can't use JSX here because it conflicts with the type definition of Vue's JSX, so use createElement
440
+ return React__default.default.createElement(PureEditorContent, {
441
+ key,
442
+ innerRef: ref,
443
+ ...props,
444
+ });
445
+ });
446
+ const EditorContent = React__default.default.memo(EditorContentWithKey);
419
447
 
420
448
  var withSelector = {exports: {}};
421
449
 
@@ -775,17 +803,20 @@ class EditorInstanceManager {
775
803
  * Create a new editor instance. And attach event listeners.
776
804
  */
777
805
  createEditor() {
778
- const editor = new Editor(this.options.current);
779
- // Always call the most recent version of the callback function by default
780
- editor.on('beforeCreate', (...args) => { var _a, _b; return (_b = (_a = this.options.current).onBeforeCreate) === null || _b === void 0 ? void 0 : _b.call(_a, ...args); });
781
- editor.on('blur', (...args) => { var _a, _b; return (_b = (_a = this.options.current).onBlur) === null || _b === void 0 ? void 0 : _b.call(_a, ...args); });
782
- editor.on('create', (...args) => { var _a, _b; return (_b = (_a = this.options.current).onCreate) === null || _b === void 0 ? void 0 : _b.call(_a, ...args); });
783
- editor.on('destroy', (...args) => { var _a, _b; return (_b = (_a = this.options.current).onDestroy) === null || _b === void 0 ? void 0 : _b.call(_a, ...args); });
784
- editor.on('focus', (...args) => { var _a, _b; return (_b = (_a = this.options.current).onFocus) === null || _b === void 0 ? void 0 : _b.call(_a, ...args); });
785
- editor.on('selectionUpdate', (...args) => { var _a, _b; return (_b = (_a = this.options.current).onSelectionUpdate) === null || _b === void 0 ? void 0 : _b.call(_a, ...args); });
786
- editor.on('transaction', (...args) => { var _a, _b; return (_b = (_a = this.options.current).onTransaction) === null || _b === void 0 ? void 0 : _b.call(_a, ...args); });
787
- editor.on('update', (...args) => { var _a, _b; return (_b = (_a = this.options.current).onUpdate) === null || _b === void 0 ? void 0 : _b.call(_a, ...args); });
788
- editor.on('contentError', (...args) => { var _a, _b; return (_b = (_a = this.options.current).onContentError) === null || _b === void 0 ? void 0 : _b.call(_a, ...args); });
806
+ const optionsToApply = {
807
+ ...this.options.current,
808
+ // Always call the most recent version of the callback function by default
809
+ onBeforeCreate: (...args) => { var _a, _b; return (_b = (_a = this.options.current).onBeforeCreate) === null || _b === void 0 ? void 0 : _b.call(_a, ...args); },
810
+ onBlur: (...args) => { var _a, _b; return (_b = (_a = this.options.current).onBlur) === null || _b === void 0 ? void 0 : _b.call(_a, ...args); },
811
+ onCreate: (...args) => { var _a, _b; return (_b = (_a = this.options.current).onCreate) === null || _b === void 0 ? void 0 : _b.call(_a, ...args); },
812
+ onDestroy: (...args) => { var _a, _b; return (_b = (_a = this.options.current).onDestroy) === null || _b === void 0 ? void 0 : _b.call(_a, ...args); },
813
+ onFocus: (...args) => { var _a, _b; return (_b = (_a = this.options.current).onFocus) === null || _b === void 0 ? void 0 : _b.call(_a, ...args); },
814
+ onSelectionUpdate: (...args) => { var _a, _b; return (_b = (_a = this.options.current).onSelectionUpdate) === null || _b === void 0 ? void 0 : _b.call(_a, ...args); },
815
+ onTransaction: (...args) => { var _a, _b; return (_b = (_a = this.options.current).onTransaction) === null || _b === void 0 ? void 0 : _b.call(_a, ...args); },
816
+ onUpdate: (...args) => { var _a, _b; return (_b = (_a = this.options.current).onUpdate) === null || _b === void 0 ? void 0 : _b.call(_a, ...args); },
817
+ onContentError: (...args) => { var _a, _b; return (_b = (_a = this.options.current).onContentError) === null || _b === void 0 ? void 0 : _b.call(_a, ...args); },
818
+ };
819
+ const editor = new core.Editor(optionsToApply);
789
820
  // no need to keep track of the event listeners, they will be removed when the editor is destroyed
790
821
  return editor;
791
822
  }
@@ -1019,7 +1050,9 @@ const useReactNodeView = () => React.useContext(ReactNodeViewContext);
1019
1050
  const NodeViewContent = props => {
1020
1051
  const Tag = props.as || 'div';
1021
1052
  const { nodeViewContentRef } = useReactNodeView();
1022
- return (React__default.default.createElement(Tag, { ...props, ref: nodeViewContentRef, "data-node-view-content": "", style: {
1053
+ return (
1054
+ // @ts-ignore
1055
+ React__default.default.createElement(Tag, { ...props, ref: nodeViewContentRef, "data-node-view-content": "", style: {
1023
1056
  whiteSpace: 'pre-wrap',
1024
1057
  ...props.style,
1025
1058
  } }));
@@ -1028,7 +1061,9 @@ const NodeViewContent = props => {
1028
1061
  const NodeViewWrapper = React__default.default.forwardRef((props, ref) => {
1029
1062
  const { onDragStart } = useReactNodeView();
1030
1063
  const Tag = props.as || 'div';
1031
- return (React__default.default.createElement(Tag, { ...props, ref: ref, "data-node-view-wrapper": "", onDragStart: onDragStart, style: {
1064
+ return (
1065
+ // @ts-ignore
1066
+ React__default.default.createElement(Tag, { ...props, ref: ref, "data-node-view-wrapper": "", onDragStart: onDragStart, style: {
1032
1067
  whiteSpace: 'normal',
1033
1068
  ...props.style,
1034
1069
  } }));
@@ -1082,19 +1117,29 @@ class ReactRenderer {
1082
1117
  this.element.setAttribute(key, attrs[key]);
1083
1118
  });
1084
1119
  }
1085
- this.render();
1120
+ if (this.editor.isInitialized) {
1121
+ // On first render, we need to flush the render synchronously
1122
+ // Renders afterwards can be async, but this fixes a cursor positioning issue
1123
+ ReactDOM.flushSync(() => {
1124
+ this.render();
1125
+ });
1126
+ }
1127
+ else {
1128
+ this.render();
1129
+ }
1086
1130
  }
1087
1131
  render() {
1088
- var _a, _b;
1132
+ var _a;
1089
1133
  const Component = this.component;
1090
1134
  const props = this.props;
1135
+ const editor = this.editor;
1091
1136
  if (isClassComponent(Component) || isForwardRefComponent(Component)) {
1092
1137
  props.ref = (ref) => {
1093
1138
  this.ref = ref;
1094
1139
  };
1095
1140
  }
1096
- this.reactElement = React__default.default.createElement(Component, { ...props });
1097
- (_b = (_a = this.editor) === null || _a === void 0 ? void 0 : _a.contentComponent) === null || _b === void 0 ? void 0 : _b.setRenderer(this.id, this);
1141
+ this.reactElement = React__default.default.createElement(Component, props);
1142
+ (_a = editor === null || editor === void 0 ? void 0 : editor.contentComponent) === null || _a === void 0 ? void 0 : _a.setRenderer(this.id, this);
1098
1143
  }
1099
1144
  updateProps(props = {}) {
1100
1145
  this.props = {
@@ -1104,8 +1149,9 @@ class ReactRenderer {
1104
1149
  this.render();
1105
1150
  }
1106
1151
  destroy() {
1107
- var _a, _b;
1108
- (_b = (_a = this.editor) === null || _a === void 0 ? void 0 : _a.contentComponent) === null || _b === void 0 ? void 0 : _b.removeRenderer(this.id);
1152
+ var _a;
1153
+ const editor = this.editor;
1154
+ (_a = editor === null || editor === void 0 ? void 0 : editor.contentComponent) === null || _a === void 0 ? void 0 : _a.removeRenderer(this.id);
1109
1155
  }
1110
1156
  }
1111
1157
 
@@ -1127,18 +1173,19 @@ class ReactNodeView extends core.NodeView {
1127
1173
  };
1128
1174
  this.component.displayName = capitalizeFirstChar(this.extension.name);
1129
1175
  }
1130
- const ReactNodeViewProvider = componentProps => {
1131
- const Component = this.component;
1132
- const onDragStart = this.onDragStart.bind(this);
1133
- const nodeViewContentRef = element => {
1134
- if (element && this.contentDOMElement && element.firstChild !== this.contentDOMElement) {
1135
- element.appendChild(this.contentDOMElement);
1136
- }
1137
- };
1138
- return (React__default.default.createElement(React__default.default.Fragment, null,
1139
- React__default.default.createElement(ReactNodeViewContext.Provider, { value: { onDragStart, nodeViewContentRef } },
1140
- React__default.default.createElement(Component, { ...componentProps }))));
1176
+ const onDragStart = this.onDragStart.bind(this);
1177
+ const nodeViewContentRef = element => {
1178
+ if (element && this.contentDOMElement && element.firstChild !== this.contentDOMElement) {
1179
+ element.appendChild(this.contentDOMElement);
1180
+ }
1141
1181
  };
1182
+ const context = { onDragStart, nodeViewContentRef };
1183
+ const Component = this.component;
1184
+ // For performance reasons, we memoize the provider component
1185
+ // And all of the things it requires are declared outside of the component, so it doesn't need to re-render
1186
+ const ReactNodeViewProvider = React__default.default.memo(componentProps => {
1187
+ return (React__default.default.createElement(ReactNodeViewContext.Provider, { value: context }, React__default.default.createElement(Component, componentProps)));
1188
+ });
1142
1189
  ReactNodeViewProvider.displayName = 'ReactNodeView';
1143
1190
  if (this.node.isLeaf) {
1144
1191
  this.contentDOMElement = null;
@@ -1257,8 +1304,11 @@ function ReactNodeViewRenderer(component, options) {
1257
1304
  };
1258
1305
  }
1259
1306
 
1307
+ Object.defineProperty(exports, "Editor", {
1308
+ enumerable: true,
1309
+ get: function () { return core.Editor; }
1310
+ });
1260
1311
  exports.BubbleMenu = BubbleMenu;
1261
- exports.Editor = Editor;
1262
1312
  exports.EditorConsumer = EditorConsumer;
1263
1313
  exports.EditorContent = EditorContent;
1264
1314
  exports.EditorContext = EditorContext;