-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathHierarchy.tsx
More file actions
190 lines (169 loc) · 5.58 KB
/
Copy pathHierarchy.tsx
File metadata and controls
190 lines (169 loc) · 5.58 KB
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
import React, { useCallback, useMemo, useState } from 'react'
import { Entity } from '@dcl/ecs'
import { CAMERA, PLAYER, ROOT } from '../../lib/sdk/tree'
import { useEntitiesWith } from '../../hooks/sdk/useEntitiesWith'
import { useTree } from '../../hooks/sdk/useTree'
import { Tree } from '../Tree'
import { ContextMenu } from './ContextMenu'
import { withSdk } from '../../hoc/withSdk'
import './Hierarchy.css'
import { useAppSelector } from '../../redux/hooks'
import { selectCustomAssets } from '../../redux/app'
const HierarchyIcon = withSdk<{ value: Entity }>(({ sdk, value }) => {
const customAssets = useAppSelector(selectCustomAssets)
const isSmart = useMemo(
() =>
sdk.components.Actions.has(value) ||
sdk.components.Triggers.has(value) ||
sdk.components.States.has(value) ||
sdk.components.TextShape.has(value) ||
sdk.components.NftShape.has(value) ||
sdk.components.VisibilityComponent.has(value) ||
sdk.components.VideoScreen.has(value) ||
sdk.components.AdminTools.has(value),
[sdk, value]
)
const isCustom = useMemo(() => {
if (sdk.components.CustomAsset.has(value)) {
const { assetId } = sdk.components.CustomAsset.get(value)
const customAsset = customAssets.find((asset) => asset.id === assetId)
return !!customAsset
}
return false
}, [sdk, value, customAssets])
const isTile = useMemo(() => sdk.components.Tile.has(value), [sdk, value])
const isGroup = useMemo(() => {
const nodes = sdk.components.Nodes.getOrNull(ROOT)?.value
const node = nodes?.find((node) => node.entity === value)
return node && node.children.length > 0
}, [value])
if (value === ROOT) {
return <span style={{ marginRight: '4px' }}></span>
} else if (value === PLAYER) {
return <span className="tree-icon player-icon"></span>
} else if (value === CAMERA) {
return <span className="tree-icon camera-icon"></span>
} else if (isCustom) {
return <span className="tree-icon custom-icon"></span>
} else if (isGroup) {
return <span className="tree-icon group-icon"></span>
} else if (isSmart) {
return <span className="tree-icon smart-icon"></span>
} else if (isTile) {
return <span className="tree-icon tile-icon"></span>
} else {
return <span className="tree-icon entity-icon"></span>
}
})
const EntityTree = Tree<Entity>()
const Hierarchy: React.FC = () => {
const {
addChild,
setParent,
remove,
rename,
select,
setOpen,
duplicate,
getId,
getChildren,
getLabel,
getSelectedItems,
isOpen,
isHidden,
canRename,
canRemove,
canDuplicate,
canDrag,
canReorder,
centerViewOnEntity,
isRoot
} = useTree()
const selectedEntities = useEntitiesWith((components) => components.Selection)
const [lastSelectedItem, setLastSelectedItem] = useState<Entity | undefined>(undefined)
const isSelected = useCallback(
(entity: Entity) => {
return selectedEntities.includes(entity)
},
[selectedEntities]
)
const getAllVisibleEntities = useCallback(() => {
const entities: Entity[] = []
const traverse = (entity: Entity) => {
if (!isHidden(entity)) {
entities.push(entity)
if (isOpen(entity)) {
getChildren(entity).forEach((child) => traverse(child))
}
}
}
traverse(ROOT)
return entities
}, [getChildren, isOpen, isHidden])
const handleRangeSelection = useCallback(
(fromEntity: Entity, toEntity: Entity) => {
const allEntities = getAllVisibleEntities()
const fromIndex = allEntities.findIndex((e) => getId(e) === getId(fromEntity))
const toIndex = allEntities.findIndex((e) => getId(e) === getId(toEntity))
const startIndex = Math.min(fromIndex, toIndex)
const endIndex = Math.max(fromIndex, toIndex)
allEntities.forEach((entity, index) => {
if (index >= startIndex && index <= endIndex) {
void select(entity, index > startIndex) // first item replaces selection, others add to selection
}
})
},
[getAllVisibleEntities, getId, select]
)
const handleSelect = useCallback(
(entity: Entity, clickType?: 'single' | 'ctrl' | 'shift') => {
if (clickType === 'shift' && lastSelectedItem) {
handleRangeSelection(lastSelectedItem, entity)
} else {
const isMultipleSelection = clickType === 'ctrl' || clickType === 'shift'
void select(entity, isMultipleSelection)
}
},
[select, lastSelectedItem, handleRangeSelection]
)
const handleLastSelectedChange = useCallback(
(entity: Entity) => {
if (entity !== lastSelectedItem) setLastSelectedItem(entity)
},
[lastSelectedItem]
)
const props = {
getExtraContextMenu: ContextMenu,
onAddChild: addChild,
onDrop: setParent,
onRemove: remove,
onRename: rename,
onSelect: handleSelect,
onDoubleSelect: centerViewOnEntity,
onSetOpen: setOpen,
onDuplicate: duplicate,
getId: getId,
getChildren: getChildren,
getLabel: getLabel,
getSelectedItems: getSelectedItems,
getIcon: (val: Entity) => <HierarchyIcon value={val} />,
isOpen: isOpen,
isSelected: isSelected,
isHidden: isHidden,
canRename: canRename,
canRemove: canRemove,
canDuplicate: canDuplicate,
canDrag: canDrag,
canReorder: canReorder,
onLastSelectedChange: handleLastSelectedChange,
isRoot: isRoot
}
return (
<div className="Hierarchy">
<EntityTree value={PLAYER} {...props} />
<EntityTree value={CAMERA} {...props} />
<EntityTree value={ROOT} {...props} />
</div>
)
}
export default React.memo(Hierarchy)