Repository navigation
[lexical-website][lexical-y] Bug Fix: Place an edit of set text at a word edge when the caret is past it #9657
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
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,7 @@ | ||
| --- | ||
| '@lexical/y': patch | ||
| --- | ||
|
|
||
| [fix] Place an insertion or a deletion of text set with the caret at the end of the node, past the edit or in a later node at a word edge, as without a caret | ||
|
|
||
| Verify: `node.setTextContent('t1z t3z'); node.selectEnd();` on a node holding `t1z t2z t3z`, while a collaborator deletes `t2z`, shows `t1z t3z` on every client (not `t1z 3z`), and a word the collaborator types after `t2z` stays a word of its own. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -767,12 +767,27 @@ function placeAtWordEdge( | |
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Whether the caret at `cursor` of `b` placed the insertion or deletion that | ||
| * makes `b` of `a`: it anchors the diff (caretAnchors) and its bound is in | ||
| * the run of equal text the edit could be in, the furthest place included. | ||
| * A caret past the furthest place (at the end of the node or past the edit, | ||
| * and the end of the node, which the writer passes for a caret in a later | ||
| * node) anchors every place of the run and places nothing. | ||
| */ | ||
| function caretPlaces(a: string, b: string, cursor: number): boolean { | ||
| return ( | ||
| caretAnchors(a, b, cursor) && | ||
| cursor - Math.max(0, b.length - a.length) <= diffStrings(a, b, -1).prefix | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * The common start and end that text set whole (`setTextContent`, a node's | ||
| * text made again, the DOM's text) keeps of the text it replaces, anchored | ||
| * at `cursor` (see diffStrings): a replacement keeps whole words | ||
| * (snapToWords); an insertion or a deletion keeps every other character, | ||
| * where the caret placed it (diffStrings), or where the diff put it if both | ||
| * where the caret placed it (caretPlaces), or where the diff put it if both | ||
| * its ends are at word edges, else at the first place in the run of equal | ||
| * text it could be in where they are (placeAtWordEdge), else where the diff | ||
| * put it. The first: | ||
|
|
@@ -791,7 +806,7 @@ export function setTextEnds( | |
| diff.prefix + diff.suffix === Math.min(a.length, b.length) && | ||
| a.length !== b.length | ||
| ) { | ||
| return composing || caretAnchors(a, b, cursor) | ||
| return composing || caretPlaces(a, b, cursor) | ||
|
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: no
Member
Author
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. Added in 9659452: |
||
| ? diff | ||
| : (placeAtWordEdge(a, b, diff, true) ?? diff); | ||
| } | ||
|
|
||
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 nit: a caret at the end of the node still places an edit that could end there.
setTextEnds('ab ab', 'ab abz ab', 9, false)keepsz abat the end (the cell atSetTextWordEdges.test.ts:229), where no caret writesabzat 2. Something like "(none, or one past the furthest place the edit could be: at the end of the node or in a later node, unless the edit could end there)" would matchcaretPlaces; the D30 row and the describe comment atSetTextWordEdges.test.ts:214-218use the same list.