-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
190 lines (165 loc) · 6.03 KB
/
index.html
File metadata and controls
190 lines (165 loc) · 6.03 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>T-edi : Just a text editor</title>
<style>
body {
margin: 0;
overflow: hidden;
font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
}
textarea {
padding: 1rem;
padding-top: 0;
font-size: x-large;
font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
outline: none;
border: none;
width: 100%;
height: 88vh;
}
textarea:focus-visible {
outline: none;
border: none;
}
.clr-btn {
width: 1.5rem;
height: 1.5rem;
border-radius: 50%;
border: none;
outline: none;
cursor: pointer;
}
.toolbar {
position: static;
padding: 0.7rem;
gap: 10px;
display: flex;
justify-content: start;
align-items: center;
}
</style>
</head>
<body>
<div class="toolbar">
<button id="plus">+</button>
<button id="minus">−</button>
<input type="color" id="clr-inp">
<select name="font" id="font-options">
<option>System Default</option>
</select>
<button id="save">Save</button>
<button id="open">Open</button>
<a href="./mde">Markdown</a>
<div style="display: inline;">
<input type="checkbox" name="saveLocal" id="saveLocal" checked>
<label for="saveLocal">Save locally</label>
</div>
</div>
<textarea placeholder="Just write..."></textarea>
<script>
let fontSize = 20;
let interval = null;
let plus = document.getElementById('plus');
let minus = document.getElementById('minus');
let textarea = document.querySelector('textarea');
let clrInp = document.getElementById('clr-inp');
let fontInp = document.getElementById('font-options');
let saveLocal = document.getElementById('saveLocal');
textarea.style.fontSize = fontSize + 'px';
plus.addEventListener('click', () => {
fontSize += 2;
textarea.style.fontSize = fontSize + 'px';
});
minus.addEventListener('click', () => {
fontSize -= 2;
textarea.style.fontSize = fontSize + 'px';
});
clrInp.addEventListener('change', () => {
textarea.style.color = clrInp.value;
});
fontInp.addEventListener('change', () => {
textarea.style.fontFamily = fontInp.value;
localStorage.setItem('t-edi-font', fontInp.value);
});
window.onload = function () {
textarea.focus();
let text = localStorage.getItem('t-edi-text');
if (text) {
textarea.value = text;
}
interval = setInterval(saveInLocalStorage, 1000);
if ("queryLocalFonts" in window) {
loadSystemFontsList();
} else {
fontInp.style.display = 'none';
}
}
document.getElementById('save').addEventListener('click', saveDoc);
document.getElementById('open').addEventListener('click', openDoc);
saveLocal.addEventListener('change', (e) => {
if (e.target.checked) {
interval = setInterval(saveInLocalStorage, 1000);
} else {
clearInterval(interval);
this.localStorage.removeItem('t-edi-text');
}
});
function saveDoc() {
if (!confirm('Do you want to save the document?')) return;
let text = textarea.value;
let blob = new Blob([text], { type: 'text/plain' });
let url = URL.createObjectURL(blob);
let a = document.createElement('a');
a.href = url;
a.download = 't-edi-text.txt';
a.click();
}
function openDoc() {
if (!confirm('Do you want to open a document? Current text will be lost.')) return;
let input = document.createElement('input');
input.type = 'file';
input.multiple = false;
input.accept = 'text/plain';
input.click();
input.addEventListener('change', () => {
let file = input.files[0];
let reader = new FileReader();
reader.readAsText(file);
reader.onload = function () {
textarea.value = reader.result;
}
});
}
function saveInLocalStorage() {
localStorage.setItem('t-edi-text', textarea.value);
}
async function loadSystemFontsList() {
try {
const availableFonts = await window.queryLocalFonts();
const fontFamilies = availableFonts.filter(font => font.style === 'Regular').map(font => font.family);
for (const font of fontFamilies) {
const option = document.createElement('option');
option.innerText = font;
option.value = font;
option.style.fontFamily = font;
fontInp.appendChild(option);
}
loadFontFromLocalStorage();
} catch (err) {
console.error(err.name, err.message);
}
}
function loadFontFromLocalStorage() {
const font = localStorage.getItem('t-edi-font');
if (font) {
fontInp.value = font;
textarea.style.fontFamily = font;
}
}
</script>
</body>
</html>