-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.html
282 lines (231 loc) · 9.08 KB
/
index.html
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
---
layout: home
---
<script src="{{ 'js-base64/base64.js' | node_module_url | relative_url }}"></script>
<script src="{{ 'pako/dist/pako.min.js' | node_module_url | relative_url }}"></script>
<script src="{{ 'showdown/dist/showdown.min.js' | node_module_url | relative_url }}"></script>
{%- assign full_width_id = 'full-width' -%}
{%- capture toggle_full_width_id -%}toggle-{{ full_width_id }}{%- endcapture -%}
{%- assign markdown_preview_id = 'markdown-preview' -%}
{%- capture toggle_markdown_preview_id -%}toggle-{{ markdown_preview_id }}{%- endcapture -%}
{%- assign read_only_id = 'read-only' -%}
{%- capture toggle_read_only_id -%}toggle-{{ read_only_id }}{%- endcapture -%}
{%- assign textarea_id = 'textarea' -%}
{%- capture textarea_placeholder -%}
{{ site.tagline }}.
{{ site.description }}
- Doesn't need cookies = immune to data loss by accident
- Doesn't use a dedicated server = low risk of downtimes
---
One example use case is this:
- You start working on a new project.
- You open a new browser window / tab group for this.
- You open a {{ site.title | downcase }} to write down to do lists, ideas, reminders, etc.
- You safely close and reopen the browser or power off and restart your machine when needed.
- You get it synced to your other devices in supported browsers and update the notes on the go.
{%- endcapture -%}
<div class="toolbar">
<button id="{{ toggle_full_width_id }}">Toggle full-width</button>
<button id="{{ toggle_markdown_preview_id }}">Toggle Markdown preview</button>
<button id="{{ toggle_read_only_id }}">Toggle read-only</button>
</div>
<div class="content">
<textarea id="{{ textarea_id }}" placeholder="{{ textarea_placeholder }}" autofocus></textarea>
<div id="{{ markdown_preview_id }}"></div>
</div>
<script>
function main() {
if (document.readyState !== 'loading') {
document.addEventListener('DOMContentLoaded', run);
} else {
run();
}
}
function run() {
const toggleFullWidth = document.getElementById('{{ toggle_full_width_id }}');
loadFullWidthStyle(toggleFullWidth);
addFullWidthToggleClickListener(toggleFullWidth);
const toggleMarkdownPreview = document.getElementById('{{ toggle_markdown_preview_id }}');
const textarea = document.getElementById('{{ textarea_id }}');
const markdownPreview = document.getElementById('{{ markdown_preview_id }}');
loadMarkdownPreviewVisibility(toggleMarkdownPreview, markdownPreview);
addMarkdownPreviewToggleClickListener(toggleMarkdownPreview);
loadValue(textarea, markdownPreview);
addTextareaValueChangeListener(textarea);
addTextareaFocusChangeListener(textarea);
addURLHashListener(textarea, markdownPreview);
const toggleReadOnly = document.getElementById('{{ toggle_read_only_id }}');
loadReadOnlyStyle(toggleReadOnly, textarea);
addReadOnlyToggleClickListener(toggleReadOnly);
}
function loadFullWidthStyle(toggleFullWidth) {
const wrappers = document.getElementsByClassName('wrapper');
Array.from(wrappers).forEach((wrapper) => {
if (isFullWidthEnabled()) {
wrapper.style.maxWidth = 'none';
} else {
wrapper.style.maxWidth = null;
}
});
if (isFullWidthEnabled()) {
toggleFullWidth.classList.add('active');
}
}
function addFullWidthToggleClickListener(toggleFullWidth) {
toggleFullWidth.addEventListener('click', () => {
toggleSearchParam('{{ full_width_id }}');
}, false);
}
function isFullWidthEnabled() {
return isSearchParamEnabled('{{ full_width_id }}');
}
function addMarkdownPreviewToggleClickListener(toggleMarkdownPreview) {
toggleMarkdownPreview.addEventListener('click', () => {
toggleSearchParam('{{ markdown_preview_id }}');
}, false);
}
function loadMarkdownPreviewVisibility(toggleMarkdownPreview, markdownPreview) {
if (isMarkdownPreviewEnabled()) {
markdownPreview.style.display = null;
toggleMarkdownPreview.classList.add('active');
} else {
markdownPreview.style.display = 'none';
}
}
function isMarkdownPreviewEnabled() {
return isSearchParamEnabled('{{ markdown_preview_id }}');
}
function loadReadOnlyStyle(toggleReadOnly, textarea) {
if (isReadOnlyEnabled()) {
if (isMarkdownPreviewEnabled()) {
textarea.style.display = 'none';
} else {
textarea.readOnly = true;
}
toggleReadOnly.classList.add('active');
} else {
textarea.style.display = null;
textarea.readOnly = false;
}
}
function addReadOnlyToggleClickListener(toggleReadOnly) {
toggleReadOnly.addEventListener('click', () => {
toggleSearchParam('{{ read_only_id }}');
}, false);
}
function isReadOnlyEnabled() {
return isSearchParamEnabled('{{ read_only_id }}');
}
function loadValue(textarea, markdownPreview) {
const value = retrieveValue();
const oldValue = textarea.value;
textarea.value = value;
updateMarkdownPreviewIfNeeded(value, markdownPreview);
updateTitle(value, markdownPreview.textContent);
if (oldValue === '') {
textarea.selectionStart = value.length;
}
}
const debounce = (func, delay) => {
let debounceTimer;
return function () {
const context = this;
const args = arguments;
clearTimeout(debounceTimer);
debounceTimer = setTimeout(() => func.apply(context, args), delay);
};
};
function addTextareaValueChangeListener(textarea) {
textarea.addEventListener('input', debounce((event) => {
storeValue(event.target.value);
}, 300), false);
}
function addTextareaFocusChangeListener(textarea) {
textarea.addEventListener('blur', () => {
storeValue(textarea.value);
}, false);
window.addEventListener('blur', () => {
storeValue(textarea.value);
}, false);
}
function addURLHashListener(textarea, markdownPreview) {
window.addEventListener('hashchange', () => {
loadValue(textarea, markdownPreview);
textarea.focus();
});
}
function retrieveValue() {
const hash = window.location.hash;
if (hash === '') { return ''; }
return deserialize(hash.substring(1));
}
function storeValue(value) {
window.location.hash = '#' + serialize(value);
}
function updateTitle(value, markdownValue) {
const finalValue = markdownValue || value;
const titleParts = [];
const customParts = [];
const rawTitle = finalValue.split('\n', 1)[0];
if (rawTitle !== '') {
const truncatedTitle = truncate(rawTitle.trim(), 30);
customParts.push(truncatedTitle);
}
if (isMarkdownPreviewEnabled()) {
customParts.push('Markdown');
}
if (customParts.length > 0) {
titleParts.push(customParts.join(' - '));
}
titleParts.push('{{ site.tagline }}');
document.title = titleParts.join(' | ');
}
function updateMarkdownPreviewIfNeeded(value, markdownPreview) {
if (!isMarkdownPreviewEnabled()) { return; }
const converter = new showdown.Converter({
omitExtraWLInCodeBlocks: true,
noHeaderId: true,
parseImgDimensions: true,
simplifiedAutoLink: true,
literalMidWordUnderscores: true,
strikethrough: true,
tables: true,
tasklists: true,
smoothLivePreview: true,
simpleLineBreaks: true,
openLinksInNewWindow: true,
emoji: true,
splitAdjacentBlockquotes: true
});
const html = converter.makeHtml(value);
markdownPreview.innerHTML = html;
}
function toggleSearchParam(param) {
const searchParams = new URLSearchParams(window.location.search);
if (isSearchParamEnabled(param)) {
searchParams.delete(param);
} else {
searchParams.set(param, 'true');
}
document.location.search = searchParams.toString();
}
function isSearchParamEnabled(param) {
const searchParams = new URLSearchParams(window.location.search);
return searchParams.has(param);
}
function truncate(string, limit) {
if (string.length < limit) { return string; }
return string.slice(0, limit - 1) + "…";
}
function serialize(value) {
if (value === '') { return ''; }
const data = new TextEncoder().encode(value);
const compressed = pako.deflate(data, { level: 9 });
return Base64.fromUint8Array(compressed, true);
}
function deserialize(value) {
const data = Base64.toUint8Array(value);
return pako.inflate(data, { to: 'string' });
}
main();
</script>