-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnode-content-renderer.js
241 lines (220 loc) · 7.31 KB
/
node-content-renderer.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
import React from 'react'
import PropTypes from 'prop-types'
import DragIcon from './DragIcon'
import useStyles from './node-content-renderer-style'
function isDescendant(older, younger) {
return (
!!older.children &&
typeof older.children !== 'function' &&
older.children.some(
child => child === younger || isDescendant(child, younger)
)
)
}
function FileThemeNodeContentRenderer(props) {
const {
scaffoldBlockPxWidth,
toggleChildrenVisibility,
connectDragPreview,
connectDragSource,
isDragging,
canDrop,
canDrag,
node,
title,
draggedNode,
path,
treeIndex,
isSearchMatch,
isSearchFocus,
icons,
buttons,
className,
style,
didDrop,
lowerSiblingCounts,
listIndex,
swapFrom,
swapLength,
swapDepth
// treeId // Not needed, but preserved for other renderers
// isOver, // Not needed, but preserved for other renderers
// parentNode, // Needed for dndManager
// rowDirection
} = props
const classes = useStyles()
const nodeTitle = title || node.title
const isDraggedDescendant = draggedNode && isDescendant(draggedNode, node)
const isLandingPadActive = !didDrop && isDragging
// Construct the scaffold representing the structure of the tree
const scaffold = []
lowerSiblingCounts.forEach((lowerSiblingCount, i) => {
if(i > 0) {
scaffold.push(
<div
className={classes.lineBlock}
key={`pre_${1 + i}`}
style={{ width: scaffoldBlockPxWidth }} />
)
if(treeIndex !== listIndex && i === swapDepth) {
// This row has been shifted, and is at the depth of
// the line pointing to the new destination
let highlightLineClass = ''
if(listIndex === swapFrom + swapLength - 1)
// This block is on the bottom (target) line
// This block points at the target block (where the row will go when released)
highlightLineClass = classes.highlightBottomLeftCorner
else if(treeIndex === swapFrom)
// This block is on the top (source) line
highlightLineClass = classes.highlightTopLeftCorner
else
// This block is between the bottom and top
highlightLineClass = classes.highlightLineVertical
scaffold.push(
<div
className={`${classes.absoluteLineBlock} ${highlightLineClass}`}
key={`highlight_${1 + i}`}
style={{
left : scaffoldBlockPxWidth * i,
width: scaffoldBlockPxWidth
}} />
)
}
}
})
const nodeContent = (
<div className={classes.nodeContent}>
{/* {
toggleChildrenVisibility &&
node.children &&
node.children.length > 0 && (
<button
aria-label={node.expanded ? 'Collapse' : 'Expand'}
className={
node.expanded ? classes.collapseButton : classes.expandButton
}
onClick={() =>
toggleChildrenVisibility({
node,
path,
treeIndex
})
}
style={{
left: (lowerSiblingCounts.length - 0.7) * scaffoldBlockPxWidth
}}
type='button' />
)
} */}
<div className={classes.rowWrapper + (!canDrag ? ` ${classes.rowWrapperDragDisabled}` : '')}>
{scaffold}
{/* {
connectDragSource(
<div>
<DragIcon />
</div>
)
} */}
{
connectDragPreview(
<div
className={
classes.row +
(isLandingPadActive ? ` ${classes.rowLandingPad}` : '') +
(isLandingPadActive && !canDrop ? ` ${classes.rowCancelPad}` : '') +
(isSearchMatch && !isSearchFocus ? ` ${classes.rowSearchMatch}` : '') +
(isSearchFocus ? ` ${classes.rowSearchFocus}` : '') +
(className ? ` ${className}` : '')
}
style={{
opacity: isDraggedDescendant ? 0.5 : 1,
...style
}}>
<div className={classes.rowContents + (!canDrag ? ` ${classes.rowContentsDragDisabled}` : '')}>
{
connectDragSource(<div><DragIcon /></div>)
}
<div className={classes.rowIcon}>
{icons}
</div>
<div className={classes.rowLabel}>
{
typeof nodeTitle === 'string' ?
<span className={classes.rowTitle}>{nodeTitle}</span> :
typeof nodeTitle === 'function' ?
nodeTitle({
node,
path,
treeIndex
}) :
nodeTitle
}
</div>
<div className={classes.rowToolbar}>
{buttons}
</div>
</div>
</div>
)
}
</div>
</div>
)
// return canDrag ?
// connectDragSource(nodeContent, { dropEffect: 'copy' }) :
// nodeContent
return nodeContent
}
FileThemeNodeContentRenderer.defaultProps = {
buttons : null,
canDrag : false,
canDrop : false,
className : '',
draggedNode : null,
icons : null,
isSearchFocus : false,
isSearchMatch : false,
parentNode : null,
style : {},
swapDepth : null,
swapFrom : null,
swapLength : null,
title : null,
toggleChildrenVisibility: null
}
FileThemeNodeContentRenderer.propTypes = {
buttons : PropTypes.node,
canDrag : PropTypes.bool,
canDrop : PropTypes.bool,
className : PropTypes.string,
connectDragPreview: PropTypes.func.isRequired,
connectDragSource : PropTypes.func.isRequired,
didDrop : PropTypes.bool.isRequired,
draggedNode : PropTypes.shape({}),
icons : PropTypes.node,
isDragging : PropTypes.bool.isRequired,
isOver : PropTypes.bool.isRequired,
isSearchFocus : PropTypes.bool,
isSearchMatch : PropTypes.bool,
listIndex : PropTypes.number.isRequired,
lowerSiblingCounts: PropTypes.arrayOf(PropTypes.number).isRequired,
node : PropTypes.shape({}).isRequired,
parentNode : PropTypes.shape({}),
path : PropTypes.arrayOf(
PropTypes.oneOfType([ PropTypes.string, PropTypes.number ])
).isRequired,
rowDirection : PropTypes.string.isRequired,
scaffoldBlockPxWidth: PropTypes.number.isRequired,
// Drag and drop API functions
// Drag source
style : PropTypes.shape({}),
swapDepth : PropTypes.number,
swapFrom : PropTypes.number,
swapLength : PropTypes.number,
title : PropTypes.oneOfType([ PropTypes.func, PropTypes.node ]),
toggleChildrenVisibility: PropTypes.func, // Needed for dndManager
// Drop target
treeId : PropTypes.string.isRequired,
treeIndex : PropTypes.number.isRequired
}
export default FileThemeNodeContentRenderer