gitoria

notes

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitd98775e8d98775e8tests/browser.mjs: wait for hydration before the clicks after a goto('/') (tables new note, phone sidebar)mred98775e8/components/notes.hl

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

Branches

Latest commits

  • d98775e8tests/browser.mjs: wait for hydration before the clicks after a goto('/') (tables new note, phone sidebar)mre
  • 1f3198b6tests/cdp.mjs: HL_CPU_THROTTLE=<rate> optional CPU throttling for gate runsmre
  • 39cf9fdfre-vendor Hybriel 5406d146 (hl:web batches 9+10: #72 emits while the socket is closed delivered after reconnect, browser boot linear, faster list appends; contains int 6–14 + m406)mre
  • db07aa84tests: short-id-switch ports from env (NOTES_SWITCH_IDENT_PORT/_PORT), defaults unchangedmre
  • ef9f9feere-vendor Hybriel 6f8a0689 (int 6–11: streaming cursors, #203 index heal + close() fix, fd403 listDir/cursor fds, hl:time browser side, hl:web batches 5+6)mre
  • 5485467cdocker-compose: nofile 65536 (Hybriel int 6+ boot opened >1024 files in the tracker)mre
  • 86ea4a85deploy: backup copies index files first and *.mpack data last (hot-copy skew, hybriel #203)mre
  • edeab20fnotes mission 015: mission, report, LOG, STATUSmre
  • 542a0383notes mission 015: e2e-chrome E2E_HOST (own cookie jar in the shared Chrome)mre
  • 8b30f510notes mission 015: main-menu from layouts 0bcc629, current = true kept (route marks only /), server-HTML marker checksmre
  • 84b574c4notes mission 015: re-vendor plugins from hybriel f9614007 (incl. web, #117)mre
  • 447da53dnotes mission 014: report — remaining gates and lisbeth E2E proofmre
  • fd9f92c3notes mission 014: report (partial), LOG, STATUSmre
  • b2501360notes mission 014: open() sets asking=false before flush(0) (hybriel#196)mre
  • 9414ce59notes mission 014: merge theme 76f99f6, components 3794c59, strict single-press openAsk (fails on old open())mre
  • c08c655anotes 013: theme re-copied from layouts 76f99f6 (menu marks the current page), current = true on the menu item, e2e marker checks, reportmre
  • 21c8f65dnotes mission 012: STATUSmre
  • 0bbb3dd9notes mission 012: delete section of the browser gate de-flaked (openAsk: calm wait, press again when the question closes by itself); report 012mre
  • f431bdf8notes mission 011: notes on the layouts theme (ListFirst shell), styles cut, check-theme gate, E2E extended; reportmre
  • 1a8603d1notes mission 011 WIP: deploy.sh gate summary also matches check-theme, browser.mjs logout via the user menumre