1
1
import { UniqueIdentifier } from '@dnd-kit/core'
2
2
import { arrayMove } from '@dnd-kit/sortable'
3
3
4
+ import { getAllEntities } from '@mexit/core'
5
+
4
6
import type { FlattenedItem , TreeItem , TreeItems } from './types'
5
7
6
8
export const iOS = / i P a d | i P h o n e | i P o d / . test ( navigator . platform )
@@ -86,20 +88,51 @@ export function flattenTree(items: TreeItems): FlattenedItem[] {
86
88
return flatten ( items )
87
89
}
88
90
91
+ const updateNode = (
92
+ node : FlattenedItem ,
93
+ nodes : Record < string , TreeItem > ,
94
+ allItems : FlattenedItem [ ] ,
95
+ options : Partial < {
96
+ root : UniqueIdentifier
97
+ isStub : boolean
98
+ collapsed : boolean
99
+ } > = {
100
+ root : 'root' ,
101
+ isStub : false ,
102
+ collapsed : false
103
+ }
104
+ ) => {
105
+ const id = node . id
106
+ const parentId = node ?. parentId ?? options . root
107
+ const parent = nodes [ parentId ] ?? findItem ( allItems , parentId )
108
+
109
+ if ( ! nodes [ id ] ) {
110
+ const updatedNode : TreeItem = {
111
+ id,
112
+ children : [ ] ,
113
+ properties : node . properties ,
114
+ isStub : options . isStub ,
115
+ collapsed : options . collapsed
116
+ }
117
+
118
+ nodes [ id ] = updatedNode
119
+ parent ?. children ?. push ( updatedNode )
120
+ }
121
+ }
122
+
89
123
export function buildPartialTree ( items : FlattenedItem [ ] , allItems : FlattenedItem [ ] ) {
90
124
const root : TreeItem = { id : 'root' , children : [ ] , properties : { } }
91
125
const nodes : Record < string , TreeItem > = { [ root . id ] : root }
92
126
93
127
for ( const item of items ) {
94
- const { id, children } = item
95
- const parentId = item . parentId ?? root . id
96
- const parent = nodes [ parentId ] ?? findItem ( allItems , parentId )
128
+ const parents = getAllEntities ( item . properties . path )
97
129
98
- nodes [ id ] = { id, children }
99
- if ( parent ) {
100
- console . log ( 'parent' , { parent, item, allItems } )
101
- parent . children . push ( item )
130
+ for ( const id of parents ) {
131
+ const node = nodes [ id ] ?? ( findItem ( allItems , id ) as any )
132
+ updateNode ( node , nodes , allItems , { root : root . id , isStub : true } )
102
133
}
134
+
135
+ updateNode ( item , nodes , allItems )
103
136
}
104
137
105
138
return root . children
@@ -111,12 +144,14 @@ export function buildTree(flattenedItems: FlattenedItem[]): TreeItems {
111
144
const items = flattenedItems . map ( ( item ) => ( { ...item , children : [ ] } ) )
112
145
113
146
for ( const item of items ) {
114
- const { id, children, properties } = item
147
+ const { id, children } = item
115
148
const parentId = item . parentId ?? root . id
116
149
const parent = nodes [ parentId ] ?? findItem ( items , parentId )
117
150
118
- nodes [ id ] = { id, children }
119
- if ( parent ) parent . children . push ( item )
151
+ if ( ! nodes [ id ] ) {
152
+ nodes [ id ] = { id, children }
153
+ parent . children . push ( item )
154
+ }
120
155
}
121
156
122
157
return root . children
0 commit comments