Skip to content

Commit 6229dcf

Browse files
Made block type dropdown items apply type to all selected blocks (#369)
1 parent 0b89b1e commit 6229dcf

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

packages/react/src/FormattingToolbar/components/DefaultDropdowns/BlockTypeDropdown.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
import { ToolbarDropdown } from "../../../SharedComponents/Toolbar/components/ToolbarDropdown";
1919
import { ToolbarDropdownItemProps } from "../../../SharedComponents/Toolbar/components/ToolbarDropdownItem";
2020
import { useEditorChange } from "../../../hooks/useEditorChange";
21+
import { useSelectedBlocks } from "../../../hooks/useSelectedBlocks";
2122

2223
export type BlockTypeDropdownItem = {
2324
name: string;
@@ -82,6 +83,8 @@ export const BlockTypeDropdown = <BSchema extends BlockSchema>(props: {
8283
editor: BlockNoteEditor<BSchema>;
8384
items?: BlockTypeDropdownItem[];
8485
}) => {
86+
const selectedBlocks = useSelectedBlocks(props.editor);
87+
8588
const [block, setBlock] = useState(
8689
props.editor.getTextCursorPosition().block
8790
);
@@ -123,10 +126,13 @@ export const BlockTypeDropdown = <BSchema extends BlockSchema>(props: {
123126
const fullItems: ToolbarDropdownItemProps[] = useMemo(() => {
124127
const onClick = (item: BlockTypeDropdownItem) => {
125128
props.editor.focus();
126-
props.editor.updateBlock(block, {
127-
type: item.type,
128-
props: item.props,
129-
} as PartialBlock<BlockSchema>);
129+
130+
for (const block of selectedBlocks) {
131+
props.editor.updateBlock(block, {
132+
type: item.type,
133+
props: item.props,
134+
} as PartialBlock<BlockSchema>);
135+
}
130136
};
131137

132138
return filteredItems.map((item) => ({
@@ -135,7 +141,7 @@ export const BlockTypeDropdown = <BSchema extends BlockSchema>(props: {
135141
onClick: () => onClick(item),
136142
isSelected: item.isSelected(block as Block<BlockSchema>),
137143
}));
138-
}, [block, filteredItems, props.editor]);
144+
}, [block, filteredItems, props.editor, selectedBlocks]);
139145

140146
useEditorChange(props.editor, () => {
141147
setBlock(props.editor.getTextCursorPosition().block);

0 commit comments

Comments
 (0)