Skip to content

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

Closed
Vedthakar wants to merge 1 commit into
mainfrom
fix/5170-max-indent-paste
Closed

Vedthakar wants to merge 1 commit into
mainfrom
fix/5170-max-indent-paste

Conversation

@Vedthakar

@Vedthakar Vedthakar commented Oct 17, 2026 •

Copy link
Copy Markdown
Owner

Description

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

Builds on meta#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 meta#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

  test-unit: 388 files, 9386 passed | 1 skipped
  tsc, flow, eslint, prettier: clean
  e2e (chromium) Tab, Indentation, List: 47 passed

Author: Ved Thakar (@Vedthakar)

@Vedthakar Vedthakar changed the title [lexical-extension] Bug Fix: Enforce TabIndentationExtension maxIndent on pasted content [lexical-extension] Bug Fix: Enforce TabIndentationExtension maxIndent on all content, including pastes Oct 17, 2026
…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>
@Vedthakar
Vedthakar force-pushed the fix/5170-max-indent-paste branch from 1d51f6d to ff08343 Compare October 21, 2026 21:37
@Vedthakar

Copy link
Copy Markdown
Owner Author

Superseded by meta#9426.

@Vedthakar Vedthakar closed this Oct 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: ListMaxIndentLevelPlugin allows indent beyond maxDepth on paste

1 participant