Skip to content

[lexical-extension][lexical-react] Bug Fix: Enforce TabIndentationExtension maxIndent on all content, including pastes - #9426

Open
Vedthakar wants to merge 3 commits into
meta:mainfrom
Vedthakar:fix/5170-max-indent-paste
Open

Vedthakar wants to merge 3 commits into
meta:mainfrom
Vedthakar:fix/5170-max-indent-paste

Conversation

@Vedthakar

Copy link
Copy Markdown
Contributor

Description

This solves issue #5170: pasted content can go deeper than maxIndent.

Builds on #6718 by @mdmuhtasimfuadfahim, reworked with node transforms as @etrepum suggested there.

Current behavior.

  • TabIndentationExtension (and registerTabIndentation) only enforces maxIndent in its INDENT_CONTENT_COMMAND handler (newIndent < maxIndent).
  • Content that arrives any other way keeps its indentation:
    • a pasted nested list keeps its full depth;
    • a pasted paragraph or heading keeps its data-lexical-indent / padding-inline-start.
  • The playground, with maxIndent: 7, accepts a pasted list nested 10 levels deep.

What this changes. The limit is enforced with node transforms rather than commands. When maxIndent is set, registerTabIndentation registers a transform on every registered ElementNode class except RootNode (it walks editor._nodes, like NormalizeInlineElementsExtension). The transform:

  • Calls setIndent(maxIndent - 1) when a node's getIndent() is >= maxIndent and the $canIndent predicate allows it to indent. That is the same bound the indent command uses.
  • Moves list items up with ListItemNode.setIndent(), which outdents through $handleOutdent, so no item or text is lost. Items deeper than the limit end up as siblings at the deepest allowed level.
  • Can't loop: after clamping, the node's indent is below the limit, so the transform does nothing on its next pass.
  • Runs in the same update as the paste, so it is part of the same history entry.
  • Is not registered when maxIndent is null (or 0, which the command handler also treats as no limit). For a signal, the transforms are registered and removed with an effect as the value changes.
  • Leaves Tab / Shift+Tab and the indent command unchanged.

Scope: all content, not only pastes. Because these are transforms, the cap applies to any content that ends up in the document. That includes:

  • the initial or loaded editor state;
  • setEditorState;
  • collaboration / remote updates;
  • nodes inserted from code.

Registering also marks every element dirty, so the whole document is checked once at registration. An existing document saved with a deeper indent is therefore clamped when it is loaded with maxIndent set. This is documented in the JSDoc of the transform helper and of registerTabIndentation.

Legacy plugin. Since each registration now re-checks the whole document, TabIndentationPlugin keeps $canIndent in a ref and passes a stable predicate. The effect now depends only on [editor, maxIndent], so an inline $canIndent arrow no longer re-registers on every render. The latest predicate is still the one used (it is stored in a ref set from useLayoutEffect). Changing $canIndent no longer re-clamps existing content: the new predicate applies the next time a node is transformed.

Closes #5170

Test plan

New packages/lexical-extension/src/__tests__/unit/IndentationLimitsPaste.test.ts (maxIndent: 4):

  • "clamps an over-indented initial editor state": indents 1, 9 from $initialEditorState become 1, 3.
  • "clamps an over-indented state set with setEditorState": an indent of 7 becomes 3.
  • "clamps a pasted nested list without losing items": a list 7 levels deep comes out as indents 0, 1, 2, 3, 3, 3, 3, with every item's text kept.
  • "clamps pasted indented paragraphs and headings": indents 2, 10, 9 become 2, 3, 3.
  • "only clamps nodes that the $canIndent predicate allows to indent": with a predicate that excludes paragraphs, only the heading is clamped.
  • "leaves pasted indentation alone when maxIndent is null": a control. Indents are kept as pasted.
  • "clamps in the same history entry as the paste": a control. One undo removes the whole paste.

New packages/lexical-react/src/__tests__/unit/LexicalTabIndentationPlugin.test.tsx:

  • "does not re-register when an inline $canIndent changes": re-rendering with a new predicate calls registerNodeTransform zero times, and the new predicate is the one applied.
  • "clamps with the default predicate": no $canIndent prop is passed.

The existing IndentationLimits.test.ts (Tab / indent command) passes unchanged.

Before

On main with the tests applied and the source change reverted:

  × clamps an over-indented initial editor state
  × clamps an over-indented state set with setEditorState
  × clamps a pasted nested list without losing items
  × clamps pasted indented paragraphs and headings
  × only clamps nodes that the $canIndent predicate allows to indent
  × clamps with the default predicate
  Tests  6 failed | 3 passed (9)

With the plugin from before the ref change, "does not re-register when an inline $canIndent changes" fails.

After

On current main:

  • IndentationLimitsPaste.test.ts and LexicalTabIndentationPlugin.test.tsx: 9 passed (6 fail on main). Chromium e2e: Indentation.spec.mjs, Tab.spec.mjs and List.spec.mjs pass in full.
  • pnpm run test-unit: all 9,580 tests pass
  • tsc, pnpm run flow (0 errors), and eslint/prettier on the changed files: clean

Author: Ved Thakar (@Vedthakar)

🤖 Generated with Claude Code

@vercel

vercel Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
lexical Ready Ready Preview Nov 16, 2026 11:56pm UTC
lexical-playground Ready Ready Preview Nov 16, 2026 11:56pm UTC

Request Review

@pkg-pr-new

pkg-pr-new Bot commented Oct 21, 2026 •

Copy link
Copy Markdown
lexical

npm i https://pkg.pr.new/lexical@9426

@lexical/a11y

npm i https://pkg.pr.new/@lexical/a11y@9426

@lexical/clipboard

npm i https://pkg.pr.new/@lexical/clipboard@9426

@lexical/code

npm i https://pkg.pr.new/@lexical/code@9426

@lexical/code-core

npm i https://pkg.pr.new/@lexical/code-core@9426

@lexical/code-prism

npm i https://pkg.pr.new/@lexical/code-prism@9426

@lexical/code-shiki

npm i https://pkg.pr.new/@lexical/code-shiki@9426

@lexical/compiler

npm i https://pkg.pr.new/@lexical/compiler@9426

@lexical/devtools-core

npm i https://pkg.pr.new/@lexical/devtools-core@9426

@lexical/dragon

npm i https://pkg.pr.new/@lexical/dragon@9426

@lexical/eslint-plugin

npm i https://pkg.pr.new/@lexical/eslint-plugin@9426

@lexical/extension

npm i https://pkg.pr.new/@lexical/extension@9426

@lexical/fast-check

npm i https://pkg.pr.new/@lexical/fast-check@9426

@lexical/file

npm i https://pkg.pr.new/@lexical/file@9426

@lexical/hashtag

npm i https://pkg.pr.new/@lexical/hashtag@9426

@lexical/headless

npm i https://pkg.pr.new/@lexical/headless@9426

@lexical/history

npm i https://pkg.pr.new/@lexical/history@9426

@lexical/html

npm i https://pkg.pr.new/@lexical/html@9426

@lexical/internal

npm i https://pkg.pr.new/@lexical/internal@9426

@lexical/link

npm i https://pkg.pr.new/@lexical/link@9426

@lexical/list

npm i https://pkg.pr.new/@lexical/list@9426

@lexical/mark

npm i https://pkg.pr.new/@lexical/mark@9426

@lexical/markdown

npm i https://pkg.pr.new/@lexical/markdown@9426

@lexical/mdast

npm i https://pkg.pr.new/@lexical/mdast@9426

@lexical/offset

npm i https://pkg.pr.new/@lexical/offset@9426

@lexical/overflow

npm i https://pkg.pr.new/@lexical/overflow@9426

@lexical/plain-text

npm i https://pkg.pr.new/@lexical/plain-text@9426

@lexical/react

npm i https://pkg.pr.new/@lexical/react@9426

@lexical/rich-text

npm i https://pkg.pr.new/@lexical/rich-text@9426

@lexical/selection

npm i https://pkg.pr.new/@lexical/selection@9426

@lexical/table

npm i https://pkg.pr.new/@lexical/table@9426

@lexical/tailwind

npm i https://pkg.pr.new/@lexical/tailwind@9426

@lexical/text

npm i https://pkg.pr.new/@lexical/text@9426

@lexical/utils

npm i https://pkg.pr.new/@lexical/utils@9426

@lexical/y

npm i https://pkg.pr.new/@lexical/y@9426

@lexical/yjs

npm i https://pkg.pr.new/@lexical/yjs@9426

commit: 34faa81

@rootnode-bot rootnode-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for picking up #5170 and building on the node-transform approach suggested in #6718. This looks good to me: the clamp converges, runs inside the paste's own update (so one undo removes the paste and the clamp together), and the new tests fail on main and pass with the change. The suggestions below are optional and non-blocking.

  1. The JSDoc says more than the code does (packages/lexical-extension/src/TabIndentationExtension.ts, lines 79-80 and 108-110). @lexical/yjs applies remote collaboration changes with skipTransforms: true (packages/lexical-yjs/src/SyncEditorStates.ts, lines 234, 542 and 570), so a remote paragraph at indent 9 stays at 9 on a peer with maxIndent: 4. Likewise, setEditorState re-runs element transforms only for a state that came from parseEditorState. Suggested wording: the cap applies to local edits, pastes, the initial state and parsed states passed to setEditorState; remote collaboration updates are not clamped. Leaving remote changes alone looks like the safer behaviour, so only the comments (and the PR description) need to change.

  2. Over-deep sublists of another list type lose their type and checked state (line 90, through ListItemNode.setIndent and $handleOutdent). A check list nested past the limit under a bullet list comes out as plain bullets with checked cleared, and an <ol> under a <ul> becomes bullets. Text and order are kept, and Shift+Tab does the same thing to the same item, but here it happens on paste and when a stored document is opened. A sentence in the registerTabIndentation JSDoc and a test that pins this result would make it an explicit choice.

  3. Nit: register the transforms untracked (lines 129-138). registerNodeTransform marks nodes dirty with an editor.update that runs every element transform synchronously inside this effect, so a transform (or a $canIndent) that reads someSignal.value makes the effect re-register, and re-mark the whole document dirty, whenever that signal changes. Wrapping the call in untracked(() => registerMaxIndentTransforms(...)) (already re-exported from ./signals) avoids that.

  4. Nit: test plan output. AGENTS.md ("Commits and Pull Requests") asks for the real output of the command under ### After; the Before block is pasted, but the After run is only described.

): () => void {
const $clampIndent = (node: ElementNode) => {
if (node.getIndent() >= maxIndent && $getCanIndent()(node)) {
node.setIndent(maxIndent - 1);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: for a list item this goes through ListItemNode.setIndent and $handleOutdent, which moves the item into the parent list. A check list (or <ol>) nested past the limit under a bullet list therefore comes out as bullets with checked cleared. Text and order survive, and Shift+Tab behaves the same, but it would be worth a sentence in the JSDoc and a test so it is an explicit choice.

Vedthakar and others added 3 commits November 16, 2026 18:54
…ension maxIndent on all content, including pastes

maxIndent was only enforced by the INDENT_CONTENT_COMMAND handler, so a
paste could bring in nested lists and indented blocks deeper than the
limit. When maxIndent is set, register a transform on every element type
that clamps indentation that the $canIndent predicate allows to below
maxIndent, the same bound the indent command uses.

Closes meta#5170

Signed-off-by: Ved Thakar <111605341+Vedthakar@users.noreply.github.com>
Co-authored-by: Ved Thakar <111605341+Vedthakar@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…orrect the JSDoc

Remote collaboration updates skip transforms and setEditorState only
re-runs them for a parsed state, so say so. Register the transforms
untracked so a signal read by a transform does not re-register them.

Co-authored-by: Ved Thakar <111605341+Vedthakar@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-authored-by: Ved Thakar <111605341+Vedthakar@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@Vedthakar

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Taken in the latest push:

  • The JSDoc now says the cap applies to local edits, pastes, the initial state and parsed states passed to setEditorState, and that remote collaboration updates are not clamped.
  • The transforms are registered inside untracked.

I left the sublist type change on outdent as is, since Shift+Tab does the same to the same item. Also rebased onto main and added a changeset.

— Ved Thakar

@rootnode-bot rootnode-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for picking up #5170 and building on the node-transform approach suggested in #6718. This looks good to me. The clamp converges and runs inside the paste's own update, so one undo removes the paste and the clamp together. The new tests fail on main and pass with the change. The last round's points are addressed: the JSDoc now says remote collaboration updates and unparsed setEditorState states are not clamped, and the registration runs untracked. Everything below is optional and non-blocking.

  1. Over-deep sublists of another list type lose their type and checked state (packages/lexical-extension/src/TabIndentationExtension.ts, line 91). For a list item, setIndent goes through $handleOutdent, which moves the item into the parent list whatever its type. So a check list nested past the limit under a bullet list comes out as plain bullets with checked cleared, and a numbered sublist becomes bullets too. Text and order are kept, and Shift+Tab does the same thing, but here it happens on paste and when a saved document is opened. A sentence in the registerTabIndentation JSDoc and a test that pins this result would make it an explicit choice.

  2. Add a Verify: paragraph to the changeset (.changeset/max-indent-clamps-pasted-content.md). CONTRIBUTING.md ("Changesets") and .changeset/README.md ("Behavior changes") ask for one when a fix changes observable behaviour, and this changes what pasted and loaded documents contain. Something like: "Verify: with maxIndent set, paste a list nested deeper than the limit, or open a saved document that has one, and check it stops at maxIndent - 1; a check or numbered sublist past the limit joins its parent list."

  3. Collaboration: the first local edit of a remote block can clamp it (same file, lines 80-81 and 110-111). Remote changes skip transforms, as the JSDoc now says. But a remote block deeper than this peer's limit is clamped as soon as a local update intentionally dirties it, for example by changing its alignment, though not by typing in it. That clamp then syncs to the other peers. The same happens document-wide when maxIndent is registered after shared content has loaded. It only ever lowers indents, so peers converge. A short note in the JSDoc would help collaborative integrators expect it.

  4. Small description and docs drift. The PR description's "Scope" list still names "collaboration / remote updates". The comment in packages/lexical-website/docs/extensions/signals.md (lines 106-108) says registerTabIndentation only peeks at maxIndent, but it now subscribes to the signal to re-register the transforms.

  5. Test plan output. AGENTS.md ("Commits and Pull Requests") asks for the real output of the command under ### After. The Before block is pasted, but the After run is only described.

Automated review (maintainer link)

'@lexical/react': patch
---

[fix] `maxIndent` of `TabIndentationExtension` and `TabIndentationPlugin` also limits indentation that arrives by paste, in the initial state or from code, not only by the Tab key

Copy link
Copy Markdown

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 .changeset/README.md ("Behavior changes") ask for a Verify: paragraph on a fix that changes observable behaviour. For example:

Verify: with `maxIndent` set, paste a list nested deeper than the limit, or open a saved document that has one, and check it stops at `maxIndent - 1`; a check or numbered sublist past the limit joins its parent list.

* the indent depth (indents below `maxIndent`), and `$canIndent` to control
* which elements may be indented. The `maxIndent` cap applies to local edits,
* pastes, the initial state and parsed states passed to `setEditorState`;
* remote collaboration updates are not clamped. Deeper indents already in the

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: remote updates skip transforms, but a remote block deeper than this peer's limit is clamped as soon as a local update intentionally dirties it, for example an alignment change, though not typing. That clamp then syncs to the other peers, and registering maxIndent after shared content has loaded clamps the whole document the same way. It only lowers indents, so peers converge; a sentence here would help collaborative integrators expect it.

This branch was successfully deployed

2 active deployments
Preview – lexical — 34faa818 Deployed Nov 16, 2026 by vercel[bot]
Preview – lexical-playground — 34faa818 Deployed Nov 16, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: ListMaxIndentLevelPlugin allows indent beyond maxDepth on paste

1 participant