Skip to content

Commit

Permalink
[lexical-table] Fix a number of table Cut command scenarios (#6528)
Browse files Browse the repository at this point in the history
Co-authored-by: Ivaylo Pavlov <ipavlov@Ivaylos-MacBook-Pro.local>
  • Loading branch information
ivailop7 and Ivaylo Pavlov authored Aug 22, 2024
1 parent fef4015 commit 3f1f765
Show file tree
Hide file tree
Showing 4 changed files with 354 additions and 108 deletions.
47 changes: 43 additions & 4 deletions packages/lexical-table/src/LexicalTableSelectionHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import type {
TextFormatType,
} from 'lexical';

import {$findMatchingParent} from '@lexical/utils';
import {copyToClipboard} from '@lexical/clipboard';
import {$findMatchingParent, objectKlassEquals} from '@lexical/utils';
import {
$createParagraphNode,
$createRangeSelectionFromDom,
Expand All @@ -34,13 +35,15 @@ import {
$getSelection,
$isDecoratorNode,
$isElementNode,
$isNodeSelection,
$isRangeSelection,
$isRootOrShadowRoot,
$isTextNode,
$setSelection,
COMMAND_PRIORITY_CRITICAL,
COMMAND_PRIORITY_HIGH,
CONTROLLED_TEXT_INSERTION_COMMAND,
CUT_COMMAND,
DELETE_CHARACTER_COMMAND,
DELETE_LINE_COMMAND,
DELETE_WORD_COMMAND,
Expand Down Expand Up @@ -314,7 +317,9 @@ export function applyTableHandlers(
},
);

const $deleteCellHandler = (event: KeyboardEvent): boolean => {
const $deleteCellHandler = (
event: KeyboardEvent | ClipboardEvent | null,
): boolean => {
const selection = $getSelection();

if (!$isSelectionInTable(selection, tableNode)) {
Expand All @@ -336,8 +341,10 @@ export function applyTableHandlers(
}

if ($isTableSelection(selection)) {
event.preventDefault();
event.stopPropagation();
if (event) {
event.preventDefault();
event.stopPropagation();
}
tableObserver.clearText();

return true;
Expand Down Expand Up @@ -371,6 +378,38 @@ export function applyTableHandlers(
),
);

tableObserver.listenersToRemove.add(
editor.registerCommand<KeyboardEvent | ClipboardEvent | null>(
CUT_COMMAND,
(event) => {
const selection = $getSelection();
if (selection) {
if ($isNodeSelection(selection)) {
return false;
}

copyToClipboard(
editor,
objectKlassEquals(event, ClipboardEvent)
? (event as ClipboardEvent)
: null,
);

if ($isTableSelection(selection)) {
$deleteCellHandler(event);
return true;
} else if ($isRangeSelection(selection)) {
$deleteCellHandler(event);
selection.removeText();
return true;
}
}
return false;
},
COMMAND_PRIORITY_CRITICAL,
),
);

tableObserver.listenersToRemove.add(
editor.registerCommand<TextFormatType>(
FORMAT_TEXT_COMMAND,
Expand Down
104 changes: 0 additions & 104 deletions packages/lexical-table/src/__tests__/unit/LexicalTableNode.test.ts

This file was deleted.

Loading

0 comments on commit 3f1f765

Please sign in to comment.