Skip to content

Commit

Permalink
Respect token mode when patching text style
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianmxb committed Jul 19, 2024
1 parent 666ccb2 commit 35f8b37
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
16 changes: 10 additions & 6 deletions packages/lexical-selection/src/lexical-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
$isRangeSelection,
$isRootNode,
$isTextNode,
$isTokenOrSegmented,
BaseSelection,
ElementNode,
LexicalEditor,
Expand Down Expand Up @@ -404,8 +405,11 @@ export function $patchStyleText(
return;
}

// The entire node is selected, so just format it
if (startOffset === 0 && endOffset === firstNodeTextLength) {
// The entire node is selected or a token/segment, so just format it
if (
$isTokenOrSegmented(firstNode) ||
(startOffset === 0 && endOffset === firstNodeTextLength)
) {
$patchStyle(firstNode, patch);
firstNode.select(startOffset, endOffset);
} else {
Expand All @@ -423,8 +427,8 @@ export function $patchStyleText(
startOffset < firstNode.getTextContentSize() &&
firstNode.canHaveFormat()
) {
if (startOffset !== 0) {
// the entire first node isn't selected, so split it
if (startOffset !== 0 && !$isTokenOrSegmented(firstNode)) {
// the entire first node isn't selected and it isn't a token or segmented, so split it
firstNode = firstNode.splitText(startOffset)[1];
startOffset = 0;
if (isBefore) {
Expand All @@ -449,8 +453,8 @@ export function $patchStyleText(
endOffset = lastNodeTextLength;
}

// if the entire last node isn't selected, split it
if (endOffset !== lastNodeTextLength) {
// if the entire last node isn't selected and it isn't a token or segmented, split it
if (endOffset !== lastNodeTextLength && !$isTokenOrSegmented(lastNode)) {
[lastNode] = lastNode.splitText(endOffset);
}

Expand Down
1 change: 1 addition & 0 deletions packages/lexical/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ export {
$isInlineElementOrDecoratorNode,
$isLeafNode,
$isRootOrShadowRoot,
$isTokenOrSegmented,
$nodesOfType,
$selectAll,
$setCompositionKey,
Expand Down

0 comments on commit 35f8b37

Please sign in to comment.