-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.js
82 lines (63 loc) · 2.62 KB
/
settings.js
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
import { goBack, dialog, autoResizeTextarea } from "./helper.js";
import { chromeGetData, chromeSetData } from "./chrome.js";
const maxNotesChars = 999;
(function () {
restoreSettings();
loadEventListeners();
})();
function loadEventListeners() {
const $submitSettingsBtn = document.querySelector(".app-submit-settings");
const $notesTemplate = document.querySelector("#app-notes-template");
const $notesCurrentChars = document.querySelector(
".app-notes-current-chars-count"
);
const $darkModeToggle = document.querySelector("#app-dark-mode");
const $html = document.querySelector("html");
autoResizeTextarea($notesTemplate);
$submitSettingsBtn.addEventListener("click", submitSettings);
$notesTemplate.addEventListener("input", () => {
$notesCurrentChars.innerHTML = maxNotesChars - $notesTemplate.value.length;
});
$darkModeToggle.addEventListener("input", () => {
$html.dataset.theme = $darkModeToggle.checked ? "dark" : "light";
});
}
async function submitSettings() {
const $notesTemplate = document.querySelector("#app-notes-template");
const $resetNotes = document.querySelector("#app-reset-notes");
const $darkModeToggle = document.querySelector("#app-dark-mode");
// Get only 999 characters from notes before save
if ($notesTemplate.value.length > maxNotesChars) {
console.log("I'm not laying in bed with a fucked up");
$notesTemplate.value = $notesTemplate.value.substring(0, maxNotesChars);
}
chromeSetData("notesTemplate", $notesTemplate.value);
chromeSetData("resetNotes", $resetNotes.checked);
chromeSetData("enableDarkMode", $darkModeToggle.checked);
dialog({
content: "Your settings have been saved.",
type: "alert",
confirmCallback: () => {
goBack();
},
});
}
async function restoreSettings() {
const $notesTemplate = document.querySelector("#app-notes-template");
const $resetNotes = document.querySelector("#app-reset-notes");
const $darkModeToggle = document.querySelector("#app-dark-mode");
const $html = document.querySelector("html");
const $notesCurrentChars = document.querySelector(
".app-notes-current-chars-count"
);
const notesTemplate = await chromeGetData("notesTemplate");
const resetNotes = await chromeGetData("resetNotes");
const enableDarkMode = await chromeGetData("enableDarkMode");
$notesTemplate.value = notesTemplate;
$resetNotes.checked = resetNotes;
$darkModeToggle.checked = enableDarkMode;
$notesCurrentChars.innerHTML = maxNotesChars - $notesTemplate.value.length;
autoResizeTextarea($notesTemplate);
// If dark mode is enabled
$html.dataset.theme = enableDarkMode ? "dark" : "light";
}