chore: minor docs & renaming

This commit is contained in:
Nick the Sick 2024-08-20 10:45:36 +02:00
parent 4ff2a4eaa1
commit 04bf24aed0
No known key found for this signature in database
GPG Key ID: F575992F156E5BCC

View File

@ -7,6 +7,7 @@ export type EditorStateSnapshot<TEditor extends Editor | null = Editor | null> =
editor: TEditor; editor: TEditor;
transactionNumber: number; transactionNumber: number;
}; };
export type UseEditorStateOptions< export type UseEditorStateOptions<
TSelectorResult, TSelectorResult,
TEditor extends Editor | null = Editor | null, TEditor extends Editor | null = Editor | null,
@ -109,30 +110,63 @@ class EditorStateManager<TEditor extends Editor | null = Editor | 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<TSelectorResult>( export function useEditorState<TSelectorResult>(
options: UseEditorStateOptions<TSelectorResult, Editor> options: UseEditorStateOptions<TSelectorResult, Editor>
): TSelectorResult; ): 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<TSelectorResult>( export function useEditorState<TSelectorResult>(
options: UseEditorStateOptions<TSelectorResult, Editor | null> options: UseEditorStateOptions<TSelectorResult, Editor | null>
): TSelectorResult | null; ): 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<TSelectorResult>( export function useEditorState<TSelectorResult>(
options: UseEditorStateOptions<TSelectorResult, Editor> | UseEditorStateOptions<TSelectorResult, Editor | null>, options: UseEditorStateOptions<TSelectorResult, Editor> | UseEditorStateOptions<TSelectorResult, Editor | null>,
): TSelectorResult | null { ): 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 // Using the `useSyncExternalStore` hook to sync the editor instance with the component state
const selectedState = useSyncExternalStoreWithSelector( const selectedState = useSyncExternalStoreWithSelector(
editorInstance.subscribe, editorStateManager.subscribe,
editorInstance.getSnapshot, editorStateManager.getSnapshot,
editorInstance.getServerSnapshot, editorStateManager.getServerSnapshot,
options.selector as UseEditorStateOptions<TSelectorResult, Editor | null>['selector'], options.selector as UseEditorStateOptions<TSelectorResult, Editor | null>['selector'],
options.equalityFn ?? deepEqual, options.equalityFn ?? deepEqual,
) )
useEffect(() => { useEffect(() => {
return editorInstance.watch(options.editor) return editorStateManager.watch(options.editor)
}, [options.editor, editorInstance]) }, [options.editor, editorStateManager])
useDebugValue(selectedState) useDebugValue(selectedState)