gitoria

layouts

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit769b99d7769b99d7theme: table cells keep a word whole, code in a title stays text-sized, 'edited' is a 44 px tap target on the phonemre769b99d7/components/comments/comments.hl

18.6 KB

  1. // comments.hl — THE COMMENTS BLOCK (layouts#2; shape of hybriel's demo-social-network), pure Hybriel: "1 comment" / "n comments",
  2. // then every comment — an avatar (the initial), a bubble with the name, the time, "edited" (the earlier versions fold out) and the
  3. // text as formatted Markdown; under it a like (♥ and the count) and, for the comment's author, Edit / Delete — and the form below,
  4. // typed in the Markdown editor (components/md-editor). `Comments { comments = list user = me onPost = posted onEdit = edited
  5. // onDelete = deleted onLike = liked }`.
  6. // GENERIC: no app data, no storage. The host passes the comments (a list of records, below) and the current user's name, and
  7. // hands four functions (hybriel#87: a child talks upward by calling a function its host gives it); the host decides what is
  8. // stored and gives a new list back — the block shows what it is given. A comment record:
  9. // { id, author, when, text (Markdown), likes (a number), liked (true when `user` likes it), history ([{ when, text }], the
  10. // earlier versions, oldest first; empty = never edited) }
  11. // Edit / Delete show where `author == user`; with `user` empty there is no form and the like does nothing.
  12. // The text is drawn by MdEditor's own renderer (`readonly = "true"`). The editor builds its content in the browser once per element;
  13. // hl:web builds a composed child anew when a value bound into it changes, so a changed text (an edit here, or a new list from a push)
  14. // is drawn anew, and the rows are keyed by `id`, so a deleted comment takes its own editor with it.
  15. // The per-comment state (which one is being edited / asked to be deleted) is one member each: one block per page (a composed
  16. // component's members are the page's state).
  17. // ATTACHMENTS (mission 009, tickets#27): a comment record may carry `files` ([{ name, url, image (true = a picture), size (bytes) }]):
  18. // pictures show as thumbnails that open the full picture, any other file as a row (kind, name, size); a file without `url` (the
  19. // viewer may not fetch it) shows as a row without a link. `attach = "true"` puts the form inside the shared FilesUpload in pick mode
  20. // (components/files-upload/): files dropped, chosen, photographed or pasted (Ctrl+V, also into the editor: its files = "off") wait
  21. // as thumbnails / rows under the editor, and the host hears them with the text: `onPost(text, files)` (browser Files — the host sends
  22. // them with its own face; the framework uploads them first). Without `attach` nothing changes: `onPost(text)`.
  23. import MdEditor from '../md-editor/md-editor.hl'
  24. import FilesUpload from '../files-upload/files-upload.hl'
  25. Hybrid comments = []
  26. String user = ''
  27. onPost = null
  28. onEdit = null
  29. onDelete = null
  30. onLike = null
  31. String oneLabel = 'comment'
  32. String manyLabel = 'comments'
  33. String emptyText = 'No comments yet.'
  34. String placeholder = 'Write a comment…'
  35. String postLabel = 'Comment'
  36. String editLabel = 'Edit'
  37. String deleteLabel = 'Delete'
  38. String saveLabel = 'Save'
  39. String cancelLabel = 'Cancel'
  40. String askText = 'Delete this comment?'
  41. String keepLabel = 'Keep'
  42. String editedLabel = 'edited'
  43. String likeLabel = 'Like'
  44. String signInText = ''
  45. // attachments in the form (above): "true" turns them on; the size limit per file (bytes, 0 = none) and the chooser's `accept`
  46. String attach = 'false'
  47. Number maxFileSize = 0
  48. String accept = ''
  49. String attachText = 'Drop or paste files here, or'
  50. String chooseText = 'Choose files'
  51. String cameraText = 'Camera'
  52. String filesLabel = 'Attachments'
  53. String editingId = ''
  54. String askingId = ''
  55. Number formSeq = 0
  56. Hybrid shown = rowsOf(comments, user, editingId, askingId)
  57. String countText = countOf(comments)
  58. Boolean hasNone = comments.length == 0
  59. Boolean canWrite = user != ''
  60. Boolean cannotWrite = user == '' && signInText != ''
  61. Hybrid forms = [{ id = 'new-' + formSeq }]
  62. // the files waiting in the form (browser Files, the FilesUpload's list: this block is its host) — sent with the next post
  63. Hybrid attached = []
  64. Boolean attaching = canWrite && attach == 'true'
  65. Boolean plainForm = canWrite && attach != 'true'
  66. picked = (list) => { attached = list }
  67. // "1 comment", "n comments"
  68. countOf(list) { return list.length == 1 ? '1 ' + oneLabel : list.length + ' ' + manyLabel }
  69. initialOf(name) {
  70. t = ('' + name).trim()
  71. return t == '' ? '?' : t.slice(0, 1).toUpperCase()
  72. }
  73. // "5 B", "1.2 KB", "3.4 MB" — whole numbers only (rounded to tenths by hand: the server side has no toFixed, hybriel#182)
  74. sizeTextOf(n) {
  75. if (n == null || n < 0) { return '' }
  76. if (n < 1024) { return n + ' B' }
  77. if (n < 1048576) { return tenthsOf(n, 1024) + ' KB' }
  78. return tenthsOf(n, 1048576) + ' MB'
  79. }
  80. tenthsOf(n, unit) {
  81. x = n * 20 + unit
  82. t = (x - x % (2 * unit)) / (2 * unit)
  83. return (t - t % 10) / 10 + '.' + t % 10
  84. }
  85. // "PNG", "PDF": the box a file row shows
  86. kindOf(name) {
  87. t = '' + name
  88. dot = t.lastIndexOf('.')
  89. return dot > 0 && dot < t.length - 1 ? t.slice(dot + 1, dot + 5).toUpperCase() : 'FILE'
  90. }
  91. // what each attached file of a comment shows: a thumbnail (a picture the viewer may fetch), a linked row, or a plain row (no url)
  92. filesOf(list) {
  93. out = []
  94. if (list == null) { return out }
  95. for (f of list) {
  96. let url = f.url != null ? '' + f.url : ''
  97. let thumb = f.image == true && url != ''
  98. out.push({ name = f.name url = url size = sizeTextOf(f.size) kind = kindOf(f.name) thumb = thumb linked = !thumb && url != '' plain = url == '' })
  99. }
  100. return out
  101. }
  102. // what one comment shows: the host's record, and what this block works out for the user looking
  103. rowsOf(list, me, editing, asking) {
  104. out = []
  105. for (c of list) {
  106. let mine = me != '' && c.author == me
  107. let past = c.history != null ? c.history : []
  108. let count = c.likes != null ? c.likes : 0
  109. let isEditing = mine && '' + c.id == editing
  110. let isAsking = mine && '' + c.id == asking && !isEditing
  111. let attachedFiles = filesOf(c.files)
  112. out.push({
  113. id = '' + c.id
  114. author = c.author
  115. initial = initialOf(c.author)
  116. when = c.when != null ? c.when : ''
  117. text = c.text
  118. likes = count
  119. likeText = (c.liked == true ? '♥ ' : '♡ ') + count
  120. pressed = c.liked == true ? 'true' : 'false'
  121. edited = past.length > 0
  122. history = past
  123. editedTitle = past.length == 1 ? 'Edited once' : 'Edited ' + past.length + ' times'
  124. mine = mine
  125. editing = isEditing
  126. notEditing = !isEditing
  127. asking = isAsking
  128. offering = mine && !isEditing && !isAsking
  129. files = attachedFiles
  130. hasFiles = attachedFiles.length > 0
  131. })
  132. }
  133. return out
  134. }
  135. View {
  136. commentsBlock {
  137. h2 { class = "comments-count" countText }
  138. if (hasNone) { p { class = "comments-empty" emptyText } }
  139. ol { class = "comment-list"
  140. for (c of shown) {
  141. li { class = "comment" 'data-id' = c.id
  142. commentAvatar { 'aria-hidden' = "true" c.initial }
  143. commentMain {
  144. commentBubble {
  145. commentMeta {
  146. strong { class = "comment-author" c.author }
  147. time { c.when }
  148. if (c.edited) { details { class = "comment-history"
  149. summary { editedLabel }
  150. p { class = "comment-history-title" c.editedTitle }
  151. ol { for (h of c.history) { li { time { h.when } pre { h.text } } } }
  152. } }
  153. }
  154. if (c.notEditing) { MdEditor { name = "comment-text" value = c.text readonly = "true" } }
  155. if (c.editing) { form { class = "comment-edit" 'data-id' = c.id on submit(e) { emit commentsSave(e) }
  156. MdEditor { name = "comment-edit" value = c.text }
  157. commentButtons {
  158. button { type = "button" class = "comment-cancel" cancelLabel on click(e) { emit commentsCancel(e) } }
  159. button { type = "submit" class = "comment-save" saveLabel }
  160. }
  161. } }
  162. if (c.hasFiles) { ul { class = "comment-files" 'aria-label' = filesLabel
  163. for (f of c.files) { li {
  164. if (f.thumb) { a { class = "comment-thumb" href = f.url target = "_blank" rel = "noopener" title = f.name
  165. img { src = f.url alt = f.name loading = "lazy" }
  166. } }
  167. if (f.linked) { a { class = "comment-file" href = f.url target = "_blank" rel = "noopener"
  168. span { class = "kind" 'aria-hidden' = "true" f.kind }
  169. fileInfo { strong { class = "name" f.name } span { class = "size" f.size } }
  170. } }
  171. if (f.plain) { span { class = "comment-file"
  172. span { class = "kind" 'aria-hidden' = "true" f.kind }
  173. fileInfo { strong { class = "name" f.name } span { class = "size" f.size } }
  174. } }
  175. } }
  176. } }
  177. }
  178. commentActions {
  179. button { type = "button" class = "comment-like" 'data-id' = c.id 'aria-pressed' = c.pressed title = likeLabel c.likeText on click(e) { emit commentsLike(e) } }
  180. if (c.offering) {
  181. button { type = "button" class = "comment-edit-start" 'data-id' = c.id editLabel on click(e) { emit commentsEdit(e) } }
  182. button { type = "button" class = "comment-delete" 'data-id' = c.id deleteLabel on click(e) { emit commentsAsk(e) } }
  183. }
  184. if (c.asking) { commentAsk {
  185. span { askText }
  186. button { type = "button" class = "comment-delete-yes" 'data-id' = c.id deleteLabel on click(e) { emit commentsDelete(e) } }
  187. button { type = "button" class = "comment-delete-keep" keepLabel on click(e) { emit commentsKeep(e) } }
  188. } }
  189. }
  190. }
  191. }
  192. }
  193. }
  194. if (plainForm) { for (f of forms) { form { class = "comment-form" on submit(e) { emit commentsPost(e) }
  195. MdEditor { name = "comment-new" value = "" placeholder = placeholder rows = 3 }
  196. commentButtons { button { type = "submit" class = "comment-post" postLabel } }
  197. } } }
  198. if (attaching) { FilesUpload { mode = "pick" files = attached onChange = picked maxSize = maxFileSize accept = accept pickText = attachText chooseText = chooseText cameraText = cameraText
  199. for (f of forms) { form { class = "comment-form" on submit(e) { emit commentsPost(e) }
  200. MdEditor { name = "comment-new" value = "" placeholder = placeholder rows = 3 files = "off" }
  201. commentButtons { button { type = "submit" class = "comment-post" postLabel } }
  202. } }
  203. } }
  204. if (cannotWrite) { p { class = "comments-sign-in" signInText } }
  205. }
  206. }
  207. idOf(e) {
  208. el = e.target.closest('[data-id]')
  209. return el == null ? '' : el.getAttribute('data-id')
  210. }
  211. // the Markdown the editor of a form holds now (its hidden field, kept up to date by every keystroke)
  212. typedIn(form) {
  213. f = form.querySelector('md-editor input.value')
  214. return f == null ? '' : f.value
  215. }
  216. // post: the host is told the text (and, with attach, the waiting files: a post may be files only); answering false keeps text and
  217. // files in the form (e.g. the server refused), else the form starts empty
  218. on commentsPost(e) {
  219. e.preventDefault()
  220. text = typedIn(e.target)
  221. if (onPost == null) { return null }
  222. if (attach == 'true') {
  223. if (text.trim() == '' && attached.length == 0) { return null }
  224. sent = onPost(text, attached)
  225. if (sent != false) {
  226. attached = []
  227. formSeq = formSeq + 1
  228. }
  229. return null
  230. }
  231. if (text.trim() == '') { return null }
  232. ok = onPost(text)
  233. if (ok != false) { formSeq = formSeq + 1 }
  234. }
  235. on commentsEdit(e) {
  236. askingId = ''
  237. editingId = idOf(e)
  238. }
  239. on commentsCancel(e) { editingId = '' }
  240. on commentsSave(e) {
  241. e.preventDefault()
  242. id = idOf(e)
  243. text = typedIn(e.target)
  244. let old = ''
  245. for (c of comments) { if ('' + c.id == id) { old = c.text } }
  246. if (text.trim() != '' && text != old && onEdit != null) { onEdit(id, text) }
  247. editingId = ''
  248. }
  249. on commentsAsk(e) {
  250. editingId = ''
  251. askingId = idOf(e)
  252. }
  253. on commentsKeep(e) { askingId = '' }
  254. on commentsDelete(e) {
  255. id = idOf(e)
  256. askingId = ''
  257. if (onDelete != null) { onDelete(id) }
  258. }
  259. on commentsLike(e) {
  260. if (user == '' || onLike == null) { return null }
  261. onLike(idOf(e))
  262. }
  263. // ---- look: the component's own Style; the base look is black, white and the greys #555 #999 #ddd #f2f2f2 (shared/base.hl names them;
  264. // root README "Look"); every colour is a semantic token with its base value as fallback, so an app that declares the tokens
  265. // (layouts.worldapi.org's theme) restyles it. No frames: the bubble is a grey field on the page, the avatar a grey disc.
  266. Style {
  267. 'comments-block' { display = 'block' color = 'var(--color-text, #000)' }
  268. 'comments-block h2.comments-count' { margin = '0 0 .8rem' fontSize = '1.05rem' }
  269. 'comments-block .comments-empty, comments-block .comments-sign-in' { margin = '0 0 .8rem' color = 'var(--color-text-muted, #555)' }
  270. 'comments-block ol.comment-list' { display = 'flex' flexDirection = 'column' gap = '.9rem' margin = '0 0 1rem' padding = '0' listStyle = 'none' }
  271. 'comments-block li.comment' { display = 'flex' gap = '.6rem' alignItems = 'flex-start' }
  272. 'comments-block comment-avatar' { flex = 'none' display = 'grid' placeItems = 'center' width = '2.2rem' height = '2.2rem' borderRadius = '50%' fontWeight = '700' color = 'var(--color-text, #000)' background = 'var(--color-surface, #ddd)' }
  273. 'comments-block comment-main' { display = 'block' flex = '1' minWidth = '0' }
  274. 'comments-block comment-bubble' { display = 'block' padding = '.5rem .8rem .6rem' background = 'var(--color-background-sunken, #f2f2f2)' borderRadius = 'var(--radius-small, 5px) var(--radius-large, 10px) var(--radius-large, 10px) var(--radius-large, 10px)' }
  275. 'comments-block comment-meta' { display = 'flex' flexWrap = 'wrap' alignItems = 'baseline' gap = '.2rem .6rem' marginBottom = '.2rem' }
  276. 'comments-block .comment-author' { fontSize = '.95rem' color = 'var(--color-text-strong, #000)' overflowWrap = 'anywhere' }
  277. 'comments-block comment-meta time' { fontSize = '.8rem' color = 'var(--color-text-muted, #555)' }
  278. // "edited": a quiet label next to the time (the foldable's own frame, grey header and margins of an app theme are switched off: the
  279. // theme styles every details/summary as a framed foldable, which is wrong for a history note); open, the earlier versions run in
  280. // under the line, muted, no frame
  281. 'comments-block details.comment-history' { display = 'contents' margin = '0' padding = '0' border = '0' borderRadius = '0' background = 'transparent' }
  282. 'comments-block details.comment-history::details-content' { display = 'contents' }
  283. 'comments-block details.comment-history summary' { display = 'inline' minHeight = '0' margin = '0' padding = '0' border = '0' background = 'transparent' fontSize = '.8rem' fontWeight = '400' color = 'var(--color-text-muted, #555)' cursor = 'pointer' textDecoration = 'underline dotted' textUnderlineOffset = '.15em' }
  284. 'comments-block details.comment-history summary:hover' { background = 'transparent' color = 'var(--color-text, #000)' }
  285. 'comments-block details.comment-history summary:focus-visible' { outline = '2px solid var(--color-accent, #555)' outlineOffset = '2px' }
  286. 'comments-block details.comment-history > :not(summary)' { flexBasis = '100%' margin = '0' fontSize = '.8rem' color = 'var(--color-text-muted, #555)' }
  287. 'comments-block .comment-history-title' { margin = '.2rem 0 0' fontWeight = '600' }
  288. 'comments-block details.comment-history ol' { padding = '0' listStyle = 'none' }
  289. 'comments-block details.comment-history li' { display = 'block' margin = '.2rem 0 0' }
  290. 'comments-block details.comment-history pre' { margin = '0' padding = '0' fontFamily = 'inherit' fontSize = '.85rem' whiteSpace = 'pre-wrap' overflowWrap = 'anywhere' color = 'var(--color-text-muted, #555)' background = 'transparent' }
  291. 'comments-block comment-actions' { display = 'flex' flexWrap = 'wrap' alignItems = 'center' gap = '.1rem .3rem' marginTop = '.2rem' fontSize = '.85rem' }
  292. 'comments-block button' { font = 'inherit' fontWeight = '600' padding = '.25rem .6rem' color = 'var(--color-text-muted, #555)' background = 'transparent' border = '0' borderRadius = 'var(--radius-small, 5px)' cursor = 'pointer' }
  293. 'comments-block button:hover' { color = 'var(--color-text, #000)' background = 'var(--color-surface, #f2f2f2)' }
  294. 'comments-block button:focus-visible' { outline = '2px solid var(--color-accent, #555)' outlineOffset = '1px' }
  295. 'comments-block button.comment-like[aria-pressed="true"]' { color = 'var(--color-text-strong, #000)' }
  296. 'comments-block comment-ask' { display = 'flex' flexWrap = 'wrap' alignItems = 'center' gap = '.3rem' }
  297. 'comments-block button.comment-delete-yes, comments-block button.comment-save, comments-block button.comment-post' { color = 'var(--color-accent-text, #fff)' background = 'var(--color-accent, #000)' }
  298. 'comments-block button.comment-delete-yes:hover, comments-block button.comment-save:hover, comments-block button.comment-post:hover' { color = 'var(--color-accent-text, #fff)' background = 'var(--color-accent, #000)' filter = 'brightness(1.3)' }
  299. 'comments-block form.comment-edit' { display = 'block' marginTop = '.3rem' }
  300. 'comments-block form.comment-edit md-editor' { background = 'var(--color-background, #fff)' }
  301. 'comments-block form.comment-form' { display = 'block' }
  302. 'comments-block comment-buttons' { display = 'flex' justifyContent = 'flex-end' gap = '.4rem' marginTop = '.4rem' }
  303. 'comments-block button.comment-post' { minHeight = '2.75rem' minWidth = '2.75rem' padding = '.25rem 1rem' }
  304. // attachments: thumbnails in a wrapping row (each a 44 px+ target that opens the picture), file rows under them; no frames
  305. 'comments-block ul.comment-files' { display = 'flex' flexWrap = 'wrap' gap = '.4rem' margin = '.4rem 0 .1rem' padding = '0' listStyle = 'none' }
  306. 'comments-block ul.comment-files li' { display = 'block' minWidth = '0' maxWidth = '100%' }
  307. 'comments-block a.comment-thumb' { display = 'block' width = '6rem' height = '6rem' borderRadius = 'var(--radius-small, 5px)' overflow = 'hidden' background = 'var(--color-surface, #ddd)' }
  308. 'comments-block a.comment-thumb img' { display = 'block' width = '100%' height = '100%' objectFit = 'cover' }
  309. 'comments-block a.comment-thumb:focus-visible, comments-block a.comment-file:focus-visible' { outline = '2px solid var(--color-accent, #555)' outlineOffset = '1px' }
  310. 'comments-block .comment-file' { display = 'flex' alignItems = 'center' gap = '.5rem' minHeight = '2.75rem' padding = '.2rem .6rem .2rem .2rem' boxSizing = 'border-box' maxWidth = '100%' color = 'var(--color-text, #000)' textDecoration = 'none' background = 'var(--color-background, #fff)' borderRadius = 'var(--radius-small, 5px)' }
  311. 'comments-block a.comment-file:hover' { background = 'var(--color-surface, #f2f2f2)' }
  312. 'comments-block .comment-file .kind' { flex = 'none' display = 'grid' placeItems = 'center' width = '2.4rem' height = '2.4rem' fontSize = '.7rem' fontWeight = '700' color = 'var(--color-text-muted, #555)' background = 'var(--color-surface, #ddd)' borderRadius = 'var(--radius-small, 5px)' }
  313. 'comments-block .comment-file file-info' { display = 'flex' flexDirection = 'column' minWidth = '0' }
  314. 'comments-block .comment-file .name' { fontSize = '.9rem' overflowWrap = 'anywhere' }
  315. 'comments-block .comment-file .size' { fontSize = '.8rem' color = 'var(--color-text-muted, #555)' }
  316. 'comments-block files-upload form.comment-form' { marginTop = '.4rem' }
  317. }

Branches

Latest commits

  • 769b99d7theme: table cells keep a word whole, code in a title stays text-sized, 'edited' is a 44 px tap target on the phonemre
  • c36b7595list rows (notes, mail) stack their parts full width: the date no longer a one-letter column beside the titlemre
  • 02aa74aasync components.hybriel 134b4e3: MdEditor builds an editor put into the page at once, redraws a surface put in anew (F5, hybriel#211)mre
  • 268b1c87sync components.hybriel 6b0936a: the 'edited' foldable drops the theme's border and marginmre
  • c51d039fsync components.hybriel 8d79ff7: bubble hugs its content, quiet 'edited' label; gate checksmre
  • f4ad7eecsync components 9a: shared Icon (icons/icons.hl), md-editor toolbar icons + single focus ring (components f4-icons ba96735)mre
  • db258c11sync-components: copy icons/icons.hl + README (F4); testsmre
  • badc1879md-editor.hl: verbatim copy of components f3-mdvalue (an older text handed back is kept)mre
  • 867a8375md-editor.hl: verbatim copy of components f3-mdvalue (an outside value change is shown by the editor)mre
  • 5d8d2c67tests: wait for hydration before real clicks after a navigation (H2)mre
  • 99db79f0re-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
  • 30353f60tests: e2e-md-showcase Chrome range from LAYOUTS_GATE_CHROME_MD, default unchangedmre
  • 16a7878cre-vendor Hybriel 6f8a0689 (int 6–11: streaming cursors, #203 index heal + close() fix, fd403 listDir/cursor fds, hl:time browser side, hl:web batches 5+6)mre
  • 6af559e8docker-compose: nofile 65536 (Hybriel int 6+ boot opened >1024 files in the tracker)mre
  • 5599cf56deploy: backup copies index files first and *.mpack data last (hot-copy skew, hybriel #203)mre
  • 0bcc6294Mission 011: e2e-197, brief, report, LOGmre
  • 137303c5MainMenu marks the current page from the route (hybriel#197, server-rendered); current prop/item.current stay as fallback; tracker demo drops current; gate: SSR marker; docsmre
  • 9325b403Re-vendor Hybriel to master 7f13a82f (hl:web batch 2: #190 #193 #197 #194 named regions); plugins whole, bin/hybriel static build (git-ignored)mre
  • b31dc74dusage: calendar (theme 76f99f6-era ContentFirst, user menu, ☰ menu) and tracker (theme 76f99f6, modal + pagination current, own current marker until hybriel#197) rowsmre
  • 8a3d5d40Mission 010: brief, report, LOG, STATUSmre