Skip to content

Commit 2c20051

Browse files
authored
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.
1 parent d91478c commit 2c20051

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

packages/richtext-lexical/src/features/indent/client/IndentPlugin.tsx

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,35 @@ export const IndentPlugin: PluginComponent<IndentFeatureProps> = ({ clientProps
2626
if (!editor || !disabledNodes?.length) {
2727
return
2828
}
29-
return editor.registerCommand(
30-
INDENT_CONTENT_COMMAND,
31-
() => {
32-
return $handleIndentAndOutdent((block) => {
33-
if (!disabledNodes.includes(block.getType())) {
34-
const indent = block.getIndent()
35-
block.setIndent(indent + 1)
29+
return mergeRegister(
30+
editor.registerCommand(
31+
INDENT_CONTENT_COMMAND,
32+
() => {
33+
return $handleIndentAndOutdent((block) => {
34+
if (!disabledNodes.includes(block.getType())) {
35+
const indent = block.getIndent()
36+
block.setIndent(indent + 1)
37+
}
38+
})
39+
},
40+
COMMAND_PRIORITY_LOW,
41+
),
42+
// If we disable indenting for certain nodes, we need to ensure that these are not indented,
43+
// if they get transformed from an indented state (e.g. an indented list node gets transformed into a
44+
// paragraph node for which indenting is disabled).
45+
editor.registerUpdateListener(({ dirtyElements, editorState }) => {
46+
editor.update(() => {
47+
for (const [nodeKey] of dirtyElements) {
48+
const node = editorState._nodeMap.get(nodeKey)
49+
if ($isElementNode(node) && disabledNodes.includes(node.getType())) {
50+
const currentIndent = node.getIndent()
51+
if (currentIndent > 0) {
52+
node.setIndent(0)
53+
}
54+
}
3655
}
3756
})
38-
},
39-
COMMAND_PRIORITY_LOW,
57+
}),
4058
)
4159
}, [editor, disabledNodes])
4260

0 commit comments

Comments
 (0)