-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformatWebHelper.js
More file actions
354 lines (303 loc) · 10.3 KB
/
Copy pathformatWebHelper.js
File metadata and controls
354 lines (303 loc) · 10.3 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
let savedConfig = JSON.parse(localStorage.getItem("formatterConfig") || "{}");
let pendingFiles = null;
document.addEventListener("DOMContentLoaded", () => {
const els = {
fileUpload: document.getElementById("fileUpload"),
configUpload: document.getElementById("configUpload"),
formatButton: document.getElementById("formatButton"),
clearFiles: document.getElementById("clearFiles"),
modeToggle: document.getElementById("modeToggle"),
codeBox: document.getElementById("codeBox"),
configBox: document.getElementById("configBox"),
uploadContainer: document.querySelector(".upload-container"),
progressBar: document.getElementById("progressBar"),
progressContainer: document.getElementById("progressContainer"),
progressText: document.getElementById("progressText"),
};
els.fileUpload?.addEventListener("change", handleFileUpload);
els.configUpload?.addEventListener("change", handleConfigUpload);
els.formatButton?.addEventListener("click", handleFormatClick);
els.clearFiles?.addEventListener("click", clearFiles);
// near lil transition for dark and light mode ehehe
els.modeToggle.addEventListener("click", () => {
const body = document.body;
const h1 = document.querySelector("h1");
const githubLogo = document.querySelector('img[alt="GitHub"]');
[body, h1].forEach((el) => el.classList.toggle("light-mode"));
document
.querySelectorAll("textarea, .upload-container")
.forEach((el) => el.classList.toggle("light-mode"));
els.progressBar.classList.toggle("light-mode");
githubLogo.src = body.classList.contains("light-mode")
? "github-mark.png"
: "github-mark-white.png";
document
.querySelectorAll("button")
.forEach((button) => button.classList.remove("light-mode"));
els.modeToggle.textContent = `Switch to ${
body.classList.contains("light-mode") ? "Dark" : "Light"
} Mode`;
});
if (Object.keys(savedConfig).length > 0) {
els.configBox.value = JSON.stringify(savedConfig, null, 2);
}
const addDragEvents = (element) => {
const events = {
dragover: (e) => {
e.preventDefault();
e.stopPropagation();
element.classList.add("dragover");
},
dragleave: (e) => {
e.preventDefault();
e.stopPropagation();
element.classList.remove("dragover");
},
};
Object.entries(events).forEach(([event, handler]) =>
element.addEventListener(event, handler)
);
};
const handleDropEvent = (element, callback) => {
element.addEventListener("drop", async (e) => {
e.preventDefault();
e.stopPropagation();
element.classList.remove("dragover");
await callback(e);
});
};
const setupDragAndDrop = () => {
[els.codeBox, els.configBox, els.uploadContainer].forEach(addDragEvents);
handleDropEvent(els.codeBox, async (e) => {
const files = Array.from(e.dataTransfer.files);
if (files.length === 1) {
els.codeBox.value = await files[0].text();
} else {
pendingFiles = files;
alert(`${files.length} files loaded!\nClick Format to process them.`);
}
});
handleDropEvent(els.configBox, async (e) => {
const files = Array.from(e.dataTransfer.files);
if (files.length === 1) {
try {
const text = await files[0].text();
savedConfig = JSON.parse(text);
els.configBox.value = JSON.stringify(savedConfig, null, 2);
localStorage.setItem("formatterConfig", JSON.stringify(savedConfig));
} catch (e) {
console.error("Config parsing error:", e);
alert("Error loading configuration file - invalid JSON");
}
}
});
handleDropEvent(els.uploadContainer, async (e) => {
const entries = Array.from(e.dataTransfer.items)
.map((item) => item.webkitGetAsEntry?.())
.filter(Boolean);
const fileList = [];
const readEntry = async (entry, path = "") => {
if (entry.isFile) {
return new Promise((resolve) => {
entry.file((file) => {
file.webkitRelativePath = path + file.name;
fileList.push(file);
resolve();
});
});
}
if (entry.isDirectory) {
return new Promise((resolve) => {
entry.createReader().readEntries(async (entries) => {
await Promise.all(
entries.map((subEntry) =>
readEntry(subEntry, path + entry.name + "/")
)
);
resolve();
});
});
}
};
await Promise.all(entries.map((entry) => readEntry(entry)));
pendingFiles = fileList;
// file upload dialog doesnt update when dragging files so we have to do this
const dataTransfer = new DataTransfer();
fileList.forEach((file) => dataTransfer.items.add(file));
els.fileUpload.files = dataTransfer.files;
alert(
`${fileList.length} ${
fileList.length === 1 ? "file" : "files"
} loaded!\nClick Format to process them.`
);
});
};
setupDragAndDrop();
});
function clearFiles() {
pendingFiles = null;
document.getElementById("fileUpload").value = "";
}
function handleConfigUpload(event) {
const file = event.target.files[0];
const reader = new FileReader();
reader.onload = function () {
try {
savedConfig = JSON.parse(reader.result);
const configBox = document.getElementById("configBox");
configBox.value = JSON.stringify(savedConfig, null, 2);
localStorage.setItem("formatterConfig", JSON.stringify(savedConfig));
} catch (e) {
console.error("Config parsing error:", e);
alert("Error loading configuration file - invalid JSON");
}
};
reader.readAsText(file);
}
document.getElementById("configBox")?.addEventListener("change", function () {
try {
const configValue = JSON.parse(this.value);
savedConfig = configValue;
localStorage.setItem("formatterConfig", JSON.stringify(configValue));
} catch (e) {
console.error("Invalid JSON in config box:", e);
}
});
function handleFileUpload(event) {
const files = Array.from(event.target.files);
if (files.length === 0) return;
pendingFiles = files;
}
function handleFormatClick() {
const codeBoxContent = document.getElementById("codeBox").value.trim();
if (codeBoxContent) {
formatCode();
return;
}
if (!pendingFiles) {
alert("Please either enter code in the text box or upload files!");
return;
}
if (pendingFiles.length === 1 && pendingFiles[0].name.endsWith(".zip")) {
handleZipFile(pendingFiles[0]);
} else {
handleMultipleFiles(pendingFiles);
}
}
function showProgress(show) {
document.getElementById("progressContainer").style.display = show
? "block"
: "none";
}
function updateProgress(current, total, filename) {
const percent = (current / total) * 100;
document.getElementById("progressBar").value = percent;
document.getElementById(
"progressText"
).textContent = `Formatting file ${filename} (${current}/${total})`;
}
function handleZipFile(zipFile) {
const zip = new JSZip();
showProgress(true);
zip.loadAsync(zipFile).then(function (contents) {
const fileCount = Object.keys(contents.files).filter(
(name) => !contents.files[name].dir
).length;
let processed = 0;
processFiles(contents.files, zip, (filename) => {
processed++;
updateProgress(processed, fileCount, filename);
}).then(() => {
zip.generateAsync({ type: "blob" }).then(function (blob) {
showProgress(false);
downloadFile(blob, "formatter_output.zip");
});
});
});
}
function finalizeZipAndDownload(zip, singleFileName = null) {
if (singleFileName) {
const content = zip.files[singleFileName].async("string");
content.then((text) => {
showProgress(false);
downloadFile(text, singleFileName);
});
} else {
zip.generateAsync({ type: "blob" }).then((blob) => {
showProgress(false);
downloadFile(blob, "formatter_output.zip");
});
}
}
function handleMultipleFiles(files) {
const zip = new JSZip();
showProgress(true);
let processed = 0;
const processPromises = files.map((file) => {
return new Promise((resolve) => {
const reader = new FileReader();
reader.onload = function () {
const fileContent = reader.result;
const formattedContent = formatCodeForFile(fileContent);
const filePath = file.webkitRelativePath || file.name;
processed++;
updateProgress(processed, files.length, file.name);
zip.file(filePath, formattedContent);
resolve();
};
reader.readAsText(file);
});
});
Promise.all(processPromises).then(() => {
finalizeZipAndDownload(zip, files.length === 1 ? files[0].name : null);
});
}
async function processFiles(files, zip, onProgress) {
const promises = [];
for (const [filename, file] of Object.entries(files)) {
if (!file.dir) {
const promise = file.async("string").then((content) => {
const formattedContent = formatCodeForFile(content);
zip.file(filename, formattedContent);
if (onProgress) onProgress(filename);
});
promises.push(promise);
}
}
return Promise.all(promises);
}
function formatCodeForFile(code) {
let configText = savedConfig
? JSON.stringify(savedConfig)
: document.getElementById("configBox").value || "{}";
try {
return formatter.Formatter.formatString(code, configText);
} catch (e) {
console.error("Formatting error:", e);
return code;
}
}
function downloadFile(content, filename) {
const blob = new Blob([content], { type: "text/plain" });
const link = document.createElement("a");
link.href = URL.createObjectURL(blob);
link.download = filename;
link.click();
}
function formatCode() {
const code = document.getElementById("codeBox").value.trim();
if (!code) {
alert("Please enter some code to format!");
return;
}
let configText = savedConfig
? JSON.stringify(savedConfig)
: document.getElementById("configBox").value || "{}";
try {
const result = formatter.Formatter.formatString(code, configText);
document.getElementById("codeBox").value = result;
} catch (e) {
console.error("Formatting error:", e);
alert("Formatting error!\n\n" + e);
}
}