Skip to content

Commit e584a36

Browse files
committed
feat: support on event types
1 parent 2047a31 commit e584a36

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

packages/react/src/hooks/useEditorState.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,23 @@ export type UseEditorStateOptions<
3030
* The editor instance. If not provided, will use the editor from BlockNoteContext.
3131
*/
3232
editor?: TEditor;
33+
3334
/**
3435
* A selector function to determine the value to compare for re-rendering.
3536
*/
3637
selector: (context: EditorStateSnapshot<TEditor>) => TSelectorResult;
38+
3739
/**
3840
* A custom equality function to determine if the editor should re-render.
3941
* @default `deepEqual` from `fast-deep-equal`
4042
*/
4143
equalityFn?: (a: TSelectorResult, b: TSelectorResult | null) => boolean;
44+
45+
/**
46+
* The event to subscribe to.
47+
* @default "all"
48+
*/
49+
on?: "all" | "selection" | "change";
4250
};
4351

4452
/**
@@ -109,6 +117,7 @@ class EditorStateManager<
109117
*/
110118
watch(
111119
nextEditor: BlockNoteEditor<any, any, any> | null,
120+
on: "all" | "selection" | "change",
112121
): undefined | (() => void) {
113122
this.editor = nextEditor as TEditor;
114123

@@ -125,9 +134,15 @@ class EditorStateManager<
125134

126135
const currentTiptapEditor = this.editor._tiptapEditor;
127136

128-
currentTiptapEditor.on("transaction", fn);
137+
const EVENT_TYPES = {
138+
all: "transaction",
139+
selection: "selectionUpdate",
140+
change: "update",
141+
} as const;
142+
143+
currentTiptapEditor.on(EVENT_TYPES[on], fn);
129144
return () => {
130-
currentTiptapEditor.off("transaction", fn);
145+
currentTiptapEditor.off(EVENT_TYPES[on], fn);
131146
};
132147
}
133148

@@ -188,6 +203,7 @@ export function useEditorState<TSelectorResult>(
188203
): TSelectorResult | null {
189204
const editorContext = useBlockNoteContext();
190205
const editor = options.editor || editorContext?.editor || null;
206+
const on = options.on || "all";
191207

192208
const [editorStateManager] = useState(() => new EditorStateManager(editor));
193209

@@ -204,8 +220,8 @@ export function useEditorState<TSelectorResult>(
204220
);
205221

206222
useIsomorphicLayoutEffect(() => {
207-
return editorStateManager.watch(editor);
208-
}, [editor, editorStateManager]);
223+
return editorStateManager.watch(editor, on);
224+
}, [editor, editorStateManager, on]);
209225

210226
useDebugValue(selectedState);
211227

0 commit comments

Comments
 (0)