gitoria

notes

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit617ee8c0617ee8c0Merge branch 'hl-standard' into mainAtlas617ee8c0/components/notes.hl

16.8 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 { messageLook } from '../lib/looks.hl'
  22. import MdEditor from './md-editor/md-editor.hl'
  23. import ListFirst from '../theme/shell/list-first.hl'
  24. import ContentFirst from '../theme/shell/content-first.hl'
  25. // ---- the list: notes, then the queue, then this tab's edits ------------------------------------------------------------
  26. // does row `r` stand for key `k` (its server id, or the id the creating tab gave it)?
  27. static isKey = (Hybrid r, String k) => { return k != null && k != '' && (r.key == k || r.sid == k || (r.local != '' && r.local == k)) }
  28. static rowOf = (Hybrid list, String k) => {
  29. for (r of list) { if (isKey(r, k)) { return r } }
  30. return null
  31. }
  32. // 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
  33. static putRow = (Hybrid list, String sid, String local, String text, Number when, Number base) => {
  34. out = []
  35. let found = false
  36. k = sid != null && sid != '' ? sid : local
  37. for (r of list) {
  38. if (!found && (isKey(r, k) || (local != null && local != '' && isKey(r, local)))) {
  39. found = true
  40. out.push({ key = r.key sid = r.sid local = r.local text = text edited = text == r.text ? r.edited : when base = r.base })
  41. } else { out.push(r) }
  42. }
  43. if (!found) { out.push({ key = k sid = sid == null ? '' : sid local = local == null ? '' : local text = text edited = when base = base }) }
  44. return out
  45. }
  46. static dropRow = (Hybrid list, String k) => {
  47. out = []
  48. for (r of list) { if (!isKey(r, k)) { out.push(r) } }
  49. return out
  50. }
  51. // the language has no escapes: a newline is written as itself
  52. static newline = '
  53. '
  54. // the words of a table row: its cells, trimmed, empty ones left out, joined by one space
  55. static tableWords = (String row) => {
  56. out = []
  57. for (c of row.split('|')) {
  58. w = c.trim()
  59. if (w != '') { out.push(w) }
  60. }
  61. return out.join(' ')
  62. }
  63. // the SUBJECT: the first non-empty line without leading `#` (a table's header row as its words), at most 80 characters
  64. static subjectOf = (String text) => {
  65. for (raw of text.split(newline)) {
  66. let line = raw.trim()
  67. while (line.length > 0 && line.slice(0, 1) == '#') { line = line.slice(1) }
  68. line = line.trim()
  69. // a note that starts with a table: the header cells as words, not the pipes (`| Item | Qty |` -> `Item Qty`)
  70. if (line.startsWith('|')) { line = tableWords(line) }
  71. if (line != '') { return line.length > 80 ? line.slice(0, 80) : line }
  72. }
  73. return 'Untitled note'
  74. }
  75. // last edited first (by hand: hybriel#1): each row goes in before the first one older than it
  76. static newestFirst = (Hybrid list) => {
  77. let out = []
  78. for (r of list) {
  79. next = []
  80. let placed = false
  81. for (x of out) {
  82. if (!placed && r.edited > x.edited) {
  83. next.push(r)
  84. placed = true
  85. }
  86. next.push(x)
  87. }
  88. if (!placed) { next.push(r) }
  89. out = next
  90. }
  91. return out
  92. }
  93. static rowsOf = (Hybrid notes, Hybrid queue, Hybrid edits, Hybrid dropped, String sel) => {
  94. let list = []
  95. for (n of notes) { list.push({ key = n.id sid = n.id local = n.local text = n.text edited = n.edited base = n.edited }) }
  96. for (q of queue) {
  97. if (q.event == 'notesPut') { list = putRow(list, q.id, q.local, q.text, q.when, q.base) }
  98. if (q.event == 'notesDrop') { list = dropRow(list, q.id != '' ? q.id : q.local) }
  99. }
  100. 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) }
  101. for (k of dropped) { list = dropRow(list, k) }
  102. out = []
  103. for (r of newestFirst(list)) {
  104. 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 current = isKey(r, sel) ? 'page' : null })
  105. }
  106. return out
  107. }
  108. static editOf = (Hybrid edits, String k) => {
  109. for (e of edits) { if (e.key == k) { return e } }
  110. return null
  111. }
  112. static withEdit = (Hybrid edits, Hybrid e) => {
  113. out = []
  114. for (x of edits) { if (x.key != e.key) { out.push(x) } }
  115. out.push(e)
  116. return out
  117. }
  118. static withKey = (Hybrid list, String k) => {
  119. out = []
  120. for (x of list) { out.push(x) }
  121. out.push(k)
  122. return out
  123. }
  124. static withoutEdit = (Hybrid edits, String k) => {
  125. out = []
  126. for (e of edits) { if (e.key != k) { out.push(e) } }
  127. return out
  128. }
  129. // ---- the page's state ---------------------------------------------------------------------------------------------------
  130. session = null
  131. id = null
  132. signedIn = userIdOfSession(session) != null
  133. signedOut = userIdOfSession(session) == null
  134. __title = siteName
  135. notes = notesFor(userIdOfSession(session))
  136. __hlQueue = []
  137. __hlOnline = true
  138. edits = []
  139. dropped = []
  140. sel = id != null ? id : ''
  141. reachable = true
  142. loginGone = false
  143. saveError = ''
  144. asking = false
  145. ticks = 0
  146. pulling = false
  147. rows = rowsOf(notes, __hlQueue, edits, dropped, sel)
  148. openRow = rowOf(rows, sel)
  149. openList = openRow == null ? [] : [{ id = openRow.key }]
  150. shownText = openRow == null ? '' : openRow.text
  151. noneOpen = sel == ''
  152. openMissing = sel != '' && openRow == null
  153. noNotes = rows.length == 0
  154. bookView = sel != '' ? 'detail' : 'list'
  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. pMessage = messageLook + { }
  158. noteList { display = 'grid' gap = '.6rem' alignContent = 'start'
  159. // 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
  160. // takes the same 12 px, so "Notes" and "New note" stand on the gutter too instead of touching the screen edge
  161. '> header' { display = 'flex' alignItems = 'center' justifyContent = 'space-between' gap = '1rem' padding = '0 .75rem' }
  162. h1 { margin = '0' }
  163. p { margin = '0' }
  164. }
  165. '#notes' { margin = '0' padding = '0' listStyle = 'none' }
  166. noteReader { display = 'block' minWidth = '0' }
  167. noteEditor { display = 'grid' gridTemplateColumns = 'minmax(0, 1fr)' gap = '.8rem'
  168. '> footer' { display = 'flex' flexWrap = 'wrap' alignItems = 'center' gap = '.6rem' padding = '.3rem 0' }
  169. }
  170. // the editor's wrapper: a block that may shrink (a wide table scrolls inside the editor, it does not widen the page)
  171. noteText { display = 'block' minWidth = '0' }
  172. 'list-first note-editor a.back' { marginBottom = '0' }
  173. // the delete confirmation: the question on its own line, then Delete and Keep as two equal buttons (a phone shows them side by side
  174. // on the full width; they used to wrap into a ragged line with "Keep" alone below)
  175. pAsk { display = 'none' alignItems = 'center' gap = '.6rem' margin = '0'
  176. button { flex = '1 1 0' maxWidth = '12rem' background = 'var(--color-surface)' }
  177. }
  178. // the open state: the question follows its own `open` attribute (the View binds it to `asking`)
  179. 'p.ask[open]' { display = 'flex' flexWrap = 'wrap' flex = '1 0 100%' padding = '0' }
  180. askText { display = 'block' flex = '1 0 100%' }
  181. // the page's clock (components/notes.hl): an invisible endless 1 s animation, each round is a tick
  182. notesTick { position = 'fixed' left = '0' top = '0' width = '1px' height = '1px' opacity = '0' pointerEvents = 'none' animation = 'notes-tick 1s linear infinite' }
  183. '@keyframes notes-tick' = {
  184. 'from' = { opacity = '0' }
  185. to = { opacity = '0' }
  186. }
  187. }
  188. View {
  189. if (signedOut) {
  190. ContentFirst {
  191. main { p { id = "loginhint" class = "muted" "Log in with ident (top right) to see your notes." } }
  192. }
  193. }
  194. if (signedIn) {
  195. // 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
  196. // (`a.back` returns); from 60rem the list (aside) and the note (main) side by side, each scrolling on its own.
  197. ListFirst { view = bookView
  198. aside {
  199. noteList {
  200. header {
  201. h1 { "Notes" }
  202. button { id = "newnote" type = "button" class = "primary" "New note" on click(e) { emit create(e) } }
  203. }
  204. p { id = "syncstate" class = "muted" syncState }
  205. if (noNotes) { p { id = "empty" class = "empty" "No notes yet." } }
  206. ol { id = "notes"
  207. for (r of rows) { li { a { href = r.href "aria-current" = r.current on click(e) { emit pick(e, r.key) } r.subject } } }
  208. }
  209. }
  210. }
  211. main {
  212. noteReader {
  213. if (noneOpen) { p { id = "pick" class = "empty" "Select a note or create a new one." } }
  214. if (openMissing) {
  215. p { id = "missing" class = "empty" "This note does not exist any more. "
  216. a { href = "/" on click(e) { emit pick(e, "") } "Back to your notes" }
  217. }
  218. }
  219. for (o of openList) {
  220. noteEditor {
  221. a { class = "back" href = "/" on click(e) { emit pick(e, "") } "All notes" }
  222. noteText { id = "notetext" on input(e) { emit typed(e) }
  223. MdEditor { name = "text" value = shownText rows = 16 files = "off" placeholder = "" }
  224. }
  225. p { id = "saveerror" Style.pMessage saveError }
  226. footer {
  227. button { id = "delete" type = "button" "Delete note" on click(e) { emit askDelete(e) } }
  228. p { id = "deleteask" Style.pAsk open = asking
  229. askText { "Delete this note for good?" }
  230. button { id = "deleteyes" type = "button" class = "danger" "Delete" on click(e) { emit dropOpen(e) } }
  231. button { id = "deleteno" type = "button" "Keep" on click(e) { emit keepOpen(e) } }
  232. }
  233. }
  234. }
  235. }
  236. }
  237. }
  238. }
  239. notesTick { "aria-hidden" = "true" on animationiteration(e) { emit tick(e) } }
  240. }
  241. }
  242. // ---- what the person does ----------------------------------------------------------------------------------------------
  243. // a link of the list (or "All notes", "Back"): opens the note in place — the address follows, no page load
  244. on pick(Hybrid e, String k) {
  245. if (e.button != 0 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) { return null }
  246. e.preventDefault()
  247. open(k)
  248. }
  249. on create(Hybrid e) {
  250. flush(0)
  251. k = 'l' + window.crypto.randomUUID().replaceAll('-', '').slice(0, 16)
  252. t = Date.now()
  253. edits = withEdit(edits, { key = k text = '' sent = '' base = 0 when = t })
  254. emit server notesPut({ key = k id = '' local = k text = '' base = 0 when = t })
  255. open(k)
  256. }
  257. // the MdEditor announces its Markdown with an `input` event on its hidden field (named `text`); its other inputs are not it
  258. on typed(Hybrid e) {
  259. if (e.target.name != 'text') { return null }
  260. v = e.target.value
  261. r = rowOf(rows, sel)
  262. if (r == null) { return null }
  263. had = editOf(edits, sel)
  264. if (had == null && v == r.text) { return null }
  265. if (had != null && had.text == v) { return null }
  266. edits = withEdit(edits, { key = sel text = v sent = had != null ? had.sent : r.text base = had != null ? had.base : r.base when = Date.now() })
  267. }
  268. on askDelete(Hybrid e) { asking = true }
  269. on keepOpen(Hybrid e) { asking = false }
  270. on dropOpen(Hybrid e) {
  271. r = rowOf(rows, sel)
  272. asking = false
  273. if (r != null) {
  274. emit server notesDrop({ key = r.key id = r.sid local = r.local base = r.base when = Date.now() })
  275. dropped = withKey(dropped, r.key)
  276. // this tab's edit of the note may be keyed by the id the tab gave it (`local`) or by the server id: remove it under every name the row has,
  277. // or it is laid over the list again (putRow) once the server's list no longer holds the note (notes#15)
  278. edits = withoutEdit(withoutEdit(withoutEdit(edits, r.key), r.local), r.sid)
  279. }
  280. open('')
  281. }
  282. // 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
  283. on tick(Hybrid e) {
  284. flush(600)
  285. ticks = ticks + 1
  286. if (ticks % 10 == 0) { pull() }
  287. }
  288. on client reconnected(Hybrid r) { pull() }
  289. on client notesChanged(String user, Hybrid list) {
  290. notes = list
  291. settle(list)
  292. }
  293. on client notesSaveError(String key, String why) { saveError = why }
  294. open = (String k) => {
  295. // 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
  296. asking = false
  297. flush(0)
  298. sel = k == null ? '' : k
  299. saveError = ''
  300. window.history.replaceState(null, '', sel == '' ? '/' : '/note/' + sel)
  301. }
  302. // send this tab's typed text that is older than `waitMs` and not sent yet
  303. flush = (Number waitMs) => {
  304. t = Date.now()
  305. let changed = false
  306. next = []
  307. for (e of edits) {
  308. if (e.text != e.sent && t - e.when >= waitMs) {
  309. r = rowOf(rows, e.key)
  310. 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 })
  311. next.push({ key = e.key text = e.text sent = e.text base = e.base when = e.when })
  312. changed = true
  313. } else { next.push(e) }
  314. }
  315. if (changed) { edits = next }
  316. }
  317. pull = () => {
  318. if (pulling) { return null }
  319. pulling = true
  320. got = emit server notesPull()
  321. pulling = false
  322. if (got == null) {
  323. reachable = false
  324. return null
  325. }
  326. reachable = true
  327. loginGone = got.login == true
  328. if (got.notes != null) {
  329. notes = got.notes
  330. settle(got.notes)
  331. }
  332. }
  333. // THE SERVER'S LIST ARRIVED: an edit the server now has (or kept as this tab's conflict copy) is done; the open note
  334. // that got its server id takes it (and the address follows); a delete that went through is forgotten
  335. settle = (Hybrid list) => {
  336. next = []
  337. for (e of edits) {
  338. let done = false
  339. for (n of list) {
  340. mine = n.id == e.key || (n.local != '' && n.local == e.key)
  341. if (mine && n.text == e.text && e.sent == e.text) { done = true }
  342. // sent, and the server has moved on since (its version is not older than this edit): the server's text is the truth now —
  343. // a later save of ANOTHER tab must show here, not stay hidden under this tab's old edit (a lost conflict became a copy)
  344. if (mine && e.sent == e.text && n.edited >= e.when) { done = true }
  345. 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 }
  346. }
  347. if (!done) { next.push(e) }
  348. }
  349. if (next.length != edits.length) { edits = next }
  350. still = []
  351. for (k of dropped) {
  352. let there = false
  353. for (n of list) { if (n.id == k || n.local == k) { there = true } }
  354. if (there) { still.push(k) }
  355. }
  356. if (still.length != dropped.length) { dropped = still }
  357. if (sel != '' && sel.slice(0, 1) == 'l') {
  358. for (n of list) {
  359. if (n.local == sel) {
  360. edits = withoutEdit(edits, sel)
  361. sel = n.id
  362. window.history.replaceState(null, '', '/note/' + n.id)
  363. }
  364. }
  365. }
  366. }
  367. // ---- the faces (thin: lib/notes.hl decides; the trailing `session` is always the server's, hybriel #16) ----------------
  368. on server notesPut(n, session) {
  369. u = userIdOfSession(session)
  370. 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 }
  371. if (n.local.length > 64 || n.id.length > 64) { return null }
  372. r = pushNote(u, n.id, n.local, n.text, n.base, n.when, tagOf(session))
  373. if (r.error != null) { emit client notesSaveError(n.key, r.error) }
  374. emit client notesChanged(u, notesFor(u))
  375. }
  376. on server notesDrop(n, session) {
  377. u = userIdOfSession(session)
  378. if (u == null || n == null || hlTypeName(n) != 'Hybrid' || !isText(n.id) || !isText(n.local) || !isTime(n.base)) { return null }
  379. removeNote(u, n.id, n.base, n.local, tagOf(session))
  380. emit client notesChanged(u, notesFor(u))
  381. }
  382. on server notesPull(session) {
  383. u = userIdOfSession(session)
  384. if (u == null) { return { login = true } }
  385. return { notes = notesFor(u) }
  386. }

Branches

Latest commits

  • 617ee8c0Merge branch 'hl-standard' into mainAtlas
  • 452458ee|||PR Type the function parameters of notes' own .hl files (HL-STANDARD L10)Atlas
  • 23758cd6Re-vendor complete Hybriel plugins and runtime to day44mre
  • 5d7cc700Backup tar: --hard-dereference, so mpackdb sidecars are never stored as hard links (Tracker's finding)mre
  • 3e07b1cfVendor complete Hybriel day39 plugin sources and librariesmre
  • d7366702Check session cookie flags after login and assert anonymous GET stays cookielessmre
  • 16bc8fd8Re-vendor Hybriel day38b with the mpackdb store-open race fixmre
  • d860dc2dnotes#12: the markup names its own rules instead of hand-written class strings: the selected row is `aria-current="page"` (the list-first theme rule styles it, no class), the delete question is `Style.pAsk` and follows its own `open` attribute (bound to `asking`), the error line is `Style.pMessage` (body once in lib/looks.hl, rule in notes.hl, main.hl and loginfailed.hl). The CSS declarations are the same, only selectors changed (.ask -> p.ask, .ask.open -> p.ask[open], .message -> p.message); the pages look the same (AE 0 at 4 sizes). main.hl's selectorClass in/out stays (login.js reads it).mre
  • 2e7f8664notes#13: the list and back links pass the note key from the loop (emit pick(e, r.key)) instead of writing data-note into the View and reading it back with getAttribute; the two window.history.replaceState calls and the identcode e.target.value read stay (hl:web has no replace-navigation, a bound hidden input is not adopted on a script's change event), see NO13 reportmre
  • 4dc98282notes#15: a deleted note made in this tab leaves the list at once (the delete now removes this tab's edit under every key the row has: local id, server id); gate: browser.mjs notes#15 section, red on the old codemre
  • e4e7402fnotes#11: plain declarations instead of let for variables never reassigned (components/notes.hl 3, lib/jsoncheck.hl 5, lib/util.hl 2, tools/migrate-short-ids.hl 8)mre
  • 27b6a09enotes#8: Style blocks in the nested form (note-list, note-editor, .ask with their children nested, :empty as a nested pseudo-class); the same CSS rules, the pages look the samemre
  • 71a9538bnotes: B27W report, log linemre
  • 78ba0073notes: an ident access key (Authorization: Bearer idt_…) reads and writes its identity's notes through /api/me and /api/notes; gate tests/ident-access-notes.mjs (ident#27)mre
  • 3de46dacnotes: the sign-in callback keeps the session it finds and signs it in (same cookie, nothing signed out; ident#28); security gate 73+5 failing → 78 passedmre
  • 5909d458notes: MdEditor from components 59704ce (text on the clipboard always pastes, a file manager copy still attaches; pipe rows without a delimiter row; tickets#49, notes#5)mre
  • 98bd6a98N5b: pipe rows without a delimiter row are a table (MdEditor ad4e929, plain copy); gate tests/dashboard-table.mjs; reportmre
  • dbf93319N5 report: components gate rerun 613 passed, 0 failedmre
  • 3a8bc0d2N5: empty Table-button table is written as a real pipe table (MdEditor 4bd1112, plain copy); gate tests/table-button.mjs; reportmre
  • c16bd443N5: step 1 report (tables pass locally; his DASHBOARD note has no delimiter row)mre