Repository navigation
[lexical-table] Bug Fix: Leave a cut with no end in the table to the editor's own cut #9708
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
therealemmanueloppong
wants to merge
2
commits into
meta:main
Choose a base branch
from
therealemmanueloppong:fix-table-cut-outside-range
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| '@lexical/table': patch | ||
| --- | ||
|
|
||
| [fix] Leave a cut whose range has no end in a table to the editor's own cut and to other CUT_COMMAND listeners, instead of the table answering every range selection once the document holds a table | ||
|
|
||
| Verify: in a document that holds a table, cut a range with neither end in the table (for example Select All over a heading, a table and a paragraph) and check that `CUT_COMMAND` listeners below `COMMAND_PRIORITY_HIGH` run, that the removal arrives in an update tagged `CUT_TAG` once the clipboard write resolves, and that an empty paragraph rather than an empty heading is left. In a document with two tables, cutting from a cell of the second table to the text after it removes that table whole. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
202 changes: 202 additions & 0 deletions
202
packages/lexical-table/src/__tests__/unit/CutOutsideTable.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,202 @@ | ||
| /** | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| * | ||
| */ | ||
|
|
||
| import {buildEditorFromExtensions} from '@lexical/extension'; | ||
| import {$createHeadingNode, RichTextExtension} from '@lexical/rich-text'; | ||
| import { | ||
| $computeTableMapSkipCellCheck, | ||
| $createTableNodeWithDimensions, | ||
| $isTableNode, | ||
| TableExtension, | ||
| } from '@lexical/table'; | ||
| import { | ||
| $createParagraphNode, | ||
| $createRangeSelection, | ||
| $createTextNode, | ||
| $getRoot, | ||
| $isElementNode, | ||
| $selectAll, | ||
| $setSelection, | ||
| COMMAND_PRIORITY_LOW, | ||
| CUT_COMMAND, | ||
| CUT_TAG, | ||
| defineExtension, | ||
| type LexicalEditor, | ||
| } from 'lexical'; | ||
| import {$assertNodeType} from 'lexical/src/__tests__/utils'; | ||
| import {describe, expect, onTestFinished, test, vi} from 'vitest'; | ||
|
|
||
| const TableTestThemeExtension = defineExtension({ | ||
| dependencies: [TableExtension], | ||
| name: 'CutOutsideTableTestThemeExtension', | ||
| theme: {tableScrollableWrapper: ''}, | ||
| }); | ||
|
|
||
| function setup() { | ||
| const editor = buildEditorFromExtensions(TableTestThemeExtension); | ||
| onTestFinished(() => editor[Symbol.dispose]()); | ||
| const root = document.createElement('div'); | ||
| document.body.append(root); | ||
| onTestFinished(() => root.remove()); | ||
| editor.setRootElement(root); | ||
| let lowPriorityCuts = 0; | ||
| editor.registerCommand( | ||
| CUT_COMMAND, | ||
| () => { | ||
| lowPriorityCuts++; | ||
| return false; | ||
| }, | ||
| COMMAND_PRIORITY_LOW, | ||
| ); | ||
| editor.update( | ||
| () => { | ||
| $getRoot() | ||
| .clear() | ||
| .append( | ||
| $createParagraphNode().append($createTextNode('before')), | ||
| $createTableNodeWithDimensions(1, 2, false), | ||
| $createParagraphNode().append($createTextNode('x y')), | ||
| ); | ||
| }, | ||
| {discrete: true}, | ||
| ); | ||
| return {editor, getLowPriorityCuts: () => lowPriorityCuts}; | ||
| } | ||
|
|
||
| function cut(editor: LexicalEditor, select: () => void) { | ||
| editor.update(select, {discrete: true}); | ||
| editor.dispatchCommand(CUT_COMMAND, new ClipboardEvent('cut')); | ||
| } | ||
|
|
||
| describe('CUT_COMMAND in a document that holds a table', () => { | ||
| test('a range outside the table is left to the listeners below the table', () => { | ||
| const {editor, getLowPriorityCuts} = setup(); | ||
| cut(editor, () => { | ||
| const last = $assertNodeType($getRoot().getLastChild(), $isElementNode); | ||
| const text = last.getFirstChildOrThrow(); | ||
| const selection = $createRangeSelection(); | ||
| selection.anchor.set(text.getKey(), 0, 'text'); | ||
| selection.focus.set(text.getKey(), 1, 'text'); | ||
| $setSelection(selection); | ||
| }); | ||
| expect(getLowPriorityCuts()).toBe(1); | ||
|
therealemmanueloppong marked this conversation as resolved.
Resolved
|
||
| }); | ||
|
|
||
| test('a range in a cell is answered by the table', () => { | ||
| const {editor, getLowPriorityCuts} = setup(); | ||
| cut(editor, () => { | ||
| const table = $assertNodeType( | ||
| $getRoot().getChildAtIndex(1), | ||
| $isTableNode, | ||
| ); | ||
| const [map] = $computeTableMapSkipCellCheck(table, null, null); | ||
| const paragraph = $assertNodeType( | ||
| map[0][0].cell.getFirstChild(), | ||
| $isElementNode, | ||
| ); | ||
| paragraph.append($createTextNode('abc')); | ||
| const text = paragraph.getFirstChildOrThrow(); | ||
| const selection = $createRangeSelection(); | ||
| selection.anchor.set(text.getKey(), 0, 'text'); | ||
| selection.focus.set(text.getKey(), 2, 'text'); | ||
| $setSelection(selection); | ||
| }); | ||
| expect(getLowPriorityCuts()).toBe(0); | ||
| }); | ||
| }); | ||
|
|
||
| describe('CUT_COMMAND with rich text in a document that holds tables', () => { | ||
| function setupRichText() { | ||
| const editor = buildEditorFromExtensions( | ||
| defineExtension({ | ||
| dependencies: [RichTextExtension, TableTestThemeExtension], | ||
| name: 'CutOutsideTableRichText', | ||
| }), | ||
| ); | ||
| onTestFinished(() => editor[Symbol.dispose]()); | ||
| const root = document.createElement('div'); | ||
| document.body.append(root); | ||
| onTestFinished(() => root.remove()); | ||
| editor.setRootElement(root); | ||
| const tags: ReadonlySet<string>[] = []; | ||
| editor.registerUpdateListener(({tags: updateTags}) => { | ||
| tags.push(updateTags); | ||
| }); | ||
| return {editor, tags}; | ||
| } | ||
|
|
||
| const rootTypes = (editor: LexicalEditor) => | ||
| editor.read(() => | ||
| $getRoot() | ||
| .getChildren() | ||
| .map(node => node.getType()), | ||
| ); | ||
|
|
||
| test('a whole-document cut leaves one paragraph, in updates tagged CUT_TAG', async () => { | ||
| const {editor, tags} = setupRichText(); | ||
| editor.update( | ||
| () => { | ||
| $getRoot() | ||
| .clear() | ||
| .append( | ||
| $createHeadingNode('h1').append($createTextNode('Title')), | ||
| $createTableNodeWithDimensions(1, 2, false), | ||
| $createParagraphNode().append($createTextNode('x y')), | ||
| ); | ||
| $selectAll(); | ||
| }, | ||
| {discrete: true}, | ||
| ); | ||
| editor.dispatchCommand(CUT_COMMAND, new ClipboardEvent('cut')); | ||
| await vi.waitFor(() => expect(rootTypes(editor)).toEqual(['paragraph'])); | ||
| expect(tags.some(updateTags => updateTags.has(CUT_TAG))).toBe(true); | ||
| }); | ||
|
|
||
| test('the table a range ends in cuts it whole when the document holds another table', async () => { | ||
| const {editor} = setupRichText(); | ||
| editor.update( | ||
| () => { | ||
| $getRoot() | ||
| .clear() | ||
| .append( | ||
| $createTableNodeWithDimensions(1, 1, false), | ||
| $createTableNodeWithDimensions(2, 2, false), | ||
| $createParagraphNode().append($createTextNode('tail')), | ||
| ); | ||
| const second = $assertNodeType( | ||
| $getRoot().getChildAtIndex(1), | ||
| $isTableNode, | ||
| ); | ||
| const [map] = $computeTableMapSkipCellCheck(second, null, null); | ||
| const paragraph = $assertNodeType( | ||
| map[0][0].cell.getFirstChild(), | ||
| $isElementNode, | ||
| ); | ||
| paragraph.append($createTextNode('cell')); | ||
| const tailParagraph = $assertNodeType( | ||
| $getRoot().getLastChild(), | ||
| $isElementNode, | ||
| ); | ||
| const tail = tailParagraph.getFirstChildOrThrow(); | ||
| const selection = $createRangeSelection(); | ||
| selection.anchor.set( | ||
| paragraph.getFirstChildOrThrow().getKey(), | ||
| 1, | ||
| 'text', | ||
| ); | ||
| selection.focus.set(tail.getKey(), 2, 'text'); | ||
| $setSelection(selection); | ||
| }, | ||
| {discrete: true}, | ||
| ); | ||
| editor.dispatchCommand(CUT_COMMAND, new ClipboardEvent('cut')); | ||
| await vi.waitFor(() => | ||
| expect(rootTypes(editor)).toEqual(['table', 'paragraph']), | ||
| ); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.