-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
324 lines (278 loc) · 8.11 KB
/
app.js
File metadata and controls
324 lines (278 loc) · 8.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
(() => {
const THEME_KEY = "bigrichtextbox:theme:v1";
/** @type {HTMLButtonElement | null} */
const themeToggleEl = document.getElementById("themeToggle");
/** @type {HTMLButtonElement | null} */
const shareToggleEl = document.getElementById("shareToggle");
/** @type {HTMLElement | null} */
const toastEl = document.getElementById("toast");
const DARK_ICON = "🌙";
const LIGHT_ICON = "☀️";
function getStoredTheme() {
try {
const v = localStorage.getItem(THEME_KEY);
return v === "light" || v === "dark" ? v : "";
} catch {
return "";
}
}
function setTheme(theme) {
if (theme === "light" || theme === "dark") {
document.documentElement.setAttribute("data-theme", theme);
try {
localStorage.setItem(THEME_KEY, theme);
} catch {
}
} else {
document.documentElement.removeAttribute("data-theme");
try {
localStorage.removeItem(THEME_KEY);
} catch {
}
}
if (themeToggleEl) {
const current = document.documentElement.getAttribute("data-theme") || "system";
themeToggleEl.textContent = current === "dark" ? DARK_ICON : LIGHT_ICON;
}
}
if (themeToggleEl) {
const initial = getStoredTheme();
setTheme(initial || "dark");
themeToggleEl.addEventListener("click", () => {
const current = document.documentElement.getAttribute("data-theme");
setTheme(current === "dark" ? "light" : "dark");
});
}
const STORAGE_KEY = "bigrichtextbox:quill-delta:v1";
const HASH_PREFIX = "#d=";
/** @type {HTMLElement | null} */
const editorEl = document.getElementById("editor");
/** @type {HTMLElement | null} */
const toolbarEl = document.getElementById("toolbar");
if (!editorEl || !toolbarEl) {
return;
}
if (typeof window.Quill !== "function") {
return;
}
const Quill = window.Quill;
const LZString = window.LZString || null;
const hljs = window.hljs || null;
const Font = Quill.import("formats/font");
Font.whitelist = ["inter", "serif", "monospace"];
Quill.register(Font, true);
function showToolbar() {
toolbarEl.hidden = false;
}
function hideToolbar() {
toolbarEl.hidden = true;
}
function isToolbarFocused() {
const active = document.activeElement;
return !!(active && toolbarEl.contains(active));
}
function isEventInsideEditorOrToolbar(target) {
if (!(target instanceof Node)) {
return false;
}
return editorEl.contains(target) || toolbarEl.contains(target);
}
function debounce(fn, delayMs) {
/** @type {number | undefined} */
let t;
return () => {
if (t) {
window.clearTimeout(t);
}
t = window.setTimeout(fn, delayMs);
};
}
const quill = new Quill(editorEl, {
theme: "snow",
modules: {
toolbar: "#toolbar",
...(hljs ? { syntax: true } : {}),
},
formats: [
"header",
"font",
"bold",
"italic",
"underline",
"list",
"align",
"code-block",
],
});
// Expose for debugging / tests.
window.__quill = quill;
function setCodeLanguageAtSelection(lang) {
const range = quill.getSelection(true);
if (!range) return;
const language = String(lang || "").trim().toLowerCase();
// Quill stores code-block language as a line attribute on the terminating newline.
// Use formatLine so the language is applied to the current line(s) and persists.
quill.formatLine(range.index, Math.max(range.length, 1), "code-block", language || true);
const [line] = quill.getLine(range.index);
if (!line || !line.domNode) return;
const container = line.domNode.closest(".ql-code-block-container");
if (!container) return;
if (language) {
container.setAttribute("data-language", language);
} else {
container.removeAttribute("data-language");
}
}
function syncCodeLanguageSelect() {
const range = quill.getSelection();
if (!range) return;
const codeLangSelect = toolbarEl.querySelector("select.code-lang");
if (!codeLangSelect) return;
const [line] = quill.getLine(range.index);
if (!line || !line.domNode) return;
const container = line.domNode.closest(".ql-code-block-container");
if (!container) {
codeLangSelect.value = "";
return;
}
const current = container.getAttribute("data-language") || "";
codeLangSelect.value = current;
}
function getShareHash(delta) {
if (!LZString) return "";
try {
const payload = JSON.stringify(delta);
const compressed = LZString.compressToEncodedURIComponent(payload);
return `${HASH_PREFIX}${compressed}`;
} catch {
return "";
}
}
function readHashDelta() {
if (!LZString) return null;
const hash = window.location.hash || "";
if (!hash.startsWith(HASH_PREFIX)) {
return null;
}
const raw = hash.slice(HASH_PREFIX.length);
if (!raw) return null;
try {
const json = LZString.decompressFromEncodedURIComponent(raw);
if (!json) return null;
return JSON.parse(json);
} catch {
return null;
}
}
function saveNow() {
try {
const delta = quill.getContents();
localStorage.setItem(STORAGE_KEY, JSON.stringify(delta));
} catch {
}
}
const saveDebounced = debounce(saveNow, 250);
function restore() {
try {
const hashDelta = readHashDelta();
if (hashDelta) {
quill.setContents(hashDelta);
return;
}
const raw = localStorage.getItem(STORAGE_KEY);
if (!raw) {
return;
}
const parsed = JSON.parse(raw);
if (parsed) {
quill.setContents(parsed);
}
} catch {
}
}
restore();
quill.on("text-change", () => {
saveDebounced();
});
quill.on("selection-change", (range) => {
if (range) {
showToolbar();
syncCodeLanguageSelect();
} else {
if (isToolbarFocused()) {
showToolbar();
} else {
hideToolbar();
}
}
});
toolbarEl.addEventListener("focusin", () => {
showToolbar();
});
document.addEventListener("mousedown", (e) => {
const target = e.target;
if (isEventInsideEditorOrToolbar(target)) {
showToolbar();
return;
}
hideToolbar();
});
if (shareToggleEl) {
shareToggleEl.addEventListener("click", async () => {
if (!LZString) {
if (toastEl) {
toastEl.textContent = "Sharing needs LZ-String to load. Refresh the page.";
toastEl.classList.add("is-visible");
toastEl.setAttribute("aria-hidden", "false");
window.setTimeout(() => {
toastEl.classList.remove("is-visible");
toastEl.setAttribute("aria-hidden", "true");
}, 3000);
}
return;
}
const delta = quill.getContents();
const hash = getShareHash(delta);
if (!hash) {
return;
}
const url = `${window.location.origin}${window.location.pathname}${hash}`;
try {
await navigator.clipboard.writeText(url);
shareToggleEl.textContent = "✅";
if (toastEl) {
toastEl.textContent = "Share link copied. Paste it to share.";
toastEl.classList.add("is-visible");
toastEl.setAttribute("aria-hidden", "false");
}
} catch {
shareToggleEl.textContent = "✅";
if (toastEl) {
toastEl.textContent = "Share link ready. Copy from the address bar.";
toastEl.classList.add("is-visible");
toastEl.setAttribute("aria-hidden", "false");
}
}
window.setTimeout(() => {
shareToggleEl.textContent = "🔗";
}, 1200);
if (toastEl) {
window.setTimeout(() => {
toastEl.classList.remove("is-visible");
toastEl.setAttribute("aria-hidden", "true");
}, 3000);
}
});
}
toolbarEl.addEventListener("change", (e) => {
const target = e.target instanceof HTMLSelectElement ? e.target : null;
if (!target || !target.classList.contains("code-lang")) {
return;
}
const lang = target.value;
setCodeLanguageAtSelection(lang);
syncCodeLanguageSelect();
});
hideToolbar();
quill.focus();
})();