Skip to content

Commit 2047a31

Browse files
committed
refactor: show the power of the useEditorState hook
1 parent 673b9c4 commit 2047a31

File tree

1 file changed

+38
-35
lines changed

1 file changed

+38
-35
lines changed

packages/react/src/components/FormattingToolbar/DefaultButtons/NestBlockButtons.tsx

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ import {
44
InlineContentSchema,
55
StyleSchema,
66
} from "@blocknote/core";
7-
import { useCallback, useMemo, useState } from "react";
7+
import { useCallback } from "react";
88
import { RiIndentDecrease, RiIndentIncrease } from "react-icons/ri";
99

1010
import { useComponentsContext } from "../../../editor/ComponentsContext.js";
1111
import { useBlockNoteEditor } from "../../../hooks/useBlockNoteEditor.js";
12-
import { useEditorContentOrSelectionChange } from "../../../hooks/useEditorContentOrSelectionChange.js";
13-
import { useSelectedBlocks } from "../../../hooks/useSelectedBlocks.js";
12+
import { useEditorState } from "../../../hooks/useEditorState.js";
1413
import { useDictionary } from "../../../i18n/dictionary.js";
1514

1615
export const NestBlockButton = () => {
@@ -23,28 +22,30 @@ export const NestBlockButton = () => {
2322
StyleSchema
2423
>();
2524

26-
const selectedBlocks = useSelectedBlocks(editor);
27-
28-
const [canNestBlock, setCanNestBlock] = useState<boolean>(() =>
29-
editor.canNestBlock(),
30-
);
31-
32-
useEditorContentOrSelectionChange(() => {
33-
setCanNestBlock(editor.canNestBlock());
34-
}, editor);
25+
const { show, canNestBlock } = useEditorState({
26+
editor,
27+
selector: ({ editor }) => {
28+
if (!editor.isEditable) {
29+
return { show: false, canNestBlock: false };
30+
}
31+
const selectedBlocks = editor.getSelection()?.blocks || [
32+
editor.getTextCursorPosition().block,
33+
];
34+
return {
35+
show: !selectedBlocks.find(
36+
(block) => editor.schema.blockSchema[block.type].content !== "inline",
37+
),
38+
canNestBlock: editor.canNestBlock(),
39+
};
40+
},
41+
});
3542

3643
const nestBlock = useCallback(() => {
3744
editor.focus();
3845
editor.nestBlock();
3946
}, [editor]);
4047

41-
const show = useMemo(() => {
42-
return !selectedBlocks.find(
43-
(block) => editor.schema.blockSchema[block.type].content !== "inline",
44-
);
45-
}, [editor.schema.blockSchema, selectedBlocks]);
46-
47-
if (!show || !editor.isEditable) {
48+
if (!show) {
4849
return null;
4950
}
5051

@@ -71,28 +72,30 @@ export const UnnestBlockButton = () => {
7172

7273
const editor = useBlockNoteEditor<any, any, any>();
7374

74-
const selectedBlocks = useSelectedBlocks(editor);
75-
76-
const [canUnnestBlock, setCanUnnestBlock] = useState<boolean>(() =>
77-
editor.canUnnestBlock(),
78-
);
79-
80-
useEditorContentOrSelectionChange(() => {
81-
setCanUnnestBlock(editor.canUnnestBlock());
82-
}, editor);
75+
const { show, canUnnestBlock } = useEditorState({
76+
editor,
77+
selector: ({ editor }) => {
78+
if (!editor.isEditable) {
79+
return { show: false, canUnnestBlock: false };
80+
}
81+
const selectedBlocks = editor.getSelection()?.blocks || [
82+
editor.getTextCursorPosition().block,
83+
];
84+
return {
85+
show: !selectedBlocks.find(
86+
(block) => editor.schema.blockSchema[block.type].content !== "inline",
87+
),
88+
canUnnestBlock: editor.canUnnestBlock(),
89+
};
90+
},
91+
});
8392

8493
const unnestBlock = useCallback(() => {
8594
editor.focus();
8695
editor.unnestBlock();
8796
}, [editor]);
8897

89-
const show = useMemo(() => {
90-
return !selectedBlocks.find(
91-
(block) => editor.schema.blockSchema[block.type].content !== "inline",
92-
);
93-
}, [editor.schema.blockSchema, selectedBlocks]);
94-
95-
if (!show || !editor.isEditable) {
98+
if (!show) {
9699
return null;
97100
}
98101

0 commit comments

Comments
 (0)