-
Notifications
You must be signed in to change notification settings - Fork 392
/
DocumentPane.tsx
331 lines (302 loc) · 10.1 KB
/
DocumentPane.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import {
unstable_useTemplatePermissions as useUnstableTemplatePermissions,
useDocumentType,
} from '@sanity/base/hooks'
import {useZIndex} from '@sanity/base/components'
import {ChangeConnectorRoot} from '@sanity/base/change-indicators'
import {getTemplateById} from '@sanity/base/initial-value-templates'
import {
BoundaryElementProvider,
Card,
Code,
DialogProvider,
DialogProviderProps,
Flex,
Stack,
Text,
useElementRect,
} from '@sanity/ui'
import React, {memo, useMemo, useState} from 'react'
import styled from 'styled-components'
import {fromString as pathFromString} from '@sanity/util/paths'
import {Unstable_ReferenceInputOptionsProvider} from '@sanity/form-builder/_internal'
import {Path} from '@sanity/types'
import {getNewDocumentOptions} from '@sanity/base/_internal'
import {DocumentPaneNode} from '../../types'
import {useDeskTool} from '../../contexts/deskTool'
import {usePaneRouter} from '../../contexts/paneRouter'
import {PaneFooter} from '../../components/pane'
import {usePaneLayout} from '../../components/pane/usePaneLayout'
import {ErrorPane} from '../error'
import {LoadingPane} from '../loading'
import {ChangesPanel} from './changesPanel'
import {DocumentPanel} from './documentPanel'
import {DocumentOperationResults} from './DocumentOperationResults'
import {DocumentActionShortcuts} from './keyboardShortcuts'
import {DocumentStatusBar} from './statusBar'
import {useDocumentPane} from './useDocumentPane'
import {DocumentPaneProvider} from './DocumentPaneProvider'
import {DocumentPaneProviderProps} from './types'
declare const __DEV__: boolean
type DocumentPaneOptions = DocumentPaneNode['options']
const DOCUMENT_PANEL_MIN_WIDTH = 320
const DOCUMENT_PANEL_INITIAL_MIN_WIDTH = 600
const CHANGES_PANEL_MIN_WIDTH = 320
const DIALOG_PROVIDER_POSITION: DialogProviderProps['position'] = [
// We use the `position: fixed` for dialogs on narrow screens (< 512px).
'fixed',
// And we use the `position: absolute` strategy (within panes) on wide screens.
'absolute',
]
const StyledChangeConnectorRoot = styled(ChangeConnectorRoot)`
flex: 1;
display: flex;
min-height: 0;
min-width: 0;
`
export const DocumentPane = memo(function DocumentPane(props: DocumentPaneProviderProps) {
const paneRouter = usePaneRouter()
const options = usePaneOptions(props.pane.options, paneRouter.params)
const {documentType, isLoaded: isDocumentLoaded} = useDocumentType(options.id, options.type)
const [templatePermissions, isTemplatePermissionsLoading] = useUnstableTemplatePermissions(
getNewDocumentOptions()
)
const isLoaded = isDocumentLoaded && !isTemplatePermissionsLoading
const providerProps = useMemo(() => {
return isLoaded && documentType && options.type !== documentType
? mergeDocumentType(props, options, documentType)
: props
}, [props, documentType, isLoaded, options])
const {ReferenceChildLink, handleEditReference, groupIndex, routerPanesState} = paneRouter
const childParams = routerPanesState[groupIndex + 1]?.[0].params || {}
const routerPanesStateLength = routerPanesState.length
const {parentRefPath} = childParams
const activePath: {path: Path; state: 'selected' | 'pressed' | 'none'} = useMemo(() => {
return parentRefPath
? {
path: pathFromString(parentRefPath),
state:
// eslint-disable-next-line no-nested-ternary
groupIndex >= routerPanesStateLength - 1
? 'none'
: groupIndex >= routerPanesStateLength - 2
? 'selected'
: 'pressed',
}
: {path: [], state: 'none'}
}, [parentRefPath, groupIndex, routerPanesStateLength])
if (options.type === '*' && !isLoaded) {
return (
<LoadingPane flex={2.5} minWidth={320} paneKey={props.paneKey} title="Loading document…" />
)
}
if (!documentType) {
return (
<ErrorPane
flex={2.5}
minWidth={320}
paneKey={props.paneKey}
title={<>The document was not found</>}
>
<Stack space={4}>
<Text as="p">
The document type is not defined, and a document with the <code>{options.id}</code>{' '}
identifier could not be found.
</Text>
</Stack>
</ErrorPane>
)
}
return (
<DocumentPaneProvider {...providerProps}>
{/* NOTE: this is a temporary location for this provider until we */}
{/* stabilize the reference input options formally in the form builder */}
{/* eslint-disable-next-line react/jsx-pascal-case */}
<Unstable_ReferenceInputOptionsProvider
EditReferenceLinkComponent={ReferenceChildLink}
onEditReference={handleEditReference}
initialValueTemplateItems={templatePermissions}
activePath={activePath}
>
<InnerDocumentPane />
</Unstable_ReferenceInputOptionsProvider>
</DocumentPaneProvider>
)
})
function usePaneOptions(
options: DocumentPaneOptions,
params: Record<string, string | undefined> = {}
): DocumentPaneOptions {
return useMemo(() => {
// The document type is provided, so return
if (options.type && options.type !== '*') {
return options
}
// Attempt to derive document type from the template configuration
const templateName = options.template || params.template
const template = templateName ? getTemplateById(templateName) : undefined
const documentType = template?.schemaType
// No document type was found in a template
if (!documentType) {
return options
}
// The template provided the document type, so modify the pane’s `options` property
return {...options, type: documentType}
}, [options, params.template])
}
function mergeDocumentType(
props: DocumentPaneProviderProps,
options: DocumentPaneOptions,
documentType: string
): DocumentPaneProviderProps {
return {
...props,
pane: {
...props.pane,
options: {...options, type: documentType},
},
}
}
function InnerDocumentPane() {
const {
changesOpen,
documentSchema,
documentType,
handleFocus,
handleHistoryOpen,
handleKeyUp,
inspectOpen,
paneKey,
value,
totalReferenceCount,
} = useDocumentPane()
const {features} = useDeskTool()
const {collapsed: layoutCollapsed} = usePaneLayout()
const zOffsets = useZIndex()
const [rootElement, setRootElement] = useState<HTMLDivElement | null>(null)
const [footerElement, setFooterElement] = useState<HTMLDivElement | null>(null)
const [actionsBoxElement, setActionsBoxElement] = useState<HTMLDivElement | null>(null)
const footerRect = useElementRect(footerElement)
const footerH = footerRect?.height
const documentIsReferenced = totalReferenceCount !== undefined && totalReferenceCount > 0
const documentPanel = useMemo(
() => (
<DocumentPanel
footerHeight={footerH || null}
rootElement={rootElement}
isInspectOpen={inspectOpen}
/>
),
[footerH, rootElement, inspectOpen]
)
const footer = useMemo(
() => (
<PaneFooter isReferencedDocument={documentIsReferenced} ref={setFooterElement}>
<DocumentStatusBar actionsBoxRef={setActionsBoxElement} />
</PaneFooter>
),
[documentIsReferenced]
)
const changesPanel = useMemo(() => {
if (!features.reviewChanges) return null
if (!changesOpen) return null
return (
<BoundaryElementProvider element={rootElement}>
<ChangesPanel />
</BoundaryElementProvider>
)
}, [changesOpen, features.reviewChanges, rootElement])
const children = useMemo(() => {
if (!documentSchema) {
return (
<ErrorPane
flex={2.5}
minWidth={320}
paneKey={paneKey}
title={
<>
Unknown document type: <code>{documentType}</code>
</>
}
tone="caution"
>
<Stack space={4}>
{documentType && (
<Text as="p">
This document has the schema type <code>{documentType}</code>, which is not defined
as a type in the local content studio schema.
</Text>
)}
{!documentType && (
<Text as="p">
This document does not exist, and no schema type was specified for it.
</Text>
)}
{__DEV__ && value && (
<>
<Text as="p">Here is the JSON representation of the document:</Text>
<Card padding={3} overflow="auto" radius={2} shadow={1} tone="inherit">
<Code language="json" size={[1, 1, 2]}>
{JSON.stringify(value, null, 2)}
</Code>
</Card>
</>
)}
</Stack>
</ErrorPane>
)
}
return (
<>
<DialogProvider position={DIALOG_PROVIDER_POSITION} zOffset={zOffsets.portal}>
<Flex direction="column" flex={1} height={layoutCollapsed ? undefined : 'fill'}>
<StyledChangeConnectorRoot
data-testid="change-connector-root"
isReviewChangesOpen={changesOpen}
onOpenReviewChanges={handleHistoryOpen}
onSetFocus={handleFocus}
>
{documentPanel}
{changesPanel}
</StyledChangeConnectorRoot>
</Flex>
</DialogProvider>
{footer}
<DocumentOperationResults />
</>
)
}, [
changesOpen,
changesPanel,
documentPanel,
documentSchema,
documentType,
footer,
handleFocus,
handleHistoryOpen,
layoutCollapsed,
paneKey,
value,
zOffsets.portal,
])
const currentMinWidth = changesOpen
? DOCUMENT_PANEL_INITIAL_MIN_WIDTH + CHANGES_PANEL_MIN_WIDTH
: DOCUMENT_PANEL_INITIAL_MIN_WIDTH
const minWidth = changesOpen
? DOCUMENT_PANEL_MIN_WIDTH + CHANGES_PANEL_MIN_WIDTH
: DOCUMENT_PANEL_MIN_WIDTH
return (
<DocumentActionShortcuts
actionsBoxElement={actionsBoxElement}
currentMinWidth={currentMinWidth}
data-testid="document-pane"
flex={2.5}
id={paneKey}
minWidth={minWidth}
onKeyUp={handleKeyUp}
rootRef={setRootElement}
>
{children}
</DocumentActionShortcuts>
)
}