gitoria

notes

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitac23e194ac23e194ident#31: the selector's sign-in returns to the page the user started on (return attribute = the login callback with next), gate checkmreac23e194/components/notes.hl

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

Branches

Latest commits

  • ac23e194ident#31: the selector's sign-in returns to the page the user started on (return attribute = the login callback with next), gate checkmre
  • 2804f0d3Merge branch 'iw-ident-widget'mre
  • 52d8d242notes: e2e script checks Log out and the selector in the header instead of the user menumre
  • e5abf7b7notes header: the selector alone top right plus Log out as before the theme missions; the user icon is gonemre
  • a2e343aeident selector back in the header (creator 2026-10-24): selector top right signed in and out on phone and desktop, no second Log in button, new gate checks (WIP: gates not re-run after the last edits)mre
  • 7a889b82notes: MdEditor with the Table button, typed pipe rows and the theme's table look (components mt-tables); a table note is named by its header words; gate checks that fail on mainmre
  • b96c7f70theme: sync from layouts 4994037 (list rows stack, td word-keep, heading code, edited tap target)mre
  • 6140e6a0sessions last 99 years (creator 2026-10-23: logins never expire; only sign out ends them)mre
  • cb7e6469deploy: security gate (tests/security.mjs)mre
  • 774bf34bnotes security: CSRF guard on /api/notes, new session at login, Secure cookie, uploadMax 0, login rate limit, nosniff, face payload check; tests/security.mjs; SN reportmre
  • ef18a68fsync components.hybriel 214c318: MdEditor on a phone, two even toolbar rows of 44 px targets, label 15 px, table head 16 px (E2)mre
  • c37488b1MN report: alignment updatemre
  • 4fb35a0enotes mobile: Delete note and the delete row exactly on the editor's edges; gate asserts 1 pxmre
  • 91736270MN report and logmre
  • 15667425notes: delete confirmation buttons at most 12rem wide on the desktopmre
  • 8f7a6a95gate: MN phone checks (list header gutter, back gap, delete question)mre
  • 90c73ed3notes mobile: back link gapmre
  • 72bd4f83notes mobile: list header on the gutter, delete confirmation as a question + two equal buttons, Delete note under the editor edgemre
  • ba1e73f8notes: a sent edit is done once the server's version is not older, so a second writer's text shows in the first tab; gate: two writers both waysmre
  • c69ef9ebsync components.hybriel 134b4e3: MdEditor builds an editor put into the page at once, redraws a surface put in anew (F5, hybriel#211)mre