gitoria

notes

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit4dc982824dc98282notes#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 codemre4dc98282/components/notes.hl

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

Branches

Latest commits

  • 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
  • 6091320anotes: MdEditor 7437e32 (surface drawn at once when the page is claimed); gate measures claim-to-text on 5 note loads at 1280 and 390mre
  • 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