Skip to content

Commit

Permalink
use idiomatic style
Browse files Browse the repository at this point in the history
  • Loading branch information
coolov committed Jan 25, 2024
1 parent 3380d28 commit 211fa52
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
35 changes: 22 additions & 13 deletions demo/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import type { MarkType } from "prosemirror-model";
import type { EditorState, Transaction } from "prosemirror-state";

Check warning on line 3 in demo/Menu.tsx

View workflow job for this annotation

GitHub Actions / check

'Transaction' is defined but never used
import React, { ReactNode } from "react";

import { useEditorEventCallback, useEditorState } from "../src/index.js";

function isMarkActive(mark: MarkType, state: EditorState): boolean {
if (!state.selection) return false;
const { from, $from, to, empty } = state.selection;
Expand Down Expand Up @@ -31,28 +33,35 @@ export function Button(props: {
);
}

export default function Menu(props: {
state: EditorState;
dispatchTransaction: (transaction: Transaction) => void;
}) {
const { marks } = props.state.schema;
export default function Menu() {
const state = useEditorState();
const { marks } = state.schema;

const toggleBold = useEditorEventCallback((view) => {
if (!view) return;
const toggleBoldMark = toggleMark(marks["bold"]);
toggleBoldMark(view.state, view.dispatch, view);
});

const toggleItalic = useEditorEventCallback((view) => {
if (!view) return;
const toggleBoldMark = toggleMark(marks["em"]);
toggleBoldMark(view.state, view.dispatch, view);
});

return (
<div className="menu">
<Button
className="bold"
isActive={isMarkActive(marks["strong"], props.state)}
onClick={() =>
toggleMark(marks["strong"])(props.state, props.dispatchTransaction)
}
isActive={isMarkActive(marks["strong"], state)}
onClick={toggleBold}
>
B
</Button>
<Button
className="italic"
isActive={isMarkActive(marks["em"], props.state)}
onClick={() =>
toggleMark(marks["em"])(props.state, props.dispatchTransaction)
}
isActive={isMarkActive(marks["em"], state)}
onClick={toggleItalic}
>
I
</Button>
Expand Down
2 changes: 1 addition & 1 deletion demo/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,13 @@ function DemoEditor() {

return (
<main>
<Menu state={state} dispatchTransaction={dispatchTransaction} />
<ProseMirror
mount={mount}
state={state}
nodeViews={nodeViews}
dispatchTransaction={dispatchTransaction}
>
<Menu />
<div ref={setMount} />
{renderNodeViews()}
</ProseMirror>
Expand Down

0 comments on commit 211fa52

Please sign in to comment.