Skip to content

Commit

Permalink
chore: minor docs & renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
nperez0111 committed Aug 20, 2024
1 parent 4ff2a4e commit 04bf24a
Showing 1 changed file with 40 additions and 6 deletions.
46 changes: 40 additions & 6 deletions packages/react/src/useEditorState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type EditorStateSnapshot<TEditor extends Editor | null = Editor | null> =
editor: TEditor;
transactionNumber: number;
};

export type UseEditorStateOptions<
TSelectorResult,
TEditor extends Editor | null = Editor | null,
Expand Down Expand Up @@ -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>(
options: UseEditorStateOptions<TSelectorResult, Editor>
): 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>(
options: UseEditorStateOptions<TSelectorResult, Editor | 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>(
options: UseEditorStateOptions<TSelectorResult, Editor> | UseEditorStateOptions<TSelectorResult, Editor | 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
const selectedState = useSyncExternalStoreWithSelector(
editorInstance.subscribe,
editorInstance.getSnapshot,
editorInstance.getServerSnapshot,
editorStateManager.subscribe,
editorStateManager.getSnapshot,
editorStateManager.getServerSnapshot,
options.selector as UseEditorStateOptions<TSelectorResult, Editor | null>['selector'],
options.equalityFn ?? deepEqual,
)

useEffect(() => {
return editorInstance.watch(options.editor)
}, [options.editor, editorInstance])
return editorStateManager.watch(options.editor)
}, [options.editor, editorStateManager])

useDebugValue(selectedState)

Expand Down

0 comments on commit 04bf24a

Please sign in to comment.