-
Notifications
You must be signed in to change notification settings - Fork 0
/
logic.js
78 lines (67 loc) · 2.22 KB
/
logic.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
import { marked } from "https://cdn.jsdelivr.net/npm/marked/lib/marked.esm.js";
document.addEventListener("DOMContentLoaded", () => {
const markdownInput = document.getElementById("markdown-input");
const htmlPreview = document.getElementById("html-preview");
const themeBox = document.getElementById("themeBox");
function updatePreview() {
const markdownText = markdownInput.value;
htmlPreview.innerHTML = marked(markdownText);
}
markdownInput.addEventListener("input", () => {
updatePreview();
saveContent();
});
updatePreview();
themeBox.addEventListener("change", () => {
document.body.className = "";
const selectedTheme = themeBox.options[themeBox.selectedIndex].className;
document.body.classList.add(`${selectedTheme}-theme`);
saveTheme(selectedTheme);
});
function saveContent() {
localStorage.setItem("markdown", markdownInput.value);
}
function loadContent() {
const savedMarkdown = localStorage.getItem("markdown");
if (savedMarkdown) {
markdownInput.value = savedMarkdown;
updatePreview();
}
}
function saveTheme(theme) {
localStorage.setItem("theme", theme);
}
function loadTheme() {
const savedTheme = localStorage.getItem("theme");
if (savedTheme) {
document.body.classList.add(`${savedTheme}-theme`);
const options = themeBox.options;
for (let i = 0; i < options.length; i++) {
if (options[i].className === savedTheme) {
themeBox.selectedIndex = i;
break;
}
}
}
}
loadContent();
loadTheme();
document.getElementById("export-md").addEventListener("click", () => {
const blob = new Blob([markdownInput.value], { type: "text/markdown" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "content.md";
a.click();
URL.revokeObjectURL(url);
});
document.getElementById("export-html").addEventListener("click", () => {
const blob = new Blob([htmlPreview.innerHTML], { type: "text/html" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "content.html";
a.click();
URL.revokeObjectURL(url);
});
});