From 04bf24aed05884646f76a220cdd90bb975c88b7b Mon Sep 17 00:00:00 2001 From: Nick the Sick Date: Tue, 20 Aug 2024 10:45:36 +0200 Subject: [PATCH] chore: minor docs & renaming --- packages/react/src/useEditorState.ts | 46 ++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/packages/react/src/useEditorState.ts b/packages/react/src/useEditorState.ts index 3c5a70f261..26aabf0382 100644 --- a/packages/react/src/useEditorState.ts +++ b/packages/react/src/useEditorState.ts @@ -7,6 +7,7 @@ export type EditorStateSnapshot = editor: TEditor; transactionNumber: number; }; + export type UseEditorStateOptions< TSelectorResult, TEditor extends Editor | null = Editor | null, @@ -109,30 +110,63 @@ class EditorStateManager { } } +/** + * This hook allows you to watch for changes on the editor instance. + * It will allow you to select a part of the editor state and re-render the component when it changes. + * @example + * ```tsx + * const editor = useEditor({...options}) + * const { currentSelection } = useEditorState({ + * editor, + * selector: snapshot => ({ currentSelection: snapshot.editor.state.selection }), + * }) + */ export function useEditorState( options: UseEditorStateOptions ): TSelectorResult; +/** + * This hook allows you to watch for changes on the editor instance. + * It will allow you to select a part of the editor state and re-render the component when it changes. + * @example + * ```tsx + * const editor = useEditor({...options}) + * const { currentSelection } = useEditorState({ + * editor, + * selector: snapshot => ({ currentSelection: snapshot.editor.state.selection }), + * }) + */ export function useEditorState( options: UseEditorStateOptions ): TSelectorResult | null; +/** + * This hook allows you to watch for changes on the editor instance. + * It will allow you to select a part of the editor state and re-render the component when it changes. + * @example + * ```tsx + * const editor = useEditor({...options}) + * const { currentSelection } = useEditorState({ + * editor, + * selector: snapshot => ({ currentSelection: snapshot.editor.state.selection }), + * }) + */ export function useEditorState( options: UseEditorStateOptions | UseEditorStateOptions, ): TSelectorResult | null { - const [editorInstance] = useState(() => new EditorStateManager(options.editor)) + const [editorStateManager] = useState(() => new EditorStateManager(options.editor)) // Using the `useSyncExternalStore` hook to sync the editor instance with the component state const selectedState = useSyncExternalStoreWithSelector( - editorInstance.subscribe, - editorInstance.getSnapshot, - editorInstance.getServerSnapshot, + editorStateManager.subscribe, + editorStateManager.getSnapshot, + editorStateManager.getServerSnapshot, options.selector as UseEditorStateOptions['selector'], options.equalityFn ?? deepEqual, ) useEffect(() => { - return editorInstance.watch(options.editor) - }, [options.editor, editorInstance]) + return editorStateManager.watch(options.editor) + }, [options.editor, editorStateManager]) useDebugValue(selectedState)