Skip to content

Commit dd80f52

Browse files
authored
fix(richtext-lexical): make the toolbar indent button consider the disabledNodes property on IndentFeature (#11739)
Fixes #11677
1 parent 975bbb7 commit dd80f52

File tree

1 file changed

+45
-23
lines changed
  • packages/richtext-lexical/src/features/indent/client

1 file changed

+45
-23
lines changed

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

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client'
22

3-
import type { ElementNode, LexicalNode } from 'lexical'
3+
import type { BaseSelection, ElementNode, LexicalNode } from 'lexical'
44

55
import { $findMatchingParent } from '@lexical/utils'
66
import {
@@ -15,10 +15,11 @@ import type { ToolbarGroup } from '../../toolbars/types.js'
1515
import { IndentDecreaseIcon } from '../../../lexical/ui/icons/IndentDecrease/index.js'
1616
import { IndentIncreaseIcon } from '../../../lexical/ui/icons/IndentIncrease/index.js'
1717
import { createClientFeature } from '../../../utilities/createClientFeature.js'
18+
import { type IndentFeatureProps } from '../server/index.js'
1819
import { IndentPlugin } from './IndentPlugin.js'
1920
import { toolbarIndentGroupWithItems } from './toolbarIndentGroup.js'
2021

21-
const toolbarGroups: ToolbarGroup[] = [
22+
const toolbarGroups = ({ disabledNodes }: IndentFeatureProps): ToolbarGroup[] => [
2223
toolbarIndentGroupWithItems([
2324
{
2425
ChildComponent: IndentDecreaseIcon,
@@ -40,16 +41,10 @@ const toolbarGroups: ToolbarGroup[] = [
4041
}
4142
}
4243
}
43-
if (!atLeastOneNodeCanOutdent && $isRangeSelection(selection)) {
44-
const anchorNode = selection.anchor.getNode()
45-
if (
46-
$findMatchingParent(anchorNode, (node) => isIndentable(node) && node.getIndent() > 0)
47-
) {
48-
return true
49-
}
50-
const focusNode = selection.focus.getNode()
44+
45+
if (!atLeastOneNodeCanOutdent) {
5146
if (
52-
$findMatchingParent(focusNode, (node) => isIndentable(node) && node.getIndent() > 0)
47+
$pointsAncestorMatch(selection, (node) => isIndentable(node) && node.getIndent() > 0)
5348
) {
5449
return true
5550
}
@@ -68,6 +63,18 @@ const toolbarGroups: ToolbarGroup[] = [
6863
{
6964
ChildComponent: IndentIncreaseIcon,
7065
isActive: () => false,
66+
isEnabled: ({ selection }) => {
67+
const nodes = selection?.getNodes()
68+
if (!nodes?.length) {
69+
return false
70+
}
71+
if (nodes.some((node) => disabledNodes?.includes(node.getType()))) {
72+
return false
73+
}
74+
return !$pointsAncestorMatch(selection, (node) =>
75+
(disabledNodes ?? []).includes(node.getType()),
76+
)
77+
},
7178
key: 'indentIncrease',
7279
label: ({ i18n }) => {
7380
return i18n.t('lexical:indent:increaseLabel')
@@ -80,17 +87,32 @@ const toolbarGroups: ToolbarGroup[] = [
8087
]),
8188
]
8289

83-
export const IndentFeatureClient = createClientFeature({
84-
plugins: [
85-
{
86-
Component: IndentPlugin,
87-
position: 'normal',
90+
export const IndentFeatureClient = createClientFeature<IndentFeatureProps>(({ props }) => {
91+
const disabledNodes = props.disabledNodes ?? []
92+
return {
93+
plugins: [
94+
{
95+
Component: IndentPlugin,
96+
position: 'normal',
97+
},
98+
],
99+
sanitizedClientFeatureProps: props,
100+
toolbarFixed: {
101+
groups: toolbarGroups({ disabledNodes }),
88102
},
89-
],
90-
toolbarFixed: {
91-
groups: toolbarGroups,
92-
},
93-
toolbarInline: {
94-
groups: toolbarGroups,
95-
},
103+
toolbarInline: {
104+
groups: toolbarGroups({ disabledNodes }),
105+
},
106+
}
96107
})
108+
109+
function $pointsAncestorMatch(
110+
selection: BaseSelection,
111+
fn: (node: LexicalNode) => boolean,
112+
): boolean {
113+
return (
114+
$isRangeSelection(selection) &&
115+
(!!$findMatchingParent(selection.anchor.getNode(), fn) ||
116+
!!$findMatchingParent(selection.focus.getNode(), fn))
117+
)
118+
}

0 commit comments

Comments
 (0)