fix(richtext-lexical): reset indent on node transforms (#12183)

### What?
Resets the indentation on editor updates for nodes for which indentation
is disabled.

### Why?
If a node gets transformed, e.g. from a list to a paragraph node, it
remains the indent property by default. If indentation for this node is
disabled, it would remain indented although it shouldn't.

### How?
Adds a listener which resets the indent status on updates for
non-indentable nodes.
This commit is contained in:
Tobias Odendahl
2025-04-22 19:40:43 +02:00
committed by GitHub
parent d91478cd24
commit 2c20051dbf

View File

@@ -26,17 +26,35 @@ export const IndentPlugin: PluginComponent<IndentFeatureProps> = ({ clientProps
if (!editor || !disabledNodes?.length) { if (!editor || !disabledNodes?.length) {
return return
} }
return editor.registerCommand( return mergeRegister(
INDENT_CONTENT_COMMAND, editor.registerCommand(
() => { INDENT_CONTENT_COMMAND,
return $handleIndentAndOutdent((block) => { () => {
if (!disabledNodes.includes(block.getType())) { return $handleIndentAndOutdent((block) => {
const indent = block.getIndent() if (!disabledNodes.includes(block.getType())) {
block.setIndent(indent + 1) const indent = block.getIndent()
block.setIndent(indent + 1)
}
})
},
COMMAND_PRIORITY_LOW,
),
// If we disable indenting for certain nodes, we need to ensure that these are not indented,
// if they get transformed from an indented state (e.g. an indented list node gets transformed into a
// paragraph node for which indenting is disabled).
editor.registerUpdateListener(({ dirtyElements, editorState }) => {
editor.update(() => {
for (const [nodeKey] of dirtyElements) {
const node = editorState._nodeMap.get(nodeKey)
if ($isElementNode(node) && disabledNodes.includes(node.getType())) {
const currentIndent = node.getIndent()
if (currentIndent > 0) {
node.setIndent(0)
}
}
} }
}) })
}, }),
COMMAND_PRIORITY_LOW,
) )
}, [editor, disabledNodes]) }, [editor, disabledNodes])