/
helpers.tsx
208 lines (186 loc) · 5.41 KB
/
helpers.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import {
BlockElementIcon,
BoldIcon,
CodeIcon,
InlineElementIcon,
ItalicIcon,
LinkIcon,
OlistIcon,
StrikethroughIcon,
UnderlineIcon,
UnknownIcon,
UlistIcon,
} from '@sanity/icons'
import {
HotkeyOptions,
PortableTextEditor,
PortableTextMemberSchemaTypes,
} from '@sanity/portable-text-editor'
import {capitalize, get} from 'lodash'
import React from 'react'
import {ObjectSchemaType} from '@sanity/types'
import {BlockItem, BlockStyleItem, PTEToolbarAction, PTEToolbarActionGroup} from './types'
import {CustomIcon} from './CustomIcon'
function getPTEFormatActions(
editor: PortableTextEditor,
disabled: boolean,
hotkeyOpts: HotkeyOptions
): PTEToolbarAction[] {
const types = editor.schemaTypes
return types.decorators.map((decorator) => {
const shortCutKey = Object.keys(hotkeyOpts.marks || {}).find(
(key) => hotkeyOpts.marks?.[key] === decorator.value
)
let hotkeys: string[] = []
if (shortCutKey) {
hotkeys = [shortCutKey]
}
return {
type: 'format',
disabled: disabled,
icon: decorator?.icon,
key: decorator.value,
handle: (): void => {
PortableTextEditor.toggleMark(editor, decorator.value)
PortableTextEditor.focus(editor)
},
hotkeys,
title: decorator.title,
}
})
}
function getPTEListActions(editor: PortableTextEditor, disabled: boolean): PTEToolbarAction[] {
const types = editor.schemaTypes
return types.lists.map((listItem) => {
return {
type: 'listStyle',
key: listItem.value,
disabled: disabled,
icon: listItem?.icon,
handle: (): void => {
PortableTextEditor.toggleList(editor, listItem.value)
},
title: listItem.title,
}
})
}
function getAnnotationIcon(type: ObjectSchemaType): React.ComponentType | string | undefined {
return (
get(type, 'icon') ||
get(type, 'type.icon') ||
get(type, 'type.to.icon') ||
get(type, 'type.to[0].icon')
)
}
function getPTEAnnotationActions(
editor: PortableTextEditor,
disabled: boolean,
onInsert: (type: ObjectSchemaType) => void
): PTEToolbarAction[] {
const types = editor.schemaTypes
const focusChild = PortableTextEditor.focusChild(editor)
const hasText = focusChild && focusChild.text
return types.annotations.map((aType) => {
return {
type: 'annotation',
disabled: !hasText || disabled,
icon: getAnnotationIcon(aType),
key: aType.name,
handle: (active?: boolean): void => {
if (active) {
PortableTextEditor.removeAnnotation(editor, aType)
PortableTextEditor.focus(editor)
} else {
onInsert(aType)
}
},
title: aType.title || capitalize(aType.name),
}
})
}
export function getPTEToolbarActionGroups(
editor: PortableTextEditor,
disabled: boolean,
onInsertAnnotation: (type: ObjectSchemaType) => void,
hotkeyOpts: HotkeyOptions
): PTEToolbarActionGroup[] {
return [
{name: 'format', actions: getPTEFormatActions(editor, disabled, hotkeyOpts)},
{name: 'list', actions: getPTEListActions(editor, disabled)},
{name: 'annotation', actions: getPTEAnnotationActions(editor, disabled, onInsertAnnotation)},
]
}
export function getBlockStyles(types: PortableTextMemberSchemaTypes): BlockStyleItem[] {
return types.styles.map((style) => {
return {
key: `style-${style.value}`,
style: style.value,
styleComponent: style && style.component,
title: style.title,
}
})
}
function getInsertMenuIcon(
type: ObjectSchemaType,
fallbackIcon: React.ComponentType
): React.ComponentType {
const referenceIcon = get(type, 'to[0].icon')
return type.icon || (type.type && type.type.icon) || referenceIcon || fallbackIcon
}
export function getInsertMenuItems(
types: PortableTextMemberSchemaTypes,
disabled: boolean,
onInsertBlock: (type: ObjectSchemaType) => void,
onInsertInline: (type: ObjectSchemaType) => void
): BlockItem[] {
const blockItems = types.blockObjects.map(
(type, index): BlockItem => ({
handle: () => onInsertBlock(type),
icon: getInsertMenuIcon(type, BlockElementIcon),
inline: false,
key: `block-${index}`,
type,
})
)
const inlineItems = types.inlineObjects.map(
(type, index): BlockItem => ({
handle: () => onInsertInline(type),
icon: getInsertMenuIcon(type, InlineElementIcon),
inline: true,
key: `inline-${index}`,
type,
})
)
// Do not include items that are supposed to be hidden
const filteredBlockItems = blockItems.concat(inlineItems).filter((item) => !item.type?.hidden)
return filteredBlockItems
}
const annotationIcons: Record<string, React.ComponentType> = {
link: LinkIcon,
}
const formatIcons: Record<string, React.ComponentType> = {
strong: BoldIcon,
em: ItalicIcon,
'strike-through': StrikethroughIcon,
underline: UnderlineIcon,
code: CodeIcon,
}
const listStyleIcons: Record<string, React.ComponentType> = {
number: OlistIcon,
bullet: UlistIcon,
}
export function getActionIcon(action: PTEToolbarAction, active: boolean) {
if (action.icon) {
if (typeof action.icon === 'string') {
return <CustomIcon active={active} icon={action.icon} />
}
return action.icon
}
if (action.type === 'annotation') {
return annotationIcons[action.key] || UnknownIcon
}
if (action.type === 'listStyle') {
return listStyleIcons[action.key] || UnknownIcon
}
return formatIcons[action.key] || UnknownIcon
}