@tiptap/react 3.0.0 → 3.0.2-beta.0

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.
Files changed (215) hide show
  1. package/LICENSE.md +21 -0
  2. package/README.md +5 -1
  3. package/dist/index.cjs +1030 -1163
  4. package/dist/index.cjs.map +1 -1
  5. package/dist/index.d.cts +350 -0
  6. package/dist/index.d.ts +350 -0
  7. package/dist/index.js +970 -1138
  8. package/dist/index.js.map +1 -1
  9. package/dist/menus/index.cjs +142 -0
  10. package/dist/menus/index.cjs.map +1 -0
  11. package/dist/menus/index.d.cts +19 -0
  12. package/dist/menus/index.d.ts +19 -0
  13. package/dist/menus/index.js +104 -0
  14. package/dist/menus/index.js.map +1 -0
  15. package/package.json +34 -21
  16. package/src/Context.tsx +18 -12
  17. package/src/Editor.ts +10 -11
  18. package/src/EditorContent.tsx +104 -64
  19. package/src/NodeViewContent.tsx +13 -8
  20. package/src/NodeViewWrapper.tsx +3 -2
  21. package/src/ReactMarkViewRenderer.tsx +111 -0
  22. package/src/ReactNodeViewRenderer.tsx +184 -67
  23. package/src/ReactRenderer.tsx +152 -51
  24. package/src/index.ts +2 -3
  25. package/src/menus/BubbleMenu.tsx +68 -0
  26. package/src/menus/FloatingMenu.tsx +68 -0
  27. package/src/menus/index.ts +2 -0
  28. package/src/types.ts +6 -0
  29. package/src/useEditor.ts +286 -166
  30. package/src/useEditorState.ts +133 -85
  31. package/src/useReactNodeView.ts +21 -5
  32. package/dist/index.umd.js +0 -1219
  33. package/dist/index.umd.js.map +0 -1
  34. package/dist/packages/core/src/CommandManager.d.ts +0 -20
  35. package/dist/packages/core/src/Editor.d.ts +0 -161
  36. package/dist/packages/core/src/EventEmitter.d.ts +0 -11
  37. package/dist/packages/core/src/Extension.d.ts +0 -343
  38. package/dist/packages/core/src/ExtensionManager.d.ts +0 -55
  39. package/dist/packages/core/src/InputRule.d.ts +0 -42
  40. package/dist/packages/core/src/Mark.d.ts +0 -451
  41. package/dist/packages/core/src/Node.d.ts +0 -611
  42. package/dist/packages/core/src/NodePos.d.ts +0 -44
  43. package/dist/packages/core/src/NodeView.d.ts +0 -31
  44. package/dist/packages/core/src/PasteRule.d.ts +0 -50
  45. package/dist/packages/core/src/Tracker.d.ts +0 -11
  46. package/dist/packages/core/src/commands/blur.d.ts +0 -13
  47. package/dist/packages/core/src/commands/clearContent.d.ts +0 -14
  48. package/dist/packages/core/src/commands/clearNodes.d.ts +0 -13
  49. package/dist/packages/core/src/commands/command.d.ts +0 -18
  50. package/dist/packages/core/src/commands/createParagraphNear.d.ts +0 -13
  51. package/dist/packages/core/src/commands/cut.d.ts +0 -20
  52. package/dist/packages/core/src/commands/deleteCurrentNode.d.ts +0 -13
  53. package/dist/packages/core/src/commands/deleteNode.d.ts +0 -15
  54. package/dist/packages/core/src/commands/deleteRange.d.ts +0 -14
  55. package/dist/packages/core/src/commands/deleteSelection.d.ts +0 -13
  56. package/dist/packages/core/src/commands/enter.d.ts +0 -13
  57. package/dist/packages/core/src/commands/exitCode.d.ts +0 -13
  58. package/dist/packages/core/src/commands/extendMarkRange.d.ts +0 -25
  59. package/dist/packages/core/src/commands/first.d.ts +0 -14
  60. package/dist/packages/core/src/commands/focus.d.ts +0 -27
  61. package/dist/packages/core/src/commands/forEach.d.ts +0 -14
  62. package/dist/packages/core/src/commands/index.d.ts +0 -55
  63. package/dist/packages/core/src/commands/insertContent.d.ts +0 -34
  64. package/dist/packages/core/src/commands/insertContentAt.d.ts +0 -47
  65. package/dist/packages/core/src/commands/join.d.ts +0 -41
  66. package/dist/packages/core/src/commands/joinItemBackward.d.ts +0 -13
  67. package/dist/packages/core/src/commands/joinItemForward.d.ts +0 -13
  68. package/dist/packages/core/src/commands/joinTextblockBackward.d.ts +0 -12
  69. package/dist/packages/core/src/commands/joinTextblockForward.d.ts +0 -12
  70. package/dist/packages/core/src/commands/keyboardShortcut.d.ts +0 -14
  71. package/dist/packages/core/src/commands/lift.d.ts +0 -17
  72. package/dist/packages/core/src/commands/liftEmptyBlock.d.ts +0 -13
  73. package/dist/packages/core/src/commands/liftListItem.d.ts +0 -15
  74. package/dist/packages/core/src/commands/newlineInCode.d.ts +0 -13
  75. package/dist/packages/core/src/commands/resetAttributes.d.ts +0 -16
  76. package/dist/packages/core/src/commands/scrollIntoView.d.ts +0 -13
  77. package/dist/packages/core/src/commands/selectAll.d.ts +0 -13
  78. package/dist/packages/core/src/commands/selectNodeBackward.d.ts +0 -13
  79. package/dist/packages/core/src/commands/selectNodeForward.d.ts +0 -13
  80. package/dist/packages/core/src/commands/selectParentNode.d.ts +0 -13
  81. package/dist/packages/core/src/commands/selectTextblockEnd.d.ts +0 -13
  82. package/dist/packages/core/src/commands/selectTextblockStart.d.ts +0 -13
  83. package/dist/packages/core/src/commands/setContent.d.ts +0 -40
  84. package/dist/packages/core/src/commands/setMark.d.ts +0 -15
  85. package/dist/packages/core/src/commands/setMeta.d.ts +0 -15
  86. package/dist/packages/core/src/commands/setNode.d.ts +0 -16
  87. package/dist/packages/core/src/commands/setNodeSelection.d.ts +0 -14
  88. package/dist/packages/core/src/commands/setTextSelection.d.ts +0 -14
  89. package/dist/packages/core/src/commands/sinkListItem.d.ts +0 -15
  90. package/dist/packages/core/src/commands/splitBlock.d.ts +0 -17
  91. package/dist/packages/core/src/commands/splitListItem.d.ts +0 -15
  92. package/dist/packages/core/src/commands/toggleList.d.ts +0 -18
  93. package/dist/packages/core/src/commands/toggleMark.d.ts +0 -30
  94. package/dist/packages/core/src/commands/toggleNode.d.ts +0 -17
  95. package/dist/packages/core/src/commands/toggleWrap.d.ts +0 -16
  96. package/dist/packages/core/src/commands/undoInputRule.d.ts +0 -13
  97. package/dist/packages/core/src/commands/unsetAllMarks.d.ts +0 -13
  98. package/dist/packages/core/src/commands/unsetMark.d.ts +0 -25
  99. package/dist/packages/core/src/commands/updateAttributes.d.ts +0 -24
  100. package/dist/packages/core/src/commands/wrapIn.d.ts +0 -16
  101. package/dist/packages/core/src/commands/wrapInList.d.ts +0 -16
  102. package/dist/packages/core/src/extensions/clipboardTextSerializer.d.ts +0 -5
  103. package/dist/packages/core/src/extensions/commands.d.ts +0 -3
  104. package/dist/packages/core/src/extensions/editable.d.ts +0 -2
  105. package/dist/packages/core/src/extensions/focusEvents.d.ts +0 -2
  106. package/dist/packages/core/src/extensions/index.d.ts +0 -6
  107. package/dist/packages/core/src/extensions/keymap.d.ts +0 -2
  108. package/dist/packages/core/src/extensions/tabindex.d.ts +0 -2
  109. package/dist/packages/core/src/helpers/combineTransactionSteps.d.ts +0 -10
  110. package/dist/packages/core/src/helpers/createChainableState.d.ts +0 -10
  111. package/dist/packages/core/src/helpers/createDocument.d.ts +0 -12
  112. package/dist/packages/core/src/helpers/createNodeFromContent.d.ts +0 -15
  113. package/dist/packages/core/src/helpers/defaultBlockAt.d.ts +0 -7
  114. package/dist/packages/core/src/helpers/findChildren.d.ts +0 -9
  115. package/dist/packages/core/src/helpers/findChildrenInRange.d.ts +0 -10
  116. package/dist/packages/core/src/helpers/findParentNode.d.ts +0 -16
  117. package/dist/packages/core/src/helpers/findParentNodeClosestToPos.d.ts +0 -17
  118. package/dist/packages/core/src/helpers/generateHTML.d.ts +0 -8
  119. package/dist/packages/core/src/helpers/generateJSON.d.ts +0 -8
  120. package/dist/packages/core/src/helpers/generateText.d.ts +0 -12
  121. package/dist/packages/core/src/helpers/getAttributes.d.ts +0 -9
  122. package/dist/packages/core/src/helpers/getAttributesFromExtensions.d.ts +0 -6
  123. package/dist/packages/core/src/helpers/getChangedRanges.d.ts +0 -11
  124. package/dist/packages/core/src/helpers/getDebugJSON.d.ts +0 -8
  125. package/dist/packages/core/src/helpers/getExtensionField.d.ts +0 -9
  126. package/dist/packages/core/src/helpers/getHTMLFromFragment.d.ts +0 -2
  127. package/dist/packages/core/src/helpers/getMarkAttributes.d.ts +0 -3
  128. package/dist/packages/core/src/helpers/getMarkRange.d.ts +0 -3
  129. package/dist/packages/core/src/helpers/getMarkType.d.ts +0 -2
  130. package/dist/packages/core/src/helpers/getMarksBetween.d.ts +0 -3
  131. package/dist/packages/core/src/helpers/getNodeAtPosition.d.ts +0 -11
  132. package/dist/packages/core/src/helpers/getNodeAttributes.d.ts +0 -3
  133. package/dist/packages/core/src/helpers/getNodeType.d.ts +0 -2
  134. package/dist/packages/core/src/helpers/getRenderedAttributes.d.ts +0 -3
  135. package/dist/packages/core/src/helpers/getSchema.d.ts +0 -4
  136. package/dist/packages/core/src/helpers/getSchemaByResolvedExtensions.d.ts +0 -10
  137. package/dist/packages/core/src/helpers/getSchemaTypeByName.d.ts +0 -8
  138. package/dist/packages/core/src/helpers/getSchemaTypeNameByName.d.ts +0 -8
  139. package/dist/packages/core/src/helpers/getSplittedAttributes.d.ts +0 -9
  140. package/dist/packages/core/src/helpers/getText.d.ts +0 -15
  141. package/dist/packages/core/src/helpers/getTextBetween.d.ts +0 -14
  142. package/dist/packages/core/src/helpers/getTextContentFromNodes.d.ts +0 -8
  143. package/dist/packages/core/src/helpers/getTextSerializersFromSchema.d.ts +0 -8
  144. package/dist/packages/core/src/helpers/index.d.ts +0 -50
  145. package/dist/packages/core/src/helpers/injectExtensionAttributesToParseRule.d.ts +0 -9
  146. package/dist/packages/core/src/helpers/isActive.d.ts +0 -2
  147. package/dist/packages/core/src/helpers/isAtEndOfNode.d.ts +0 -2
  148. package/dist/packages/core/src/helpers/isAtStartOfNode.d.ts +0 -2
  149. package/dist/packages/core/src/helpers/isExtensionRulesEnabled.d.ts +0 -2
  150. package/dist/packages/core/src/helpers/isList.d.ts +0 -2
  151. package/dist/packages/core/src/helpers/isMarkActive.d.ts +0 -3
  152. package/dist/packages/core/src/helpers/isNodeActive.d.ts +0 -3
  153. package/dist/packages/core/src/helpers/isNodeEmpty.d.ts +0 -2
  154. package/dist/packages/core/src/helpers/isNodeSelection.d.ts +0 -2
  155. package/dist/packages/core/src/helpers/isTextSelection.d.ts +0 -2
  156. package/dist/packages/core/src/helpers/posToDOMRect.d.ts +0 -2
  157. package/dist/packages/core/src/helpers/resolveFocusPosition.d.ts +0 -4
  158. package/dist/packages/core/src/helpers/selectionToInsertionEnd.d.ts +0 -2
  159. package/dist/packages/core/src/helpers/splitExtensions.d.ts +0 -9
  160. package/dist/packages/core/src/index.d.ts +0 -24
  161. package/dist/packages/core/src/inputRules/index.d.ts +0 -5
  162. package/dist/packages/core/src/inputRules/markInputRule.d.ts +0 -13
  163. package/dist/packages/core/src/inputRules/nodeInputRule.d.ts +0 -23
  164. package/dist/packages/core/src/inputRules/textInputRule.d.ts +0 -10
  165. package/dist/packages/core/src/inputRules/textblockTypeInputRule.d.ts +0 -15
  166. package/dist/packages/core/src/inputRules/wrappingInputRule.d.ts +0 -28
  167. package/dist/packages/core/src/pasteRules/index.d.ts +0 -3
  168. package/dist/packages/core/src/pasteRules/markPasteRule.d.ts +0 -13
  169. package/dist/packages/core/src/pasteRules/nodePasteRule.d.ts +0 -13
  170. package/dist/packages/core/src/pasteRules/textPasteRule.d.ts +0 -10
  171. package/dist/packages/core/src/style.d.ts +0 -1
  172. package/dist/packages/core/src/types.d.ts +0 -255
  173. package/dist/packages/core/src/utilities/callOrReturn.d.ts +0 -9
  174. package/dist/packages/core/src/utilities/createStyleTag.d.ts +0 -1
  175. package/dist/packages/core/src/utilities/deleteProps.d.ts +0 -6
  176. package/dist/packages/core/src/utilities/elementFromString.d.ts +0 -1
  177. package/dist/packages/core/src/utilities/escapeForRegEx.d.ts +0 -1
  178. package/dist/packages/core/src/utilities/findDuplicates.d.ts +0 -1
  179. package/dist/packages/core/src/utilities/fromString.d.ts +0 -1
  180. package/dist/packages/core/src/utilities/index.d.ts +0 -20
  181. package/dist/packages/core/src/utilities/isAndroid.d.ts +0 -1
  182. package/dist/packages/core/src/utilities/isEmptyObject.d.ts +0 -1
  183. package/dist/packages/core/src/utilities/isFunction.d.ts +0 -1
  184. package/dist/packages/core/src/utilities/isMacOS.d.ts +0 -1
  185. package/dist/packages/core/src/utilities/isNumber.d.ts +0 -1
  186. package/dist/packages/core/src/utilities/isPlainObject.d.ts +0 -1
  187. package/dist/packages/core/src/utilities/isRegExp.d.ts +0 -1
  188. package/dist/packages/core/src/utilities/isString.d.ts +0 -1
  189. package/dist/packages/core/src/utilities/isiOS.d.ts +0 -1
  190. package/dist/packages/core/src/utilities/mergeAttributes.d.ts +0 -1
  191. package/dist/packages/core/src/utilities/mergeDeep.d.ts +0 -1
  192. package/dist/packages/core/src/utilities/minMax.d.ts +0 -1
  193. package/dist/packages/core/src/utilities/objectIncludes.d.ts +0 -8
  194. package/dist/packages/core/src/utilities/removeDuplicates.d.ts +0 -8
  195. package/dist/packages/extension-bubble-menu/src/bubble-menu-plugin.d.ts +0 -76
  196. package/dist/packages/extension-bubble-menu/src/bubble-menu.d.ts +0 -15
  197. package/dist/packages/extension-bubble-menu/src/index.d.ts +0 -4
  198. package/dist/packages/extension-floating-menu/src/floating-menu-plugin.d.ts +0 -66
  199. package/dist/packages/extension-floating-menu/src/floating-menu.d.ts +0 -15
  200. package/dist/packages/extension-floating-menu/src/index.d.ts +0 -4
  201. package/dist/packages/react/src/BubbleMenu.d.ts +0 -11
  202. package/dist/packages/react/src/Context.d.ts +0 -23
  203. package/dist/packages/react/src/Editor.d.ts +0 -12
  204. package/dist/packages/react/src/EditorContent.d.ts +0 -24
  205. package/dist/packages/react/src/FloatingMenu.d.ts +0 -10
  206. package/dist/packages/react/src/NodeViewContent.d.ts +0 -6
  207. package/dist/packages/react/src/NodeViewWrapper.d.ts +0 -6
  208. package/dist/packages/react/src/ReactNodeViewRenderer.d.ts +0 -16
  209. package/dist/packages/react/src/ReactRenderer.d.ts +0 -62
  210. package/dist/packages/react/src/index.d.ts +0 -13
  211. package/dist/packages/react/src/useEditor.d.ts +0 -39
  212. package/dist/packages/react/src/useEditorState.d.ts +0 -22
  213. package/dist/packages/react/src/useReactNodeView.d.ts +0 -6
  214. package/src/BubbleMenu.tsx +0 -57
  215. package/src/FloatingMenu.tsx +0 -64
@@ -1,123 +1,171 @@
1
- import { useDebugValue, useEffect, useState } from 'react'
2
- import { useSyncExternalStoreWithSelector } from 'use-sync-external-store/shim/with-selector'
1
+ import type { Editor } from '@tiptap/core'
2
+ import deepEqual from 'fast-deep-equal/es6/react.js'
3
+ import { useDebugValue, useEffect, useLayoutEffect, useState } from 'react'
4
+ import { useSyncExternalStoreWithSelector } from 'use-sync-external-store/shim/with-selector.js'
3
5
 
4
- import type { Editor } from './Editor.js'
6
+ const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect
5
7
 
6
8
  export type EditorStateSnapshot<TEditor extends Editor | null = Editor | null> = {
7
- editor: TEditor;
8
- transactionNumber: number;
9
- };
10
- export type UseEditorStateOptions<
11
- TSelectorResult,
12
- TEditor extends Editor | null = Editor | null,
13
- > = {
9
+ editor: TEditor
10
+ transactionNumber: number
11
+ }
12
+
13
+ export type UseEditorStateOptions<TSelectorResult, TEditor extends Editor | null = Editor | null> = {
14
14
  /**
15
15
  * The editor instance.
16
16
  */
17
- editor: TEditor;
17
+ editor: TEditor
18
18
  /**
19
19
  * A selector function to determine the value to compare for re-rendering.
20
20
  */
21
- selector: (context: EditorStateSnapshot<TEditor>) => TSelectorResult;
21
+ selector: (context: EditorStateSnapshot<TEditor>) => TSelectorResult
22
22
  /**
23
23
  * A custom equality function to determine if the editor should re-render.
24
- * @default `(a, b) => a === b`
24
+ * @default `deepEqual` from `fast-deep-equal`
25
25
  */
26
- equalityFn?: (a: TSelectorResult, b: TSelectorResult | null) => boolean;
27
- };
26
+ equalityFn?: (a: TSelectorResult, b: TSelectorResult | null) => boolean
27
+ }
28
28
 
29
29
  /**
30
30
  * To synchronize the editor instance with the component state,
31
31
  * we need to create a separate instance that is not affected by the component re-renders.
32
32
  */
33
- function makeEditorStateInstance<TEditor extends Editor | null = Editor | null>(initialEditor: TEditor) {
34
- let transactionNumber = 0
35
- let lastTransactionNumber = 0
36
- let lastSnapshot: EditorStateSnapshot<TEditor> = { editor: initialEditor, transactionNumber: 0 }
37
- let editor = initialEditor
38
- const subscribers = new Set<() => void>()
39
-
40
- const editorInstance = {
41
- /**
42
- * Get the current editor instance.
43
- */
44
- getSnapshot(): EditorStateSnapshot<TEditor> {
45
- if (transactionNumber === lastTransactionNumber) {
46
- return lastSnapshot
33
+ class EditorStateManager<TEditor extends Editor | null = Editor | null> {
34
+ private transactionNumber = 0
35
+
36
+ private lastTransactionNumber = 0
37
+
38
+ private lastSnapshot: EditorStateSnapshot<TEditor>
39
+
40
+ private editor: TEditor
41
+
42
+ private subscribers = new Set<() => void>()
43
+
44
+ constructor(initialEditor: TEditor) {
45
+ this.editor = initialEditor
46
+ this.lastSnapshot = { editor: initialEditor, transactionNumber: 0 }
47
+
48
+ this.getSnapshot = this.getSnapshot.bind(this)
49
+ this.getServerSnapshot = this.getServerSnapshot.bind(this)
50
+ this.watch = this.watch.bind(this)
51
+ this.subscribe = this.subscribe.bind(this)
52
+ }
53
+
54
+ /**
55
+ * Get the current editor instance.
56
+ */
57
+ getSnapshot(): EditorStateSnapshot<TEditor> {
58
+ if (this.transactionNumber === this.lastTransactionNumber) {
59
+ return this.lastSnapshot
60
+ }
61
+ this.lastTransactionNumber = this.transactionNumber
62
+ this.lastSnapshot = { editor: this.editor, transactionNumber: this.transactionNumber }
63
+ return this.lastSnapshot
64
+ }
65
+
66
+ /**
67
+ * Always disable the editor on the server-side.
68
+ */
69
+ getServerSnapshot(): EditorStateSnapshot<null> {
70
+ return { editor: null, transactionNumber: 0 }
71
+ }
72
+
73
+ /**
74
+ * Subscribe to the editor instance's changes.
75
+ */
76
+ subscribe(callback: () => void): () => void {
77
+ this.subscribers.add(callback)
78
+ return () => {
79
+ this.subscribers.delete(callback)
80
+ }
81
+ }
82
+
83
+ /**
84
+ * Watch the editor instance for changes.
85
+ */
86
+ watch(nextEditor: Editor | null): undefined | (() => void) {
87
+ this.editor = nextEditor as TEditor
88
+
89
+ if (this.editor) {
90
+ /**
91
+ * This will force a re-render when the editor state changes.
92
+ * This is to support things like `editor.can().toggleBold()` in components that `useEditor`.
93
+ * This could be more efficient, but it's a good trade-off for now.
94
+ */
95
+ const fn = () => {
96
+ this.transactionNumber += 1
97
+ this.subscribers.forEach(callback => callback())
47
98
  }
48
- lastTransactionNumber = transactionNumber
49
- lastSnapshot = { editor, transactionNumber }
50
- return lastSnapshot
51
- },
52
- /**
53
- * Always disable the editor on the server-side.
54
- */
55
- getServerSnapshot(): EditorStateSnapshot<null> {
56
- return { editor: null, transactionNumber: 0 }
57
- },
58
- /**
59
- * Subscribe to the editor instance's changes.
60
- */
61
- subscribe(callback: () => void) {
62
- subscribers.add(callback)
99
+
100
+ const currentEditor = this.editor
101
+
102
+ currentEditor.on('transaction', fn)
63
103
  return () => {
64
- subscribers.delete(callback)
104
+ currentEditor.off('transaction', fn)
65
105
  }
66
- },
67
- /**
68
- * Watch the editor instance for changes.
69
- */
70
- watch(nextEditor: Editor | null) {
71
- editor = nextEditor as TEditor
72
-
73
- if (editor) {
74
- /**
75
- * This will force a re-render when the editor state changes.
76
- * This is to support things like `editor.can().toggleBold()` in components that `useEditor`.
77
- * This could be more efficient, but it's a good trade-off for now.
78
- */
79
- const fn = () => {
80
- transactionNumber += 1
81
- subscribers.forEach(callback => callback())
82
- }
83
-
84
- const currentEditor = editor
85
-
86
- currentEditor.on('transaction', fn)
87
- return () => {
88
- currentEditor.off('transaction', fn)
89
- }
90
- }
91
- },
92
- }
106
+ }
93
107
 
94
- return editorInstance
108
+ return undefined
109
+ }
95
110
  }
96
111
 
112
+ /**
113
+ * This hook allows you to watch for changes on the editor instance.
114
+ * It will allow you to select a part of the editor state and re-render the component when it changes.
115
+ * @example
116
+ * ```tsx
117
+ * const editor = useEditor({...options})
118
+ * const { currentSelection } = useEditorState({
119
+ * editor,
120
+ * selector: snapshot => ({ currentSelection: snapshot.editor.state.selection }),
121
+ * })
122
+ */
97
123
  export function useEditorState<TSelectorResult>(
98
- options: UseEditorStateOptions<TSelectorResult, Editor>
99
- ): TSelectorResult;
124
+ options: UseEditorStateOptions<TSelectorResult, Editor>,
125
+ ): TSelectorResult
126
+ /**
127
+ * This hook allows you to watch for changes on the editor instance.
128
+ * It will allow you to select a part of the editor state and re-render the component when it changes.
129
+ * @example
130
+ * ```tsx
131
+ * const editor = useEditor({...options})
132
+ * const { currentSelection } = useEditorState({
133
+ * editor,
134
+ * selector: snapshot => ({ currentSelection: snapshot.editor.state.selection }),
135
+ * })
136
+ */
100
137
  export function useEditorState<TSelectorResult>(
101
- options: UseEditorStateOptions<TSelectorResult, Editor | null>
102
- ): TSelectorResult | null;
138
+ options: UseEditorStateOptions<TSelectorResult, Editor | null>,
139
+ ): TSelectorResult | null
103
140
 
141
+ /**
142
+ * This hook allows you to watch for changes on the editor instance.
143
+ * It will allow you to select a part of the editor state and re-render the component when it changes.
144
+ * @example
145
+ * ```tsx
146
+ * const editor = useEditor({...options})
147
+ * const { currentSelection } = useEditorState({
148
+ * editor,
149
+ * selector: snapshot => ({ currentSelection: snapshot.editor.state.selection }),
150
+ * })
151
+ */
104
152
  export function useEditorState<TSelectorResult>(
105
153
  options: UseEditorStateOptions<TSelectorResult, Editor> | UseEditorStateOptions<TSelectorResult, Editor | null>,
106
154
  ): TSelectorResult | null {
107
- const [editorInstance] = useState(() => makeEditorStateInstance(options.editor))
155
+ const [editorStateManager] = useState(() => new EditorStateManager(options.editor))
108
156
 
109
157
  // Using the `useSyncExternalStore` hook to sync the editor instance with the component state
110
158
  const selectedState = useSyncExternalStoreWithSelector(
111
- editorInstance.subscribe,
112
- editorInstance.getSnapshot,
113
- editorInstance.getServerSnapshot,
159
+ editorStateManager.subscribe,
160
+ editorStateManager.getSnapshot,
161
+ editorStateManager.getServerSnapshot,
114
162
  options.selector as UseEditorStateOptions<TSelectorResult, Editor | null>['selector'],
115
- options.equalityFn,
163
+ options.equalityFn ?? deepEqual,
116
164
  )
117
165
 
118
- useEffect(() => {
119
- return editorInstance.watch(options.editor)
120
- }, [options.editor])
166
+ useIsomorphicLayoutEffect(() => {
167
+ return editorStateManager.watch(options.editor)
168
+ }, [options.editor, editorStateManager])
121
169
 
122
170
  useDebugValue(selectedState)
123
171
 
@@ -1,12 +1,28 @@
1
- import { createContext, useContext } from 'react'
1
+ import type { ReactNode } from 'react'
2
+ import { createContext, createElement, useContext } from 'react'
2
3
 
3
4
  export interface ReactNodeViewContextProps {
4
- onDragStart: (event: DragEvent) => void,
5
- nodeViewContentRef: (element: HTMLElement | null) => void,
5
+ onDragStart?: (event: DragEvent) => void
6
+ nodeViewContentRef?: (element: HTMLElement | null) => void
7
+ /**
8
+ * This allows you to add children into the NodeViewContent component.
9
+ * This is useful when statically rendering the content of a node view.
10
+ */
11
+ nodeViewContentChildren?: ReactNode
6
12
  }
7
13
 
8
- export const ReactNodeViewContext = createContext<Partial<ReactNodeViewContextProps>>({
9
- onDragStart: undefined,
14
+ export const ReactNodeViewContext = createContext<ReactNodeViewContextProps>({
15
+ onDragStart: () => {
16
+ // no-op
17
+ },
18
+ nodeViewContentChildren: undefined,
19
+ nodeViewContentRef: () => {
20
+ // no-op
21
+ },
10
22
  })
11
23
 
24
+ export const ReactNodeViewContentProvider = ({ children, content }: { children: ReactNode; content: ReactNode }) => {
25
+ return createElement(ReactNodeViewContext.Provider, { value: { nodeViewContentChildren: content } }, children)
26
+ }
27
+
12
28
  export const useReactNodeView = () => useContext(ReactNodeViewContext)