From d83bde36467e26393f78de1acc602469c58ad729 Mon Sep 17 00:00:00 2001 From: Maksim Horbachevsky Date: Fri, 27 Sep 2024 15:40:39 -0400 Subject: [PATCH 1/2] Support backward selection in rows deletion (#6680) --- packages/lexical-table/src/LexicalTableUtils.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/lexical-table/src/LexicalTableUtils.ts b/packages/lexical-table/src/LexicalTableUtils.ts index c674c4a8b88..7a4aa7534ea 100644 --- a/packages/lexical-table/src/LexicalTableUtils.ts +++ b/packages/lexical-table/src/LexicalTableUtils.ts @@ -510,8 +510,9 @@ export function $deleteTableRow__EXPERIMENTAL(): void { $isRangeSelection(selection) || $isTableSelection(selection), 'Expected a RangeSelection or TableSelection', ); - const anchor = selection.anchor.getNode(); - const focus = selection.focus.getNode(); + const [anchor, focus] = selection.isBackward() + ? [selection.focus.getNode(), selection.anchor.getNode()] + : [selection.anchor.getNode(), selection.focus.getNode()]; const [anchorCell, , grid] = $getNodeTriplet(anchor); const [focusCell] = $getNodeTriplet(focus); const [gridMap, anchorCellMap, focusCellMap] = $computeTableMap( From 304ede6fe06c4dfbdd82edbd9a90a2289a142826 Mon Sep 17 00:00:00 2001 From: Katsia <47710336+KatsiarynaDzibrova@users.noreply.github.com> Date: Sat, 28 Sep 2024 00:12:33 +0100 Subject: [PATCH 2/2] [lexical-table][lexical-playground] Bug Fix: Make style buttons in toolbar respect table selection (#6678) Co-authored-by: Ivaylo Pavlov --- .../src/plugins/ToolbarPlugin/index.tsx | 17 +++++++------ .../src/LexicalTableSelection.ts | 25 +++++++++++++++++++ 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/packages/lexical-playground/src/plugins/ToolbarPlugin/index.tsx b/packages/lexical-playground/src/plugins/ToolbarPlugin/index.tsx index b32d841c7ec..7ce9abbdbcc 100644 --- a/packages/lexical-playground/src/plugins/ToolbarPlugin/index.tsx +++ b/packages/lexical-playground/src/plugins/ToolbarPlugin/index.tsx @@ -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 @@ -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'), ); diff --git a/packages/lexical-table/src/LexicalTableSelection.ts b/packages/lexical-table/src/LexicalTableSelection.ts index acb80d2a20d..03c8543cb73 100644 --- a/packages/lexical-table/src/LexicalTableSelection.ts +++ b/packages/lexical-table/src/LexicalTableSelection.ts @@ -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'; @@ -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) { const focusNode = this.focus.getNode(); invariant(