-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
83 lines (68 loc) · 2.35 KB
/
Copy pathapp.js
File metadata and controls
83 lines (68 loc) · 2.35 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
const editor = document.getElementById("editor");
const saveBtn = document.getElementById("saveBtn");
const loadBtn = document.getElementById("loadBtn");
const themeBtn = document.getElementById("themeBtn");
const linedBtn = document.getElementById("linedBtn");
const fileNameLabel = document.getElementById("fileName");
let currentFileName = "Untitled.txt";
let isDirty = false;
function updateTitle() {
const mark = isDirty ? "*" : "";
document.title = `${mark}JDL - ${currentFileName}`;
fileNameLabel.textContent = currentFileName;
}
function saveFile() {
const text = editor.value;
const link = document.createElement("a");
link.href = "data:text/plain;charset=utf-8," + encodeURIComponent(text);
link.download = currentFileName;
link.click();
isDirty = false;
updateTitle();
}
function loadFile() {
const input = document.createElement("input");
input.type = "file";
input.accept = ".txt,text/plain,*/*";
input.addEventListener("change", () => {
const file = input.files && input.files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = () => {
editor.value = String(reader.result ?? "");
currentFileName = file.name || "Untitled.txt";
isDirty = false;
updateTitle();
editor.focus();
};
reader.readAsText(file);
});
input.click();
}
function toggleTheme() {
const next = document.body.dataset.theme === "light" ? "dark" : "light";
document.body.dataset.theme = next;
themeBtn.textContent = next === "dark" ? "Light Mode" : "Dark Mode";
themeBtn.setAttribute("aria-pressed", String(next === "dark"));
}
function toggleLines() {
const next = document.body.dataset.lined === "on" ? "off" : "on";
document.body.dataset.lined = next;
linedBtn.textContent = next === "on" ? "Lines: On" : "Lines: Off";
linedBtn.setAttribute("aria-pressed", String(next === "on"));
}
editor.addEventListener("input", () => {
isDirty = true;
updateTitle();
});
saveBtn.addEventListener("click", saveFile);
loadBtn.addEventListener("click", loadFile);
themeBtn.addEventListener("click", toggleTheme);
linedBtn.addEventListener("click", toggleLines);
window.addEventListener("beforeunload", (e) => {
if (!isDirty) return;
e.preventDefault();
e.returnValue = "";
});
updateTitle();
editor.focus();