Skip to content

Commit

Permalink
Minor refactor on ListMaxIndentLevelPlugin (facebook#5886)
Browse files Browse the repository at this point in the history
  • Loading branch information
2wheeh authored Apr 17, 2024
1 parent 39e6f1f commit baf4f45
Showing 1 changed file with 9 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
*/

import type {RangeSelection} from 'lexical';
import type {ElementNode, RangeSelection} from 'lexical';

import {$getListDepth, $isListItemNode, $isListNode} from '@lexical/list';
import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
Expand All @@ -15,15 +15,10 @@ import {
$isElementNode,
$isRangeSelection,
COMMAND_PRIORITY_CRITICAL,
ElementNode,
INDENT_CONTENT_COMMAND,
} from 'lexical';
import {useEffect} from 'react';

type Props = Readonly<{
maxDepth: number | null | undefined;
}>;

function getElementNodesInSelection(
selection: RangeSelection,
): Set<ElementNode> {
Expand All @@ -41,7 +36,7 @@ function getElementNodesInSelection(
);
}

function isIndentPermitted(maxDepth: number): boolean {
function shouldPreventIndent(maxDepth: number): boolean {
const selection = $getSelection();

if (!$isRangeSelection(selection)) {
Expand Down Expand Up @@ -69,16 +64,20 @@ function isIndentPermitted(maxDepth: number): boolean {
}
}

return totalDepth <= maxDepth;
return totalDepth > maxDepth;
}

export default function ListMaxIndentLevelPlugin({maxDepth}: Props): null {
export default function ListMaxIndentLevelPlugin({
maxDepth = 7,
}: {
maxDepth?: number;
}): null {
const [editor] = useLexicalComposerContext();

useEffect(() => {
return editor.registerCommand(
INDENT_CONTENT_COMMAND,
() => !isIndentPermitted(maxDepth ?? 7),
() => shouldPreventIndent(maxDepth),
COMMAND_PRIORITY_CRITICAL,
);
}, [editor, maxDepth]);
Expand Down

0 comments on commit baf4f45

Please sign in to comment.