@handlewithcare/react-prosemirror 2.8.0-tiptap.17 → 2.8.0-tiptap.19

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.
@@ -10,10 +10,9 @@ Object.defineProperty(exports, "TiptapEditorContent", {
10
10
  });
11
11
  const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
12
12
  const _reactdom = require("react-dom");
13
- const _ReactEditorView = require("../ReactEditorView.js");
14
13
  const _ProseMirrorDoc = require("../components/ProseMirrorDoc.js");
15
- const _EditorContext = require("../contexts/EditorContext.js");
16
- const _useClientLayoutEffect = require("../hooks/useClientLayoutEffect.js");
14
+ const _useEditorEffect = require("../hooks/useEditorEffect.js");
15
+ const _TiptapEditorContext = require("./contexts/TiptapEditorContext.js");
17
16
  function _getRequireWildcardCache(nodeInterop) {
18
17
  if (typeof WeakMap !== "function") return null;
19
18
  var cacheBabelInterop = new WeakMap();
@@ -104,9 +103,9 @@ function getInstance() {
104
103
  function TiptapEditorContent(param) {
105
104
  let { editor: editorProp, ...props } = param;
106
105
  const editor = editorProp;
107
- const { view } = (0, _react.useContext)(_EditorContext.EditorContext);
108
- (0, _useClientLayoutEffect.useClientLayoutEffect)(()=>{
109
- if (!(view instanceof _ReactEditorView.ReactEditorView) || editor.view === view) {
106
+ const { onEditorInitialize, onEditorDeinitialize } = (0, _react.useContext)(_TiptapEditorContext.TiptapEditorContext);
107
+ (0, _useEditorEffect.useEditorEffect)((view)=>{
108
+ if (editor.view === view) {
110
109
  return;
111
110
  }
112
111
  // @ts-expect-error private property
@@ -125,14 +124,17 @@ function TiptapEditorContent(param) {
125
124
  editor
126
125
  });
127
126
  editor.isInitialized = true;
127
+ onEditorInitialize();
128
128
  });
129
129
  return ()=>{
130
130
  editor.isInitialized = false;
131
131
  editor.contentComponent = null;
132
+ onEditorDeinitialize();
132
133
  };
133
134
  }, [
134
135
  editor,
135
- view
136
+ onEditorDeinitialize,
137
+ onEditorInitialize
136
138
  ]);
137
139
  return /*#__PURE__*/ _react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/ _react.default.createElement(_ProseMirrorDoc.ProseMirrorDoc, props), editor?.contentComponent && /*#__PURE__*/ _react.default.createElement(Portals, {
138
140
  contentComponent: editor.contentComponent
@@ -12,6 +12,7 @@ const _react = require("@tiptap/react");
12
12
  const _react1 = /*#__PURE__*/ _interop_require_wildcard(require("react"));
13
13
  const _ProseMirror = require("../components/ProseMirror.js");
14
14
  const _useForceUpdate = require("../hooks/useForceUpdate.js");
15
+ const _TiptapEditorContext = require("./contexts/TiptapEditorContext.js");
15
16
  function _getRequireWildcardCache(nodeInterop) {
16
17
  if (typeof WeakMap !== "function") return null;
17
18
  var cacheBabelInterop = new WeakMap();
@@ -55,6 +56,7 @@ function _interop_require_wildcard(obj, nodeInterop) {
55
56
  }
56
57
  function TiptapEditorView(param) {
57
58
  let { editor, nodeViews, children, static: isStatic = false } = param;
59
+ const [isEditorInitialized, setIsEditorInitialized] = (0, _react1.useState)(editor.isInitialized);
58
60
  const forceUpdate = (0, _useForceUpdate.useForceUpdate)();
59
61
  const dispatchTransaction = (0, _react1.useCallback)((tr)=>{
60
62
  // @ts-expect-error calling private method
@@ -83,6 +85,21 @@ function TiptapEditorView(param) {
83
85
  }), [
84
86
  editor
85
87
  ]);
88
+ const onEditorInitialize = (0, _react1.useCallback)(()=>{
89
+ setIsEditorInitialized(true);
90
+ }, []);
91
+ const onEditorDeinitialize = (0, _react1.useCallback)(()=>{
92
+ setIsEditorInitialized(false);
93
+ }, []);
94
+ const tiptapEditorContextValue = (0, _react1.useMemo)(()=>({
95
+ isEditorInitialized,
96
+ onEditorInitialize,
97
+ onEditorDeinitialize
98
+ }), [
99
+ isEditorInitialized,
100
+ onEditorDeinitialize,
101
+ onEditorInitialize
102
+ ]);
86
103
  return /*#__PURE__*/ _react1.default.createElement(_ProseMirror.ProseMirror, {
87
104
  static: isStatic,
88
105
  className: "tiptap",
@@ -90,11 +107,11 @@ function TiptapEditorView(param) {
90
107
  markViews: markViews,
91
108
  nodeViews: nodeViews,
92
109
  customNodeViews: customNodeViews,
93
- state: editor.isDestroyed || editor.state.plugins.length ? editor.state : editor.state.reconfigure({
94
- plugins: editor.extensionManager.plugins
95
- }),
110
+ state: editor.state,
96
111
  dispatchTransaction: dispatchTransaction
97
112
  }, /*#__PURE__*/ _react1.default.createElement(_react.EditorContext.Provider, {
98
113
  value: contextValue
99
- }, children));
114
+ }, /*#__PURE__*/ _react1.default.createElement(_TiptapEditorContext.TiptapEditorContext.Provider, {
115
+ value: tiptapEditorContextValue
116
+ }, children)));
100
117
  }
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "TiptapEditorContext", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return TiptapEditorContext;
9
+ }
10
+ });
11
+ const _react = require("react");
12
+ const TiptapEditorContext = (0, _react.createContext)(null);
@@ -25,5 +25,17 @@ function useTiptapEditor(options, deps) {
25
25
  ...editor.options.editorProps.attributes
26
26
  }
27
27
  });
28
+ // @ts-expect-error private property
29
+ const stateHasPlugins = !!editor.editorState.plugins.length;
30
+ const managerHasPlugins = !!editor.extensionManager.plugins.length;
31
+ const stateNeedsReconfigure = !stateHasPlugins && managerHasPlugins && !editor.isDestroyed;
32
+ // @ts-expect-error private property
33
+ editor.editorState = stateNeedsReconfigure ? editor.editorState.reconfigure({
34
+ plugins: editor.extensionManager.plugins
35
+ }) : editor.editorState;
36
+ if (stateNeedsReconfigure) {
37
+ // @ts-expect-error private property
38
+ editor.editorView.updateState(editor.editorState);
39
+ }
28
40
  return editor;
29
41
  }
@@ -13,11 +13,13 @@ const _react1 = require("react");
13
13
  const _ReactEditorView = require("../../ReactEditorView.js");
14
14
  const _EditorContext = require("../../contexts/EditorContext.js");
15
15
  const _useEditorEffect = require("../../hooks/useEditorEffect.js");
16
+ const _TiptapEditorContext = require("../contexts/TiptapEditorContext.js");
16
17
  function useTiptapEditorEffect(effect, dependencies) {
17
18
  const { view } = (0, _react1.useContext)(_EditorContext.EditorContext);
18
19
  const { editor } = (0, _react.useCurrentEditor)();
20
+ const { isEditorInitialized } = (0, _react1.useContext)(_TiptapEditorContext.TiptapEditorContext);
19
21
  (0, _useEditorEffect.useEditorEffect)(()=>{
20
- if (editor?.view instanceof _ReactEditorView.ReactEditorView && view instanceof _ReactEditorView.ReactEditorView) {
22
+ if (editor?.view instanceof _ReactEditorView.ReactEditorView && view instanceof _ReactEditorView.ReactEditorView && isEditorInitialized) {
21
23
  return effect(editor);
22
24
  }
23
25
  // The rules of hooks want to be able to statically
@@ -27,6 +29,7 @@ function useTiptapEditorEffect(effect, dependencies) {
27
29
  }, dependencies && [
28
30
  editor,
29
31
  view,
32
+ isEditorInitialized,
30
33
  ...dependencies
31
34
  ]);
32
35
  }
@@ -1,9 +1,8 @@
1
1
  import React, { useContext, useSyncExternalStore } from "react";
2
2
  import { createPortal } from "react-dom";
3
- import { ReactEditorView } from "../ReactEditorView.js";
4
3
  import { ProseMirrorDoc } from "../components/ProseMirrorDoc.js";
5
- import { EditorContext } from "../contexts/EditorContext.js";
6
- import { useClientLayoutEffect } from "../hooks/useClientLayoutEffect.js";
4
+ import { useEditorEffect } from "../hooks/useEditorEffect.js";
5
+ import { TiptapEditorContext } from "./contexts/TiptapEditorContext.js";
7
6
  /**
8
7
  * This component renders all of the editor's registered "React renderers".
9
8
  */ const Portals = (param)=>{
@@ -53,9 +52,9 @@ function getInstance() {
53
52
  export function TiptapEditorContent(param) {
54
53
  let { editor: editorProp, ...props } = param;
55
54
  const editor = editorProp;
56
- const { view } = useContext(EditorContext);
57
- useClientLayoutEffect(()=>{
58
- if (!(view instanceof ReactEditorView) || editor.view === view) {
55
+ const { onEditorInitialize, onEditorDeinitialize } = useContext(TiptapEditorContext);
56
+ useEditorEffect((view)=>{
57
+ if (editor.view === view) {
59
58
  return;
60
59
  }
61
60
  // @ts-expect-error private property
@@ -74,14 +73,17 @@ export function TiptapEditorContent(param) {
74
73
  editor
75
74
  });
76
75
  editor.isInitialized = true;
76
+ onEditorInitialize();
77
77
  });
78
78
  return ()=>{
79
79
  editor.isInitialized = false;
80
80
  editor.contentComponent = null;
81
+ onEditorDeinitialize();
81
82
  };
82
83
  }, [
83
84
  editor,
84
- view
85
+ onEditorDeinitialize,
86
+ onEditorInitialize
85
87
  ]);
86
88
  return /*#__PURE__*/ React.createElement(React.Fragment, null, /*#__PURE__*/ React.createElement(ProseMirrorDoc, props), editor?.contentComponent && /*#__PURE__*/ React.createElement(Portals, {
87
89
  contentComponent: editor.contentComponent
@@ -1,11 +1,13 @@
1
1
  import { EditorContext } from "@tiptap/react";
2
- import React, { useCallback, useMemo } from "react";
2
+ import React, { useCallback, useMemo, useState } from "react";
3
3
  import { ProseMirror } from "../components/ProseMirror.js";
4
4
  import { useForceUpdate } from "../hooks/useForceUpdate.js";
5
+ import { TiptapEditorContext } from "./contexts/TiptapEditorContext.js";
5
6
  /**
6
7
  * Render a Tiptap-compatible React ProseMirror editor.
7
8
  */ export function TiptapEditorView(param) {
8
9
  let { editor, nodeViews, children, static: isStatic = false } = param;
10
+ const [isEditorInitialized, setIsEditorInitialized] = useState(editor.isInitialized);
9
11
  const forceUpdate = useForceUpdate();
10
12
  const dispatchTransaction = useCallback((tr)=>{
11
13
  // @ts-expect-error calling private method
@@ -34,6 +36,21 @@ import { useForceUpdate } from "../hooks/useForceUpdate.js";
34
36
  }), [
35
37
  editor
36
38
  ]);
39
+ const onEditorInitialize = useCallback(()=>{
40
+ setIsEditorInitialized(true);
41
+ }, []);
42
+ const onEditorDeinitialize = useCallback(()=>{
43
+ setIsEditorInitialized(false);
44
+ }, []);
45
+ const tiptapEditorContextValue = useMemo(()=>({
46
+ isEditorInitialized,
47
+ onEditorInitialize,
48
+ onEditorDeinitialize
49
+ }), [
50
+ isEditorInitialized,
51
+ onEditorDeinitialize,
52
+ onEditorInitialize
53
+ ]);
37
54
  return /*#__PURE__*/ React.createElement(ProseMirror, {
38
55
  static: isStatic,
39
56
  className: "tiptap",
@@ -41,11 +58,11 @@ import { useForceUpdate } from "../hooks/useForceUpdate.js";
41
58
  markViews: markViews,
42
59
  nodeViews: nodeViews,
43
60
  customNodeViews: customNodeViews,
44
- state: editor.isDestroyed || editor.state.plugins.length ? editor.state : editor.state.reconfigure({
45
- plugins: editor.extensionManager.plugins
46
- }),
61
+ state: editor.state,
47
62
  dispatchTransaction: dispatchTransaction
48
63
  }, /*#__PURE__*/ React.createElement(EditorContext.Provider, {
49
64
  value: contextValue
50
- }, children));
65
+ }, /*#__PURE__*/ React.createElement(TiptapEditorContext.Provider, {
66
+ value: tiptapEditorContextValue
67
+ }, children)));
51
68
  }
@@ -0,0 +1,2 @@
1
+ import { createContext } from "react";
2
+ export const TiptapEditorContext = createContext(null);
@@ -15,5 +15,17 @@ export function useTiptapEditor(options, deps) {
15
15
  ...editor.options.editorProps.attributes
16
16
  }
17
17
  });
18
+ // @ts-expect-error private property
19
+ const stateHasPlugins = !!editor.editorState.plugins.length;
20
+ const managerHasPlugins = !!editor.extensionManager.plugins.length;
21
+ const stateNeedsReconfigure = !stateHasPlugins && managerHasPlugins && !editor.isDestroyed;
22
+ // @ts-expect-error private property
23
+ editor.editorState = stateNeedsReconfigure ? editor.editorState.reconfigure({
24
+ plugins: editor.extensionManager.plugins
25
+ }) : editor.editorState;
26
+ if (stateNeedsReconfigure) {
27
+ // @ts-expect-error private property
28
+ editor.editorView.updateState(editor.editorState);
29
+ }
18
30
  return editor;
19
31
  }
@@ -3,6 +3,7 @@ import { useContext } from "react";
3
3
  import { ReactEditorView } from "../../ReactEditorView.js";
4
4
  import { EditorContext } from "../../contexts/EditorContext.js";
5
5
  import { useEditorEffect } from "../../hooks/useEditorEffect.js";
6
+ import { TiptapEditorContext } from "../contexts/TiptapEditorContext.js";
6
7
  /**
7
8
  * Registers a layout effect to run after the EditorView has
8
9
  * been updated with the latest EditorState and Decorations.
@@ -23,8 +24,9 @@ import { useEditorEffect } from "../../hooks/useEditorEffect.js";
23
24
  */ export function useTiptapEditorEffect(effect, dependencies) {
24
25
  const { view } = useContext(EditorContext);
25
26
  const { editor } = useCurrentEditor();
27
+ const { isEditorInitialized } = useContext(TiptapEditorContext);
26
28
  useEditorEffect(()=>{
27
- if (editor?.view instanceof ReactEditorView && view instanceof ReactEditorView) {
29
+ if (editor?.view instanceof ReactEditorView && view instanceof ReactEditorView && isEditorInitialized) {
28
30
  return effect(editor);
29
31
  }
30
32
  // The rules of hooks want to be able to statically
@@ -34,6 +36,7 @@ import { useEditorEffect } from "../../hooks/useEditorEffect.js";
34
36
  }, dependencies && [
35
37
  editor,
36
38
  view,
39
+ isEditorInitialized,
37
40
  ...dependencies
38
41
  ]);
39
42
  }