Repository navigation
Expand file tree
/
Copy patheditor.js
More file actions
324 lines (276 loc) · 9.26 KB
/
Copy patheditor.js
File metadata and controls
324 lines (276 loc) · 9.26 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
"use strict";
const DRAFT_KEY = "writerapp.draft";
const PREFS_KEY = "writerapp.prefs";
const FONTS = ["ovo", "muli", "karla", "lusitana"];
const THEMES = ["felt", "purple", "process", "plain", "midnight"];
const SCHEMES = ["auto", "light", "dark"];
const FONT_SIZE_MIN = 10;
const FONT_SIZE_MAX = 32;
const FONT_SIZE_STEP = 2;
const FONT_SIZE_DEFAULT = 14;
const editor = document.getElementById("editor");
const fontSelect = document.getElementById("font-select");
const fontSizeDecrease = document.getElementById("font-size-decrease");
const fontSizeIncrease = document.getElementById("font-size-increase");
const fontSizeValue = document.getElementById("font-size-value");
const backgroundSelect = document.getElementById("background-select");
const schemeSelect = document.getElementById("scheme-select");
const settingsButton = document.getElementById("settings-button");
const settingsPopover = document.getElementById("settings-popover");
const saveButton = document.getElementById("save-button");
const loadButton = document.getElementById("load-button");
const fileInput = document.getElementById("file-input");
const wordCount = document.getElementById("word-count");
const charCount = document.getElementById("char-count");
const readingTime = document.getElementById("reading-time");
const autosaveStatus = document.getElementById("autosave-status");
const WORDS_PER_MINUTE = 200;
const STATUS_FADE_MS = 2000;
let autosaveTimer = null;
let statusFadeTimer = null;
let fontSize = FONT_SIZE_DEFAULT;
/** Editor appearance ********************************************************/
function setEditorFont(font) {
editor.classList.remove(...FONTS.map((f) => "font-" + f));
editor.classList.add("font-" + font);
}
function setEditorFontSize(size) {
editor.style.fontSize = size + "pt";
}
function updateFontSizeControls() {
fontSizeValue.textContent = fontSize;
fontSizeDecrease.disabled = fontSize <= FONT_SIZE_MIN;
fontSizeIncrease.disabled = fontSize >= FONT_SIZE_MAX;
}
function applyFontSize(size) {
fontSize = Math.min(FONT_SIZE_MAX, Math.max(FONT_SIZE_MIN, size));
setEditorFontSize(fontSize);
updateFontSizeControls();
}
function setBackground(theme) {
document.body.classList.remove(...THEMES.map((t) => "theme-" + t));
document.body.classList.add("theme-" + theme);
}
// "auto" follows the OS via the prefers-color-scheme media query (no
// attribute); "light"/"dark" set data-scheme on <html> so the higher-
// specificity CSS override in style.css wins regardless of the OS setting.
function setColorScheme(scheme) {
if (scheme === "light" || scheme === "dark") {
document.documentElement.setAttribute("data-scheme", scheme);
} else {
document.documentElement.removeAttribute("data-scheme");
}
}
function savePrefs() {
try {
localStorage.setItem(PREFS_KEY, JSON.stringify({
font: fontSelect.value,
fontSize: fontSize,
background: backgroundSelect.value,
colorScheme: schemeSelect.value,
}));
} catch (err) {
// Storage may be unavailable (private mode, quota); appearance just
// won't persist.
}
}
function restorePrefs() {
let prefs;
try {
prefs = JSON.parse(localStorage.getItem(PREFS_KEY));
} catch (err) {
return;
}
if (!prefs) {
return;
}
if (FONTS.includes(prefs.font)) {
fontSelect.value = prefs.font;
setEditorFont(prefs.font);
}
if (prefs.fontSize) {
applyFontSize(Number(prefs.fontSize));
}
if (THEMES.includes(prefs.background)) {
backgroundSelect.value = prefs.background;
setBackground(prefs.background);
}
if (SCHEMES.includes(prefs.colorScheme)) {
schemeSelect.value = prefs.colorScheme;
setColorScheme(prefs.colorScheme);
}
}
/** Word count ***************************************************************/
function countWords(text) {
const trimmed = text.trim();
return trimmed === "" ? 0 : trimmed.split(/\s+/).length;
}
function formatReadingTime(words) {
const minutes = Math.max(1, Math.ceil(words / WORDS_PER_MINUTE));
return minutes + " min read";
}
function updateCounts() {
const text = editor.value;
const words = countWords(text);
wordCount.textContent = words;
charCount.textContent = text.length;
readingTime.textContent = words > 0 ? " · " + formatReadingTime(words) : "";
}
/** Autosave *****************************************************************/
// Shows a brief status message in the status bar. Transient messages
// (the common case) fade out on their own after STATUS_FADE_MS; persistent
// ones (like a storage error) stay visible until replaced.
function showStatus(message, transient) {
clearTimeout(statusFadeTimer);
autosaveStatus.textContent = message;
autosaveStatus.classList.add("visible");
if (transient) {
statusFadeTimer = setTimeout(function () {
autosaveStatus.classList.remove("visible");
}, STATUS_FADE_MS);
}
}
function autosaveDraft() {
try {
localStorage.setItem(DRAFT_KEY, editor.value);
showStatus("Saved ✓", true);
} catch (err) {
showStatus("Autosave unavailable — save your work to a file", false);
}
}
function scheduleAutosave() {
clearTimeout(autosaveTimer);
autosaveTimer = setTimeout(autosaveDraft, 500);
}
function restoreDraft() {
let draft = null;
try {
draft = localStorage.getItem(DRAFT_KEY);
} catch (err) {
return;
}
if (draft) {
editor.value = draft;
showStatus("Draft restored", true);
}
}
/** Save and load files ******************************************************/
function saveToFile() {
const blob = new Blob([editor.value], {type: "text/plain;charset=utf-8"});
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = "MyAmbiWriterFile.txt";
link.click();
URL.revokeObjectURL(url);
}
function loadFromFile(file) {
const reader = new FileReader();
reader.onload = function (event) {
editor.value = event.target.result;
updateCounts();
autosaveDraft();
};
reader.onerror = function () {
alert("Could not read " + file.name + ".");
};
reader.readAsText(file);
}
/** Distraction-free writing mode ********************************************/
function enterWritingMode() {
document.body.classList.add("writing");
}
function exitWritingMode() {
document.body.classList.remove("writing");
}
/** Settings popover ***********************************************************/
function openSettings() {
settingsPopover.hidden = false;
settingsButton.setAttribute("aria-expanded", "true");
// The popover has its own focus target; don't let the chrome fade while
// it's open.
exitWritingMode();
}
function closeSettings() {
settingsPopover.hidden = true;
settingsButton.setAttribute("aria-expanded", "false");
}
function toggleSettings() {
if (settingsPopover.hidden) {
openSettings();
} else {
closeSettings();
}
}
/** Wiring *******************************************************************/
fontSelect.addEventListener("change", function () {
setEditorFont(fontSelect.value);
savePrefs();
});
fontSizeDecrease.addEventListener("click", function () {
applyFontSize(fontSize - FONT_SIZE_STEP);
savePrefs();
});
fontSizeIncrease.addEventListener("click", function () {
applyFontSize(fontSize + FONT_SIZE_STEP);
savePrefs();
});
backgroundSelect.addEventListener("change", function () {
setBackground(backgroundSelect.value);
savePrefs();
});
schemeSelect.addEventListener("change", function () {
setColorScheme(schemeSelect.value);
savePrefs();
});
settingsButton.addEventListener("click", function (event) {
event.stopPropagation();
toggleSettings();
});
document.addEventListener("click", function (event) {
if (!settingsPopover.hidden && !settingsPopover.contains(event.target)) {
closeSettings();
}
});
document.addEventListener("keydown", function (event) {
if (event.key === "Escape" && !settingsPopover.hidden) {
closeSettings();
settingsButton.focus();
}
});
saveButton.addEventListener("click", saveToFile);
document.addEventListener("keydown", function (event) {
if ((event.ctrlKey || event.metaKey) && event.key === "s") {
event.preventDefault();
saveToFile();
}
});
loadButton.addEventListener("click", function () {
fileInput.click();
});
fileInput.addEventListener("change", function () {
const file = fileInput.files[0];
if (!file) {
return;
}
if (editor.value.trim() !== "" &&
!confirm("Loading " + file.name + " will replace your current text. Continue?")) {
fileInput.value = "";
return;
}
loadFromFile(file);
fileInput.value = "";
});
editor.addEventListener("input", function () {
updateCounts();
scheduleAutosave();
enterWritingMode();
});
editor.addEventListener("keydown", enterWritingMode);
editor.addEventListener("blur", exitWritingMode);
document.addEventListener("mousemove", exitWritingMode);
document.addEventListener("touchstart", exitWritingMode);
updateFontSizeControls();
restorePrefs();
restoreDraft();
updateCounts();