Skip to content

Commit

Permalink
[lexical-table][lexical-playground] Bug Fix: Make style buttons in to…
Browse files Browse the repository at this point in the history
…olbar respect table selection (#6678)

Co-authored-by: Ivaylo Pavlov <ivailo90@gmail.com>
  • Loading branch information
KatsiarynaDzibrova and ivailop7 authored Sep 27, 2024
1 parent d83bde3 commit 304ede6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
17 changes: 9 additions & 8 deletions packages/lexical-playground/src/plugins/ToolbarPlugin/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -578,14 +578,6 @@ export default function ToolbarPlugin({
const elementKey = element.getKey();
const elementDOM = activeEditor.getElementByKey(elementKey);

// Update text format
setIsBold(selection.hasFormat('bold'));
setIsItalic(selection.hasFormat('italic'));
setIsUnderline(selection.hasFormat('underline'));
setIsStrikethrough(selection.hasFormat('strikethrough'));
setIsSubscript(selection.hasFormat('subscript'));
setIsSuperscript(selection.hasFormat('superscript'));
setIsCode(selection.hasFormat('code'));
setIsRTL($isParentElementRTL(selection));

// Update links
Expand Down Expand Up @@ -665,6 +657,15 @@ export default function ToolbarPlugin({
);
}
if ($isRangeSelection(selection) || $isTableSelection(selection)) {
// Update text format
setIsBold(selection.hasFormat('bold'));
setIsItalic(selection.hasFormat('italic'));
setIsUnderline(selection.hasFormat('underline'));
setIsStrikethrough(selection.hasFormat('strikethrough'));
setIsSubscript(selection.hasFormat('subscript'));
setIsSuperscript(selection.hasFormat('superscript'));
setIsCode(selection.hasFormat('code'));

setFontSize(
$getSelectionStyleValueForProperty(selection, 'font-size', '15px'),
);
Expand Down
25 changes: 25 additions & 0 deletions packages/lexical-table/src/LexicalTableSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ import {
$createPoint,
$getNodeByKey,
$isElementNode,
$isParagraphNode,
$normalizeSelection__EXPERIMENTAL,
BaseSelection,
isCurrentlyReadOnlyMode,
LexicalNode,
NodeKey,
PointType,
TEXT_TYPE_TO_FORMAT,
TextFormatType,
} from 'lexical';
import invariant from 'shared/invariant';

Expand Down Expand Up @@ -116,6 +119,28 @@ export class TableSelection implements BaseSelection {
// Do nothing?
}

/**
* Returns whether the provided TextFormatType is present on the Selection.
* This will be true if any paragraph in table cells has the specified format.
*
* @param type the TextFormatType to check for.
* @returns true if the provided format is currently toggled on on the Selection, false otherwise.
*/
hasFormat(type: TextFormatType): boolean {
let format = 0;

const cellNodes = this.getNodes().filter($isTableCellNode);
cellNodes.forEach((cellNode: TableCellNode) => {
const paragraph = cellNode.getFirstChild();
if ($isParagraphNode(paragraph)) {
format |= paragraph.getTextFormat();
}
});

const formatFlag = TEXT_TYPE_TO_FORMAT[type];
return (format & formatFlag) !== 0;
}

insertNodes(nodes: Array<LexicalNode>) {
const focusNode = this.focus.getNode();
invariant(
Expand Down

0 comments on commit 304ede6

Please sign in to comment.