gitoria

notes

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitfd36da27fd36da27notes mission 010: the note editor is the Hybriel MdEditor block (components.hybriel 1442e58, verbatim); md-editor.js, its route, script tag and offlineFiles retired; gate 66/0, realdata-offline 12/0, round-trip and Chrome E2E tests; report 010mrefd36da27/components/notes.hl

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

Branches

Latest commits

  • fd36da27notes mission 010: the note editor is the Hybriel MdEditor block (components.hybriel 1442e58, verbatim); md-editor.js, its route, script tag and offlineFiles retired; gate 66/0, realdata-offline 12/0, round-trip and Chrome E2E tests; report 010mre
  • efa92170notes tests/identkit.mjs: refuse a taken ident port, cold-wait deadline, emit, identities; mission 009 reportmre
  • f3dec0f1Merge branch 'w7'mre
  • d1cf966dre-vendor hybriel 97bcf128: offlineFiles for the editor script, 44 px taps, 390 px offline checks, mission 008mre
  • 4a610a21paths: projects moved into /media/STORAGE/projects/WorldAPI/mre
  • 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