@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.
- package/LICENSE.md +21 -0
- package/README.md +5 -1
- package/dist/index.cjs +1030 -1163
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +350 -0
- package/dist/index.d.ts +350 -0
- package/dist/index.js +970 -1138
- package/dist/index.js.map +1 -1
- package/dist/menus/index.cjs +142 -0
- package/dist/menus/index.cjs.map +1 -0
- package/dist/menus/index.d.cts +19 -0
- package/dist/menus/index.d.ts +19 -0
- package/dist/menus/index.js +104 -0
- package/dist/menus/index.js.map +1 -0
- package/package.json +34 -21
- package/src/Context.tsx +18 -12
- package/src/Editor.ts +10 -11
- package/src/EditorContent.tsx +104 -64
- package/src/NodeViewContent.tsx +13 -8
- package/src/NodeViewWrapper.tsx +3 -2
- package/src/ReactMarkViewRenderer.tsx +111 -0
- package/src/ReactNodeViewRenderer.tsx +184 -67
- package/src/ReactRenderer.tsx +152 -51
- package/src/index.ts +2 -3
- package/src/menus/BubbleMenu.tsx +68 -0
- package/src/menus/FloatingMenu.tsx +68 -0
- package/src/menus/index.ts +2 -0
- package/src/types.ts +6 -0
- package/src/useEditor.ts +286 -166
- package/src/useEditorState.ts +133 -85
- package/src/useReactNodeView.ts +21 -5
- package/dist/index.umd.js +0 -1219
- package/dist/index.umd.js.map +0 -1
- package/dist/packages/core/src/CommandManager.d.ts +0 -20
- package/dist/packages/core/src/Editor.d.ts +0 -161
- package/dist/packages/core/src/EventEmitter.d.ts +0 -11
- package/dist/packages/core/src/Extension.d.ts +0 -343
- package/dist/packages/core/src/ExtensionManager.d.ts +0 -55
- package/dist/packages/core/src/InputRule.d.ts +0 -42
- package/dist/packages/core/src/Mark.d.ts +0 -451
- package/dist/packages/core/src/Node.d.ts +0 -611
- package/dist/packages/core/src/NodePos.d.ts +0 -44
- package/dist/packages/core/src/NodeView.d.ts +0 -31
- package/dist/packages/core/src/PasteRule.d.ts +0 -50
- package/dist/packages/core/src/Tracker.d.ts +0 -11
- package/dist/packages/core/src/commands/blur.d.ts +0 -13
- package/dist/packages/core/src/commands/clearContent.d.ts +0 -14
- package/dist/packages/core/src/commands/clearNodes.d.ts +0 -13
- package/dist/packages/core/src/commands/command.d.ts +0 -18
- package/dist/packages/core/src/commands/createParagraphNear.d.ts +0 -13
- package/dist/packages/core/src/commands/cut.d.ts +0 -20
- package/dist/packages/core/src/commands/deleteCurrentNode.d.ts +0 -13
- package/dist/packages/core/src/commands/deleteNode.d.ts +0 -15
- package/dist/packages/core/src/commands/deleteRange.d.ts +0 -14
- package/dist/packages/core/src/commands/deleteSelection.d.ts +0 -13
- package/dist/packages/core/src/commands/enter.d.ts +0 -13
- package/dist/packages/core/src/commands/exitCode.d.ts +0 -13
- package/dist/packages/core/src/commands/extendMarkRange.d.ts +0 -25
- package/dist/packages/core/src/commands/first.d.ts +0 -14
- package/dist/packages/core/src/commands/focus.d.ts +0 -27
- package/dist/packages/core/src/commands/forEach.d.ts +0 -14
- package/dist/packages/core/src/commands/index.d.ts +0 -55
- package/dist/packages/core/src/commands/insertContent.d.ts +0 -34
- package/dist/packages/core/src/commands/insertContentAt.d.ts +0 -47
- package/dist/packages/core/src/commands/join.d.ts +0 -41
- package/dist/packages/core/src/commands/joinItemBackward.d.ts +0 -13
- package/dist/packages/core/src/commands/joinItemForward.d.ts +0 -13
- package/dist/packages/core/src/commands/joinTextblockBackward.d.ts +0 -12
- package/dist/packages/core/src/commands/joinTextblockForward.d.ts +0 -12
- package/dist/packages/core/src/commands/keyboardShortcut.d.ts +0 -14
- package/dist/packages/core/src/commands/lift.d.ts +0 -17
- package/dist/packages/core/src/commands/liftEmptyBlock.d.ts +0 -13
- package/dist/packages/core/src/commands/liftListItem.d.ts +0 -15
- package/dist/packages/core/src/commands/newlineInCode.d.ts +0 -13
- package/dist/packages/core/src/commands/resetAttributes.d.ts +0 -16
- package/dist/packages/core/src/commands/scrollIntoView.d.ts +0 -13
- package/dist/packages/core/src/commands/selectAll.d.ts +0 -13
- package/dist/packages/core/src/commands/selectNodeBackward.d.ts +0 -13
- package/dist/packages/core/src/commands/selectNodeForward.d.ts +0 -13
- package/dist/packages/core/src/commands/selectParentNode.d.ts +0 -13
- package/dist/packages/core/src/commands/selectTextblockEnd.d.ts +0 -13
- package/dist/packages/core/src/commands/selectTextblockStart.d.ts +0 -13
- package/dist/packages/core/src/commands/setContent.d.ts +0 -40
- package/dist/packages/core/src/commands/setMark.d.ts +0 -15
- package/dist/packages/core/src/commands/setMeta.d.ts +0 -15
- package/dist/packages/core/src/commands/setNode.d.ts +0 -16
- package/dist/packages/core/src/commands/setNodeSelection.d.ts +0 -14
- package/dist/packages/core/src/commands/setTextSelection.d.ts +0 -14
- package/dist/packages/core/src/commands/sinkListItem.d.ts +0 -15
- package/dist/packages/core/src/commands/splitBlock.d.ts +0 -17
- package/dist/packages/core/src/commands/splitListItem.d.ts +0 -15
- package/dist/packages/core/src/commands/toggleList.d.ts +0 -18
- package/dist/packages/core/src/commands/toggleMark.d.ts +0 -30
- package/dist/packages/core/src/commands/toggleNode.d.ts +0 -17
- package/dist/packages/core/src/commands/toggleWrap.d.ts +0 -16
- package/dist/packages/core/src/commands/undoInputRule.d.ts +0 -13
- package/dist/packages/core/src/commands/unsetAllMarks.d.ts +0 -13
- package/dist/packages/core/src/commands/unsetMark.d.ts +0 -25
- package/dist/packages/core/src/commands/updateAttributes.d.ts +0 -24
- package/dist/packages/core/src/commands/wrapIn.d.ts +0 -16
- package/dist/packages/core/src/commands/wrapInList.d.ts +0 -16
- package/dist/packages/core/src/extensions/clipboardTextSerializer.d.ts +0 -5
- package/dist/packages/core/src/extensions/commands.d.ts +0 -3
- package/dist/packages/core/src/extensions/editable.d.ts +0 -2
- package/dist/packages/core/src/extensions/focusEvents.d.ts +0 -2
- package/dist/packages/core/src/extensions/index.d.ts +0 -6
- package/dist/packages/core/src/extensions/keymap.d.ts +0 -2
- package/dist/packages/core/src/extensions/tabindex.d.ts +0 -2
- package/dist/packages/core/src/helpers/combineTransactionSteps.d.ts +0 -10
- package/dist/packages/core/src/helpers/createChainableState.d.ts +0 -10
- package/dist/packages/core/src/helpers/createDocument.d.ts +0 -12
- package/dist/packages/core/src/helpers/createNodeFromContent.d.ts +0 -15
- package/dist/packages/core/src/helpers/defaultBlockAt.d.ts +0 -7
- package/dist/packages/core/src/helpers/findChildren.d.ts +0 -9
- package/dist/packages/core/src/helpers/findChildrenInRange.d.ts +0 -10
- package/dist/packages/core/src/helpers/findParentNode.d.ts +0 -16
- package/dist/packages/core/src/helpers/findParentNodeClosestToPos.d.ts +0 -17
- package/dist/packages/core/src/helpers/generateHTML.d.ts +0 -8
- package/dist/packages/core/src/helpers/generateJSON.d.ts +0 -8
- package/dist/packages/core/src/helpers/generateText.d.ts +0 -12
- package/dist/packages/core/src/helpers/getAttributes.d.ts +0 -9
- package/dist/packages/core/src/helpers/getAttributesFromExtensions.d.ts +0 -6
- package/dist/packages/core/src/helpers/getChangedRanges.d.ts +0 -11
- package/dist/packages/core/src/helpers/getDebugJSON.d.ts +0 -8
- package/dist/packages/core/src/helpers/getExtensionField.d.ts +0 -9
- package/dist/packages/core/src/helpers/getHTMLFromFragment.d.ts +0 -2
- package/dist/packages/core/src/helpers/getMarkAttributes.d.ts +0 -3
- package/dist/packages/core/src/helpers/getMarkRange.d.ts +0 -3
- package/dist/packages/core/src/helpers/getMarkType.d.ts +0 -2
- package/dist/packages/core/src/helpers/getMarksBetween.d.ts +0 -3
- package/dist/packages/core/src/helpers/getNodeAtPosition.d.ts +0 -11
- package/dist/packages/core/src/helpers/getNodeAttributes.d.ts +0 -3
- package/dist/packages/core/src/helpers/getNodeType.d.ts +0 -2
- package/dist/packages/core/src/helpers/getRenderedAttributes.d.ts +0 -3
- package/dist/packages/core/src/helpers/getSchema.d.ts +0 -4
- package/dist/packages/core/src/helpers/getSchemaByResolvedExtensions.d.ts +0 -10
- package/dist/packages/core/src/helpers/getSchemaTypeByName.d.ts +0 -8
- package/dist/packages/core/src/helpers/getSchemaTypeNameByName.d.ts +0 -8
- package/dist/packages/core/src/helpers/getSplittedAttributes.d.ts +0 -9
- package/dist/packages/core/src/helpers/getText.d.ts +0 -15
- package/dist/packages/core/src/helpers/getTextBetween.d.ts +0 -14
- package/dist/packages/core/src/helpers/getTextContentFromNodes.d.ts +0 -8
- package/dist/packages/core/src/helpers/getTextSerializersFromSchema.d.ts +0 -8
- package/dist/packages/core/src/helpers/index.d.ts +0 -50
- package/dist/packages/core/src/helpers/injectExtensionAttributesToParseRule.d.ts +0 -9
- package/dist/packages/core/src/helpers/isActive.d.ts +0 -2
- package/dist/packages/core/src/helpers/isAtEndOfNode.d.ts +0 -2
- package/dist/packages/core/src/helpers/isAtStartOfNode.d.ts +0 -2
- package/dist/packages/core/src/helpers/isExtensionRulesEnabled.d.ts +0 -2
- package/dist/packages/core/src/helpers/isList.d.ts +0 -2
- package/dist/packages/core/src/helpers/isMarkActive.d.ts +0 -3
- package/dist/packages/core/src/helpers/isNodeActive.d.ts +0 -3
- package/dist/packages/core/src/helpers/isNodeEmpty.d.ts +0 -2
- package/dist/packages/core/src/helpers/isNodeSelection.d.ts +0 -2
- package/dist/packages/core/src/helpers/isTextSelection.d.ts +0 -2
- package/dist/packages/core/src/helpers/posToDOMRect.d.ts +0 -2
- package/dist/packages/core/src/helpers/resolveFocusPosition.d.ts +0 -4
- package/dist/packages/core/src/helpers/selectionToInsertionEnd.d.ts +0 -2
- package/dist/packages/core/src/helpers/splitExtensions.d.ts +0 -9
- package/dist/packages/core/src/index.d.ts +0 -24
- package/dist/packages/core/src/inputRules/index.d.ts +0 -5
- package/dist/packages/core/src/inputRules/markInputRule.d.ts +0 -13
- package/dist/packages/core/src/inputRules/nodeInputRule.d.ts +0 -23
- package/dist/packages/core/src/inputRules/textInputRule.d.ts +0 -10
- package/dist/packages/core/src/inputRules/textblockTypeInputRule.d.ts +0 -15
- package/dist/packages/core/src/inputRules/wrappingInputRule.d.ts +0 -28
- package/dist/packages/core/src/pasteRules/index.d.ts +0 -3
- package/dist/packages/core/src/pasteRules/markPasteRule.d.ts +0 -13
- package/dist/packages/core/src/pasteRules/nodePasteRule.d.ts +0 -13
- package/dist/packages/core/src/pasteRules/textPasteRule.d.ts +0 -10
- package/dist/packages/core/src/style.d.ts +0 -1
- package/dist/packages/core/src/types.d.ts +0 -255
- package/dist/packages/core/src/utilities/callOrReturn.d.ts +0 -9
- package/dist/packages/core/src/utilities/createStyleTag.d.ts +0 -1
- package/dist/packages/core/src/utilities/deleteProps.d.ts +0 -6
- package/dist/packages/core/src/utilities/elementFromString.d.ts +0 -1
- package/dist/packages/core/src/utilities/escapeForRegEx.d.ts +0 -1
- package/dist/packages/core/src/utilities/findDuplicates.d.ts +0 -1
- package/dist/packages/core/src/utilities/fromString.d.ts +0 -1
- package/dist/packages/core/src/utilities/index.d.ts +0 -20
- package/dist/packages/core/src/utilities/isAndroid.d.ts +0 -1
- package/dist/packages/core/src/utilities/isEmptyObject.d.ts +0 -1
- package/dist/packages/core/src/utilities/isFunction.d.ts +0 -1
- package/dist/packages/core/src/utilities/isMacOS.d.ts +0 -1
- package/dist/packages/core/src/utilities/isNumber.d.ts +0 -1
- package/dist/packages/core/src/utilities/isPlainObject.d.ts +0 -1
- package/dist/packages/core/src/utilities/isRegExp.d.ts +0 -1
- package/dist/packages/core/src/utilities/isString.d.ts +0 -1
- package/dist/packages/core/src/utilities/isiOS.d.ts +0 -1
- package/dist/packages/core/src/utilities/mergeAttributes.d.ts +0 -1
- package/dist/packages/core/src/utilities/mergeDeep.d.ts +0 -1
- package/dist/packages/core/src/utilities/minMax.d.ts +0 -1
- package/dist/packages/core/src/utilities/objectIncludes.d.ts +0 -8
- package/dist/packages/core/src/utilities/removeDuplicates.d.ts +0 -8
- package/dist/packages/extension-bubble-menu/src/bubble-menu-plugin.d.ts +0 -76
- package/dist/packages/extension-bubble-menu/src/bubble-menu.d.ts +0 -15
- package/dist/packages/extension-bubble-menu/src/index.d.ts +0 -4
- package/dist/packages/extension-floating-menu/src/floating-menu-plugin.d.ts +0 -66
- package/dist/packages/extension-floating-menu/src/floating-menu.d.ts +0 -15
- package/dist/packages/extension-floating-menu/src/index.d.ts +0 -4
- package/dist/packages/react/src/BubbleMenu.d.ts +0 -11
- package/dist/packages/react/src/Context.d.ts +0 -23
- package/dist/packages/react/src/Editor.d.ts +0 -12
- package/dist/packages/react/src/EditorContent.d.ts +0 -24
- package/dist/packages/react/src/FloatingMenu.d.ts +0 -10
- package/dist/packages/react/src/NodeViewContent.d.ts +0 -6
- package/dist/packages/react/src/NodeViewWrapper.d.ts +0 -6
- package/dist/packages/react/src/ReactNodeViewRenderer.d.ts +0 -16
- package/dist/packages/react/src/ReactRenderer.d.ts +0 -62
- package/dist/packages/react/src/index.d.ts +0 -13
- package/dist/packages/react/src/useEditor.d.ts +0 -39
- package/dist/packages/react/src/useEditorState.d.ts +0 -22
- package/dist/packages/react/src/useReactNodeView.d.ts +0 -6
- package/src/BubbleMenu.tsx +0 -57
- package/src/FloatingMenu.tsx +0 -64
package/src/useEditorState.ts
CHANGED
|
@@ -1,123 +1,171 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
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
|
-
|
|
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
|
-
|
|
11
|
-
|
|
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 `
|
|
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
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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
|
-
|
|
49
|
-
|
|
50
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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 [
|
|
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
|
-
|
|
112
|
-
|
|
113
|
-
|
|
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
|
-
|
|
119
|
-
return
|
|
120
|
-
}, [options.editor])
|
|
166
|
+
useIsomorphicLayoutEffect(() => {
|
|
167
|
+
return editorStateManager.watch(options.editor)
|
|
168
|
+
}, [options.editor, editorStateManager])
|
|
121
169
|
|
|
122
170
|
useDebugValue(selectedState)
|
|
123
171
|
|
package/src/useReactNodeView.ts
CHANGED
|
@@ -1,12 +1,28 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { ReactNode } from 'react'
|
|
2
|
+
import { createContext, createElement, useContext } from 'react'
|
|
2
3
|
|
|
3
4
|
export interface ReactNodeViewContextProps {
|
|
4
|
-
onDragStart
|
|
5
|
-
nodeViewContentRef
|
|
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<
|
|
9
|
-
onDragStart:
|
|
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)
|