forked from payloadcms/payload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
113 lines (105 loc) · 3.25 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
'use client'
import type { BaseSelection, ElementNode, LexicalNode } from 'lexical'
import { $findMatchingParent } from '@lexical/utils'
import {
$isElementNode,
$isRangeSelection,
INDENT_CONTENT_COMMAND,
OUTDENT_CONTENT_COMMAND,
} from 'lexical'
import type { ToolbarGroup } from '../../toolbars/types.js'
import { IndentDecreaseIcon } from '../../../lexical/ui/icons/IndentDecrease/index.js'
import { IndentIncreaseIcon } from '../../../lexical/ui/icons/IndentIncrease/index.js'
import { createClientFeature } from '../../../utilities/createClientFeature.js'
import { type IndentFeatureProps } from '../server/index.js'
import { IndentPlugin } from './IndentPlugin.js'
import { toolbarIndentGroupWithItems } from './toolbarIndentGroup.js'
const toolbarGroups = ({ disabledNodes }: IndentFeatureProps): ToolbarGroup[] => [
toolbarIndentGroupWithItems([
{
ChildComponent: IndentDecreaseIcon,
isActive: () => false,
isEnabled: ({ selection }) => {
const nodes = selection?.getNodes()
if (!nodes?.length) {
return false
}
let atLeastOneNodeCanOutdent = false
const isIndentable = (node: LexicalNode): node is ElementNode =>
$isElementNode(node) && node.canIndent()
for (const node of nodes) {
if (isIndentable(node)) {
if (node.getIndent() <= 0) {
return false
} else {
atLeastOneNodeCanOutdent = true
}
}
}
if (!atLeastOneNodeCanOutdent) {
if (
$pointsAncestorMatch(selection, (node) => isIndentable(node) && node.getIndent() > 0)
) {
return true
}
}
return atLeastOneNodeCanOutdent
},
key: 'indentDecrease',
label: ({ i18n }) => {
return i18n.t('lexical:indent:decreaseLabel')
},
onSelect: ({ editor }) => {
editor.dispatchCommand(OUTDENT_CONTENT_COMMAND, undefined)
},
order: 1,
},
{
ChildComponent: IndentIncreaseIcon,
isActive: () => false,
isEnabled: ({ selection }) => {
const nodes = selection?.getNodes()
if (!nodes?.length) {
return false
}
return !nodes.some((node) => disabledNodes?.includes(node.getType()))
},
key: 'indentIncrease',
label: ({ i18n }) => {
return i18n.t('lexical:indent:increaseLabel')
},
onSelect: ({ editor }) => {
editor.dispatchCommand(INDENT_CONTENT_COMMAND, undefined)
},
order: 2,
},
]),
]
export const IndentFeatureClient = createClientFeature<IndentFeatureProps>(({ props }) => {
const disabledNodes = props.disabledNodes ?? []
return {
plugins: [
{
Component: IndentPlugin,
position: 'normal',
},
],
sanitizedClientFeatureProps: props,
toolbarFixed: {
groups: toolbarGroups({ disabledNodes }),
},
toolbarInline: {
groups: toolbarGroups({ disabledNodes }),
},
}
})
function $pointsAncestorMatch(
selection: BaseSelection,
fn: (node: LexicalNode) => boolean,
): boolean {
return (
$isRangeSelection(selection) &&
(!!$findMatchingParent(selection.anchor.getNode(), fn) ||
!!$findMatchingParent(selection.focus.getNode(), fn))
)
}