-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
117 lines (107 loc) · 3.09 KB
/
index.ts
File metadata and controls
117 lines (107 loc) · 3.09 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
import "./index.s";
import { createFileTree } from "./file-tree";
import { faker } from "@faker-js/faker";
import eruda from "eruda";
eruda.init();
function randomIntFromInterval(min: number, max: number) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
function randomFileItem() {
const isDirectory = Math.random() >= 0.5;
return {
name: isDirectory
? faker.string.alpha({ length: { min: 4, max: 12 } })
: faker.system.commonFileName(),
isDirectory
};
}
function fakeDirectoryContent() {
const itemsCount = randomIntFromInterval(1, 10);
return new Array(itemsCount).fill(null).map(randomFileItem);
}
function copy(text: string) {
var input = document.createElement("input");
input.setAttribute("value", text);
document.body.appendChild(input);
input.select();
var result = document.execCommand("copy");
document.body.removeChild(input);
return result;
}
const cache = {};
const iconCache = {};
const colorCache = new Set();
const directoryIconOpen = document.createElement("div");
directoryIconOpen.innerText = "🔽";
const directoryIconClose = document.createElement("div");
directoryIconClose.innerText = "▶️";
const fileTree = createFileTree({
readDirectory: (path) => {
console.log(path);
let content = cache[path];
if (!content) {
content = fakeDirectoryContent();
cache[path] = content;
}
return content;
},
isDirectory: (_) => randomFileItem().isDirectory,
directoryIcons: {
open: directoryIconOpen,
close: directoryIconClose
},
prefix: (path) => {
let imgSrc = iconCache[path];
if (!imgSrc) {
imgSrc = faker.image.avatar();
iconCache[path] = imgSrc;
}
const img = document.createElement("img");
img.src = imgSrc;
return img;
},
suffix: (_) => {
const div = document.createElement("div");
div.innerText = "···";
return div;
},
classes: (path) => {
if (colorCache.has(path)) return ["red"];
return [];
},
onSelect: (path) => copy(path),
onRename: (oldPath: string, newPath: string) => {
console.log(oldPath, newPath);
fileTree.removeItem(oldPath);
fileTree.addItem(newPath);
}
});
document.body.append(fileTree.container);
const input = document.createElement("input");
const add = document.createElement("button");
add.innerText = "Add";
add.onclick = () => {
const v = input.value;
fileTree.addItem(v);
input.value = "";
};
const remove = document.createElement("button");
remove.innerText = "Remove";
remove.onclick = () => {
const v = input.value;
fileTree.removeItem(v);
input.value = "";
};
const update = document.createElement("button");
update.innerText = "Update";
update.onclick = () => {
const v = input.value;
if (colorCache.has(v)) {
colorCache.delete(v);
} else {
colorCache.add(v);
}
fileTree.refreshItem(v);
input.value = "";
};
document.body.append(input, add, remove, update);