Skip to content

Commit

Permalink
includeIfCollapsed
Browse files Browse the repository at this point in the history
  • Loading branch information
GermanJablo committed Dec 19, 2024
1 parent ba2f11d commit f6407b7
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions packages/lexical-selection/src/lexical-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,22 +289,33 @@ export function $patchStyleText(
) => string)
>,
): void {
if (selection.isCollapsed() && $isRangeSelection(selection)) {
$patchStyle(selection, patch);
} else {
$forEachSelectedTextNode((textNode) => {
$patchStyle(textNode, patch);
});
}
$forEachSelectedTextNode((textNode) => {
$patchStyle(textNode, patch);
});
}

/**
* @param fn - The function to apply to each selected TextNode.
* @param includeIfCollapsed - Whether to include the TextNode if the selection is collapsed. Defaults to false.
*/
export function $forEachSelectedTextNode(
fn: (textNode: TextNode) => void,
includeIfCollapsed = false,
): void {
const selection = $getSelection();
if (!$isRangeSelection(selection)) {
return;
}
if (selection.isCollapsed()) {
if (!includeIfCollapsed) {
return;
}
const node = selection.anchor.getNode();
if ($isTextNode(node)) {
fn(node);
}
return;
}
const selectedNodes = selection.getNodes();
const selectedNodesLength = selectedNodes.length;
const {anchor, focus} = selection;
Expand Down

0 comments on commit f6407b7

Please sign in to comment.