| 1 |
import { h, t, getById, debounce, writeError, writeInfo, resetLog } from "/common.module.js"; |
| 2 |
import lzString from "/lz-string.module.js"; |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
const htmlPrefix = "h:"; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
const allowedTags = new Set([ |
| 12 |
"B", "STRONG", "I", "EM", "U", "UL", "OL", "LI", "DIV", "P", "BR", |
| 13 |
"TABLE", "THEAD", "TBODY", "TFOOT", "TR", "TH", "TD", |
| 14 |
]); |
| 15 |
const droppedTags = new Set(["SCRIPT", "STYLE", "TITLE", "TEXTAREA"]); |
| 16 |
|
| 17 |
const input = getById("input"); |
| 18 |
const toolbar = getById("toolbar"); |
| 19 |
const tableToggle = getById("table-toggle"); |
| 20 |
const tableTools = getById("table-tools"); |
| 21 |
const buttons = [...toolbar.querySelectorAll("button[data-command]")]; |
| 22 |
const saveButton = getById("save"); |
| 23 |
const savedNotes = getById("saved-notes"); |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
const database = "mb-storage"; |
| 28 |
const notesStore = decodeURIComponent(new URL(savedNotes.href).hash.substring(1)); |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
const idParam = "id"; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
const tableActions = { |
| 39 |
rowAbove: cell => focusCell(insertRow(cell.parentElement, "before"), cell.cellIndex), |
| 40 |
rowBelow: cell => focusCell(insertRow(cell.parentElement, "after"), cell.cellIndex), |
| 41 |
columnLeft: cell => placeCaret(insertColumn(cell, "before")), |
| 42 |
columnRight: cell => placeCaret(insertColumn(cell, "after")), |
| 43 |
deleteRow, |
| 44 |
deleteColumn, |
| 45 |
}; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
let written = null; |
| 50 |
|
| 51 |
document.execCommand("defaultParagraphSeparator", false, "div"); |
| 52 |
document.execCommand("styleWithCSS", false, false); |
| 53 |
|
| 54 |
|
| 55 |
toolbar.addEventListener("mousedown", event => { |
| 56 |
if (event.target.closest("button")) event.preventDefault(); |
| 57 |
}); |
| 58 |
toolbar.addEventListener("click", event => { |
| 59 |
const button = event.target.closest("button"); |
| 60 |
if (!button) return; |
| 61 |
input.focus(); |
| 62 |
const { command, table } = button.dataset; |
| 63 |
if (command) { |
| 64 |
document.execCommand(command); |
| 65 |
} else if (table === "toggle") { |
| 66 |
|
| 67 |
|
| 68 |
const cell = selectedCell(); |
| 69 |
if (cell) { |
| 70 |
removeTable(cell.closest("table")); |
| 71 |
} else { |
| 72 |
insertTable(); |
| 73 |
} |
| 74 |
changed(); |
| 75 |
} else if (table) { |
| 76 |
const cell = selectedCell(); |
| 77 |
if (!cell) return; |
| 78 |
tableActions[table](cell); |
| 79 |
changed(); |
| 80 |
} |
| 81 |
updateToolbar(); |
| 82 |
}); |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
input.addEventListener("keydown", event => { |
| 88 |
if (event.key !== "Tab" || event.ctrlKey || event.altKey || event.metaKey) return; |
| 89 |
const item = selectedElement()?.closest("li"); |
| 90 |
const cell = selectedCell(); |
| 91 |
if (item && (!cell || cell.contains(item))) { |
| 92 |
event.preventDefault(); |
| 93 |
document.execCommand(event.shiftKey ? "outdent" : "indent"); |
| 94 |
} else if (cell && moveFromCell(cell, event.shiftKey ? -1 : 1)) { |
| 95 |
event.preventDefault(); |
| 96 |
} |
| 97 |
}); |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
input.addEventListener("paste", event => { |
| 102 |
const html = event.clipboardData?.getData("text/html"); |
| 103 |
if (!html) return; |
| 104 |
event.preventDefault(); |
| 105 |
document.execCommand("insertHTML", false, h("div", clean(parse(html))).innerHTML); |
| 106 |
}); |
| 107 |
|
| 108 |
input.addEventListener("input", debounce(() => { |
| 109 |
if (input.textContent === '') { |
| 110 |
written = ''; |
| 111 |
} |
| 112 |
else { |
| 113 |
const html = h("div", clean(input.childNodes)).innerHTML; |
| 114 |
written = '#' + htmlPrefix + lzString.compressToEncodedURIComponent(html); |
| 115 |
} |
| 116 |
window.location.hash = written; |
| 117 |
|
| 118 |
if (written === '') setId(null); |
| 119 |
resetLog(); |
| 120 |
}, 10)); |
| 121 |
|
| 122 |
document.addEventListener("selectionchange", updateToolbar); |
| 123 |
|
| 124 |
saveButton.addEventListener("click", async () => { |
| 125 |
resetLog(); |
| 126 |
try { |
| 127 |
await save(); |
| 128 |
} catch (e) { |
| 129 |
writeError(e); |
| 130 |
} |
| 131 |
}); |
| 132 |
|
| 133 |
window.addEventListener('hashchange', loadState); |
| 134 |
loadState(); |
| 135 |
|
| 136 |
function loadState() { |
| 137 |
if (window.location.hash === written) return; |
| 138 |
written = window.location.hash; |
| 139 |
|
| 140 |
const hash = window.location.hash.substring(1); |
| 141 |
if (hash === '') { |
| 142 |
input.replaceChildren(); |
| 143 |
return; |
| 144 |
} |
| 145 |
|
| 146 |
const isHtml = hash.startsWith(htmlPrefix); |
| 147 |
const note = lzString.decompressFromEncodedURIComponent(isHtml ? hash.substring(htmlPrefix.length) : hash); |
| 148 |
if (!note) { |
| 149 |
|
| 150 |
writeError("Failed to load note from url."); |
| 151 |
return; |
| 152 |
} |
| 153 |
input.replaceChildren(...(isHtml ? clean(parse(note)) : lines(note))); |
| 154 |
} |
| 155 |
|
| 156 |
function updateToolbar() { |
| 157 |
for (const button of buttons) { |
| 158 |
button.setAttribute("aria-pressed", String(document.queryCommandState(button.dataset.command))); |
| 159 |
} |
| 160 |
const inTable = selectedCell() !== null; |
| 161 |
tableToggle.setAttribute("aria-pressed", String(inTable)); |
| 162 |
tableToggle.title = inTable ? "Delete this table" : "Insert a table"; |
| 163 |
tableTools.hidden = !inTable; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
function changed() { |
| 168 |
input.dispatchEvent(new Event("input")); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
async function save() { |
| 176 |
if (window.location.hash === '') { |
| 177 |
writeError("There is nothing to save yet."); |
| 178 |
return; |
| 179 |
} |
| 180 |
const id = new URLSearchParams(window.location.search).get(idParam) ?? newId(); |
| 181 |
setId(id); |
| 182 |
|
| 183 |
const entry = { title: firstLine(), url: window.location.href }; |
| 184 |
const db = await openNotesStore(); |
| 185 |
try { |
| 186 |
const saved = await done(db.transaction(notesStore).objectStore(notesStore).get(id)); |
| 187 |
const unchanged = saved?.url === entry.url && saved?.title === entry.title; |
| 188 |
if (!unchanged) { |
| 189 |
const transaction = db.transaction(notesStore, "readwrite"); |
| 190 |
transaction.objectStore(notesStore).put(entry, id); |
| 191 |
await done(transaction); |
| 192 |
} |
| 193 |
writeInfo([ |
| 194 |
unchanged ? "Already saved in " : saved ? "Updated in " : "Saved in ", |
| 195 |
h("a", { href: savedNotes.href }, notesStore), |
| 196 |
".", |
| 197 |
]); |
| 198 |
} finally { |
| 199 |
|
| 200 |
db.close(); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
function setId(id) { |
| 206 |
const url = new URL(window.location.href); |
| 207 |
if (id === null) { |
| 208 |
url.searchParams.delete(idParam); |
| 209 |
} else { |
| 210 |
url.searchParams.set(idParam, id); |
| 211 |
} |
| 212 |
|
| 213 |
if (url.href !== window.location.href) history.replaceState(history.state, "", url); |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
function newId() { |
| 218 |
return Array.from(crypto.getRandomValues(new Uint8Array(6)), b => b.toString(16).padStart(2, "0")).join(""); |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
function firstLine() { |
| 223 |
return input.innerText.split("\n").map(line => line.trim()).find(line => line !== '') ?? "Untitled"; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
async function openNotesStore() { |
| 232 |
const db = await openDatabase(); |
| 233 |
if (db.objectStoreNames.contains(notesStore)) return db; |
| 234 |
db.close(); |
| 235 |
return openDatabase(db.version + 1, upgrading => { |
| 236 |
|
| 237 |
if (!upgrading.objectStoreNames.contains(notesStore)) { |
| 238 |
upgrading.createObjectStore(notesStore, { autoIncrement: true }); |
| 239 |
} |
| 240 |
}); |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
function openDatabase(version, upgrade) { |
| 249 |
return new Promise((resolve, reject) => { |
| 250 |
const request = indexedDB.open(database, version); |
| 251 |
request.onupgradeneeded = () => upgrade?.(request.result); |
| 252 |
request.onsuccess = () => { |
| 253 |
request.result.onversionchange = () => request.result.close(); |
| 254 |
resolve(request.result); |
| 255 |
}; |
| 256 |
request.onerror = () => reject(request.error); |
| 257 |
request.onblocked = () => writeInfo("Waiting for the Storage page in another tab to let go of the database."); |
| 258 |
}); |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
function done(pending) { |
| 267 |
return new Promise((resolve, reject) => { |
| 268 |
if (pending instanceof IDBTransaction) { |
| 269 |
pending.oncomplete = () => resolve(undefined); |
| 270 |
pending.onabort = () => reject(pending.error); |
| 271 |
} else { |
| 272 |
pending.onsuccess = () => resolve(pending.result); |
| 273 |
} |
| 274 |
pending.onerror = () => reject(pending.error); |
| 275 |
}); |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
function selectedElement() { |
| 280 |
const node = document.getSelection()?.anchorNode; |
| 281 |
return node instanceof Element ? node : node?.parentElement ?? null; |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
function selectedCell() { |
| 286 |
const cell = selectedElement()?.closest("td, th"); |
| 287 |
return cell && input.contains(cell) ? cell : null; |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
function placeCaret(node) { |
| 292 |
const range = document.createRange(); |
| 293 |
range.selectNodeContents(node); |
| 294 |
range.collapse(true); |
| 295 |
const selection = document.getSelection(); |
| 296 |
selection.removeAllRanges(); |
| 297 |
selection.addRange(range); |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
function insertTable() { |
| 305 |
const table = h("table", [h("tbody", [newRow(2), newRow(2)])]); |
| 306 |
const line = currentLine(); |
| 307 |
if (line === null) { |
| 308 |
input.prepend(table); |
| 309 |
} else if (line instanceof Element && ["DIV", "P"].includes(line.tagName) |
| 310 |
&& line.textContent === '' && !line.querySelector("table")) { |
| 311 |
line.replaceWith(table); |
| 312 |
} else { |
| 313 |
endOfLine(line).after(table); |
| 314 |
} |
| 315 |
if (!table.nextSibling) { |
| 316 |
table.after(h("div", [h("br")])); |
| 317 |
} |
| 318 |
placeCaret(table.rows[0].cells[0]); |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
function currentLine() { |
| 326 |
const selection = document.getSelection(); |
| 327 |
let node = selection?.anchorNode; |
| 328 |
if (!node || !input.contains(node)) return null; |
| 329 |
if (node === input) return input.childNodes[selection.anchorOffset - 1] ?? null; |
| 330 |
while (node.parentNode !== input) node = node.parentNode; |
| 331 |
return node; |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
function endOfLine(node) { |
| 340 |
const isBlock = n => n instanceof Element && ["DIV", "P", "UL", "OL", "TABLE"].includes(n.tagName); |
| 341 |
while (!isBlock(node) && node.nodeName !== "BR" && node.nextSibling && !isBlock(node.nextSibling)) { |
| 342 |
node = node.nextSibling; |
| 343 |
} |
| 344 |
return node; |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
function newCell(tag = "td") { |
| 349 |
return h(tag, [h("br")]); |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
function newRow(count) { |
| 354 |
return h("tr", Array.from({ length: count }, () => newCell())); |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
function columnCount(table) { |
| 362 |
return Math.max(...[...table.rows].map(row => row.cells.length)); |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
function insertRow(row, where) { |
| 370 |
const created = newRow(columnCount(row.closest("table"))); |
| 371 |
row[where](created); |
| 372 |
return created; |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
function insertColumn(cell, where) { |
| 381 |
const index = cell.cellIndex; |
| 382 |
for (const row of cell.closest("table").rows) { |
| 383 |
const reference = row.cells[index]; |
| 384 |
if (reference) { |
| 385 |
|
| 386 |
reference[where](newCell(reference.localName)); |
| 387 |
} else { |
| 388 |
row.append(newCell()); |
| 389 |
} |
| 390 |
} |
| 391 |
return where === "before" ? cell.previousElementSibling : cell.nextElementSibling; |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
function deleteRow(cell) { |
| 396 |
const row = cell.parentElement; |
| 397 |
const table = row.closest("table"); |
| 398 |
const rows = [...table.rows]; |
| 399 |
const neighbour = rows[rows.indexOf(row) + 1] ?? rows[rows.indexOf(row) - 1]; |
| 400 |
row.remove(); |
| 401 |
if (neighbour) { |
| 402 |
focusCell(neighbour, cell.cellIndex); |
| 403 |
} else { |
| 404 |
removeTable(table); |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
function deleteColumn(cell) { |
| 410 |
const index = cell.cellIndex; |
| 411 |
const row = cell.parentElement; |
| 412 |
const table = row.closest("table"); |
| 413 |
for (const r of [...table.rows]) { |
| 414 |
r.cells[index]?.remove(); |
| 415 |
if (r.cells.length === 0) r.remove(); |
| 416 |
} |
| 417 |
if (table.rows.length === 0) { |
| 418 |
removeTable(table); |
| 419 |
} else { |
| 420 |
focusCell(row.isConnected ? row : table.rows[0], index); |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
function removeTable(table) { |
| 430 |
const next = table.nextElementSibling; |
| 431 |
if (next?.tagName === "DIV" && next.textContent === '' && !next.querySelector("table")) { |
| 432 |
table.remove(); |
| 433 |
placeCaret(next); |
| 434 |
} else { |
| 435 |
const line = h("div", [h("br")]); |
| 436 |
table.replaceWith(line); |
| 437 |
placeCaret(line); |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
function focusCell(row, index) { |
| 446 |
placeCaret(row.cells[Math.min(index, row.cells.length - 1)]); |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
function moveFromCell(cell, step) { |
| 455 |
const table = cell.closest("table"); |
| 456 |
const cells = [...table.rows].flatMap(row => [...row.cells]); |
| 457 |
const next = cells[cells.indexOf(cell) + step]; |
| 458 |
if (next) { |
| 459 |
placeCaret(next); |
| 460 |
} else if (step > 0) { |
| 461 |
focusCell(insertRow(cell.parentElement, "after"), 0); |
| 462 |
changed(); |
| 463 |
} else { |
| 464 |
return false; |
| 465 |
} |
| 466 |
return true; |
| 467 |
} |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
function parse(html) { |
| 476 |
return new DOMParser().parseFromString(html, "text/html").body.childNodes; |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
function clean(nodes) { |
| 485 |
const result = []; |
| 486 |
for (const node of nodes) { |
| 487 |
if (node.nodeType === Node.TEXT_NODE) { |
| 488 |
result.push(t(node.data)); |
| 489 |
} else if (node.nodeType !== Node.ELEMENT_NODE || droppedTags.has(node.tagName)) { |
| 490 |
|
| 491 |
} else if (allowedTags.has(node.tagName)) { |
| 492 |
result.push(h(node.tagName.toLowerCase(), clean(node.childNodes))); |
| 493 |
} else { |
| 494 |
result.push(...clean(node.childNodes)); |
| 495 |
} |
| 496 |
} |
| 497 |
return result; |
| 498 |
} |
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
function lines(text) { |
| 507 |
return text.split("\n").map(line => |
| 508 |
line === '' ? h("div", [h("br")]) : h("div", line.replace(/ (?= )/g, " "))); |
| 509 |
} |