18
18
{{ slotProps.model.name }}
19
19
</span >
20
20
</template >
21
+ <span style =" display :none " slot =" addTreeNodeIcon" ></span >
22
+ <span style =" display :none " slot =" addLeafNodeIcon" ></span >
21
23
</vue-tree-list >
22
24
</div >
23
25
</template >
24
26
25
27
<script >
26
28
import { VueTreeList , Tree , TreeNode } from ' vue-tree-list'
27
29
import { readDir , createDir , writeFile , removeDir , removeFile , renameFile } from ' @tauri-apps/api/fs'
28
- import { videoDir } from ' @tauri-apps/api/path'
30
+ import { videoDir , resolve as pathResolve , join as pathJoin } from ' @tauri-apps/api/path'
29
31
import { open } from ' @tauri-apps/api/dialog'
30
32
31
33
const getPartialNodePath = (node , accum , override ) => {
@@ -55,14 +57,20 @@ const slugify = (text) =>
55
57
.trim ()
56
58
.replace (/ \s + / g , ' -' )
57
59
.replace (/ [^ \w -] + / g , ' ' )
58
- .replace (/ --+ / g , ' -' )
60
+ .replace (/ --+ / g , ' -' ) + Date .now ().toString ()
61
+
62
+ const getNodeObj = (node ) => {
63
+ return node .parent .children .filter (function (el ) {
64
+ return el .name === node .name
65
+ })[0 ]
66
+ }
59
67
export default {
60
68
components: {
61
69
VueTreeList
62
70
},
63
71
async created () {
64
72
this .rootDir = await videoDir ().then (result => result)
65
- const struct = await readDir (this .rootDir , { recursive: true }).then (result => result)
73
+ const struct = await readDir (this .rootDir , { recursive: false }).then (result => result)
66
74
this .data = new Tree ([])
67
75
this .generateNewTree (struct)
68
76
this .loading = false
@@ -77,10 +85,31 @@ export default {
77
85
inputValue: ' ' ,
78
86
nodeOldName: undefined ,
79
87
renamingNode: null ,
88
+ selectedNode: null ,
80
89
click: null
81
90
}
82
91
},
83
92
methods: {
93
+ async updateNode (node ) {
94
+ let path = getFullNodePath (node)
95
+ const nodeObj = getNodeObj (node)
96
+ path = await pathResolve (this .rootDir , path).then (result => result)
97
+ const content = await readDir (path, { recursive: false }).then (result => result)
98
+ if (! nodeObj .children ) nodeObj .children = []
99
+ content .forEach (item => {
100
+ const matches = nodeObj .children .filter ((el ) => {
101
+ return el .name === item .name
102
+ })
103
+ if (matches .length === 0 ) {
104
+ if (item .children ) {
105
+ this .addDir (item, nodeObj)
106
+ } else {
107
+ this .addLeaf (item, nodeObj)
108
+ }
109
+ }
110
+ })
111
+ return node
112
+ },
84
113
onDel (node ) {
85
114
const path = getFullNodePath (node)
86
115
if (node .isLeaf ) {
@@ -122,10 +151,21 @@ export default {
122
151
this .handleClick (params).then ((result ) => {
123
152
const { type , params } = result
124
153
if (type === ' single' ) {
125
- console .log (type, params)
154
+ if (this .selectedNode !== null ) {
155
+ const elem = document .getElementById (this .selectedNode .id )
156
+ if (elem === null ) {
157
+ this .selectedNode = null
158
+ } else {
159
+ elem .classList .toggle (' active' )
160
+ }
161
+ }
162
+ this .selectedNode = params
163
+ document .getElementById (params .id ).classList .toggle (' active' )
126
164
}
127
165
if (type === ' double' ) {
128
- params .toggle ()
166
+ if (! params .isLeaf ) {
167
+ this .updateNode (params).then ((node ) => node .toggle ())
168
+ }
129
169
}
130
170
})
131
171
},
@@ -137,7 +177,7 @@ export default {
137
177
this .loading = true
138
178
open ({ directory: true }).then (result => {
139
179
this .rootDir = result
140
- readDir (result, { recursive: true }).then (result => {
180
+ readDir (result, { recursive: false }).then (result => {
141
181
this .data = new Tree ([])
142
182
this .generateNewTree (result)
143
183
this .sortTree ()
@@ -163,11 +203,20 @@ export default {
163
203
document .getElementById (' floatingInput' ).style .top = ' 0px'
164
204
document .querySelector (' #floatingInput > input' ).focus ()
165
205
},
166
- addRootDir (value ) {
167
- console .log (' ~ ROOT DIR: ' , this .rootDir + value)
168
- // const absPath = this.rootDir + value
169
- createDir (this .rootDir + value).then ((result ) => {
170
- this .addDir ({ name: value })
206
+ async addRootDir (value ) {
207
+ let path = this .rootDir + value
208
+ let parent = this .selectedNode !== null ? getNodeObj (this .selectedNode ) : null
209
+ if (parent .isLeaf ) {
210
+ parent = parent .parent
211
+ }
212
+ if (parent !== null ) {
213
+ path = await pathJoin (this .rootDir , getFullNodePath (parent)).then (result => result) + ' \\ ' + value
214
+ }
215
+ createDir (path).then ((result ) => {
216
+ this .addDir ({ name: value }, parent)
217
+ if (parent !== null ) {
218
+ this .selectedNode .toggle ()
219
+ }
171
220
})
172
221
},
173
222
addDir (item , parent = null ) {
@@ -182,10 +231,20 @@ export default {
182
231
this .sortTree ()
183
232
return node
184
233
},
185
- addRootLeaf (value ) {
186
- console .log (' ~ ROOT LEAF: ' , this .rootDir + value)
187
- writeFile ({ path: this .rootDir + value, contents: ' ' }).then ((result ) => {
188
- this .addLeaf ({ name: value })
234
+ async addRootLeaf (value ) {
235
+ let path = this .rootDir + value
236
+ let parent = this .selectedNode !== null ? getNodeObj (this .selectedNode ) : null
237
+ if (parent .isLeaf ) {
238
+ parent = parent .parent
239
+ }
240
+ if (parent !== null ) {
241
+ path = await pathJoin (this .rootDir , getFullNodePath (parent)).then (result => result) + ' \\ ' + value
242
+ }
243
+ writeFile ({ path: path, contents: ' ' }).then ((result ) => {
244
+ this .addLeaf ({ name: value }, parent)
245
+ if (parent !== null ) {
246
+ this .selectedNode .toggle ()
247
+ }
189
248
})
190
249
},
191
250
addLeaf (item , parent = null ) {
@@ -203,6 +262,7 @@ export default {
203
262
folderStruct .forEach (item => {
204
263
if (item .children ) {
205
264
const node = this .addDir (item, parent)
265
+ node .children = []
206
266
this .generateNewTree (item .children , node)
207
267
} else {
208
268
this .addLeaf (item, parent)
@@ -252,4 +312,11 @@ export default {
252
312
color : gray ;
253
313
font-size : 80% ;
254
314
}
315
+
316
+ </style >
317
+
318
+ <style >
319
+ .vtl-node.active {
320
+ border : 1px solid red ;
321
+ }
255
322
</style >
0 commit comments