From 6294793a6fce8468b1a2ee44a210dd591c8baabd Mon Sep 17 00:00:00 2001 From: AJAY KUMAR <66991625+kumarajay0412@users.noreply.github.com> Date: Wed, 5 Jun 2024 01:56:35 +0530 Subject: [PATCH] update : logic of iterating over nodes using every in turn into dropdown component --- .../playground-turn-into-dropdown-menu.tsx | 28 ++++++++--------- .../plate-ui/turn-into-dropdown-menu.tsx | 30 ++++++++----------- .../plate-ui/turn-into-dropdown-menu.tsx | 28 ++++++++--------- 3 files changed, 37 insertions(+), 49 deletions(-) diff --git a/apps/www/src/components/plate-ui/playground-turn-into-dropdown-menu.tsx b/apps/www/src/components/plate-ui/playground-turn-into-dropdown-menu.tsx index d9ef24b8fc..1eccfdbc94 100644 --- a/apps/www/src/components/plate-ui/playground-turn-into-dropdown-menu.tsx +++ b/apps/www/src/components/plate-ui/playground-turn-into-dropdown-menu.tsx @@ -107,28 +107,24 @@ const defaultItem = items.find((item) => item.value === ELEMENT_PARAGRAPH)!; export function PlaygroundTurnIntoDropdownMenu(props: DropdownMenuProps) { const value: string = useEditorSelector((editor) => { - let commonSelection: string | undefined; - let uniqueTypeFound = false; + let firstNodeType: string = ELEMENT_PARAGRAPH; + let allNodesMatchFirstNode = false; const codeBlockEntries = getNodeEntries(editor, { match: (n) => isBlock(editor, n), + mode: 'highest', }); - const nodes = Array.from(codeBlockEntries); - nodes.forEach(([node]) => { - const type: string = (node?.type as string) || ELEMENT_PARAGRAPH; - if (uniqueTypeFound) { - if (commonSelection !== type) { - commonSelection = undefined; - uniqueTypeFound = false; - } - } else { - commonSelection = type; - uniqueTypeFound = true; - } - }); + if (nodes.length > 0) { + firstNodeType = nodes[0][0].type as string; + allNodesMatchFirstNode = nodes.every(([node]) => { + const type: string = (node?.type as string) || ELEMENT_PARAGRAPH; + + return type === firstNodeType; + }); + } - return commonSelection ?? ELEMENT_PARAGRAPH; + return allNodesMatchFirstNode ? firstNodeType : ELEMENT_PARAGRAPH; }, []); const editor = useEditorRef(); diff --git a/apps/www/src/registry/default/plate-ui/turn-into-dropdown-menu.tsx b/apps/www/src/registry/default/plate-ui/turn-into-dropdown-menu.tsx index 96732edad6..db4d3cf3e5 100644 --- a/apps/www/src/registry/default/plate-ui/turn-into-dropdown-menu.tsx +++ b/apps/www/src/registry/default/plate-ui/turn-into-dropdown-menu.tsx @@ -77,28 +77,24 @@ const defaultItem = items.find((item) => item.value === ELEMENT_PARAGRAPH)!; export function TurnIntoDropdownMenu(props: DropdownMenuProps) { const value: string = useEditorSelector((editor) => { - let commonSelection: string | undefined; - let uniqueTypeFound = false; + let firstNodeType: string = ELEMENT_PARAGRAPH; + let allNodesMatchFirstNode = false; const codeBlockEntries = getNodeEntries(editor, { match: (n) => isBlock(editor, n), + mode: 'highest', }); - const nodes = Array.from(codeBlockEntries); - nodes.forEach(([node]) => { - const type: string = (node?.type as string) || ELEMENT_PARAGRAPH; - - if (uniqueTypeFound) { - if (commonSelection !== type) { - commonSelection = undefined; - uniqueTypeFound = false; - } - } else { - commonSelection = type; - uniqueTypeFound = true; - } - }); - return commonSelection ?? ELEMENT_PARAGRAPH; + if (nodes.length > 0) { + firstNodeType = nodes[0][0].type as string; + allNodesMatchFirstNode = nodes.every(([node]) => { + const type: string = (node?.type as string) || ELEMENT_PARAGRAPH; + + return type === firstNodeType; + }); + } + + return allNodesMatchFirstNode ? firstNodeType : ELEMENT_PARAGRAPH; }, []); const editor = useEditorRef(); diff --git a/templates/plate-playground-template/src/components/plate-ui/turn-into-dropdown-menu.tsx b/templates/plate-playground-template/src/components/plate-ui/turn-into-dropdown-menu.tsx index fb9e108c3f..dd8f40223c 100644 --- a/templates/plate-playground-template/src/components/plate-ui/turn-into-dropdown-menu.tsx +++ b/templates/plate-playground-template/src/components/plate-ui/turn-into-dropdown-menu.tsx @@ -75,28 +75,24 @@ const defaultItem = items.find((item) => item.value === ELEMENT_PARAGRAPH)!; export function TurnIntoDropdownMenu(props: DropdownMenuProps) { const value: string = useEditorSelector((editor) => { - let commonSelection: string | undefined; - let uniqueTypeFound = false; + let firstNodeType: string = ELEMENT_PARAGRAPH; + let allNodesMatchFirstNode = false; const codeBlockEntries = getNodeEntries(editor, { match: (n) => isBlock(editor, n), + mode: 'highest', }); - const nodes = Array.from(codeBlockEntries); - nodes.forEach(([node]) => { - const type: string = (node?.type as string) || ELEMENT_PARAGRAPH; - if (uniqueTypeFound) { - if (commonSelection !== type) { - commonSelection = undefined; - uniqueTypeFound = false; - } - } else { - commonSelection = type; - uniqueTypeFound = true; - } - }); + if (nodes.length > 0) { + firstNodeType = nodes[0][0].type as string; + allNodesMatchFirstNode = nodes.every(([node]) => { + const type: string = (node?.type as string) || ELEMENT_PARAGRAPH; + + return type === firstNodeType; + }); + } - return commonSelection ?? ELEMENT_PARAGRAPH; + return allNodesMatchFirstNode ? firstNodeType : ELEMENT_PARAGRAPH; }, []); const editor = useEditorRef();