Repository navigation
[lexical][lexical-extension][lexical-selection] Bug Fix: Fill an empty declared container in $setBlocksType whichever end of the range is in it - #9616
Conversation
…y declared container in $setBlocksType whichever end of the range is in it ## Description Follow-up to #9590, from the Meta code-review bot's last review there (approved, optional suggestions), which came after the merge: - `$setBlocksType` filled an empty declared container only when the selection's anchor was in it, so a backward range ending in the container left it empty. It now fills it when either point is in it, and moves those points into the new block, also on the active selection that a replacement earlier in the same call can make anew. - The `$isBlockContainer` JSDoc says that only an empty declared container gets the new block from `$setBlocksType` (an empty root or shadow root still does not, as before). ## Changeset `.changeset/selection-set-blocks-type-backward-container.md` (`[fix]`, `@lexical/selection`). ## Test plan ### Before ``` $ pnpm exec vitest run --project unit packages/lexical-extension/src/__tests__/unit/BlockContainers.test.ts -t "backward range" # range-selection.ts from main × $setBlocksType keeps a backward range that ends in the empty container Expected: "p("before") / dbox[indent1](h()) / h("after")" Received: "p("before") / dbox[indent1]() / h("after")" Tests 1 failed | 399 skipped (400) ``` ### After ``` $ pnpm exec vitest run --project unit packages/lexical-extension/src/__tests__/unit/BlockContainers.test.ts packages/lexical-selection packages/lexical-list Tests 921 passed (921) $ pnpm run ci-check Found 0 errors ```
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
AI review status for this pull request.
|
lexical
@lexical/a11y
@lexical/clipboard
@lexical/code
@lexical/code-core
@lexical/code-prism
@lexical/code-shiki
@lexical/compiler
@lexical/devtools-core
@lexical/dragon
@lexical/eslint-plugin
@lexical/extension
@lexical/fast-check
@lexical/file
@lexical/hashtag
@lexical/headless
@lexical/history
@lexical/html
@lexical/internal
@lexical/link
@lexical/list
@lexical/mark
@lexical/markdown
@lexical/mdast
@lexical/offset
@lexical/overflow
@lexical/plain-text
@lexical/react
@lexical/rich-text
@lexical/selection
@lexical/table
@lexical/tailwind
@lexical/text
@lexical/utils
@lexical/y
@lexical/yjs
commit: |
There was a problem hiding this comment.
This looks good: $setBlocksType now fills an empty declared container from either end of the range, reading the active selection covers the clone that LexicalNode.replace installs when it converts the anchor's block first, and the new test fails without the fix. Everything below is optional and non-blocking.
1. Decide the fill on the passed selection (packages/lexical-selection/src/range-selection.ts:205-214)
Points of $getSelection() currently count toward whether the container is filled, not only toward moving points into the new block. So when a caller passes a selection that is not the active one (for example a range from the paragraph before an empty declared container to the paragraph after it) while the caret sits in that container, the container gets an empty heading and the caret moves into it. The same range passed as the active selection leaves the container empty, which matches the existing rule for a range that passes over an empty container and the changeset's "whichever end of the selection is in it". Deciding on the passed selection's points while still moving the active selection's keeps the clone handling and passes the existing BlockContainers and @lexical/selection suites:
const points: Point[] = [];
let inPassed = false;
for (const s of new Set([selection, $getSelection()])) {
if ($isRangeSelection(s)) {
for (const point of [s.anchor, s.focus]) {
if (point.key === prevNode.getKey()) {
points.push(point);
inPassed = inPassed || s === selection;
}
}
}
}
if (inPassed && prevNode.isEmpty()) {2. Cover the forward case (packages/lexical-extension/src/__tests__/unit/BlockContainers.test.ts:1426)
A forward range whose focus ends at offset 0 of the empty container changes with this PR as well (main leaves the container empty; this branch fills it), which is the intended "either end" behavior. A sibling of the new test with the anchor in before and the focus in the container would pin it.
3. Comment wording (range-selection.ts:198-200)
"(either end, in either direction), the new block goes in it, as with $insertList and $wrapNodes" reads as if those two also fill from either end; both still fill only when the anchor is in the container. Limiting the comparison to what they share would keep the comment accurate.
4. Changeset coverage for lexical
The Changesets check warns that packages/lexical/src/LexicalStructure.ts changed without a changeset. The edit there is JSDoc, which .changeset/README.md classes as docs; adding 'lexical': patch to .changeset/selection-set-blocks-type-backward-container.md clears the warning (see "Changesets" in CONTRIBUTING.md).
5. Docs wording
packages/lexical-website/docs/serialization/serialization.md (around line 679) still says "with the selection in an empty container"; the same "empty declared container" narrowing you made in the $isBlockContainer JSDoc would keep the two aligned.
| } | ||
| } | ||
| } | ||
| if (points.length > 0 && prevNode.isEmpty()) { |
There was a problem hiding this comment.
Optional: the fill is decided by points of the passed selection or the active one, so a caller that passes a non-active range which only passes over this empty container fills it whenever the caret sits in it, while the same range passed as the active selection leaves it empty. Deciding on the passed selection's points (inPassed = inPassed || s === selection) and still moving both keeps the clone handling; the review comment has a sketch.
| // (either end, in either direction), the new block goes in it, as | ||
| // with $insertList and $wrapNodes. The container stays, so its indent |
There was a problem hiding this comment.
Nit (optional): $insertList and $wrapNodes still fill only when the anchor is in the container, so "either end, in either direction ... as with" overstates the parity; consider limiting the comparison to the new block going in the container.
| ).toEqual(['heading:0', 'text:2']); | ||
| }); | ||
|
|
||
| test('$setBlocksType keeps a backward range that ends in the empty container', () => { |
There was a problem hiding this comment.
Optional: a forward sibling (anchor in before, focus at offset 0 of the empty box) also changes with this PR, since main leaves the box empty there, and would pin the either-end rule.
| @@ -0,0 +1,7 @@ | |||
| --- | |||
| '@lexical/selection': patch | |||
There was a problem hiding this comment.
Optional: the Changesets check also asks for lexical because of the $isBlockContainer JSDoc edit (docs per .changeset/README.md); adding 'lexical': patch here clears the warning.
Description
Follow-up to #9590, from the Meta code-review bot's last review there (approved, optional suggestions), which came after the merge:
$setBlocksTypefilled an empty declared container only when the selection's anchor was in it, so a backward range ending in the container left it empty. It now fills it when either point is in it, and moves those points into the new block, also on the active selection that a replacement earlier in the same call can make anew.$isBlockContainerJSDoc says that only an empty declared container gets the new block from$setBlocksType(an empty root or shadow root still does not, as before).Changeset
.changeset/selection-set-blocks-type-backward-container.md([fix],@lexical/selection).Test plan
Before
After