-
Notifications
You must be signed in to change notification settings - Fork 958
Expand file tree
/
Copy pathfileList.js
More file actions
512 lines (450 loc) · 11 KB
/
fileList.js
File metadata and controls
512 lines (450 loc) · 11 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
import fsOperation from "fileSystem";
import toast from "components/toast";
import { minimatch } from "minimatch";
import Url from "utils/Url";
import { addedFolder } from "./openFolder";
import settings from "./settings";
/**
* @typedef {import('fileSystem').File} File
*/
const filesTree = {};
const events = {
"add-file": [],
"remove-file": [],
"add-folder": [],
"remove-folder": [],
refresh: [],
};
export function initFileList() {
if (editorManager?.activeFile.loading) {
editorManager.activeFile.on("loadend", initFileList);
return;
}
// editorManager.on('add-folder', onAddFolder);
editorManager.on("remove-folder", onRemoveFolder);
settings.on("update:excludeFolders:after", refresh);
}
/**
* Add a file to the list
* @param {string} parent file directory
* @param {string} child file url
*/
export async function append(parent, child) {
const tree = getTree(Object.values(filesTree), parent);
if (!tree || !tree.children) return;
const childTree = await Tree.create(child);
tree.children.push(childTree);
getAllFiles(childTree);
emit("add-file", childTree);
}
/**
* Remove a file from the list
* @param {string} item url
*/
export function remove(item) {
if (filesTree[item]) {
delete filesTree[item];
emit("remove-file", item);
return;
}
const tree = getTree(Object.values(filesTree), item);
if (!tree) return;
const { parent } = tree;
const index = parent.children.indexOf(tree);
parent.children.splice(index, 1);
emit("remove-file", tree);
}
/**
* Refresh file list
*/
export async function refresh() {
Object.keys(filesTree).forEach((key) => {
delete filesTree[key];
});
await Promise.all(
addedFolder.map(async ({ url, title }) => {
const tree = await Tree.createRoot(url, title);
filesTree[url] = tree;
getAllFiles(tree);
}),
);
emit("refresh", filesTree);
}
/**
* Renames a tree
* @param {string} oldUrl
* @param {string} newUrl
* @returns
*/
export function rename(oldUrl, newUrl) {
const tree = getTree(Object.values(filesTree), oldUrl);
if (!tree) return;
tree.update(newUrl);
}
/**
* Get all files in a folder
* @param {string|()=>object} dir
* @returns {Tree[]}
*/
export default function files(dir) {
const listedDirs = [];
let transform = (item) => item;
if (typeof dir === "string") {
return Object.values(filesTree).find((item) => getFile(dir, item));
} else if (typeof dir === "function") {
transform = dir;
}
const allFiles = [];
Object.values(filesTree).forEach((item) => {
allFiles.push(...flattenTree(item, transform, listedDirs));
});
return allFiles;
}
/**
* @typedef {'add-file'|'remove-file'|'add-folder'|'remove-folder'|'refresh'} FileListEvent
*/
/**
* Adds event listener for file list
* @param {FileListEvent} event - Event name
* @param {(tree:Tree)=>void} callback - Callback function
*/
files.on = function (event, callback) {
if (!events[event]) events[event] = [];
events[event].push(callback);
};
/**
* Removes event listener for file list
* @param {FileListEvent} event - Event name
* @param {(tree:Tree)=>void} callback - Callback function
*/
files.off = function (event, callback) {
if (!events[event]) return;
events[event] = events[event].filter((cb) => cb !== callback);
};
/**
* Are we doing a search?
* @type {boolean}
*/
files.search;
/**
* Get directory tree
* @param {Tree[]} treeList list of tree
* @param {string} dir path to find
* @returns {Tree}
*/
function getTree(treeList, dir) {
if (!treeList) return;
let tree = treeList.find(({ url }) => url === dir);
if (tree) return tree;
for (const item of treeList) {
tree = getTree(item.children, dir);
if (tree) return tree;
}
return null;
}
/**
* Get all files in a folder
* e.g /dir1/dir2/dir3
* This function will first test if dir1 exists in the tree,
* if not, it will return null, otherwise it will traverse the tree
* and return the files in dir3
* @param {string} path - Folder path
* @param {Tree} tree - Files tree
*/
function getFile(path, tree) {
const { children } = tree;
let { url } = tree;
if (url === path) return tree;
if (!children) return null;
const len = children.length;
for (let i = 0; i < len; i++) {
const item = children[i];
const result = getFile(path, item);
if (result) return result;
}
return null;
}
/**
* Get all files
* @param {Tree} tree
* @param {(item:Tree)=>object} transform
*/
function flattenTree(tree, transform, listedDirs) {
const list = [];
const { children } = tree;
if (!children) {
return [transform(tree)];
}
if (listedDirs.includes(tree.url)) return list;
listedDirs.push(tree.url);
children.forEach((item) => {
if (item.children) list.push(...flattenTree(item, transform, listedDirs));
else list.push(transform(item));
});
return list;
}
/**
* Called when a folder is added
* @param {{url: string, name: string}} folder - Folder path
*/
export async function addRoot({ url, name }) {
try {
const TERMUX_STORAGE =
"content://com.termux.documents/tree/%2Fdata%2Fdata%2Fcom.termux%2Ffiles%2Fhome::/data/data/com.termux/files/home/storage";
const TERMUX_SHARED =
"content://com.termux.documents/tree/%2Fdata%2Fdata%2Fcom.termux%2Ffiles%2Fhome::/data/data/com.termux/files/home/storage/shared";
if (url === TERMUX_STORAGE) return;
if (url === TERMUX_SHARED) return;
const tree = await Tree.createRoot(url, name);
filesTree[url] = tree;
getAllFiles(tree);
emit("add-folder", tree);
} catch (error) {
// ignore
window.log("error", error);
}
}
/**
* Called when a folder is removed
* @param {{url: string, name: string}} folder - Folder path
*/
function onRemoveFolder({ url }) {
const tree = filesTree[url];
if (!tree) return;
delete filesTree[url];
emit("remove-folder", tree);
}
/**
* Get all file recursively
* @param {Tree} parent - An array to store files
* @param {Tree} [root] - Root path
*/
async function getAllFiles(parent, root) {
root = root || parent.root;
if (!parent.children || !root.isConnected) return;
try {
const entries = await fsOperation(parent.url).lsDir();
const promises = [];
for (const item of entries) {
promises.push(createChildTree(parent, item, root));
}
await Promise.all(promises);
} catch (error) {
// retry after 3s
parent.retriedCount += 1;
if (parent.retriedCount > settings.value.maxRetryCount) return;
if (settings.value.showRetryToast) {
toast(`retrying: ${parent.path}`);
}
setTimeout(() => {
// why not outside? because parent may be removed
if (!root.isConnected) return;
parent.children.length = 0;
getAllFiles(parent);
}, 3000);
}
}
/**
* Emit an event
* @param {string} event
* @param {...any} args
*/
function emit(event, ...args) {
const list = events[event];
if (!list) return;
list.forEach((fn) => fn(...args));
}
/**
* Create a child tree
* @param {Tree} parent
* @param {File} item
* @param {Tree} root
*/
async function createChildTree(parent, item, root) {
if (!root.isConnected) return;
const { name, url, isDirectory } = item;
const exists = parent.children.findIndex(({ value }) => value === url);
if (exists > -1) {
return;
}
const file = await Tree.create(url, name, isDirectory);
if (!root.isConnected) return;
const ignores = files.search
? [...settings.value.excludeFiles, ...settings.value.searchExclude]
: settings.value.excludeFiles;
const ignore = !!ignores.find((folder) =>
minimatch(file.path, folder, { matchBase: true }),
);
if (ignore) return;
const existingTree = getTree(Object.values(filesTree), file.url);
if (existingTree) {
file.children = existingTree.children;
parent.children.push(file);
return;
}
parent.children.push(file);
if (isDirectory) {
getAllFiles(file, root);
return;
}
emit("push-file", file);
}
export class Tree {
/**@type {string}*/
#name;
/**@type {string}*/
#url;
/**@type {string}*/
#path;
/**@type {Array<Tree>}*/
#children;
/**@type {Tree}*/
#parent;
retriedCount = 0;
/**
* Create a tree using constructor
* @param {string} name
* @param {string} root
* @param {string} url
* @param {boolean} isDirectory
*/
constructor(name, url, isDirectory) {
this.#name = name;
this.#url = url;
this.#children = isDirectory ? this.#childrenArray() : null;
this.#parent = null;
}
#childrenArray() {
const ar = [];
const oldPush = ar.push;
ar.push = (...args) => {
args.forEach((item) => {
if (!(item instanceof Tree)) throw new Error("Invalid tree");
item.parent = this;
oldPush.call(ar, item);
});
};
return ar;
}
/**
* Create a tree
* @param {string} url file url
* @param {string} [name] file name
* @param {boolean} [isDirectory] if the file is a directory
*/
static async create(url, name, isDirectory) {
if (!name && !isDirectory) {
const stat = await fsOperation(url).stat();
name = stat.name;
isDirectory = stat.isDirectory;
}
return new Tree(name, url, isDirectory);
}
/**
* Create a root tree
* @param {string} url
* @param {string} name
* @returns
*/
static async createRoot(url, name) {
const tree = await Tree.create(url, name, true);
tree.#path = name;
return tree;
}
/**@returns {string} */
get name() {
return this.#name;
}
/**@returns {string} */
get url() {
return this.#url;
}
/**@returns {string} */
get path() {
return this.#path;
}
/**@returns {Array<Tree>} */
get children() {
return this.#children;
}
set children(value) {
if (!Array.isArray(value)) throw new Error("Invalid children");
this.#children = value;
}
/**@returns {Tree} */
get parent() {
return this.#parent;
}
/**@param {Tree} value */
set parent(value) {
if (!(value instanceof Tree)) throw new Error("Invalid parent");
this.#parent = value;
if (this.#parent) {
this.#path = Url.join(this.#parent.path, this.#name);
}
}
/**
* Check if the root of the tree is added to the open folder list.
* @returns {boolean}
*/
get isConnected() {
const root = this.root;
return !!addedFolder.find(({ url }) => url === root.url);
}
/**
* Get the root of the tree
* @returns {Tree}
*/
get root() {
let root = this;
while (root.parent) {
root = root.parent;
}
return root;
}
/**
* Update tree name and url
* @param {string} url
* @param {string} [name]
*/
update(url, name) {
if (!name) name = Url.basename(url);
this.#url = url;
this.#name = name;
this.#path = Url.join(this.#parent.path, name);
getAllFiles(this);
}
/**
* @typedef {object} TreeJson
* @property {string} name
* @property {string} url
* @property {string} path
* @property {string} parent
* @property {boolean} isDirectory
*/
/**
* To tree object to json
* @returns {TreeJson}
*/
toJSON() {
return {
name: this.#name,
url: this.#url,
path: this.#path,
parent: this.#parent?.url,
isDirectory: !!this.#children,
};
}
/**
* Create a tree from json
* @param {TreeJson} json
* @returns {Tree}
*/
static fromJSON(json) {
const { name, url, path, parent, isDirectory } = json;
const tree = new Tree(name, url, isDirectory);
tree.#parent = getTree(Object.values(filesTree), parent);
tree.#path = path;
return tree;
}
}