1818 {{ slotProps.model.name }}
1919 </span >
2020 </template >
21+ <span style =" display :none " slot =" addTreeNodeIcon" ></span >
22+ <span style =" display :none " slot =" addLeafNodeIcon" ></span >
2123 </vue-tree-list >
2224 </div >
2325</template >
2426
2527<script >
2628import { VueTreeList , Tree , TreeNode } from ' vue-tree-list'
2729import { 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'
2931import { open } from ' @tauri-apps/api/dialog'
3032
3133const getPartialNodePath = (node , accum , override ) => {
@@ -55,14 +57,20 @@ const slugify = (text) =>
5557 .trim ()
5658 .replace (/ \s + / g , ' -' )
5759 .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+ }
5967export default {
6068 components: {
6169 VueTreeList
6270 },
6371 async created () {
6472 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)
6674 this .data = new Tree ([])
6775 this .generateNewTree (struct)
6876 this .loading = false
@@ -77,10 +85,31 @@ export default {
7785 inputValue: ' ' ,
7886 nodeOldName: undefined ,
7987 renamingNode: null ,
88+ selectedNode: null ,
8089 click: null
8190 }
8291 },
8392 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+ },
84113 onDel (node ) {
85114 const path = getFullNodePath (node)
86115 if (node .isLeaf ) {
@@ -122,10 +151,21 @@ export default {
122151 this .handleClick (params).then ((result ) => {
123152 const { type , params } = result
124153 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' )
126164 }
127165 if (type === ' double' ) {
128- params .toggle ()
166+ if (! params .isLeaf ) {
167+ this .updateNode (params).then ((node ) => node .toggle ())
168+ }
129169 }
130170 })
131171 },
@@ -137,7 +177,7 @@ export default {
137177 this .loading = true
138178 open ({ directory: true }).then (result => {
139179 this .rootDir = result
140- readDir (result, { recursive: true }).then (result => {
180+ readDir (result, { recursive: false }).then (result => {
141181 this .data = new Tree ([])
142182 this .generateNewTree (result)
143183 this .sortTree ()
@@ -163,11 +203,20 @@ export default {
163203 document .getElementById (' floatingInput' ).style .top = ' 0px'
164204 document .querySelector (' #floatingInput > input' ).focus ()
165205 },
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+ }
171220 })
172221 },
173222 addDir (item , parent = null ) {
@@ -182,10 +231,20 @@ export default {
182231 this .sortTree ()
183232 return node
184233 },
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+ }
189248 })
190249 },
191250 addLeaf (item , parent = null ) {
@@ -203,6 +262,7 @@ export default {
203262 folderStruct .forEach (item => {
204263 if (item .children ) {
205264 const node = this .addDir (item, parent)
265+ node .children = []
206266 this .generateNewTree (item .children , node)
207267 } else {
208268 this .addLeaf (item, parent)
@@ -252,4 +312,11 @@ export default {
252312 color : gray ;
253313 font-size : 80% ;
254314 }
315+
316+ </style >
317+
318+ <style >
319+ .vtl-node.active {
320+ border : 1px solid red ;
321+ }
255322 </style >
0 commit comments