gitoria

notes

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitef18a68fef18a68fsync components.hybriel 214c318: MdEditor on a phone, two even toolbar rows of 44 px targets, label 15 px, table head 16 px (E2)mreef18a68f/components/notes.hl

13.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 (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
  186. askText { "Delete this note for good?" }
  187. button { id = "deleteyes" type = "button" class = "danger" "Delete" on click(e) { emit dropOpen(e) } }
  188. button { id = "deleteno" type = "button" "Keep" on click(e) { emit keepOpen(e) } }
  189. }
  190. }
  191. }
  192. }
  193. }
  194. }
  195. }
  196. notesTick { "aria-hidden" = "true" on animationiteration(e) { emit tick(e) } }
  197. }
  198. }
  199. // ---- what the person does ----------------------------------------------------------------------------------------------
  200. // a link of the list (or "All notes", "Back"): opens the note in place — the address follows, no page load
  201. on pick(e) {
  202. if (e.button != 0 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) { return null }
  203. e.preventDefault()
  204. open(e.currentTarget.getAttribute('data-note'))
  205. }
  206. on create(e) {
  207. flush(0)
  208. k = 'l' + window.crypto.randomUUID().replaceAll('-', '').slice(0, 16)
  209. t = Date.now()
  210. edits = withEdit(edits, { key = k text = '' sent = '' base = 0 when = t })
  211. emit server notesPut({ key = k id = '' local = k text = '' base = 0 when = t })
  212. open(k)
  213. }
  214. // the MdEditor announces its Markdown with an `input` event on its hidden field (named `text`); its other inputs are not it
  215. on typed(e) {
  216. if (e.target.name != 'text') { return null }
  217. v = e.target.value
  218. r = rowOf(rows, sel)
  219. if (r == null) { return null }
  220. had = editOf(edits, sel)
  221. if (had == null && v == r.text) { return null }
  222. if (had != null && had.text == v) { return null }
  223. edits = withEdit(edits, { key = sel text = v sent = had != null ? had.sent : r.text base = had != null ? had.base : r.base when = Date.now() })
  224. }
  225. on askDelete(e) { asking = true }
  226. on keepOpen(e) { asking = false }
  227. on dropOpen(e) {
  228. r = rowOf(rows, sel)
  229. asking = false
  230. if (r != null) {
  231. emit server notesDrop({ key = r.key id = r.sid local = r.local base = r.base when = Date.now() })
  232. dropped = withKey(dropped, r.key)
  233. edits = withoutEdit(edits, r.key)
  234. }
  235. open('')
  236. }
  237. // 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
  238. on tick(e) {
  239. flush(600)
  240. ticks = ticks + 1
  241. if (ticks % 10 == 0) { pull() }
  242. }
  243. on client reconnected(r) { pull() }
  244. on client notesChanged(user, list) {
  245. notes = list
  246. settle(list)
  247. }
  248. on client notesSaveError(key, why) { saveError = why }
  249. open = (k) => {
  250. // 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
  251. asking = false
  252. flush(0)
  253. sel = k == null ? '' : k
  254. saveError = ''
  255. window.history.replaceState(null, '', sel == '' ? '/' : '/note/' + sel)
  256. }
  257. // send this tab's typed text that is older than `waitMs` and not sent yet
  258. flush = (waitMs) => {
  259. t = Date.now()
  260. let changed = false
  261. next = []
  262. for (e of edits) {
  263. if (e.text != e.sent && t - e.when >= waitMs) {
  264. r = rowOf(rows, e.key)
  265. 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 })
  266. next.push({ key = e.key text = e.text sent = e.text base = e.base when = e.when })
  267. changed = true
  268. } else { next.push(e) }
  269. }
  270. if (changed) { edits = next }
  271. }
  272. pull = () => {
  273. if (pulling) { return null }
  274. pulling = true
  275. got = emit server notesPull()
  276. pulling = false
  277. if (got == null) {
  278. reachable = false
  279. return null
  280. }
  281. reachable = true
  282. loginGone = got.login == true
  283. if (got.notes != null) {
  284. notes = got.notes
  285. settle(got.notes)
  286. }
  287. }
  288. // THE SERVER'S LIST ARRIVED: an edit the server now has (or kept as this tab's conflict copy) is done; the open note
  289. // that got its server id takes it (and the address follows); a delete that went through is forgotten
  290. settle = (list) => {
  291. next = []
  292. for (e of edits) {
  293. let done = false
  294. for (n of list) {
  295. mine = n.id == e.key || (n.local != '' && n.local == e.key)
  296. if (mine && n.text == e.text && e.sent == e.text) { done = true }
  297. // sent, and the server has moved on since (its version is not older than this edit): the server's text is the truth now —
  298. // a later save of ANOTHER tab must show here, not stay hidden under this tab's old edit (a lost conflict became a copy)
  299. if (mine && e.sent == e.text && n.edited >= e.when) { done = true }
  300. 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 }
  301. }
  302. if (!done) { next.push(e) }
  303. }
  304. if (next.length != edits.length) { edits = next }
  305. still = []
  306. for (k of dropped) {
  307. let there = false
  308. for (n of list) { if (n.id == k || n.local == k) { there = true } }
  309. if (there) { still.push(k) }
  310. }
  311. if (still.length != dropped.length) { dropped = still }
  312. if (sel != '' && sel.slice(0, 1) == 'l') {
  313. for (n of list) {
  314. if (n.local == sel) {
  315. edits = withoutEdit(edits, sel)
  316. sel = n.id
  317. window.history.replaceState(null, '', '/note/' + n.id)
  318. }
  319. }
  320. }
  321. }
  322. // ---- the faces (thin: lib/notes.hl decides; the trailing `session` is always the server's, hybriel #16) ----------------
  323. on server notesPut(n, session) {
  324. u = userIdOfSession(session)
  325. if (u == null || n == null || !isText(n.id) || !isText(n.local) || !isText(n.text) || !isTime(n.base) || !isTime(n.when)) { return null }
  326. if (n.local.length > 64 || n.id.length > 64) { return null }
  327. r = pushNote(u, n.id, n.local, n.text, n.base, n.when, tagOf(session))
  328. if (r.error != null) { emit client notesSaveError(n.key, r.error) }
  329. emit client notesChanged(u, notesFor(u))
  330. }
  331. on server notesDrop(n, session) {
  332. u = userIdOfSession(session)
  333. if (u == null || n == null || !isText(n.id) || !isText(n.local) || !isTime(n.base)) { return null }
  334. removeNote(u, n.id, n.base, n.local, tagOf(session))
  335. emit client notesChanged(u, notesFor(u))
  336. }
  337. on server notesPull(session) {
  338. u = userIdOfSession(session)
  339. if (u == null) { return { login = true } }
  340. return { notes = notesFor(u) }
  341. }

Branches

Latest commits

  • 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
  • 2eb783f5sync components 9a: shared Icon (icons/icons.hl), md-editor toolbar icons + single focus ring (components f4-icons ba96735)mre
  • 4dd46dadsync-components: copy icons/icons.hl + README (F4); testsmre
  • c92e4a0ftests/e2e-outside.mjs: real-Chrome E2E, a note pushed from another device while open, then typingmre
  • 0516b4ccmd-editor.hl: verbatim copy of components f3-mdvalue (an older text handed back is kept)mre
  • 38cc11ecmd-editor.hl: verbatim copy of components f3-mdvalue (an outside value change is shown by the editor)mre
  • 3e7e79f5reports/H1-hydrate.mdmre
  • 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