gitoria

components

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit9db927df9db927dfpaths: projects moved into /media/STORAGE/projects/WorldAPI/mre9db927df/components/comments/comments.hl

11.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. import MdEditor from '../md-editor/md-editor.hl'
  18. Hybrid comments = []
  19. String user = ''
  20. onPost = null
  21. onEdit = null
  22. onDelete = null
  23. onLike = null
  24. String oneLabel = 'comment'
  25. String manyLabel = 'comments'
  26. String emptyText = 'No comments yet.'
  27. String placeholder = 'Write a comment…'
  28. String postLabel = 'Comment'
  29. String editLabel = 'Edit'
  30. String deleteLabel = 'Delete'
  31. String saveLabel = 'Save'
  32. String cancelLabel = 'Cancel'
  33. String askText = 'Delete this comment?'
  34. String keepLabel = 'Keep'
  35. String editedLabel = 'edited'
  36. String likeLabel = 'Like'
  37. String signInText = ''
  38. String editingId = ''
  39. String askingId = ''
  40. Number formSeq = 0
  41. Hybrid shown = rowsOf(comments, user, editingId, askingId)
  42. String countText = countOf(comments)
  43. Boolean hasNone = comments.length == 0
  44. Boolean canWrite = user != ''
  45. Boolean cannotWrite = user == '' && signInText != ''
  46. Hybrid forms = [{ id = 'new-' + formSeq }]
  47. // "1 comment", "n comments"
  48. countOf(list) { return list.length == 1 ? '1 ' + oneLabel : list.length + ' ' + manyLabel }
  49. initialOf(name) {
  50. t = ('' + name).trim()
  51. return t == '' ? '?' : t.slice(0, 1).toUpperCase()
  52. }
  53. // what one comment shows: the host's record, and what this block works out for the user looking
  54. rowsOf(list, me, editing, asking) {
  55. out = []
  56. for (c of list) {
  57. let mine = me != '' && c.author == me
  58. let past = c.history != null ? c.history : []
  59. let count = c.likes != null ? c.likes : 0
  60. let isEditing = mine && '' + c.id == editing
  61. let isAsking = mine && '' + c.id == asking && !isEditing
  62. out.push({
  63. id = '' + c.id
  64. author = c.author
  65. initial = initialOf(c.author)
  66. when = c.when != null ? c.when : ''
  67. text = c.text
  68. likes = count
  69. likeText = (c.liked == true ? '♥ ' : '♡ ') + count
  70. pressed = c.liked == true ? 'true' : 'false'
  71. edited = past.length > 0
  72. history = past
  73. editedTitle = past.length == 1 ? 'Edited once' : 'Edited ' + past.length + ' times'
  74. mine = mine
  75. editing = isEditing
  76. notEditing = !isEditing
  77. asking = isAsking
  78. offering = mine && !isEditing && !isAsking
  79. })
  80. }
  81. return out
  82. }
  83. View {
  84. commentsBlock {
  85. h2 { class = "comments-count" countText }
  86. if (hasNone) { p { class = "comments-empty" emptyText } }
  87. ol { class = "comment-list"
  88. for (c of shown) {
  89. li { class = "comment" 'data-id' = c.id
  90. commentAvatar { 'aria-hidden' = "true" c.initial }
  91. commentMain {
  92. commentBubble {
  93. commentMeta {
  94. strong { class = "comment-author" c.author }
  95. time { c.when }
  96. if (c.edited) { details { class = "comment-history"
  97. summary { editedLabel }
  98. p { class = "comment-history-title" c.editedTitle }
  99. ol { for (h of c.history) { li { time { h.when } pre { h.text } } } }
  100. } }
  101. }
  102. if (c.notEditing) { MdEditor { name = "comment-text" value = c.text readonly = "true" } }
  103. if (c.editing) { form { class = "comment-edit" 'data-id' = c.id on submit(e) { emit commentsSave(e) }
  104. MdEditor { name = "comment-edit" value = c.text }
  105. commentButtons {
  106. button { type = "button" class = "comment-cancel" cancelLabel on click(e) { emit commentsCancel(e) } }
  107. button { type = "submit" class = "comment-save" saveLabel }
  108. }
  109. } }
  110. }
  111. commentActions {
  112. button { type = "button" class = "comment-like" 'data-id' = c.id 'aria-pressed' = c.pressed title = likeLabel c.likeText on click(e) { emit commentsLike(e) } }
  113. if (c.offering) {
  114. button { type = "button" class = "comment-edit-start" 'data-id' = c.id editLabel on click(e) { emit commentsEdit(e) } }
  115. button { type = "button" class = "comment-delete" 'data-id' = c.id deleteLabel on click(e) { emit commentsAsk(e) } }
  116. }
  117. if (c.asking) { commentAsk {
  118. span { askText }
  119. button { type = "button" class = "comment-delete-yes" 'data-id' = c.id deleteLabel on click(e) { emit commentsDelete(e) } }
  120. button { type = "button" class = "comment-delete-keep" keepLabel on click(e) { emit commentsKeep(e) } }
  121. } }
  122. }
  123. }
  124. }
  125. }
  126. }
  127. if (canWrite) { for (f of forms) { form { class = "comment-form" on submit(e) { emit commentsPost(e) }
  128. MdEditor { name = "comment-new" value = "" placeholder = placeholder rows = 4 }
  129. commentButtons { button { type = "submit" class = "comment-post" postLabel } }
  130. } } }
  131. if (cannotWrite) { p { class = "comments-sign-in" signInText } }
  132. }
  133. }
  134. idOf(e) {
  135. el = e.target.closest('[data-id]')
  136. return el == null ? '' : el.getAttribute('data-id')
  137. }
  138. // the Markdown the editor of a form holds now (its hidden field, kept up to date by every keystroke)
  139. typedIn(form) {
  140. f = form.querySelector('md-editor input.value')
  141. return f == null ? '' : f.value
  142. }
  143. // post: the host is told the text; answering false keeps it in the editor (e.g. the server refused), else the form starts empty
  144. on commentsPost(e) {
  145. e.preventDefault()
  146. text = typedIn(e.target)
  147. if (text.trim() == '' || onPost == null) { return null }
  148. ok = onPost(text)
  149. if (ok != false) { formSeq = formSeq + 1 }
  150. }
  151. on commentsEdit(e) {
  152. askingId = ''
  153. editingId = idOf(e)
  154. }
  155. on commentsCancel(e) { editingId = '' }
  156. on commentsSave(e) {
  157. e.preventDefault()
  158. id = idOf(e)
  159. text = typedIn(e.target)
  160. let old = ''
  161. for (c of comments) { if ('' + c.id == id) { old = c.text } }
  162. if (text.trim() != '' && text != old && onEdit != null) { onEdit(id, text) }
  163. editingId = ''
  164. }
  165. on commentsAsk(e) {
  166. editingId = ''
  167. askingId = idOf(e)
  168. }
  169. on commentsKeep(e) { askingId = '' }
  170. on commentsDelete(e) {
  171. id = idOf(e)
  172. askingId = ''
  173. if (onDelete != null) { onDelete(id) }
  174. }
  175. on commentsLike(e) {
  176. if (user == '' || onLike == null) { return null }
  177. onLike(idOf(e))
  178. }
  179. // ---- look: the component's own Style; the base look is black, white and the greys #555 #999 #ddd #f2f2f2 (shared/base.hl names them;
  180. // root README "Look"); every colour is a semantic token with its base value as fallback, so an app that declares the tokens
  181. // (layouts.worldapi.org's theme) restyles it. No frames: the bubble is a grey field on the page, the avatar a grey disc.
  182. Style {
  183. 'comments-block' { display = 'block' color = 'var(--color-text, #000)' }
  184. 'comments-block h2.comments-count' { margin = '0 0 .8rem' fontSize = '1.05rem' }
  185. 'comments-block .comments-empty, comments-block .comments-sign-in' { margin = '0 0 .8rem' color = 'var(--color-text-muted, #555)' }
  186. 'comments-block ol.comment-list' { display = 'flex' flexDirection = 'column' gap = '.9rem' margin = '0 0 1rem' padding = '0' listStyle = 'none' }
  187. 'comments-block li.comment' { display = 'flex' gap = '.6rem' alignItems = 'flex-start' }
  188. '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)' }
  189. 'comments-block comment-main' { display = 'block' flex = '1' minWidth = '0' }
  190. '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)' }
  191. 'comments-block comment-meta' { display = 'flex' flexWrap = 'wrap' alignItems = 'baseline' gap = '.2rem .6rem' marginBottom = '.2rem' }
  192. 'comments-block .comment-author' { fontSize = '.95rem' color = 'var(--color-text-strong, #000)' overflowWrap = 'anywhere' }
  193. 'comments-block comment-meta time' { fontSize = '.8rem' color = 'var(--color-text-muted, #555)' }
  194. 'comments-block details.comment-history' { marginLeft = 'auto' fontSize = '.8rem' color = 'var(--color-text-muted, #555)' }
  195. 'comments-block details.comment-history[open]' { flexBasis = '100%' marginLeft = '0' }
  196. 'comments-block details.comment-history summary' { cursor = 'pointer' textDecoration = 'underline' textUnderlineOffset = '.15em' textAlign = 'right' }
  197. 'comments-block .comment-history-title' { margin = '.3rem 0' fontWeight = '600' }
  198. 'comments-block details.comment-history ol' { margin = '0' padding = '0' listStyle = 'none' }
  199. 'comments-block details.comment-history li' { margin = '0 0 .4rem' }
  200. '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)' }
  201. 'comments-block comment-actions' { display = 'flex' flexWrap = 'wrap' alignItems = 'center' gap = '.1rem .3rem' marginTop = '.2rem' fontSize = '.85rem' }
  202. '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' }
  203. 'comments-block button:hover' { color = 'var(--color-text, #000)' background = 'var(--color-surface, #f2f2f2)' }
  204. 'comments-block button:focus-visible' { outline = '2px solid var(--color-accent, #555)' outlineOffset = '1px' }
  205. 'comments-block button.comment-like[aria-pressed="true"]' { color = 'var(--color-text-strong, #000)' }
  206. 'comments-block comment-ask' { display = 'flex' flexWrap = 'wrap' alignItems = 'center' gap = '.3rem' }
  207. '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)' }
  208. '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)' }
  209. 'comments-block form.comment-edit' { display = 'block' marginTop = '.3rem' }
  210. 'comments-block form.comment-edit md-editor' { background = 'var(--color-background, #fff)' }
  211. 'comments-block form.comment-form' { display = 'block' }
  212. 'comments-block comment-buttons' { display = 'flex' justifyContent = 'flex-end' gap = '.4rem' marginTop = '.4rem' }
  213. }

Branches

Latest commits

  • 9db927dfpaths: projects moved into /media/STORAGE/projects/WorldAPI/mre
  • 00a95329components (mission 007): re-vendor Hybriel to the shared static build 0f396652 (plugins; bin/ git-ignored), gate 324/0 old and new, real-data copy unchanged, report 007, same-output.mjs ports overridablemre
  • fe50766bmission brief 007mre
  • 2dce51b8components (mission 006) 4/4: docs (README comments + gate 324, STATUS, LOG), report reports/006-comments.mdmre
  • 9ec7273ecomponents layouts#2 (mission 006) 3/4: comments — each text is MdEditor readonly directly in its row (hl:web builds a composed child anew when a bound value changes, so the keyed one-row list was not needed); demo 'bob edits his comment elsewhere (a push)' + gate check that a text the host changes from outside is drawn anew; theme radii (var(--radius-small/large) with 5px/10px fallbacks: layouts' check-theme 0 problems with comments.hl copied in); README 'How it is built'; gate 324/0 (34 comments checks)mre
  • fd57c18bcomponents layouts#2 (mission 006) 2/3: comments block (components/comments, demo /comments) — pure Hybriel: '1 comment' / 'n comments', avatar initial, bubble with name, time, 'edited' (<details> with the earlier versions), the text drawn by MdEditor readonly (one-row list keyed by id + text, so an edit redraws), like ♥ n (aria-pressed), Edit / Delete (asks first) only where author == user, the form below in MdEditor (Comment button or Ctrl+Enter; a fresh editor after a post). Host passes comments + user and hands onPost / onEdit / onDelete / onLike (hybriel#87); demo keeps a mock thread from lib/comments.hl in the page, switches alice / bob / nobody. Home entry, routes, map line, README; base-look gate covers comments; 33 gate checks with real clicks / keysmre
  • 850a376bcomponents (mission 006) 1/3: MdEditor readonly = "true" — the Markdown shown formatted and nothing else (no toolbar, link form, file rows, placeholder, source view or switch; article contenteditable false, tabindex -1, role document; no field background or focus ring); the mount only loads the value. For the comments block, which draws every comment with the editor's own renderermre
  • ee9f1b57mission brief w6mre
  • fe691586components (mission 005) 4/4: docs (README components + gate 290, STATUS, LOG), report reports/005-fixes-16-17-5.mdmre
  • f1ebab53components #5 (mission 005) 3/4: file upload for several files (components/files-upload, demo /files-upload) — pure Hybriel, each file through emit server uploadFile (hybriel#94), drop or choose many, a row per file with live progress, one after the other in order, a drop during an upload waits, a file over uploadMax fails in its row and the next goes, 'n of m uploaded'; home entry, routes, map line, README; pagination keyboard focus ring #555 (Chrome painted its own orange one); docker-compose init: true (hybriel#156); gate 290/0 (17 #5 checks with real file drops, 1 #16 focus check)mre
  • 3b73ae5bcomponents #16 (mission 005) 2/4: Pagination tells the host the target page — the host hands onPage (hybriel#87: a function, not emit), called on every click that changes the page (pointer, Enter/Space, script click; Previous/Next/numbers/first/last; not … or the current page) with the target read from the clicked button's data-page before the rebuild; demo shows what the page was told; README Data out; 15 gate checks (14 fail on the old pagination)mre
  • 6b27c0f0components #17 (mission 005) 1/4: md-editor keeps backslash + r — parseInto dropped the two characters '\r' (C:\root -> C:oot) instead of a real carriage return; CR now decoded from JSON (String.fromCharCode does not parse in a member), real CRs also go from pasted plain text; 5 gate checks (3 fail on the old md-editor.hl)mre
  • 1a5dc327mission brief w5mre
  • a567b579components (mission 004) 3/3: docs (README Hybriel a832f73b + tables, STATUS, LOG), report reports/004-md-editor-tables.mdmre
  • 70fe7ab5components (mission 004) 2/3: GitHub pipe tables in the Hybriel MdEditor (creator notes#5; behaviour of notes md-editor.js 9acc27f, no JavaScript) — parse/render (header, :--/:-:/--: alignment, \| in cells and code, inline Markdown in cells, outer pipes optional), padded writer, unchanged table byte for byte, typing | a | b | Enter |---|---| Enter makes a table, Tab/Shift+Tab, Enter new row, Enter in empty last row leaves, Backspace/Delete never merge, selection across a table empties cells, pasted HTML table -> table, thin #999 lines, wide table scrolls in its box; demo third editor 'plan'; gate 252/0 (22 new table checks, 20 fail on the old md-editor.hl)mre
  • f2adb3f3components (mission 004) 1/3: Hybriel master a832f73b (incremental build cache, HL_CACHE_DIR, #151) — bin/hybriel sha256 e1b53f7ce687f5afbe3db34bcda69958f56e730571e4841923c9c8eb3d272db9 built from git archive a832f73b (HL_NO_PLUGINS=1 native/build.sh), the same nine plugins; container: HL_CACHE_DIR=/cache on volume hybriel-cache, /tmp tmpfs; tests wait COMPONENTS_COLD_MS (120 s) for every start; deploy.sh DEPLOY_URL_WAIT 120 s; gate 230/0 coldmre
  • 91bd8a0dmission brief (w4)mre
  • 3bc905c5components #15 (mission 003) 2/2: docs (README Look, STATUS, LOG), reportmre
  • 5a98d77ecomponents #15 (mission 003) 1/2: black/white base look — greys for nuances, no WorldAPI fallbacks, no decorative frames; site + icon black/white; gate 230/0mre
  • 3f93d716components code order (mission 002) 4/4: docs, tests/letcount.py, tests/same-output*, reportmre