|
1 | 1 | import React, { memo, useCallback, useMemo, useReducer, useState } from "react"; |
2 | | - |
3 | 2 | import styled from "@emotion/styled"; |
4 | 3 | import { TreeView } from "@editor-ui/editor"; |
5 | | -import { LayerRow } from "./editor-layer-hierarchy-item"; |
| 4 | +import { |
| 5 | + LayerRow, |
| 6 | + IconContainer, |
| 7 | + LayerIcon, |
| 8 | +} from "./editor-layer-hierarchy-item"; |
6 | 9 | import { useEditorState } from "core/states"; |
7 | 10 | import { useDispatch } from "core/dispatch"; |
8 | 11 |
|
9 | 12 | export function EditorLayerHierarchy() { |
10 | 13 | const [state] = useEditorState(); |
11 | 14 | const dispatch = useDispatch(); |
12 | 15 | const root = state.design?.current?.entry; |
13 | | - const layers = root ? flatten(root) : []; |
| 16 | + const layers = root ? flatten(root, ["origin"]) : []; |
14 | 17 |
|
15 | 18 | const renderItem = useCallback( |
16 | | - ({ id, name, depth }) => { |
| 19 | + ({ id, name, depth, type }) => { |
| 20 | + const selected = state?.selectedNodes?.includes(id); |
| 21 | + |
17 | 22 | return ( |
18 | 23 | <LayerRow |
| 24 | + icon={ |
| 25 | + <IconContainer> |
| 26 | + <LayerIcon type={type} selected={selected} /> |
| 27 | + </IconContainer> |
| 28 | + } |
19 | 29 | name={name} |
20 | 30 | depth={depth} |
21 | 31 | id={id} |
22 | 32 | expanded={haschildren(id) == true ? true : undefined} |
23 | 33 | key={id} |
24 | | - selected={state?.selectedNodes?.includes(id)} |
| 34 | + selected={selected} |
25 | 35 | onAddClick={() => {}} |
26 | 36 | onMenuClick={() => {}} |
27 | 37 | onDoubleClick={() => {}} |
@@ -55,35 +65,44 @@ export function EditorLayerHierarchy() { |
55 | 65 | interface ITreeNode { |
56 | 66 | id: string; |
57 | 67 | name: string; |
| 68 | + type: string; |
58 | 69 | children?: ITreeNode[]; |
59 | 70 | } |
60 | 71 |
|
61 | 72 | interface FlattenedNode { |
62 | 73 | id: string; |
63 | 74 | name: string; |
64 | 75 | depth: number; |
| 76 | + type: string; |
65 | 77 | parent: string; |
66 | 78 | } |
67 | 79 |
|
68 | | -const flatten = ( |
69 | | - tree: ITreeNode, |
| 80 | +const flatten = <T extends ITreeNode>( |
| 81 | + tree: T, |
| 82 | + properties?: string[], |
70 | 83 | parent?: string, |
71 | 84 | depth: number = 0 |
72 | 85 | ): FlattenedNode[] => { |
73 | | - const convert = (node: ITreeNode, depth: number, parent?: string) => { |
| 86 | + const convert = (node: T, properties, depth: number, parent?: string) => { |
74 | 87 | const result: FlattenedNode = { |
75 | 88 | id: node.id, |
76 | 89 | name: node.name, |
| 90 | + type: node.type, |
77 | 91 | depth: depth, |
78 | 92 | parent, |
79 | 93 | }; |
| 94 | + |
| 95 | + properties?.forEach((property) => { |
| 96 | + (result as object)[property] = node[property]; |
| 97 | + }); |
| 98 | + |
80 | 99 | return result; |
81 | 100 | }; |
82 | 101 |
|
83 | 102 | const final = []; |
84 | | - final.push(convert(tree, depth, parent)); |
| 103 | + final.push(convert(tree, properties, depth, parent)); |
85 | 104 | for (const child of tree.children || []) { |
86 | | - final.push(...flatten(child, tree.id, depth + 1)); |
| 105 | + final.push(...flatten(child, null, tree.id, depth + 1)); |
87 | 106 | } |
88 | 107 | return final; |
89 | 108 | }; |
0 commit comments