From baf4f457804d3852c1bdc540090e1ad97fe777d5 Mon Sep 17 00:00:00 2001 From: wnhlee <40269597+2wheeh@users.noreply.github.com> Date: Thu, 18 Apr 2024 01:41:08 +0900 Subject: [PATCH] Minor refactor on ListMaxIndentLevelPlugin (#5886) --- .../plugins/ListMaxIndentLevelPlugin/index.ts | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/packages/lexical-playground/src/plugins/ListMaxIndentLevelPlugin/index.ts b/packages/lexical-playground/src/plugins/ListMaxIndentLevelPlugin/index.ts index b8f3b3ce976..3feb82b9f96 100644 --- a/packages/lexical-playground/src/plugins/ListMaxIndentLevelPlugin/index.ts +++ b/packages/lexical-playground/src/plugins/ListMaxIndentLevelPlugin/index.ts @@ -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'; @@ -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 { @@ -41,7 +36,7 @@ function getElementNodesInSelection( ); } -function isIndentPermitted(maxDepth: number): boolean { +function shouldPreventIndent(maxDepth: number): boolean { const selection = $getSelection(); if (!$isRangeSelection(selection)) { @@ -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]);