Repository navigation
[lexical-selection] Bug Fix: Keep the part of a paste that fits when it exceeds the max length #9423
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
base: main
Are you sure you want to change the base?
[lexical-selection] Bug Fix: Keep the part of a paste that fits when it exceeds the max length #9423
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@lexical/selection': patch | ||
| --- | ||
|
|
||
| [fix] `$trimTextContentFromAnchor` keeps the part of a paste that fits under the limit and no longer cuts an emoji or other grapheme in half | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,215 @@ | ||
| /** | ||
| * 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. | ||
| * | ||
| */ | ||
|
|
||
| // @vitest-environment node | ||
|
|
||
| import {buildEditorFromExtensions} from '@lexical/extension'; | ||
| import {$trimTextContentFromAnchor} from '@lexical/selection'; | ||
| import { | ||
| $createParagraphNode, | ||
| $createTextNode, | ||
| $getRoot, | ||
| $getSelection, | ||
| $isRangeSelection, | ||
| type LexicalEditor, | ||
| } from 'lexical'; | ||
| import {describe, expect, test, vi} from 'vitest'; | ||
|
|
||
| /** | ||
| * Commit `paragraphs` with the caret at `caret` (paragraph index, offset), | ||
| * then paste `text` at the caret and trim to `maxLength` from the anchor, | ||
| * the way MaxLengthPlugin does in a transform of the same update. | ||
| */ | ||
| function pasteWithMaxLength( | ||
| editor: LexicalEditor, | ||
| paragraphs: string[], | ||
| caret: [number, number], | ||
| text: string, | ||
| maxLength: number, | ||
| ) { | ||
| editor.update( | ||
| () => { | ||
| $getRoot().clear(); | ||
| paragraphs.forEach((content, index) => { | ||
| const paragraph = $createParagraphNode(); | ||
| $getRoot().append(paragraph); | ||
| if (content === '') { | ||
| if (index === caret[0]) { | ||
| paragraph.select(); | ||
| } | ||
| return; | ||
| } | ||
| const textNode = $createTextNode(content); | ||
| paragraph.append(textNode); | ||
| if (index === caret[0]) { | ||
| textNode.select(caret[1], caret[1]); | ||
| } | ||
| }); | ||
| }, | ||
| {discrete: true}, | ||
| ); | ||
| editor.update( | ||
| () => { | ||
| const selection = $getSelection(); | ||
| if (!$isRangeSelection(selection)) { | ||
| throw new Error('Expected a range selection'); | ||
| } | ||
| selection.insertText(text); | ||
| const delCount = $getRoot().getTextContentSize() - maxLength; | ||
| if (delCount > 0) { | ||
| $trimTextContentFromAnchor(editor, selection.anchor, delCount); | ||
| } | ||
| }, | ||
| {discrete: true}, | ||
| ); | ||
| return editor.read(() => { | ||
| const selection = $getSelection(); | ||
| if (!$isRangeSelection(selection)) { | ||
| throw new Error('Expected a range selection'); | ||
| } | ||
| return { | ||
| anchorOffset: selection.anchor.offset, | ||
| anchorText: selection.anchor.getNode().getTextContent(), | ||
| isCollapsed: selection.isCollapsed(), | ||
| size: $getRoot().getTextContentSize(), | ||
| text: $getRoot().getTextContent(), | ||
| }; | ||
| }); | ||
| } | ||
|
|
||
| describe('$trimTextContentFromAnchor (#3775)', () => { | ||
| test('keeps the start of a paste into an empty editor', () => { | ||
| using editor = buildEditorFromExtensions({name: 'trim-test'}); | ||
| expect(pasteWithMaxLength(editor, [''], [0, 0], '0123456789', 4)).toEqual({ | ||
| anchorOffset: 4, | ||
| anchorText: '0123', | ||
| isCollapsed: true, | ||
| size: 4, | ||
| text: '0123', | ||
| }); | ||
| }); | ||
|
|
||
| test('keeps the start of a paste after existing text', () => { | ||
| using editor = buildEditorFromExtensions({name: 'trim-test'}); | ||
| expect(pasteWithMaxLength(editor, ['a'], [0, 1], '0123456789', 5)).toEqual({ | ||
| anchorOffset: 5, | ||
| anchorText: 'a0123', | ||
| isCollapsed: true, | ||
| size: 5, | ||
| text: 'a0123', | ||
| }); | ||
| }); | ||
|
|
||
| test('trims a paste in the middle of the text just before the caret', () => { | ||
| using editor = buildEditorFromExtensions({name: 'trim-test'}); | ||
| expect( | ||
| pasteWithMaxLength(editor, ['Hello world'], [0, 5], 'XYZ', 13), | ||
| ).toEqual({ | ||
| anchorOffset: 7, | ||
| anchorText: 'HelloXY world', | ||
| isCollapsed: true, | ||
| size: 13, | ||
| text: 'HelloXY world', | ||
| }); | ||
| }); | ||
|
|
||
| test('trims a paste into the last of several paragraphs', () => { | ||
| using editor = buildEditorFromExtensions({name: 'trim-test'}); | ||
| expect( | ||
| pasteWithMaxLength(editor, ['abc', 'de'], [1, 2], '123456', 10), | ||
| ).toEqual({ | ||
| anchorOffset: 5, | ||
| anchorText: 'de123', | ||
| isCollapsed: true, | ||
| size: 10, | ||
| text: 'abc\n\nde123', | ||
| }); | ||
| }); | ||
|
|
||
| test('drops an emoji whole instead of splitting its surrogate pair', () => { | ||
| using editor = buildEditorFromExtensions({name: 'trim-test'}); | ||
| expect(pasteWithMaxLength(editor, ['abc'], [0, 3], 'd💏', 5)).toEqual({ | ||
| anchorOffset: 4, | ||
| anchorText: 'abcd', | ||
| isCollapsed: true, | ||
| size: 4, | ||
| text: 'abcd', | ||
| }); | ||
| }); | ||
|
|
||
| test('drops a multi code point grapheme whole', () => { | ||
| using editor = buildEditorFromExtensions({name: 'trim-test'}); | ||
| expect(pasteWithMaxLength(editor, ['ab'], [0, 2], 'c👨💻', 6)).toEqual({ | ||
| anchorOffset: 3, | ||
| anchorText: 'abc', | ||
| isCollapsed: true, | ||
| size: 3, | ||
| text: 'abc', | ||
| }); | ||
| }); | ||
|
|
||
| test('drops a flag emoji whole', () => { | ||
| using editor = buildEditorFromExtensions({name: 'trim-test'}); | ||
| expect(pasteWithMaxLength(editor, ['ab'], [0, 2], 'c🇨🇦', 5)).toEqual({ | ||
| anchorOffset: 3, | ||
| anchorText: 'abc', | ||
| isCollapsed: true, | ||
| size: 3, | ||
| text: 'abc', | ||
| }); | ||
| }); | ||
|
|
||
| test('drops a letter with a combining mark whole', () => { | ||
| using editor = buildEditorFromExtensions({name: 'trim-test'}); | ||
| expect(pasteWithMaxLength(editor, ['ab'], [0, 2], 'ce\u0301', 4)).toEqual({ | ||
| anchorOffset: 3, | ||
| anchorText: 'abc', | ||
| isCollapsed: true, | ||
| size: 3, | ||
| text: 'abc', | ||
| }); | ||
| }); | ||
|
|
||
| test('drops an emoji whole when the cut lands in an earlier node', () => { | ||
|
rootnode-bot[bot] marked this conversation as resolved.
Resolved
|
||
| using editor = buildEditorFromExtensions({name: 'trim-test'}); | ||
| expect(pasteWithMaxLength(editor, ['ab💏', 'x'], [1, 1], 'yz', 3)).toEqual({ | ||
| anchorOffset: 2, | ||
| anchorText: 'ab', | ||
| isCollapsed: true, | ||
| size: 2, | ||
| text: 'ab', | ||
| }); | ||
| }); | ||
|
|
||
| test('removes an earlier node whose first grapheme the cut lands in', () => { | ||
| using editor = buildEditorFromExtensions({name: 'trim-test'}); | ||
| expect( | ||
| pasteWithMaxLength(editor, ['ab', '💏c', 'x'], [2, 1], 'yz', 5), | ||
| ).toMatchObject({text: 'ab'}); | ||
| }); | ||
|
|
||
| test('keeps surrogate pairs whole without Intl.Segmenter', () => { | ||
| // Intl's members are not enumerable, so a spread would drop them all | ||
| vi.stubGlobal( | ||
| 'Intl', | ||
| Object.assign(Object.create(Intl), {Segmenter: undefined}), | ||
| ); | ||
| try { | ||
| using editor = buildEditorFromExtensions({name: 'trim-test'}); | ||
| expect(pasteWithMaxLength(editor, ['abc'], [0, 3], 'd💏', 5)).toEqual({ | ||
| anchorOffset: 4, | ||
| anchorText: 'abcd', | ||
| isCollapsed: true, | ||
| size: 4, | ||
| text: 'abcd', | ||
| }); | ||
| } finally { | ||
| vi.unstubAllGlobals(); | ||
| } | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,8 +11,6 @@ import { | |
| $caretRangeFromSelection, | ||
| $cloneWithPropertiesEphemeral, | ||
| $createTextNode, | ||
| $getNodeByKey, | ||
| $getPreviousSelection, | ||
| $getSelection, | ||
| $getTextPointCaretSliceForNode, | ||
| $isElementNode, | ||
|
|
@@ -108,10 +106,13 @@ export function $isAtNodeEnd(point: Point): boolean { | |
| } | ||
|
|
||
| /** | ||
| * Trims text from a node in order to shorten it, eg. to enforce a text's max length. If it deletes text | ||
| * that is an ancestor of the anchor then it will leave 2 indents, otherwise, if no text content exists, it deletes | ||
| * the TextNode. It will move the focus to either the end of any left over text or beginning of a new TextNode. | ||
| * @param editor - The lexical editor. | ||
| * Trims `delCount` characters of text content ending at the anchor, eg. to enforce a max length. Text just | ||
| * before the anchor is removed first, then text from preceding nodes. Nodes left with no text are removed, and | ||
| * an empty block counts as 2 characters (its line breaks). Where `Intl.Segmenter` is available, a grapheme | ||
| * (eg. an emoji) is never cut in half (without it, only surrogate pairs are kept whole): it is removed whole, | ||
| * so slightly more than `delCount` characters may be removed. This always trims; rejecting a keystroke made at | ||
| * the limit is up to the caller (MaxLengthPlugin restores the previous editor state in that case). | ||
| * @param editor - Unused, kept for backwards compatibility. | ||
| * @param anchor - The anchor of the current selection, where the selection should be pointing. | ||
| * @param delCount - The amount of characters to delete. Useful as a dynamic variable eg. textContentSize - maxLength; | ||
| */ | ||
|
|
@@ -167,7 +168,13 @@ export function $trimTextContentFromAnchor( | |
| } | ||
| const currentNodeSize = text.length; | ||
|
|
||
| if (!$isTextNode(currentNode) || remaining >= currentNodeSize) { | ||
| if ( | ||
| !$isTextNode(currentNode) || | ||
| remaining >= currentNodeSize || | ||
| // The cut is inside the first grapheme of a node before the anchor's, so nothing of it is kept | ||
| (anchor.key !== currentNode.getKey() && | ||
| getGraphemeStart(text, currentNodeSize - remaining) === 0) | ||
| ) { | ||
| const parent = currentNode.getParent(); | ||
| currentNode.remove(); | ||
| if ( | ||
|
|
@@ -181,31 +188,9 @@ export function $trimTextContentFromAnchor( | |
| currentNode = nextNode; | ||
| } else { | ||
| const key = currentNode.getKey(); | ||
| // See if we can just revert it to what was in the last editor state | ||
| const prevTextContent: string | null = editor.read('latest', () => { | ||
| const prevNode = $getNodeByKey(key); | ||
| if ($isTextNode(prevNode) && prevNode.isSimpleText()) { | ||
| return prevNode.getTextContent(); | ||
| } | ||
| return null; | ||
| }); | ||
| const offset = currentNodeSize - remaining; | ||
| const offset = getGraphemeStart(text, currentNodeSize - remaining); | ||
|
rootnode-bot[bot] marked this conversation as resolved.
Resolved
|
||
| const slicedText = text.slice(0, offset); | ||
| if (prevTextContent !== null && prevTextContent !== text) { | ||
| const prevSelection = $getPreviousSelection(); | ||
| let target = currentNode; | ||
| if (!currentNode.isSimpleText()) { | ||
| const textNode = $createTextNode(prevTextContent); | ||
| currentNode.replace(textNode); | ||
| target = textNode; | ||
| } else { | ||
| currentNode.setTextContent(prevTextContent); | ||
| } | ||
| if ($isRangeSelection(prevSelection) && prevSelection.isCollapsed()) { | ||
| const prevOffset = prevSelection.anchor.offset; | ||
| target.select(prevOffset, prevOffset); | ||
| } | ||
| } else if (currentNode.isSimpleText()) { | ||
| if (currentNode.isSimpleText()) { | ||
| // Split text | ||
| const isSelected = anchor.key === key; | ||
| let anchorOffset = anchor.offset; | ||
|
|
@@ -214,7 +199,10 @@ export function $trimTextContentFromAnchor( | |
| if (anchorOffset < remaining) { | ||
| anchorOffset = currentNodeSize; | ||
| } | ||
|
Comment on lines
199
to
201
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optional, non-blocking: with the revert branch gone, this fallback is now reached for an existing node too. In text that is already over the limit (loaded with |
||
| const splitStart = isSelected ? anchorOffset - remaining : 0; | ||
| // Never cut a grapheme (eg. an emoji) in half; drop it whole instead. | ||
| const splitStart = isSelected | ||
| ? getGraphemeStart(text, anchorOffset - remaining) | ||
| : 0; | ||
| const splitEnd = isSelected ? anchorOffset : offset; | ||
| if (isSelected && splitStart === 0) { | ||
| const [excessNode] = currentNode.splitText(splitStart, splitEnd); | ||
|
|
@@ -406,3 +394,18 @@ export function $ensureForwardRangeSelection(selection: RangeSelection): void { | |
| focus.set(key, offset, type); | ||
| } | ||
| } | ||
|
|
||
| function getGraphemeStart(text: string, offset: number): number { | ||
| if (typeof Intl.Segmenter === 'function') { | ||
| let start = 0; | ||
| for (const {index} of new Intl.Segmenter().segment(text)) { | ||
| if (index > offset) { | ||
| break; | ||
| } | ||
| start = index; | ||
| } | ||
| return start; | ||
| } | ||
| const code = text.charCodeAt(offset); | ||
| return offset > 0 && code >= 0xdc00 && code <= 0xdfff ? offset - 1 : offset; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional: CONTRIBUTING.md ("Changesets") and AGENTS.md ask a fix that changes observable behavior to carry a
Verify:paragraph, so it is listed under "Behavior changes". For example: