gitoria

notes

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Branchnotes9-no-pollc49ce093|||PR WIP remove notes polling and use framework reachabilityAtlasnotes9-no-poll/components/notes.hl

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

Branches

Latest commits

  • c49ce093|||PR WIP remove notes polling and use framework reachabilityAtlas
  • c028ac61Merge branch 'notes-pr7-kunibert' into mainAtlas
  • 9cc92bdfKeep conflict and history fixes while awaiting queued save supportAtlas
  • a5c27cb6Allow real edits back to earlier text and honor configured identAtlas
  • 15c46ced|||PR Preserve note edits, conflict bases and visible historyAtlas
  • 4c6e0bf6Merge branch 'notes13-day61' into mainAtlas
  • eb51864f|||PR Back/Forward via navigate, server drafts, Hybriel day61Atlas
  • 3537cf7eMerge branch 'vendor-day59' into mainAtlas
  • c13d188b|||PR Vendor Hybriel day59Atlas
  • 24c95433Merge branch 'notes17-ghost-note' into mainAtlas
  • ae1075b2Preserve rejected drafts during note ID adoptionAtlas
  • 383cce23|||PR Prevent saved local edits from restoring deleted notesAtlas
  • 99ca1cf6Merge branch 'vendor-day56' into mainAtlas
  • 649cef37|||PR Vendor Hybriel day56Atlas
  • aadd0abcMerge branch 'hl-standard-2' into mainAtlas
  • 52ffac9f|||PR Keyboard and reload checks for the notes gate (HL-STANDARD F21, F5)Atlas
  • 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