Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 58 additions & 2 deletions packages/lexical-extension/src/TabIndentationExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ import {
COMMAND_PRIORITY_CRITICAL,
COMMAND_PRIORITY_EDITOR,
defineExtension,
type ElementNode,
ElementNode,
INDENT_CONTENT_COMMAND,
INSERT_TAB_COMMAND,
KEY_TAB_COMMAND,
type Klass,
type LexicalCommand,
type LexicalEditor,
mergeRegister,
OUTDENT_CONTENT_COMMAND,
type RangeSelection,
RootNode,
safeCast,
} from 'lexical';

Expand Down Expand Up @@ -70,10 +72,42 @@ function $defaultCanIndent(node: ElementNode) {
return node.canIndent();
}

/**
* Indentation from anywhere other than the indent command must stay below
* maxIndent too (#5170), so clamp it with a transform on every block element
* type that may be indented. This applies to all content, not only pastes:
* the initial or loaded editor state, `setEditorState`, collaboration
* updates and nodes inserted from code. Registering marks every element
* dirty, so the whole document is checked (and clamped) once at that point.
*/
function registerMaxIndentTransforms(
editor: LexicalEditor,
maxIndent: number,
$getCanIndent: () => CanIndentPredicate,
): () => void {
const $clampIndent = (node: ElementNode) => {
if (node.getIndent() >= maxIndent && $getCanIndent()(node)) {
node.setIndent(maxIndent - 1);
}
};
const removers: (() => void)[] = [];
for (const {klass} of editor._nodes.values()) {
if (klass.prototype instanceof ElementNode && klass !== RootNode) {
removers.push(
editor.registerNodeTransform(klass as Klass<ElementNode>, $clampIndent),
);
}
}
return mergeRegister(...removers);
}

/**
* Registers a `KEY_TAB_COMMAND` handler that makes Tab and Shift+Tab indent and
* outdent block elements (and otherwise insert a tab). Pass `maxIndent` to cap
* the indent depth and `$canIndent` to control which elements may be indented.
* the indent depth (indents below `maxIndent`), and `$canIndent` to control
* which elements may be indented. The `maxIndent` cap applies to any content,
* including pastes, loaded state, `setEditorState` and collaboration updates;
* deeper indents already in the document are clamped once on registration.
*
* @returns A cleanup function that unregisters the handler.
*/
Expand All @@ -84,7 +118,29 @@ export function registerTabIndentation(
| CanIndentPredicate
| ReadonlySignal<CanIndentPredicate> = $defaultCanIndent,
) {
const $getCanIndent = () =>
typeof $canIndent === 'function' ? $canIndent : $canIndent.peek();
let unregisterClamp: () => void;
if (typeof maxIndent === 'number') {
unregisterClamp = maxIndent
? registerMaxIndentTransforms(editor, maxIndent, $getCanIndent)
: () => {};
} else if (maxIndent) {
unregisterClamp = effect(() => {
const currentMaxIndent = maxIndent.value;
if (currentMaxIndent) {
return registerMaxIndentTransforms(
editor,
currentMaxIndent,
$getCanIndent,
);
}
});
} else {
unregisterClamp = () => {};
}
return mergeRegister(
unregisterClamp,
editor.registerCommand(
KEY_TAB_COMMAND,
event => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
/**
* 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.
*
*/

import {$insertDataTransferForRichText} from '@lexical/clipboard';
import {HistoryExtension} from '@lexical/history';
import {$isListItemNode, ListExtension} from '@lexical/list';
import {RichTextExtension} from '@lexical/rich-text';
import {
$createParagraphNode,
$createTextNode,
$getRoot,
$getSelection,
$isElementNode,
$isParagraphNode,
configExtension,
defineExtension,
type ElementNode,
type LexicalEditor,
type LexicalNode,
UNDO_COMMAND,
} from 'lexical';
import {assert, describe, expect, test} from 'vitest';

import {buildEditorFromExtensions, TabIndentationExtension} from '../../index';

// Pasted content must respect maxIndent too, not only the indent command
// (#5170). Indent command caps indentation at maxIndent - 1.
function createEditor(
maxIndent: null | number,
$canIndent?: (node: ElementNode) => boolean,
) {
return buildEditorFromExtensions(
defineExtension({
dependencies: [
RichTextExtension,
ListExtension,
HistoryExtension,
configExtension(
TabIndentationExtension,
$canIndent ? {$canIndent, maxIndent} : {maxIndent},
),
],
name: 'IndentationLimitsPaste',
}),
);
}

function paste(editor: LexicalEditor, html: string) {
editor.update(
() => {
const paragraph = $createParagraphNode();
$getRoot().clear().append(paragraph);
paragraph.select();
},
{discrete: true},
);
editor.update(
() => {
const selection = $getSelection();
assert(selection !== null, 'Expected a selection');
const dataTransfer = {
getData: (type: string) => (type === 'text/html' ? html : ''),
types: ['text/html'],
} as unknown as DataTransfer;
$insertDataTransferForRichText(dataTransfer, selection, editor);
},
{discrete: true},
);
}

function $blocks(
node: LexicalNode = $getRoot(),
): {indent: number; text: string; type: string}[] {
if ($isListItemNode(node) && $isElementNode(node.getFirstChild())) {
return node.getChildren().flatMap(child => $blocks(child));
}
if (
$isElementNode(node) &&
!node.isInline() &&
node
.getChildren()
.every(child => !$isElementNode(child) || child.isInline())
) {
return node.getTextContent() === ''
? []
: [
{
indent: node.getIndent(),
text: node.getTextContent(),
type: node.getType(),
},
];
}
return $isElementNode(node)
? node.getChildren().flatMap(child => $blocks(child))
: [];
}

function nestedListHTML(depth: number): string {
let html = '';
for (let i = depth; i >= 0; i--) {
html = `<ul><li>level ${i}</li>${html ? `<li>${html}</li>` : ''}</ul>`;
}
return html;
}

const indentedBlocksHTML =
'<p data-lexical-indent="2">two</p>' +
'<p data-lexical-indent="10">ten</p>' +
'<h1 data-lexical-indent="9">heading</h1>';

describe('TabIndentationExtension maxIndent on paste (#5170)', () => {
test('clamps an over-indented initial editor state', () => {
using editor = buildEditorFromExtensions(
defineExtension({
$initialEditorState: () => {
$getRoot().append(
$createParagraphNode().setIndent(1).append($createTextNode('one')),
$createParagraphNode().setIndent(9).append($createTextNode('nine')),
);
},
dependencies: [
RichTextExtension,
configExtension(TabIndentationExtension, {maxIndent: 4}),
],
name: 'IndentationLimitsInitialState',
}),
);
expect(editor.read(() => $blocks())).toEqual([
{indent: 1, text: 'one', type: 'paragraph'},
{indent: 3, text: 'nine', type: 'paragraph'},
]);
});

test('clamps an over-indented state set with setEditorState', () => {
using editor = createEditor(4);
const json = JSON.stringify({
root: {
children: [
{
children: [
{
detail: 0,
format: 0,
mode: 'normal',
style: '',
text: 'deep',
type: 'text',
version: 1,
},
],
direction: null,
format: '',
indent: 7,
type: 'paragraph',
version: 1,
},
],
direction: null,
format: '',
indent: 0,
type: 'root',
version: 1,
},
});
editor.setEditorState(editor.parseEditorState(json));
expect(editor.read(() => $blocks())).toEqual([
{indent: 3, text: 'deep', type: 'paragraph'},
]);
});

test('clamps a pasted nested list without losing items', () => {
using editor = createEditor(4);
paste(editor, nestedListHTML(6));
expect(editor.read(() => $blocks())).toEqual(
[0, 1, 2, 3, 3, 3, 3].map((indent, i) => ({
indent,
text: `level ${i}`,
type: 'listitem',
})),
);
});

test('clamps pasted indented paragraphs and headings', () => {
using editor = createEditor(4);
paste(editor, indentedBlocksHTML);
expect(editor.read(() => $blocks())).toEqual([
{indent: 2, text: 'two', type: 'paragraph'},
{indent: 3, text: 'ten', type: 'paragraph'},
{indent: 3, text: 'heading', type: 'heading'},
]);
});

test('leaves pasted indentation alone when maxIndent is null', () => {
using editor = createEditor(null);
paste(editor, nestedListHTML(6) + indentedBlocksHTML);
expect(editor.read(() => $blocks().map(({indent}) => indent))).toEqual([
0, 1, 2, 3, 4, 5, 6, 2, 10, 9,
]);
});

test('only clamps nodes that the $canIndent predicate allows to indent', () => {
using editor = createEditor(4, node => !$isParagraphNode(node));
paste(editor, indentedBlocksHTML);
expect(editor.read(() => $blocks().map(({indent}) => indent))).toEqual([
2, 10, 3,
]);
});

test('clamps in the same history entry as the paste', () => {
using editor = createEditor(4);
paste(editor, indentedBlocksHTML);
editor.dispatchCommand(UNDO_COMMAND, undefined);
expect(editor.read(() => $getRoot().getTextContent())).toBe('');
});
});
17 changes: 14 additions & 3 deletions packages/lexical-react/src/LexicalTabIndentationPlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import {
registerTabIndentation,
} from '@lexical/extension';
import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
import {useEffect} from 'react';
import {useEffect, useRef} from 'react';

import useLayoutEffect from './shared/useLayoutEffect';

export {registerTabIndentation};

Expand All @@ -35,9 +37,18 @@ export function TabIndentationPlugin({
$canIndent?: CanIndentPredicate;
}): null {
const [editor] = useLexicalComposerContext();
// Registering re-checks the whole document against maxIndent, so keep the
// latest $canIndent in a ref instead of re-registering when it changes.
const $canIndentRef = useRef($canIndent);
useLayoutEffect(() => {
$canIndentRef.current = $canIndent;
});
useEffect(() => {
return registerTabIndentation(editor, maxIndent, $canIndent);
}, [editor, maxIndent, $canIndent]);
return registerTabIndentation(editor, maxIndent, node => {
const $currentCanIndent = $canIndentRef.current;
return $currentCanIndent ? $currentCanIndent(node) : node.canIndent();
});
}, [editor, maxIndent]);

return null;
}
Loading
Loading