-
Notifications
You must be signed in to change notification settings - Fork 398
/
DeskToolPanes.js
403 lines (347 loc) · 12.6 KB
/
DeskToolPanes.js
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import React from 'react'
import PropTypes from 'prop-types'
import {sumBy} from 'lodash'
import {Observable, merge, of} from 'rxjs'
import {map, mapTo, delay, share, debounceTime, distinctUntilChanged} from 'rxjs/operators'
import {StateLink} from 'part:@sanity/base/router'
import SplitController from 'part:@sanity/components/panes/split-controller'
import SplitPaneWrapper from 'part:@sanity/components/panes/split-pane-wrapper'
import LoadingPane from './pane/LoadingPane'
import Pane from './pane/Pane'
import {PaneRouterContext, LOADING_PANE} from './index'
const COLLAPSED_WIDTH = 55
const BREAKPOINT_SCREEN_MEDIUM = 512
const fromWindowEvent = eventName =>
new Observable(subscriber => {
const handler = event => subscriber.next(event)
window.addEventListener(eventName, handler)
return () => {
window.removeEventListener(eventName, handler)
}
})
const orientationChange$ = fromWindowEvent('orientationchange')
const resize$ = fromWindowEvent('resize')
const windowWidth$ = merge(orientationChange$, resize$).pipe(
share(),
debounceTime(50),
map(() => window.innerWidth)
)
function getPaneMinSize(pane) {
return pane.type === 'document' ? 500 : 320
}
function getPaneDefaultSize(pane) {
return pane.type === 'document' ? 672 : 350
}
function getWaitMessages(path) {
const thresholds = [{ms: 300, message: 'Loading…'}, {ms: 5000, message: 'Still loading…'}]
if (__DEV__) {
const message = [
'Check console for errors?',
'Is your observable/promise resolving?',
path.length > 0 ? `Structure path: ${path.join(' ➝ ')}` : ''
]
thresholds.push({
ms: 10000,
message: message.join('\n')
})
}
const src = of(null)
return merge(
...thresholds.map(({ms, message}) =>
src.pipe(
mapTo(message),
delay(ms)
)
)
)
}
// eslint-disable-next-line react/require-optimization
export default class DeskToolPanes extends React.Component {
static propTypes = {
keys: PropTypes.arrayOf(PropTypes.string).isRequired,
groupIndexes: PropTypes.arrayOf(PropTypes.number).isRequired,
autoCollapse: PropTypes.bool,
panes: PropTypes.arrayOf(
PropTypes.oneOfType([
PropTypes.shape({
id: PropTypes.string.isRequired,
params: PropTypes.object
}),
PropTypes.symbol
])
).isRequired,
router: PropTypes.shape({
navigate: PropTypes.func.isRequired,
navigateIntent: PropTypes.func.isRequired,
state: PropTypes.shape({
panes: PropTypes.arrayOf(
PropTypes.arrayOf(
PropTypes.shape({
id: PropTypes.string.isRequired,
params: PropTypes.object
})
)
),
editDocumentId: PropTypes.string
})
}).isRequired
}
static defaultProps = {
autoCollapse: false
}
state = {
collapsedPanes: [],
windowWidth: typeof window === 'undefined' ? 1000 : window.innerWidth,
isMobile: typeof window !== 'undefined' && window.innerWidth < BREAKPOINT_SCREEN_MEDIUM
}
userCollapsedPanes = []
// Memoized copy of contexts
paneRouterContexts = new Map()
getPaneRouterContext = (groupIndex, siblingIndex, flatIndex) => {
const key = `${flatIndex}-${groupIndex}[${siblingIndex}]`
if (this.paneRouterContexts.has(key)) {
return this.paneRouterContexts.get(key)
}
const modifyCurrentGroup = modifier => {
const {router} = this.props
const newPanes = (router.state.panes || []).slice()
const group = newPanes[groupIndex].slice()
newPanes.splice(groupIndex, 1, modifier(group, group[siblingIndex]))
const newRouterState = {...router.state, panes: newPanes}
router.navigate(newRouterState)
return newRouterState
}
const getPaneParameters = () => {
const panes = this.props.router.state.panes || []
const group = panes[groupIndex] || []
const pane = group[siblingIndex]
return pane.params || {}
}
const ctx = {
// Zero-based index (position) of pane, visually
index: flatIndex,
// Zero-based index of pane group (within URL structure)
groupIndex,
// Zero-based index of pane within sibling group
siblingIndex,
getPayload: () => (this.props.router.state.panes || [])[groupIndex][siblingIndex].payload,
// Curried StateLink that passes the correct state automatically
ChildLink: ({childId, childPayload, ...props}) => {
const oldPanes = this.props.router.state.panes || []
const panes = oldPanes
.slice(0, groupIndex + 1)
.concat([[{id: childId, payload: childPayload}]])
return <StateLink {...props} state={{panes}} />
},
// Get the current pane ID and parameters
getCurrentPane: () => {
const routerGroups = this.props.router.state.panes || []
const routerGroup = routerGroups[groupIndex]
const routerPane = routerGroup && routerGroup[siblingIndex]
const childGroup = routerGroups[groupIndex + 1] || []
return {
pane: this.props.panes[flatIndex],
router: routerPane,
child: childGroup[0],
siblings: routerGroup
}
},
// Replaces the current pane with a new one
replaceCurrentPane: (itemId, payload) => {
const {router} = this.props
const {editDocumentId} = router.state
if (editDocumentId) {
router.navigate({...router.state, editDocumentId: itemId})
} else {
modifyCurrentGroup(() => [{id: itemId, payload}])
}
},
// Removes the current pane from the group
closeCurrentPane: () => {
modifyCurrentGroup((siblings, item) => siblings.filter(sibling => sibling !== item))
},
// Replace or create a child pane with the given id and parameters
replaceChildPane: (itemId, payload) => {
const {router} = this.props
const {editDocumentId, panes} = router.state
if (editDocumentId) {
router.navigate({...router.state, editDocumentId: itemId})
} else {
const newPanes = panes.slice()
newPanes.splice(groupIndex + 1, 1, [{id: itemId, payload}])
router.navigate({...router.state, panes: newPanes})
}
},
// Duplicate the current pane, with optional overrides for item ID and parameters
duplicateCurrentPane: (itemId, payload, params) => {
modifyCurrentGroup((siblings, item) => [
...siblings,
{
...item,
id: itemId || item.id,
payload: payload || item.payload,
params: params || item.params
}
])
},
setPaneView: viewId => {
modifyCurrentGroup((siblings, item) => {
const newGroup = siblings.slice()
const newItem = {...item, params: viewId ? {view: viewId} : {}}
newGroup.splice(siblingIndex, 1, newItem)
return newGroup
})
},
getPaneParameters,
getPaneView: () => getPaneParameters().view,
// Proxied navigation to a given intent. Consider just exposing `router` instead?
navigateIntent: this.props.router.navigateIntent
}
this.paneRouterContexts.set(key, ctx)
return ctx
}
componentDidUpdate(prevProps) {
if (this.props.panes.length !== prevProps.panes.length) {
this.userCollapsedPanes = []
this.handleAutoCollapse(this.state.windowWidth, undefined, this.userCollapsedPanes)
}
// Expand new panes
const paneToForceExpand = this.props.panes.reduce((acc, pane, i) => {
return prevProps.panes[i] === pane ? acc : i
}, undefined)
if (typeof paneToForceExpand !== 'undefined') {
this.handleAutoCollapse(this.state.windowWidth, paneToForceExpand, this.userCollapsedPanes)
}
}
componentDidMount() {
const {autoCollapse, panes} = this.props
if (autoCollapse) {
this.resizeSubscriber = windowWidth$.pipe(distinctUntilChanged()).subscribe(windowWidth => {
this.setState({
windowWidth,
isMobile: windowWidth < BREAKPOINT_SCREEN_MEDIUM
})
this.handleAutoCollapse(windowWidth, undefined, this.userCollapsedPanes)
})
if (window) {
this.handleAutoCollapse(window.innerWidth, panes.length - 1, this.userCollapsedPanes)
}
}
}
componentWillUnmount() {
if (this.props.autoCollapse && this.resizeSubscriber) {
this.resizeSubscriber.unsubscribe()
}
}
handlePaneCollapse = index => {
if (this.state.isMobile || this.props.panes.length === 1) {
return
}
this.userCollapsedPanes[index] = true
this.handleAutoCollapse(this.state.windowWidth, undefined, this.userCollapsedPanes)
}
handlePaneExpand = index => {
if (this.state.isMobile || this.props.panes.length === 1) {
return
}
this.userCollapsedPanes[index] = false
this.handleAutoCollapse(this.state.windowWidth, index, this.userCollapsedPanes)
}
handleAutoCollapse = (windowWidth, paneWantExpand, userCollapsedPanes = []) => {
const {autoCollapse, panes} = this.props
const {isMobile} = this.state
const paneToForceExpand = typeof paneWantExpand === 'number' ? paneWantExpand : panes.length - 1
if (isMobile || !autoCollapse || !panes || panes.length === 0) {
return
}
const autoCollapsedPanes = []
const totalMinSize = sumBy(panes, pane => getPaneMinSize(pane))
let remainingMinSize = totalMinSize
remainingMinSize -= getPaneMinSize(panes[paneToForceExpand])
autoCollapsedPanes[paneToForceExpand] = false
userCollapsedPanes[paneToForceExpand] = false
if (totalMinSize > windowWidth) {
panes.forEach((pane, i) => {
if (paneToForceExpand != i) {
if (remainingMinSize > windowWidth - getPaneMinSize(panes[paneToForceExpand])) {
autoCollapsedPanes[i] = true
remainingMinSize -= getPaneMinSize(pane) - COLLAPSED_WIDTH
}
}
})
}
// Respect userCollapsed before autoCollapsed
const collapsedPanes = panes.map((pane, i) => userCollapsedPanes[i] || autoCollapsedPanes[i])
this.setState({collapsedPanes})
}
renderPanes() {
const {panes, groupIndexes, keys} = this.props
const {isMobile} = this.state
const path = []
const paneGroups = [['']].concat(this.props.router.state.panes || [])
let i = -1
return paneGroups.reduce((components, group, index) => {
return components.concat(
group.map((paneId, siblingIndex) => {
const pane = panes[++i]
const isCollapsed = Boolean(!isMobile && this.state.collapsedPanes[i])
const paneKey = `${i}-${keys[i - 1] || 'root'}-${groupIndexes[i - 1]}`
// Same pane might appear multiple times, so use index as tiebreaker
const wrapperKey = pane === LOADING_PANE ? `loading-${i}` : `${i}-${pane.id}`
path.push(pane.id || `[${i}]`)
return (
<SplitPaneWrapper
key={wrapperKey}
isCollapsed={isCollapsed}
minSize={getPaneMinSize(pane)}
defaultSize={getPaneDefaultSize(pane)}
>
<PaneRouterContext.Provider
value={this.getPaneRouterContext(index - 1, siblingIndex, i)}
>
{pane === LOADING_PANE ? (
<LoadingPane
key={paneKey} // Use key to force rerendering pane on ID change
path={path}
index={i}
message={getWaitMessages}
onExpand={this.handlePaneExpand}
onCollapse={this.handlePaneCollapse}
isCollapsed={isCollapsed}
isSelected={i === panes.length - 1}
/>
) : (
<Pane
key={paneKey} // Use key to force rerendering pane on ID change
paneKey={paneKey}
index={i}
itemId={keys[i]}
onExpand={this.handlePaneExpand}
onCollapse={this.handlePaneCollapse}
isCollapsed={isCollapsed}
isSelected={i === panes.length - 1}
isClosable={siblingIndex > 0}
{...pane}
/>
)}
</PaneRouterContext.Provider>
</SplitPaneWrapper>
)
})
)
}, [])
}
render() {
const {isMobile} = this.state
return (
<SplitController
isMobile={isMobile}
autoCollapse={this.props.autoCollapse}
collapsedWidth={COLLAPSED_WIDTH}
onCheckCollapse={this.handleCheckCollapse}
>
{this.renderPanes()}
</SplitController>
)
}
}