Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setState and useEditorStateWithDispatch #139

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -420,10 +420,13 @@ function MyProseMirrorField() {
### `useEditorState`

```tsx
type useEditorState = () => EditorState;
type useEditorState = () => [
EditorState,
React.Dispatch<ReactSetStateAction<EditorState>>
];
```

Provides access to the current EditorState value.
Provides access to the current EditorState value and a method to update it.

### `useEditorEventCallback`

Expand Down
2 changes: 1 addition & 1 deletion demo/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export function Button(props: {
}

export default function Menu() {
const state = useEditorState();
const [state] = useEditorState();

const toggleBold = useEditorEventCallback((view) => {
const toggleBoldMark = toggleMark(view.state.schema.marks["strong"]);
Expand Down
3 changes: 2 additions & 1 deletion src/contexts/EditorContext.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import type { EditorState } from "prosemirror-state";
import type { DOMEventMap, EditorView } from "prosemirror-view";
import { createContext } from "react";
import React, { createContext } from "react";

import type { EventHandler } from "../plugins/componentEventListeners";

export interface EditorContextValue {
editorView: EditorView | null;
editorState: EditorState;
setEditorState: React.Dispatch<React.SetStateAction<EditorState>>;
registerEventListener<EventType extends keyof DOMEventMap>(
eventType: EventType,
handler: EventHandler<EventType>
Expand Down
17 changes: 10 additions & 7 deletions src/hooks/useEditorState.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { EditorContext } from "../contexts/EditorContext.js";
import type { EditorContextValue } from "../contexts/EditorContext.js";

import type { EditorState } from "prosemirror-state";
import { useContext } from "react";

import { EditorContext } from "../contexts/EditorContext.js";

/**
* Provides access to the current EditorState value.
* Provides access to the current EditorState value and method to update it.
*/
export function useEditorState(): EditorState {
const { editorState } = useContext(EditorContext);

return editorState;
export function useEditorState(): [
EditorState,
EditorContextValue["setEditorState"],
] {
const { editorState, setEditorState } = useContext(EditorContext);
return [editorState, setEditorState];
}
7 changes: 5 additions & 2 deletions src/hooks/useEditorView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ let didWarnValueDefaultValue = false;
export interface UseEditorViewOptions extends EditorProps {
defaultState?: EditorState;
state?: EditorState;
setState?: EditorContextValue["setEditorState"];
plugins?: Plugin[];
dispatchTransaction?(this: EditorView, tr: Transaction): void;
}
Expand Down Expand Up @@ -61,8 +62,9 @@ export function useEditorView<T extends HTMLElement = HTMLElement>(
}

const defaultState = options.defaultState ?? EMPTY_STATE;
const [_state, setState] = useState<EditorState>(defaultState);
const [_state, _setState] = useState<EditorState>(defaultState);
const state = options.state ?? _state;
const setState = options.setState ?? _setState;

const {
componentEventListenersPlugin,
Expand Down Expand Up @@ -121,10 +123,11 @@ export function useEditorView<T extends HTMLElement = HTMLElement>(
return useMemo(
() => ({
editorState: state,
setEditorState: setState,
editorView: view,
registerEventListener,
unregisterEventListener,
}),
[state, view, registerEventListener, unregisterEventListener]
[state, setState, view, registerEventListener, unregisterEventListener]
);
}
2 changes: 1 addition & 1 deletion src/hooks/useNodePos.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Props = {
const NodePosContext = createContext<number>(null as unknown as number);

export function NodePosProvider({ nodeKey, children }: Props) {
const editorState = useEditorState();
const [editorState] = useEditorState();
const pluginState = reactPluginKey.getState(editorState);
if (!pluginState) return <>{children}</>;
return (
Expand Down