gitoria

layouts

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit78478f6e78478f6eSync showcase to components.hybriel 1442e58: pick-mode page, comments attach, md-editor rows/files off; gates 285/0mre78478f6e/components/comments/comments.hl

17.8 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. 'comments-block details.comment-history' { marginLeft = 'auto' fontSize = '.8rem' color = 'var(--color-text-muted, #555)' }
  279. 'comments-block details.comment-history[open]' { flexBasis = '100%' marginLeft = '0' }
  280. 'comments-block details.comment-history summary' { cursor = 'pointer' textDecoration = 'underline' textUnderlineOffset = '.15em' textAlign = 'right' }
  281. 'comments-block .comment-history-title' { margin = '.3rem 0' fontWeight = '600' }
  282. 'comments-block details.comment-history ol' { margin = '0' padding = '0' listStyle = 'none' }
  283. 'comments-block details.comment-history li' { margin = '0 0 .4rem' }
  284. 'comments-block details.comment-history pre' { margin = '.15rem 0 0' padding = '.4rem .6rem' fontFamily = 'inherit' fontSize = '.85rem' whiteSpace = 'pre-wrap' overflowWrap = 'anywhere' color = 'var(--color-text, #000)' background = 'var(--color-background, #fff)' borderRadius = 'var(--radius-small, 5px)' }
  285. 'comments-block comment-actions' { display = 'flex' flexWrap = 'wrap' alignItems = 'center' gap = '.1rem .3rem' marginTop = '.2rem' fontSize = '.85rem' }
  286. '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' }
  287. 'comments-block button:hover' { color = 'var(--color-text, #000)' background = 'var(--color-surface, #f2f2f2)' }
  288. 'comments-block button:focus-visible' { outline = '2px solid var(--color-accent, #555)' outlineOffset = '1px' }
  289. 'comments-block button.comment-like[aria-pressed="true"]' { color = 'var(--color-text-strong, #000)' }
  290. 'comments-block comment-ask' { display = 'flex' flexWrap = 'wrap' alignItems = 'center' gap = '.3rem' }
  291. '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)' }
  292. '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)' }
  293. 'comments-block form.comment-edit' { display = 'block' marginTop = '.3rem' }
  294. 'comments-block form.comment-edit md-editor' { background = 'var(--color-background, #fff)' }
  295. 'comments-block form.comment-form' { display = 'block' }
  296. 'comments-block comment-buttons' { display = 'flex' justifyContent = 'flex-end' gap = '.4rem' marginTop = '.4rem' }
  297. 'comments-block button.comment-post' { minHeight = '2.75rem' minWidth = '2.75rem' padding = '.25rem 1rem' }
  298. // attachments: thumbnails in a wrapping row (each a 44 px+ target that opens the picture), file rows under them; no frames
  299. 'comments-block ul.comment-files' { display = 'flex' flexWrap = 'wrap' gap = '.4rem' margin = '.4rem 0 .1rem' padding = '0' listStyle = 'none' }
  300. 'comments-block ul.comment-files li' { display = 'block' minWidth = '0' maxWidth = '100%' }
  301. 'comments-block a.comment-thumb' { display = 'block' width = '6rem' height = '6rem' borderRadius = 'var(--radius-small, 5px)' overflow = 'hidden' background = 'var(--color-surface, #ddd)' }
  302. 'comments-block a.comment-thumb img' { display = 'block' width = '100%' height = '100%' objectFit = 'cover' }
  303. 'comments-block a.comment-thumb:focus-visible, comments-block a.comment-file:focus-visible' { outline = '2px solid var(--color-accent, #555)' outlineOffset = '1px' }
  304. '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)' }
  305. 'comments-block a.comment-file:hover' { background = 'var(--color-surface, #f2f2f2)' }
  306. '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)' }
  307. 'comments-block .comment-file file-info' { display = 'flex' flexDirection = 'column' minWidth = '0' }
  308. 'comments-block .comment-file .name' { fontSize = '.9rem' overflowWrap = 'anywhere' }
  309. 'comments-block .comment-file .size' { fontSize = '.8rem' color = 'var(--color-text-muted, #555)' }
  310. 'comments-block files-upload form.comment-form' { marginTop = '.4rem' }
  311. }

Branches

Latest commits

  • 78478f6eSync showcase to components.hybriel 1442e58: pick-mode page, comments attach, md-editor rows/files off; gates 285/0mre
  • dbe3de79Display bugs from a real Chrome walk (mission 007): Settings page and in-shell doc reader instead of raw .md links, closed phone search hidden whatever a page says, /elements form rules scoped, three-pane danger contrast; display audit gate (132 -> 244 checks), walk + Settings e2e toolsmre
  • 1875a051Theme: 44 px tap targets mobile first (layoutTap), 16 px fields, desktop denser only in @media 60rem; 390 px tap audit + gate checks (mission 006)mre
  • 3d4f8e19Sync showcase to components.hybriel 9db927d: comments and files-upload pages, pagination onPage, md-editor readonly, gate 99 checksmre
  • 64d635acmission brief 005mre
  • 0d0fef25paths: projects moved into /media/STORAGE/projects/WorldAPI/mre
  • 2dbe0df5Re-vendor Hybriel to the shared static build 0f396652 (plugins; bin/ is git-ignored), compose cache volume + tmpfs /tmp, gate wait 120 s, report 004mre
  • 0adc551emission brief 004mre
  • 1376e905deploy.sh: wait up to DEPLOY_URL_WAIT (120 s) for 200 after the restart, as the other apps (cold compile, hybriel#148)mre
  • c0fdbf6cdocker-compose: init: true (hybriel#156, PID 1 ignored SIGTERM, every restart waited 10 s)mre
  • ecf70665usage: layouts on components a567b57mre
  • 315731efcomponents re-synced to components.hybriel a567b57 (md-editor tables)mre
  • b59e2ee9docs/usage.md + tools/usage.sh: which app carries which component/theme/tokens copy, and whether it matches the sourcemre
  • 0222f67ctheme: two corner radii only (radiusSmall 5px, radiusLarge 10px); check-theme flags any other radius; gate 76/0mre
  • 877b63acdesign rules: two radii only (5px small, 10px large)mre
  • 635234dfcomponents re-synced to components.hybriel 5a98d77 (#15 black/white base look); gate 73/0, check-theme 0, screenshots unchangedmre
  • 876b2064docs: workaround note for hybriel#130 (slot marker mid-line)mre
  • 85b56541layouts mission 002 3/3: same-output tests, letcount, shots ports, docs, reportmre
  • 0f325d3blayouts mission 002 2/3: code rules — styles.hl -> components/styles.hl, the one never-reassigned let dropped; gate 73/0, same outputmre
  • a3e74c8alayouts mission 002 1/3: components re-copied byte-verbatim from components.hybriel 3f93d71 by tools/sync-components.sh (+ --check, components/SOURCE); gate 73/0, check-theme 0mre