Skip to content

Commit

Permalink
feat : improve logic for checking common selection
Browse files Browse the repository at this point in the history
  • Loading branch information
kumarajay0412 committed Jun 4, 2024
1 parent 445d8d7 commit 04fad5a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
focusEditor,
getNodeEntries,
isBlock,
isDefined,
toggleNodeType,
useEditorRef,
useEditorSelector,
Expand Down Expand Up @@ -109,17 +108,23 @@ 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;
const codeBlockEntries = getNodeEntries(editor, {
match: (n) => isBlock(editor, n),
});

const nodes = Array.from(codeBlockEntries);
nodes.forEach(([node]) => {
const type: string = (node?.type as string) || ELEMENT_PARAGRAPH;

if (!isDefined(commonSelection)) {
if (uniqueTypeFound) {
if (commonSelection !== type) {
commonSelection = undefined;
uniqueTypeFound = false;
}
} else {
commonSelection = type;
} else if (commonSelection !== type) {
commonSelection = undefined;
uniqueTypeFound = true;
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
focusEditor,
getNodeEntries,
isBlock,
isDefined,
toggleNodeType,
useEditorRef,
useEditorSelector,
Expand Down Expand Up @@ -79,17 +78,23 @@ 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;
const codeBlockEntries = getNodeEntries(editor, {
match: (n) => isBlock(editor, n),
});

const nodes = Array.from(codeBlockEntries);
nodes.forEach(([node]) => {
const type: string = (node?.type as string) || ELEMENT_PARAGRAPH;

if (!isDefined(commonSelection)) {
if (uniqueTypeFound) {
if (commonSelection !== type) {
commonSelection = undefined;
uniqueTypeFound = false;
}
} else {
commonSelection = type;
} else if (commonSelection !== type) {
commonSelection = undefined;
uniqueTypeFound = true;
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
focusEditor,
getNodeEntries,
isBlock,
isDefined,
toggleNodeType,
useEditorRef,
useEditorSelector,
Expand Down Expand Up @@ -77,17 +76,23 @@ 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;
const codeBlockEntries = getNodeEntries(editor, {
match: (n) => isBlock(editor, n),
});

const nodes = Array.from(codeBlockEntries);
nodes.forEach(([node]) => {
const type: string = (node?.type as string) || ELEMENT_PARAGRAPH;

if (!isDefined(commonSelection)) {
if (uniqueTypeFound) {
if (commonSelection !== type) {
commonSelection = undefined;
uniqueTypeFound = false;
}
} else {
commonSelection = type;
} else if (commonSelection !== type) {
commonSelection = undefined;
uniqueTypeFound = true;
}
});

Expand Down

0 comments on commit 04fad5a

Please sign in to comment.