gitoria

notes

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit0aa9277e0aa9277enotes: move each component's styles into its own Style block (shell and header selector in main.hl, list, editor frame and delete question in notes.hl); styles.hl keeps the theme inherit, the accent and the message colour; .gitignore ignores a bin symlink too; tests/shots.mjs for the pixel comparemre0aa9277e/components/notes.hl

16.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 (this file's Style `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. Style {
  157. noteList { display = 'grid' gap = '.6rem' alignContent = 'start' }
  158. // the aside has only 4 px of side padding (the rows carry their own 12 px, so their text stands on the 16 px gutter): the header
  159. // takes the same 12 px, so "Notes" and "New note" stand on the gutter too instead of touching the screen edge
  160. 'note-list > header' { display = 'flex' alignItems = 'center' justifyContent = 'space-between' gap = '1rem' padding = '0 .75rem' }
  161. 'note-list h1' { margin = '0' }
  162. 'note-list p' { margin = '0' }
  163. '#notes' { margin = '0' padding = '0' listStyle = 'none' }
  164. noteReader { display = 'block' minWidth = '0' }
  165. noteEditor { display = 'grid' gridTemplateColumns = 'minmax(0, 1fr)' gap = '.8rem' }
  166. // the editor's wrapper: a block that may shrink (a wide table scrolls inside the editor, it does not widen the page)
  167. noteText { display = 'block' minWidth = '0' }
  168. 'note-editor > footer' { display = 'flex' flexWrap = 'wrap' alignItems = 'center' gap = '.6rem' padding = '.3rem 0' }
  169. 'list-first note-editor a.back' { marginBottom = '0' }
  170. '.ask' { display = 'none' alignItems = 'center' gap = '.6rem' margin = '0' }
  171. // the delete confirmation: the question on its own line, then Delete and Keep as two equal buttons (a phone shows them side by side
  172. // on the full width; they used to wrap into a ragged line with "Keep" alone below)
  173. '.ask.open' { display = 'flex' flexWrap = 'wrap' flex = '1 0 100%' padding = '0' }
  174. askText { display = 'block' flex = '1 0 100%' }
  175. '.ask button' { flex = '1 1 0' maxWidth = '12rem' background = 'var(--color-surface)' }
  176. // the footer has no side padding: the "Delete note" button's left edge and the Delete/Keep row's two edges are the editor's edges
  177. // the page's clock (components/notes.hl): an invisible endless 1 s animation, each round is a tick
  178. notesTick { position = 'fixed' left = '0' top = '0' width = '1px' height = '1px' opacity = '0' pointerEvents = 'none' animation = 'notes-tick 1s linear infinite' }
  179. '@keyframes notes-tick' = {
  180. 'from' = { opacity = '0' }
  181. to = { opacity = '0' }
  182. }
  183. }
  184. View {
  185. if (signedOut) {
  186. ContentFirst {
  187. main { p { id = "loginhint" class = "muted" "Log in with ident (top right) to see your notes." } }
  188. }
  189. }
  190. if (signedIn) {
  191. // 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
  192. // (`a.back` returns); from 60rem the list (aside) and the note (main) side by side, each scrolling on its own.
  193. ListFirst { view = bookView
  194. aside {
  195. noteList {
  196. header {
  197. h1 { "Notes" }
  198. button { id = "newnote" type = "button" class = "primary" "New note" on click(e) { emit create(e) } }
  199. }
  200. p { id = "syncstate" class = "muted" syncState }
  201. if (noNotes) { p { id = "empty" class = "empty" "No notes yet." } }
  202. ol { id = "notes"
  203. for (r of rows) { li { a { href = r.href class = r.cls "data-note" = r.key on click(e) { emit pick(e) } r.subject } } }
  204. }
  205. }
  206. }
  207. main {
  208. noteReader {
  209. if (noneOpen) { p { id = "pick" class = "empty" "Select a note or create a new one." } }
  210. if (openMissing) {
  211. p { id = "missing" class = "empty" "This note does not exist any more. "
  212. a { href = "/" "data-note" = "" on click(e) { emit pick(e) } "Back to your notes" }
  213. }
  214. }
  215. for (o of openList) {
  216. noteEditor {
  217. a { class = "back" href = "/" "data-note" = "" on click(e) { emit pick(e) } "All notes" }
  218. noteText { id = "notetext" on input(e) { emit typed(e) }
  219. MdEditor { name = "text" value = shownText rows = 16 files = "off" placeholder = "" }
  220. }
  221. p { id = "saveerror" class = "message" saveError }
  222. footer {
  223. button { id = "delete" type = "button" "Delete note" on click(e) { emit askDelete(e) } }
  224. p { id = "deleteask" class = askClass
  225. askText { "Delete this note for good?" }
  226. button { id = "deleteyes" type = "button" class = "danger" "Delete" on click(e) { emit dropOpen(e) } }
  227. button { id = "deleteno" type = "button" "Keep" on click(e) { emit keepOpen(e) } }
  228. }
  229. }
  230. }
  231. }
  232. }
  233. }
  234. }
  235. notesTick { "aria-hidden" = "true" on animationiteration(e) { emit tick(e) } }
  236. }
  237. }
  238. // ---- what the person does ----------------------------------------------------------------------------------------------
  239. // a link of the list (or "All notes", "Back"): opens the note in place — the address follows, no page load
  240. on pick(e) {
  241. if (e.button != 0 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) { return null }
  242. e.preventDefault()
  243. open(e.currentTarget.getAttribute('data-note'))
  244. }
  245. on create(e) {
  246. flush(0)
  247. k = 'l' + window.crypto.randomUUID().replaceAll('-', '').slice(0, 16)
  248. t = Date.now()
  249. edits = withEdit(edits, { key = k text = '' sent = '' base = 0 when = t })
  250. emit server notesPut({ key = k id = '' local = k text = '' base = 0 when = t })
  251. open(k)
  252. }
  253. // the MdEditor announces its Markdown with an `input` event on its hidden field (named `text`); its other inputs are not it
  254. on typed(e) {
  255. if (e.target.name != 'text') { return null }
  256. v = e.target.value
  257. r = rowOf(rows, sel)
  258. if (r == null) { return null }
  259. had = editOf(edits, sel)
  260. if (had == null && v == r.text) { return null }
  261. if (had != null && had.text == v) { return null }
  262. edits = withEdit(edits, { key = sel text = v sent = had != null ? had.sent : r.text base = had != null ? had.base : r.base when = Date.now() })
  263. }
  264. on askDelete(e) { asking = true }
  265. on keepOpen(e) { asking = false }
  266. on dropOpen(e) {
  267. r = rowOf(rows, sel)
  268. asking = false
  269. if (r != null) {
  270. emit server notesDrop({ key = r.key id = r.sid local = r.local base = r.base when = Date.now() })
  271. dropped = withKey(dropped, r.key)
  272. edits = withoutEdit(edits, r.key)
  273. }
  274. open('')
  275. }
  276. // 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
  277. on tick(e) {
  278. flush(600)
  279. ticks = ticks + 1
  280. if (ticks % 10 == 0) { pull() }
  281. }
  282. on client reconnected(r) { pull() }
  283. on client notesChanged(user, list) {
  284. notes = list
  285. settle(list)
  286. }
  287. on client notesSaveError(key, why) { saveError = why }
  288. open = (k) => {
  289. // 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
  290. asking = false
  291. flush(0)
  292. sel = k == null ? '' : k
  293. saveError = ''
  294. window.history.replaceState(null, '', sel == '' ? '/' : '/note/' + sel)
  295. }
  296. // send this tab's typed text that is older than `waitMs` and not sent yet
  297. flush = (waitMs) => {
  298. t = Date.now()
  299. let changed = false
  300. next = []
  301. for (e of edits) {
  302. if (e.text != e.sent && t - e.when >= waitMs) {
  303. r = rowOf(rows, e.key)
  304. 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 })
  305. next.push({ key = e.key text = e.text sent = e.text base = e.base when = e.when })
  306. changed = true
  307. } else { next.push(e) }
  308. }
  309. if (changed) { edits = next }
  310. }
  311. pull = () => {
  312. if (pulling) { return null }
  313. pulling = true
  314. got = emit server notesPull()
  315. pulling = false
  316. if (got == null) {
  317. reachable = false
  318. return null
  319. }
  320. reachable = true
  321. loginGone = got.login == true
  322. if (got.notes != null) {
  323. notes = got.notes
  324. settle(got.notes)
  325. }
  326. }
  327. // THE SERVER'S LIST ARRIVED: an edit the server now has (or kept as this tab's conflict copy) is done; the open note
  328. // that got its server id takes it (and the address follows); a delete that went through is forgotten
  329. settle = (list) => {
  330. next = []
  331. for (e of edits) {
  332. let done = false
  333. for (n of list) {
  334. mine = n.id == e.key || (n.local != '' && n.local == e.key)
  335. if (mine && n.text == e.text && e.sent == e.text) { done = true }
  336. // sent, and the server has moved on since (its version is not older than this edit): the server's text is the truth now —
  337. // a later save of ANOTHER tab must show here, not stay hidden under this tab's old edit (a lost conflict became a copy)
  338. if (mine && e.sent == e.text && n.edited >= e.when) { done = true }
  339. 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 }
  340. }
  341. if (!done) { next.push(e) }
  342. }
  343. if (next.length != edits.length) { edits = next }
  344. still = []
  345. for (k of dropped) {
  346. let there = false
  347. for (n of list) { if (n.id == k || n.local == k) { there = true } }
  348. if (there) { still.push(k) }
  349. }
  350. if (still.length != dropped.length) { dropped = still }
  351. if (sel != '' && sel.slice(0, 1) == 'l') {
  352. for (n of list) {
  353. if (n.local == sel) {
  354. edits = withoutEdit(edits, sel)
  355. sel = n.id
  356. window.history.replaceState(null, '', '/note/' + n.id)
  357. }
  358. }
  359. }
  360. }
  361. // ---- the faces (thin: lib/notes.hl decides; the trailing `session` is always the server's, hybriel #16) ----------------
  362. on server notesPut(n, session) {
  363. u = userIdOfSession(session)
  364. 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 }
  365. if (n.local.length > 64 || n.id.length > 64) { return null }
  366. r = pushNote(u, n.id, n.local, n.text, n.base, n.when, tagOf(session))
  367. if (r.error != null) { emit client notesSaveError(n.key, r.error) }
  368. emit client notesChanged(u, notesFor(u))
  369. }
  370. on server notesDrop(n, session) {
  371. u = userIdOfSession(session)
  372. if (u == null || n == null || hlTypeName(n) != 'Hybrid' || !isText(n.id) || !isText(n.local) || !isTime(n.base)) { return null }
  373. removeNote(u, n.id, n.base, n.local, tagOf(session))
  374. emit client notesChanged(u, notesFor(u))
  375. }
  376. on server notesPull(session) {
  377. u = userIdOfSession(session)
  378. if (u == null) { return { login = true } }
  379. return { notes = notesFor(u) }
  380. }

Branches

Latest commits

  • 0aa9277enotes: move each component's styles into its own Style block (shell and header selector in main.hl, list, editor frame and delete question in notes.hl); styles.hl keeps the theme inherit, the accent and the message colour; .gitignore ignores a bin symlink too; tests/shots.mjs for the pixel comparemre
  • 9ba01de3notes: write the standby service out in full so the boot gate can read the main servicemre
  • 98cab40anotes: standby service on its port plus 10 and a deploy that hands over through it without a gapmre
  • 1fadb473notes gate: the editor table now has 1 px cell lines from the notes thememre
  • 8762115cnotes: take the components e54f83a (paste as Markdown, text typed before the editor is built is kept, Table button); table lines come from the notes thememre
  • 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