From 6229dcfd8753a3fccbd35110736f16ef123c8fd3 Mon Sep 17 00:00:00 2001 From: Matthew Lipski <50169049+matthewlipski@users.noreply.github.com> Date: Mon, 9 Oct 2023 15:01:29 +0200 Subject: [PATCH] Made block type dropdown items apply type to all selected blocks (#369) --- .../DefaultDropdowns/BlockTypeDropdown.tsx | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/packages/react/src/FormattingToolbar/components/DefaultDropdowns/BlockTypeDropdown.tsx b/packages/react/src/FormattingToolbar/components/DefaultDropdowns/BlockTypeDropdown.tsx index 68dcee714..34868362b 100644 --- a/packages/react/src/FormattingToolbar/components/DefaultDropdowns/BlockTypeDropdown.tsx +++ b/packages/react/src/FormattingToolbar/components/DefaultDropdowns/BlockTypeDropdown.tsx @@ -18,6 +18,7 @@ import { import { ToolbarDropdown } from "../../../SharedComponents/Toolbar/components/ToolbarDropdown"; import { ToolbarDropdownItemProps } from "../../../SharedComponents/Toolbar/components/ToolbarDropdownItem"; import { useEditorChange } from "../../../hooks/useEditorChange"; +import { useSelectedBlocks } from "../../../hooks/useSelectedBlocks"; export type BlockTypeDropdownItem = { name: string; @@ -82,6 +83,8 @@ export const BlockTypeDropdown = (props: { editor: BlockNoteEditor; items?: BlockTypeDropdownItem[]; }) => { + const selectedBlocks = useSelectedBlocks(props.editor); + const [block, setBlock] = useState( props.editor.getTextCursorPosition().block ); @@ -123,10 +126,13 @@ export const BlockTypeDropdown = (props: { const fullItems: ToolbarDropdownItemProps[] = useMemo(() => { const onClick = (item: BlockTypeDropdownItem) => { props.editor.focus(); - props.editor.updateBlock(block, { - type: item.type, - props: item.props, - } as PartialBlock); + + for (const block of selectedBlocks) { + props.editor.updateBlock(block, { + type: item.type, + props: item.props, + } as PartialBlock); + } }; return filteredItems.map((item) => ({ @@ -135,7 +141,7 @@ export const BlockTypeDropdown = (props: { onClick: () => onClick(item), isSelected: item.isSelected(block as Block), })); - }, [block, filteredItems, props.editor]); + }, [block, filteredItems, props.editor, selectedBlocks]); useEditorChange(props.editor, () => { setBlock(props.editor.getTextCursorPosition().block);