/
Toolbar.tsx
246 lines (222 loc) · 7.29 KB
/
Toolbar.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import {
HotkeyOptions,
usePortableTextEditor,
usePortableTextEditorSelection,
PortableTextEditor,
} from '@sanity/portable-text-editor'
import React, {memo, useCallback, useMemo, useState} from 'react'
import {ObjectSchemaType, Path, SchemaType} from '@sanity/types'
import {Box, Button, Flex, Hotkeys, Text, Tooltip, useElementRect, useToast} from '@sanity/ui'
import {CollapseIcon, ExpandIcon} from '@sanity/icons'
import styled, {css} from 'styled-components'
import {useRovingFocus} from '../../../../components'
import {useResolveInitialValueForType} from '../../../../store'
import {ActionMenu} from './ActionMenu'
import {BlockStyleSelect} from './BlockStyleSelect'
import {InsertMenu} from './InsertMenu'
import {getBlockStyles, getInsertMenuItems} from './helpers'
import {useActionGroups} from './hooks'
import {BlockItem, BlockStyleItem, PTEToolbarActionGroup} from './types'
interface ToolbarProps {
hotkeys: HotkeyOptions
isFullscreen: boolean
readOnly?: boolean
onMemberOpen: (relativePath: Path) => void
onToggleFullscreen: () => void
}
const RootFlex = styled(Flex)`
width: 100%;
`
const StyleSelectBox = styled(Box)`
width: 8em;
`
const StyleSelectFlex = styled(Flex)`
border-right: 1px solid var(--card-border-color);
`
const ActionMenuBox = styled(Box)<{$withInsertMenu: boolean}>`
${({$withInsertMenu}) =>
$withInsertMenu &&
css`
max-width: max-content;
border-right: 1px solid var(--card-border-color);
`}
`
const FullscreenButtonBox = styled(Box)`
border-left: 1px solid var(--card-border-color);
`
const SLOW_INITIAL_VALUE_LIMIT = 300
const IS_MAC =
typeof window != 'undefined' && /Mac|iPod|iPhone|iPad/.test(window.navigator.platform)
const InnerToolbar = memo(function InnerToolbar({
actionGroups,
blockStyles,
disabled,
insertMenuItems,
isFullscreen,
onToggleFullscreen,
}: {
actionGroups: PTEToolbarActionGroup[]
blockStyles: BlockStyleItem[]
disabled: boolean
insertMenuItems: BlockItem[]
isFullscreen: boolean
onToggleFullscreen: () => void
}) {
const actionsLen = actionGroups.reduce((acc, x) => acc + x.actions.length, 0)
const showActionMenu = actionsLen > 0
const showInsertMenu = insertMenuItems.length > 0
const [rootElement, setRootElement] = useState<HTMLDivElement | null>(null)
const rootElementRect = useElementRect(rootElement)
const collapsed = rootElementRect ? rootElementRect?.width < 400 : false
const showBlockStyleSelect = blockStyles.length > 1
useRovingFocus({
rootElement: rootElement,
})
return (
<RootFlex align="center" ref={setRootElement}>
{showBlockStyleSelect && (
<StyleSelectFlex flex={collapsed ? 1 : undefined}>
<StyleSelectBox padding={isFullscreen ? 2 : 1}>
<BlockStyleSelect disabled={disabled} items={blockStyles} />
</StyleSelectBox>
</StyleSelectFlex>
)}
<Flex flex={1}>
{showActionMenu && (
<ActionMenuBox
flex={collapsed ? undefined : 1}
padding={isFullscreen ? 2 : 1}
$withInsertMenu={showInsertMenu}
>
<ActionMenu
disabled={disabled}
collapsed={collapsed}
groups={actionGroups}
isFullscreen={isFullscreen}
/>
</ActionMenuBox>
)}
{showInsertMenu && (
<Box flex={collapsed ? undefined : 1} padding={isFullscreen ? 2 : 1}>
<InsertMenu
disabled={disabled}
collapsed={collapsed}
items={insertMenuItems}
isFullscreen={isFullscreen}
/>
</Box>
)}
</Flex>
<FullscreenButtonBox padding={isFullscreen ? 2 : 1}>
<Tooltip
content={
<Flex align="center" padding={1}>
<Box flex={1} paddingX={1}>
<Text size={1}>{`${isFullscreen ? 'Collapse' : 'Expand'} editor`}</Text>
</Box>
<Box marginLeft={1}>
<Hotkeys keys={[`${IS_MAC ? 'Cmd' : 'Ctrl'}`, 'Enter']} />
</Box>
</Flex>
}
placement={isFullscreen ? 'bottom' : 'top'}
portal="default"
>
<Button
padding={2}
icon={isFullscreen ? CollapseIcon : ExpandIcon}
mode="bleed"
onClick={onToggleFullscreen}
/>
</Tooltip>
</FullscreenButtonBox>
</RootFlex>
)
})
export function Toolbar(props: ToolbarProps) {
const {hotkeys, isFullscreen, readOnly, onMemberOpen, onToggleFullscreen} = props
const editor = usePortableTextEditor()
const selection = usePortableTextEditorSelection()
const resolveInitialValueForType = useResolveInitialValueForType()
const disabled = readOnly || !selection
const {push} = useToast()
const resolveInitialValue = useCallback(
(type: ObjectSchemaType) => {
let isSlow = false
const slowTimer = setTimeout(() => {
isSlow = true
push({
id: 'resolving-initial-value',
status: 'info',
title: 'Resolving initial value…',
})
}, SLOW_INITIAL_VALUE_LIMIT)
return resolveInitialValueForType(type as unknown as SchemaType, {})
.then((value) => {
if (isSlow) {
// I found no way to close an existing toast, so this will replace the message in the
// "Resolving initial value…"-toast and then make sure it gets closed.
push({
id: 'resolving-initial-value',
status: 'info',
duration: 500,
title: 'Initial value resolved',
})
}
return value
})
.catch((error) => {
push({
title: `Could not resolve initial value`,
id: 'resolving-initial-value',
description: `Unable to resolve initial value for type: ${type.name}: ${error.message}.`,
status: 'error',
})
return undefined
})
.finally(() => clearTimeout(slowTimer))
},
[push]
)
const handleInsertBlock = useCallback(
async (type: ObjectSchemaType) => {
const initialValue = await resolveInitialValue(type)
const path = PortableTextEditor.insertBlock(editor, type, initialValue)
if (path) {
onMemberOpen(path)
}
},
[editor, onMemberOpen, resolveInitialValue]
)
const handleInsertInline = useCallback(
async (type: ObjectSchemaType) => {
const initialValue = await resolveInitialValue(type)
const path = PortableTextEditor.insertChild(editor, type, initialValue)
if (path) {
onMemberOpen(path)
}
},
[editor, onMemberOpen, resolveInitialValue]
)
const actionGroups = useActionGroups({
hotkeys,
onMemberOpen,
resolveInitialValue,
disabled: true,
})
const blockStyles = useMemo(() => getBlockStyles(editor.schemaTypes), [editor])
const insertMenuItems = useMemo(
() => getInsertMenuItems(editor.schemaTypes, disabled, handleInsertBlock, handleInsertInline),
[disabled, editor, handleInsertBlock, handleInsertInline]
)
return (
<InnerToolbar
actionGroups={actionGroups}
blockStyles={blockStyles}
disabled={disabled}
insertMenuItems={insertMenuItems}
isFullscreen={isFullscreen}
onToggleFullscreen={onToggleFullscreen}
/>
)
}