-
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
// Copyright 2026 Shota FUJI <pockawoooh@gmail.com>
// SPDX-License-Identifier: MIT
class TabWidthSelector {
static LOCAL_STORAGE_KEY = "legit_tab_width";
static #isValidWidth(width) {
if (typeof width !== "string") {
return false;
}
const n = parseInt(width, 10);
return Number.isFinite(n) && n > 0;
}
static #selector() {
return document.getElementById("global-tab-width");
}
static sync() {
const saved = localStorage.getItem(this.LOCAL_STORAGE_KEY);
const value = this.#isValidWidth(saved) ? saved : "4";
document.body.style.tabSize = value;
const selector = this.#selector();
if (selector) {
selector.value = saved;
}
}
static addListener() {
this.#selector().addEventListener("change", event => {
if (!this.#isValidWidth(event.currentTarget.value)) {
return;
}
localStorage.setItem(this.LOCAL_STORAGE_KEY, event.currentTarget.value);
this.sync();
});
}
}
class ThemeColor {
static LOCAL_STORAGE_KEY = "legit_theme_color";
static VALID_COLORS = ["red", "orange", "green", "gray", "pink", "purple", "yellow"];
static #selector() {
return document.getElementById("theme-color");
}
static sync() {
const value = localStorage.getItem(this.LOCAL_STORAGE_KEY) || "";
if (value) {
document.documentElement.dataset.themeColor = value;
} else {
document.documentElement.dataset.themeColor = "";
}
const selector = this.#selector();
if (selector) {
selector.value = value;
}
}
static addListener() {
this.#selector()?.addEventListener("change", event => {
const value = event.currentTarget.value;
if (value && !this.VALID_COLORS.includes(value)) {
return;
}
if (value) {
localStorage.setItem(this.LOCAL_STORAGE_KEY, value);
} else {
localStorage.removeItem(this.LOCAL_STORAGE_KEY);
}
this.sync();
});
}
}
class TreeLayout {
static LOCAL_STORAGE_KEY = "legit_tree_layout";
static VALID_LAYOUTS = ["compact"];
static #selector() {
return document.getElementById("tree-layout");
}
static sync() {
const value = localStorage.getItem(this.LOCAL_STORAGE_KEY) || "";
for (const tree of document.getElementsByClassName("tree-row")) {
tree.dataset.layout = value;
}
const selector = this.#selector();
if (selector) {
selector.value = value;
}
}
static addListener() {
this.#selector()?.addEventListener("change", event => {
const value = event.currentTarget.value;
if (value && !this.VALID_LAYOUTS.includes(value)) {
return;
}
if (value) {
localStorage.setItem(this.LOCAL_STORAGE_KEY, value);
} else {
localStorage.removeItem(this.LOCAL_STORAGE_KEY);
}
this.sync();
});
}
}
class FileModeFormat {
static LOCAL_STORAGE_KEY = "legit_file_mode_format";
static VALID_FORMATS = ["octal", "hidden"];
static FILE_MODE_PATTERN = /^[d-]([r-][w-][x-]){3}$/;
static #selector() {
return document.getElementById("file-mode-format");
}
static sync() {
const value = localStorage.getItem(this.LOCAL_STORAGE_KEY) || "";
for (const el of document.querySelectorAll("[data-file-mode]")) {
const mode = el.dataset.fileMode;
if (!this.FILE_MODE_PATTERN.test(mode || "")) {
continue;
}
el.dataset.format = value;
switch (value) {
case "octal": {
const n = 0
+ (mode[1] === "r" ? 0o400 : 0)
+ (mode[2] === "w" ? 0o200 : 0)
+ (mode[3] === "x" ? 0o100 : 0)
+ (mode[4] === "r" ? 0o40 : 0)
+ (mode[5] === "w" ? 0o20 : 0)
+ (mode[6] === "x" ? 0o10 : 0)
+ (mode[7] === "r" ? 0o4 : 0)
+ (mode[8] === "w" ? 0o2 : 0)
+ (mode[9] === "x" ? 0o1 : 0);
el.textContent = n.toString(8).padStart(3, "0");
break;
}
default:
el.textContent = mode;
break;
}
}
const selector = this.#selector();
if (selector) {
selector.value = value;
}
}
static addListener() {
this.#selector()?.addEventListener("change", event => {
const value = event.currentTarget.value;
if (value && !this.VALID_FORMATS.includes(value)) {
return;
}
if (value) {
localStorage.setItem(this.LOCAL_STORAGE_KEY, value);
} else {
localStorage.removeItem(this.LOCAL_STORAGE_KEY);
}
this.sync();
});
}
}
class FileSizeFormat {
static LOCAL_STORAGE_KEY = "legit_file_size_format";
static VALID_FORMATS = ["hidden"];
static #selector() {
return document.getElementById("file-size-format");
}
static sync() {
const value = localStorage.getItem(this.LOCAL_STORAGE_KEY) || "";
for (const el of document.querySelectorAll("[data-file-size]")) {
const size = parseInt(el.dataset.fileSize, 10);
if (!Number.isFinite(size)) {
continue;
}
el.dataset.format = value;
}
const selector = this.#selector();
if (selector) {
selector.value = value;
}
}
static addListener() {
this.#selector()?.addEventListener("change", event => {
const value = event.currentTarget.value;
if (value && !this.VALID_FORMATS.includes(value)) {
return;
}
if (value) {
localStorage.setItem(this.LOCAL_STORAGE_KEY, value);
} else {
localStorage.removeItem(this.LOCAL_STORAGE_KEY);
}
this.sync();
});
}
}
function setupPreferencesDialog() {
const preferences = document.getElementById("preferences-dialog");
document.getElementById("preferences-dialog-trigger")?.addEventListener("click", (event) => {
preferences?.showModal();
});
document.getElementById("preferences-dialog-closer")?.addEventListener("click", (event) => {
preferences?.close();
});
}
TabWidthSelector.addListener();
ThemeColor.addListener();
TreeLayout.addListener();
FileModeFormat.addListener();
FileSizeFormat.addListener();
setupPreferencesDialog();
window.addEventListener("pageshow", event => {
TabWidthSelector.sync();
ThemeColor.sync();
TreeLayout.sync();
FileModeFormat.sync();
FileSizeFormat.sync();
});