forked from creatorrr/itty-bitty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit.js
424 lines (350 loc) · 10.5 KB
/
edit.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
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
var QS = document.querySelector.bind(document);
var QSS = document.querySelectorAll.bind(document);
var DATA_PREFIX = "data:text/html;base64,";
var DATA_PREFIX_8 = "data:text/html;charset=utf-8;base64,";
var DATA_PREFIX_BXZE = "data:text/html;charset=utf-8;bxze64,";
var b = document.documentElement.setAttribute(
"data-useragent",
navigator.userAgent
);
var importedFileData = undefined;
var content = undefined;
window.onload = function() {
document.addEventListener("notesSaved", renderSavedNotes);
window.onpopstate = function(e) {
setContent(e.state);
};
window.onhashchange = function(e) {
console.log("hash", e);
location.reload();
};
document.body.onclick = function(e) {
if (e.target == document.body) content.focus();
};
content = document.getElementById("content");
content.ondragenter = function(e) {
document.body.classList.add("drag");
};
content.ondragleave = function(e) {
document.body.classList.remove("drag");
};
content.addEventListener("keydown", handleKey);
content.addEventListener("keyup", handleInput);
QS("#doc-title").addEventListener("keyup", handleInput);
content.addEventListener("drop", handleDrop);
content.addEventListener("paste", handlePaste);
content.contentEditable = "true";
content.focus();
document.execCommand("selectAll", false, null);
QS("#qrcode").onclick = makeQRCode;
QS("#copy").onclick = copyLink;
var hash = window.location.hash.substring(1);
if (hash.length) {
var slashIndex = hash.indexOf("/");
var title = hash.substring(0, slashIndex);
if (title.length)
QS("#doc-title").innerText = document.title = decodeURIComponent(
title.replace(/_/g, " ")
);
hash = hash.substring(slashIndex + 1);
updateLink(hash, title);
if (hash.startsWith("?")) {
hash = hash.substring(1);
zipToString(hash, setContent);
}
} else {
updateBodyClass();
}
// Set title to today's date on load, if not set
var titleElement = document.getElementById("doc-title");
if (titleElement.innerText.trim() === "") {
var today = new Date().toDateString();
titleElement.innerText = today + " " + randomAlphanum();
}
// Trigger load saved notes from localStorage
setSavedNotes(getSavedNotes());
};
function getSavedNotes() {
var notesJSON = window.localStorage.getItem("notesStore") || "[]";
var notes = JSON.parse(notesJSON);
return notes;
}
function setSavedNotes(notes) {
notes = notes || [];
var notesJSON = JSON.stringify(
notes.filter(({ link, title }) => link && title)
);
window.localStorage.setItem("notesStore", notesJSON);
// Trigger notesSaved event
var event = new CustomEvent("notesSaved", { detail: notes });
document.dispatchEvent(event);
return notes;
}
function renderSavedNotes(event) {
var template = ({ link, title }) => `
<li>
<a href="${link}" class="link-title" target="_blank"> ${title} </a>
<a class="remove" href="#"> × </a>
</li>
`;
var savedContainer = document.getElementById("saved");
savedContainer.innerHTML = null;
return event.detail.map(note => {
var element = htmlToElement(template(note));
var removeBtn = element.querySelector(".remove");
removeBtn.onclick = removeNoteByLink.bind(null, note.link);
savedContainer.appendChild(element);
return element;
});
}
function removeNoteByLink(link, event) {
event.preventDefault();
var currentNotes = getSavedNotes();
var prevLinks = currentNotes.map(function(note) {
return note.link;
});
var existingIndex = prevLinks.indexOf(link);
if (!!~existingIndex) {
currentNotes.splice(existingIndex, 1);
}
return setSavedNotes(currentNotes);
}
function saveNoteToLocalStorage(link, title, prevLink) {
var currentNotes = getSavedNotes();
var prevLinks = currentNotes.map(function(note) {
return note.link;
});
var existingIndex = prevLinks.indexOf(prevLink);
var note = { link: link, title: title };
if (!!~existingIndex) {
currentNotes[existingIndex] = note;
} else {
currentNotes.push(note);
}
return setSavedNotes(currentNotes);
}
function setContent(html) {
content.innerHTML = html;
updateBodyClass();
}
function setFileName(name) {
QS("#doc-file").innerText = name;
if (name.length) {
setContent("");
document.body.classList.add("edited");
}
}
function updateBodyClass() {
var length = content.innerText.length;
if (length || importedFileData) {
document.body.classList.add("edited");
} else {
document.body.classList.remove("edited");
}
document.body.classList.add("loaded");
}
function handleDrop(e) {
e.preventDefault();
if (e.dataTransfer.files) {
var file = e.dataTransfer.files[0];
var reader = new FileReader();
reader.addEventListener(
"load",
function() {
var url = reader.result;
url = url.replace(DATA_PREFIX, DATA_PREFIX_8);
compressDataURI(url, function(url2) {
var ratio = url2.length / url.length;
console.log("Compressed to", ratio);
if (e.ctrlKey)
decompressDataURI(url2, undefined, function(url3) {
console.log("Verified", url == url3);
});
if (ratio > 0.95) url2 = url;
if (e.altKey) url2 = url2.replace(DATA_PREFIX_BXZE, "!");
importedFileData = url2;
updateLink(url2, file.name, true);
setFileName("📄" + file.name);
});
},
false
);
reader.readAsDataURL(file);
}
document.body.classList.remove("drag");
}
// TODO Command+Shift+T for title (H1), Command+Shift+H for headline (H2), Command+Shift+B for body text (remove any of the above)
function handleKey(e) {
var code = e.which;
var handled = false;
if (e.metaKey && e.altKey) {
handled = true;
if (code == "1".charCodeAt(0)) {
document.execCommand("formatBlock", true, "<h1>");
} else if (code == "2".charCodeAt(0)) {
document.execCommand("formatBlock", true, "<h2>");
} else if (code == 220) {
// \
document.execCommand("removeFormat");
} else if (code == "0".charCodeAt(0)) {
document.execCommand("formatBlock", true, "");
} else {
handled = false;
}
} else if (e.metaKey) {
if (code == "K".charCodeAt(0)) {
handled = true;
var url = prompt("Add a link", "");
if (url) {
document.execCommand("createLink", true, url);
}
}
}
if (handled) e.preventDefault();
}
var codepenRE = /(https:\/\/codepen\.io\/[\w]+\/(\w+)\/(\w+))/;
function handlePaste(e) {
var clipboard = window.clipboardData || e.clipboardData;
var text = clipboard.getData("Text") || clipboard.getData("text/plain");
if ((match = text.match(codepenRE))) {
fetchCodepen(match[0]);
}
}
var TEMPLATE_MARKER = "/*use-itty-bitty-template*/";
function fetchCodepen(url) {
var h, c, j;
$.when(
$.get({ url: url + ".html", cache: false }, function(html) {
h = html;
}),
$.get({ url: url + ".css", cache: false }, function(css) {
c = css;
}),
$.get({ url: url + ".js", cache: false }, function(js) {
j = js;
})
).then(function() {
var useTemplate = c.indexOf(TEMPLATE_MARKER) >= 0;
var string =
'<style type="text/css">' +
c +
"</style>" +
h +
'<script type="text/javascript">' +
j +
"</script>";
stringToZip(string, function(zip) {
setFileName("✒️" + url);
var title = QS("#doc-title").innerText;
setTimeout(function() {
var data = (useTemplate ? "" : DATA_PREFIX_BXZE) + zip;
importedFileData = data;
updateLink(data, title);
}, 300);
});
});
}
function handleInput(e) {
updateBodyClass();
var text = content.innerText;
var title = QS("#doc-title").innerText;
var rawHTML = text.indexOf("</") > 0;
if (rawHTML) {
text = text.replace(/[\n|\t]+/g, " ").replace(/> +</g, "> <");
} else {
text = content.innerHTML;
}
if (text.trim().length) {
stringToZip(text, function(zip) {
if (rawHTML) {
updateLink(DATA_PREFIX_BXZE + zip, title);
} else {
updateLink("?" + zip, title);
}
});
setFileName("");
} else if (importedFileData) {
updateLink(importedFileData, title);
} else {
updateLink("");
}
}
var maxLengths = {
// "#twitter": 4088,
// "#bitly": 2048,
"#qrcode": 2953
};
function updateLink(url, title, push) {
var originalTitle = title;
if (title) title = encodeURIComponent(title.trim().replace(/\s/g, "_"));
if (url.length) {
url = "/#" + (title || "") + "/" + url;
} else {
url = "/edit";
}
var hash = location.hash;
var originalUrl = "/" + hash;
if (push || !hash || !hash.length) {
window.history.pushState(content.innerHTML, null, url);
} else {
window.history.replaceState(content.innerHTML, null, url);
}
var length = location.href.length;
QS("#length").innerText = length + " bytes";
QS("#length").href = url;
for (var key in maxLengths) {
var maxLength = maxLengths[key];
if (length > maxLength) {
QS(key).classList.add("invalid");
} else {
QS(key).classList.remove("invalid");
}
}
// All done? Update localStorage
saveNoteToLocalStorage(url, originalTitle, originalUrl);
}
function makeQRCode() {
var url =
"https://zxing.org/w/chart?cht=qr&chs=548x548&chld=L|1&choe=UTF-8&chl=" +
encodeURIComponent(location.href);
this.href = url;
}
function toggleMenu() {
QS("#toolbar").classList.toggle("menu-visible");
}
function copyThenLink() {
copyLink();
return confirm("Copied your link to the clipboard. Paste it to share.");
}
function copyLink() {
var text = location.href;
var dummy = document.createElement("input");
document.body.appendChild(dummy);
dummy.value = text;
dummy.select();
document.execCommand("copy");
document.body.removeChild(dummy);
document.body.addClass("copied");
setTimeout(function() {
document.body.removeClass("copied");
}, 300);
}
function saveLink() {
var url = "/" + location.hash;
window.history.pushState(null, null, url);
location.reload();
}
function tweetLink() {
var url =
"https://twitter.com/intent/tweet?url=" + encodeURIComponent(location.href);
window.open(url, "_blank");
}
function htmlToElement(html) {
var template = document.createElement("template");
html = html.trim(); // Never return a text node of whitespace as the result
template.innerHTML = html;
return template.content.firstChild;
}
function randomAlphanum() {
return btoa(Math.ceil(Math.random() * 10000).toString()).replace(/=/g, "");
}