gitoria

notes

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Branchw7eeaea980report 007: say which runs included the removed checkmrew7/components/notes.hl

12.6 KB

  1. // components/notes.hl — THE NOTES PAGE, `/` and `/note/:id` (tickets notes#1, notes#2, notes#4): the sidebar (subject =
  2. // first line, last edited first), the open note in <md-editor>, New note, Delete. Plain Hybriel: project.hl lists this
  3. // page in `offline`, so hl:web keeps it — its service worker, the page's state in the browser (per user, forgotten at a
  4. // log-out) and a queue for the saves made without a network (replayed when it is back). No JavaScript of our own.
  5. //
  6. // notes what the server says: every live note of the user, with its text (lib/notes.hl notesFor). Kept offline.
  7. // __hlQueue the saves / deletes waiting for the network (hl:web keeps it across a reload)
  8. // edits what THIS tab typed: [{ key, text, sent, base, when }] — `sent` = the text last emitted
  9. // rows = notes, then the queue, then this tab's edits laid over (rowsOf) — the sidebar and the open note
  10. // A note made here has the key `l…` until the server gives it its id; the address follows (settle).
  11. // A save names `base` (the version the tab started from); the server continues a run of saves of one login, and the
  12. // later edit wins a conflict, the other becomes a copy (lib/notes.hl pushNote). Every save goes through the face
  13. // notesPut (statement form: queued offline), every change of a user's notes is pushed (notesChanged) to their tabs.
  14. // A tick (styles.hl `notesTick`, a 1 s CSS animation) sends what was typed 0.6 s ago and asks the server every 10 s.
  15. // The helper statics below must stay in THIS file: a braced import would make `rows` a server-only value.
  16. parent './main.hl'
  17. import { siteName } from '../project.hl'
  18. import { userIdOfSession, tagOf } from '../lib/users.hl'
  19. import { notesFor, pushNote, removeNote } from '../lib/notes.hl'
  20. import { isText, isTime } from '../lib/api-helpers.hl'
  21. // ---- the list: notes, then the queue, then this tab's edits ------------------------------------------------------------
  22. // does row `r` stand for key `k` (its server id, or the id the creating tab gave it)?
  23. static isKey = (r, k) => { return k != null && k != '' && (r.key == k || r.sid == k || (r.local != '' && r.local == k)) }
  24. static rowOf = (list, k) => {
  25. for (r of list) { if (isKey(r, k)) { return r } }
  26. return null
  27. }
  28. // a save laid over the list: the row it names gets the text (and moves up only when the text changed), or it is new
  29. static putRow = (list, sid, local, text, when, base) => {
  30. out = []
  31. let found = false
  32. k = sid != null && sid != '' ? sid : local
  33. for (r of list) {
  34. if (!found && (isKey(r, k) || (local != null && local != '' && isKey(r, local)))) {
  35. found = true
  36. out.push({ key = r.key sid = r.sid local = r.local text = text edited = text == r.text ? r.edited : when base = r.base })
  37. } else { out.push(r) }
  38. }
  39. if (!found) { out.push({ key = k sid = sid == null ? '' : sid local = local == null ? '' : local text = text edited = when base = base }) }
  40. return out
  41. }
  42. static dropRow = (list, k) => {
  43. out = []
  44. for (r of list) { if (!isKey(r, k)) { out.push(r) } }
  45. return out
  46. }
  47. // the language has no escapes: a newline is written as itself
  48. static newline = '
  49. '
  50. // the SUBJECT: the first non-empty line without leading `#`, at most 80 characters
  51. static subjectOf = (text) => {
  52. for (raw of text.split(newline)) {
  53. let line = raw.trim()
  54. while (line.length > 0 && line.slice(0, 1) == '#') { line = line.slice(1) }
  55. line = line.trim()
  56. if (line != '') { return line.length > 80 ? line.slice(0, 80) : line }
  57. }
  58. return 'Untitled note'
  59. }
  60. // last edited first (by hand: hybriel#1): each row goes in before the first one older than it
  61. static newestFirst = (list) => {
  62. let out = []
  63. for (r of list) {
  64. let next = []
  65. let placed = false
  66. for (x of out) {
  67. if (!placed && r.edited > x.edited) {
  68. next.push(r)
  69. placed = true
  70. }
  71. next.push(x)
  72. }
  73. if (!placed) { next.push(r) }
  74. out = next
  75. }
  76. return out
  77. }
  78. static rowsOf = (notes, queue, edits, dropped, sel) => {
  79. let list = []
  80. for (n of notes) { list.push({ key = n.id sid = n.id local = n.local text = n.text edited = n.edited base = n.edited }) }
  81. for (q of queue) {
  82. if (q.event == 'notesPut') { list = putRow(list, q.id, q.local, q.text, q.when, q.base) }
  83. if (q.event == 'notesDrop') { list = dropRow(list, q.id != '' ? q.id : q.local) }
  84. }
  85. for (e of edits) { list = putRow(list, rowOf(list, e.key) != null ? rowOf(list, e.key).sid : '', e.key, e.text, e.when, e.base) }
  86. for (k of dropped) { list = dropRow(list, k) }
  87. out = []
  88. for (r of newestFirst(list)) {
  89. out.push({ id = r.key key = r.key sid = r.sid local = r.local text = r.text edited = r.edited base = r.base subject = subjectOf(r.text) href = '/note/' + r.key cls = isKey(r, sel) ? 'selected' : '' })
  90. }
  91. return out
  92. }
  93. static editOf = (edits, k) => {
  94. for (e of edits) { if (e.key == k) { return e } }
  95. return null
  96. }
  97. static withEdit = (edits, e) => {
  98. out = []
  99. for (x of edits) { if (x.key != e.key) { out.push(x) } }
  100. out.push(e)
  101. return out
  102. }
  103. static withKey = (list, k) => {
  104. out = []
  105. for (x of list) { out.push(x) }
  106. out.push(k)
  107. return out
  108. }
  109. static withoutEdit = (edits, k) => {
  110. out = []
  111. for (e of edits) { if (e.key != k) { out.push(e) } }
  112. return out
  113. }
  114. // ---- the page's state ---------------------------------------------------------------------------------------------------
  115. session = null
  116. id = null
  117. signedIn = userIdOfSession(session) != null
  118. signedOut = userIdOfSession(session) == null
  119. __title = siteName
  120. notes = notesFor(userIdOfSession(session))
  121. __hlQueue = []
  122. __hlOnline = true
  123. edits = []
  124. dropped = []
  125. sel = id != null ? id : ''
  126. reachable = true
  127. loginGone = false
  128. saveError = ''
  129. asking = false
  130. ticks = 0
  131. pulling = false
  132. rows = rowsOf(notes, __hlQueue, edits, dropped, sel)
  133. openRow = rowOf(rows, sel)
  134. openList = openRow == null ? [] : [{ id = openRow.key }]
  135. shownText = openRow == null ? '' : openRow.text
  136. noneOpen = sel == ''
  137. openMissing = sel != '' && openRow == null
  138. noNotes = rows.length == 0
  139. bookClass = sel != '' ? 'open' : ''
  140. askClass = asking ? 'ask open' : 'ask'
  141. syncState = loginGone ? 'Your login has expired — log in again to sync your notes.' : (!__hlOnline || !reachable ? 'Offline — your changes are kept in this browser and sent when you are back online.' : '')
  142. View {
  143. if (signedOut) { p { id = "loginhint" class = "muted" "Log in with ident (top right) to see your notes." } }
  144. if (signedIn) {
  145. noteBook { id = "notesapp" class = bookClass
  146. noteList {
  147. header {
  148. h1 { "Notes" }
  149. button { id = "newnote" type = "button" "New note" on click(e) { emit create(e) } }
  150. }
  151. p { id = "syncstate" class = "muted" syncState }
  152. if (noNotes) { p { id = "empty" class = "empty" "No notes yet." } }
  153. ol { id = "notes"
  154. for (r of rows) { li { a { href = r.href class = r.cls "data-note" = r.key on click(e) { emit pick(e) } r.subject } } }
  155. }
  156. }
  157. noteReader {
  158. if (noneOpen) { p { id = "pick" class = "empty" "Select a note or create a new one." } }
  159. if (openMissing) {
  160. p { id = "missing" class = "empty" "This note does not exist any more. "
  161. a { href = "/" "data-note" = "" on click(e) { emit pick(e) } "Back to your notes" }
  162. }
  163. }
  164. for (o of openList) {
  165. noteEditor {
  166. a { class = "back" href = "/" "data-note" = "" on click(e) { emit pick(e) } "All notes" }
  167. mdEditor { textarea { id = "notetext" name = "text" rows = "16" value = shownText on input(e) { emit typed(e) } } }
  168. p { id = "saveerror" class = "message" saveError }
  169. footer {
  170. button { id = "delete" type = "button" class = "quiet" "Delete note" on click(e) { emit askDelete(e) } }
  171. p { id = "deleteask" class = askClass "Delete this note for good? "
  172. button { id = "deleteyes" type = "button" class = "danger" "Delete" on click(e) { emit dropOpen(e) } }
  173. button { id = "deleteno" type = "button" class = "quiet" "Keep" on click(e) { emit keepOpen(e) } }
  174. }
  175. }
  176. }
  177. }
  178. }
  179. notesTick { "aria-hidden" = "true" on animationiteration(e) { emit tick(e) } }
  180. }
  181. }
  182. }
  183. // ---- what the person does ----------------------------------------------------------------------------------------------
  184. // a link of the list (or "All notes", "Back"): opens the note in place — the address follows, no page load
  185. on pick(e) {
  186. if (e.button != 0 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) { return null }
  187. e.preventDefault()
  188. open(e.currentTarget.getAttribute('data-note'))
  189. }
  190. on create(e) {
  191. flush(0)
  192. k = 'l' + window.crypto.randomUUID().replaceAll('-', '').slice(0, 16)
  193. t = Date.now()
  194. edits = withEdit(edits, { key = k text = '' sent = '' base = 0 when = t })
  195. emit server notesPut({ key = k id = '' local = k text = '' base = 0 when = t })
  196. open(k)
  197. }
  198. on typed(e) {
  199. v = e.target.value
  200. r = rowOf(rows, sel)
  201. if (r == null) { return null }
  202. had = editOf(edits, sel)
  203. if (had == null && v == r.text) { return null }
  204. if (had != null && had.text == v) { return null }
  205. edits = withEdit(edits, { key = sel text = v sent = had != null ? had.sent : r.text base = had != null ? had.base : r.base when = Date.now() })
  206. }
  207. on askDelete(e) { asking = true }
  208. on keepOpen(e) { asking = false }
  209. on dropOpen(e) {
  210. r = rowOf(rows, sel)
  211. asking = false
  212. if (r != null) {
  213. emit server notesDrop({ key = r.key id = r.sid local = r.local base = r.base when = Date.now() })
  214. dropped = withKey(dropped, r.key)
  215. edits = withoutEdit(edits, r.key)
  216. }
  217. open('')
  218. }
  219. // the tick: what was typed 0.6 s ago goes to the server; every 10 s the server is asked for the other devices' changes
  220. on tick(e) {
  221. flush(600)
  222. ticks = ticks + 1
  223. if (ticks % 10 == 0) { pull() }
  224. }
  225. on client reconnected(r) { pull() }
  226. on client notesChanged(user, list) {
  227. notes = list
  228. settle(list)
  229. }
  230. on client notesSaveError(key, why) { saveError = why }
  231. open = (k) => {
  232. flush(0)
  233. sel = k == null ? '' : k
  234. asking = false
  235. saveError = ''
  236. window.history.replaceState(null, '', sel == '' ? '/' : '/note/' + sel)
  237. }
  238. // send this tab's typed text that is older than `waitMs` and not sent yet
  239. flush = (waitMs) => {
  240. t = Date.now()
  241. let changed = false
  242. next = []
  243. for (e of edits) {
  244. if (e.text != e.sent && t - e.when >= waitMs) {
  245. r = rowOf(rows, e.key)
  246. emit server notesPut({ key = e.key id = r != null ? r.sid : '' local = r != null && r.local != '' ? r.local : (e.key.slice(0, 1) == 'l' ? e.key : '') text = e.text base = e.base when = e.when })
  247. next.push({ key = e.key text = e.text sent = e.text base = e.base when = e.when })
  248. changed = true
  249. } else { next.push(e) }
  250. }
  251. if (changed) { edits = next }
  252. }
  253. pull = () => {
  254. if (pulling) { return null }
  255. pulling = true
  256. got = emit server notesPull()
  257. pulling = false
  258. if (got == null) {
  259. reachable = false
  260. return null
  261. }
  262. reachable = true
  263. loginGone = got.login == true
  264. if (got.notes != null) {
  265. notes = got.notes
  266. settle(got.notes)
  267. }
  268. }
  269. // THE SERVER'S LIST ARRIVED: an edit the server now has (or kept as this tab's conflict copy) is done; the open note
  270. // that got its server id takes it (and the address follows); a delete that went through is forgotten
  271. settle = (list) => {
  272. next = []
  273. for (e of edits) {
  274. let done = false
  275. for (n of list) {
  276. mine = n.id == e.key || (n.local != '' && n.local == e.key)
  277. if (mine && n.text == e.text && e.sent == e.text) { done = true }
  278. if (n.copyOf != '' && e.sent == e.text && n.text.startsWith(e.text) && rowOf(rows, e.key) != null && n.copyOf == rowOf(rows, e.key).sid) { done = true }
  279. }
  280. if (!done) { next.push(e) }
  281. }
  282. if (next.length != edits.length) { edits = next }
  283. still = []
  284. for (k of dropped) {
  285. let there = false
  286. for (n of list) { if (n.id == k || n.local == k) { there = true } }
  287. if (there) { still.push(k) }
  288. }
  289. if (still.length != dropped.length) { dropped = still }
  290. if (sel != '' && sel.slice(0, 1) == 'l') {
  291. for (n of list) {
  292. if (n.local == sel) {
  293. edits = withoutEdit(edits, sel)
  294. sel = n.id
  295. window.history.replaceState(null, '', '/note/' + n.id)
  296. }
  297. }
  298. }
  299. }
  300. // ---- the faces (thin: lib/notes.hl decides; the trailing `session` is always the server's, hybriel #16) ----------------
  301. on server notesPut(n, session) {
  302. u = userIdOfSession(session)
  303. if (u == null || n == null || !isText(n.id) || !isText(n.local) || !isText(n.text) || !isTime(n.base) || !isTime(n.when)) { return null }
  304. if (n.local.length > 64 || n.id.length > 64) { return null }
  305. r = pushNote(u, n.id, n.local, n.text, n.base, n.when, tagOf(session))
  306. if (r.error != null) { emit client notesSaveError(n.key, r.error) }
  307. emit client notesChanged(u, notesFor(u))
  308. }
  309. on server notesDrop(n, session) {
  310. u = userIdOfSession(session)
  311. if (u == null || n == null || !isText(n.id) || !isText(n.local) || !isTime(n.base)) { return null }
  312. removeNote(u, n.id, n.base, n.local, tagOf(session))
  313. emit client notesChanged(u, notesFor(u))
  314. }
  315. on server notesPull(session) {
  316. u = userIdOfSession(session)
  317. if (u == null) { return { login = true } }
  318. return { notes = notesFor(u) }
  319. }

Branches

Latest commits

  • eeaea980report 007: say which runs included the removed checkmre
  • 8a8ad9banotes mission 007: report, LOG, STATUSmre
  • 2cce9c96code order: plain declarations where never reassigned (letcount 0 never); gate: scan every IndexedDB at log-out, soft mode for old-code runs, drop a check that could not failmre
  • 7f6a4090README: offline in Hybriel (hl:web offline, continuation, known md-editor gap); gate waits report instead of crash; realdata-offline checkmre
  • 632223f2notes#4: delete notes-offline.js and sw.js (hl:web's generated worker and kept state replace them)mre
  • 52a2acadgate: offline checks on hl:web's worker/queue, Chrome offline via CDP (reload offline first), one-offline conflict, log-out forgets the kept copymre
  • e5785b03notes#4: the notes page in Hybriel — hl:web offline = [Notes], queued faces notesPut/notesDrop, push notesChanged, value-form notesPull on a tick; notes-offline.js/sw.js no longer loaded or routedmre
  • 510aff75notes#4: server continues a run of saves of one login (by + since), loser keeps writing into its copy; notesFor listmre
  • 0176c4b5mission brief 007mre
  • 06cfa870notes mission 006: re-vendor hybriel 0f396652 (static), reportmre
  • 6c07bd91mission brief w6mre
  • cf64d3d5notes mission 005: stopped at a Hybriel gap (for list repainted during hl:web boot throws, rowKidless); report, LOG, STATUSmre
  • a43b5d2dmission brief 005mre
  • 04a625edmission 004: re-vendor hybriel a832f73b (native bin + plugins as tickets), compose HL_CACHE_DIR volume + tmpfs /tmp, deploy URL wait 120 s, tests wait 120 s per start; gate 57/0 old and new; real-data same output. Not deployed.mre
  • aefb9d44mission brief w4mre
  • 202098b3docker-compose: init: true (hybriel#156, PID 1 ignored SIGTERM, every restart waited 10 s)mre
  • 304f6a9dnotes mission 003 (#5): report, STATUS, LOGmre
  • 9acc27f8notes mission 003 (#5): GitHub pipe tables in md-editor (parse, render, type, write) + gate checksmre
  • 2149e902notes mission 002 (4/4): code order — README file map + same-output test, STATUS, LOG, report; tests/letcount.py, tests/realdata-baseline.mjs, tests/realdata-compare.pymre
  • 47db4fc1notes mission 002 (3/4): code order — let only where reassigned (58 dropped; 34 left: 19 reassigned, 15 loop-bound); gates 50/0 + 18/0, live-data run = step 2mre