Skip to content

Commit

Permalink
Merge pull request #3239 from kumarajay0412/Bug-Refactor-Type-determi…
Browse files Browse the repository at this point in the history
…nation-logic-in-turn-into-dropdown-componet

Bug-Refactor-Type-determination-logic-in-turn-into-dropdown-component
  • Loading branch information
zbeyens committed Jun 9, 2024
1 parent 99857c7 commit a7a192e
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions src/components/plate-ui/turn-into-dropdown-menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ import { DropdownMenuProps } from '@radix-ui/react-dropdown-menu';
import { ELEMENT_BLOCKQUOTE } from '@udecode/plate-block-quote';
import {
collapseSelection,
findNode,
focusEditor,
getNodeEntries,
isBlock,
isCollapsed,
TElement,
toggleNodeType,
useEditorRef,
useEditorSelector,
Expand Down Expand Up @@ -77,20 +75,24 @@ const defaultItem = items.find((item) => item.value === ELEMENT_PARAGRAPH)!;

export function TurnIntoDropdownMenu(props: DropdownMenuProps) {
const value: string = useEditorSelector((editor) => {
if (isCollapsed(editor.selection)) {
const entry = findNode<TElement>(editor, {
match: (n) => isBlock(editor, n),
});
let initialNodeType: string = ELEMENT_PARAGRAPH;
let allNodesMatchInitialNodeType = false;
const codeBlockEntries = getNodeEntries(editor, {
match: (n) => isBlock(editor, n),
mode: 'highest',
});
const nodes = Array.from(codeBlockEntries);

if (nodes.length > 0) {
initialNodeType = nodes[0][0].type as string;
allNodesMatchInitialNodeType = nodes.every(([node]) => {
const type: string = (node?.type as string) || ELEMENT_PARAGRAPH;

if (entry) {
return (
items.find((item) => item.value === entry[0].type)?.value ??
ELEMENT_PARAGRAPH
);
}
return type === initialNodeType;
});
}

return ELEMENT_PARAGRAPH;
return allNodesMatchInitialNodeType ? initialNodeType : ELEMENT_PARAGRAPH;
}, []);

const editor = useEditorRef();
Expand Down

0 comments on commit a7a192e

Please sign in to comment.