gitoria

tickets

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitd74a5482d74a5482sync components.hybriel 134b4e3: MdEditor builds an editor put into the page at once, redraws a surface put in anew (F5, hybriel#211)mred74a5482/components/files-upload/files-upload.hl

15.4 KB

  1. // files-upload.hl — THE FILE UPLOAD FOR SEVERAL FILES (ticket #5), pure Hybriel, in two modes (`mode`).
  2. // UPLOAD AT ONCE (the default, `mode = "upload"`): a drop zone with a "Choose files" button (more than one at a time). Every dropped,
  3. // chosen or pasted file gets its own row — name, size, a progress bar, its state — and goes to the app through
  4. // `emit server uploadFile(file)` (the framework carries it over HTTP, in parts, with progress: hybriel#94), one file after the other in
  5. // the order they came; the app's `on server uploadFile(file)` decides where it is stored and answers anything but null when it took it
  6. // (an error or null = that file failed; the next one still goes). Files dropped during an upload wait in their row ("Waiting") and follow.
  7. // The line under the rows counts what is uploaded. The rows are written by DOM calls in the handlers: an emit blocks its handler, and
  8. // `on client uploadProgress` paints the bar of the row that is sent.
  9. // PICK FIRST, SEND LATER (`mode = "pick"`, ticket #18 for HyperConductor's chat attachments): dropped, chosen, pasted (Ctrl+V) or
  10. // photographed files are COLLECTED, nothing is sent. THE HOST OWNS THE LIST (the Comments block's pattern, hybriel#87): it binds
  11. // `files = attached` and hands `onChange = (list) => { attached = list }`; the component calls onChange with the new list of browser
  12. // Files after every pick and every ✕, and shows the list it is given — an image as a thumbnail, any other file as a name + size row,
  13. // each with ✕. At send time the host passes its list to its own face (`emit server chatSend(text, attached)`) and clears it
  14. // (`attached = []`). The host's input area goes INSIDE the component (`FilesUpload { mode = "pick" … textarea { } button { } }`, the
  15. // slot): a paste or a drop there is caught too. `maxSize` (bytes, 0 = none, both modes): a bigger file gets a row with a message and is
  16. // not collected / not sent. On a touch screen a Camera button (`accept="image/*" capture`) opens the camera; "Choose files" offers the
  17. // phone's gallery and files. USE ONE PER PAGE (a composed component's members are the page's state).
  18. import Icon from '../icons/icons.hl'
  19. String mode = 'upload'
  20. Hybrid files = []
  21. onChange = null
  22. Number maxSize = 0
  23. String accept = ''
  24. slot = null
  25. String dropText = 'Drop files here or'
  26. String chooseText = 'Choose files'
  27. String pickText = 'Drop or paste files here, or'
  28. String cameraText = 'Camera'
  29. String removeText = 'Remove'
  30. String tooBigText = 'Too big — at most'
  31. Boolean uploads = mode != 'pick'
  32. Boolean picks = mode == 'pick'
  33. // pick mode: what each collected file shows, the refused files of the last pick, the line under them
  34. Hybrid refused = []
  35. Hybrid rows = rowsOf(files)
  36. Hybrid refusedRows = refusedOf(refused, files)
  37. String pickedText = pickedTextOf(files)
  38. sizeText(n) {
  39. if (n < 1024) { return n + ' B' }
  40. if (n < 1048576) { return (n / 1024).toFixed(1) + ' KB' }
  41. return (n / 1048576).toFixed(1) + ' MB'
  42. }
  43. // "Too big — at most 2.0 MB" (in the browser: the server has no toFixed)
  44. maxText() { return tooBigText + ' ' + sizeText(maxSize) }
  45. // "TXT", "PDF": the box a file that is not an image shows in place of a thumbnail
  46. kindOf(name) {
  47. dot = name.lastIndexOf('.')
  48. return dot > 0 && dot < name.length - 1 ? name.slice(dot + 1, dot + 5).toUpperCase() : 'FILE'
  49. }
  50. // one row per collected file; an image gets an object URL for its thumbnail (given back once the thumbnail has loaded)
  51. rowsOf(list) {
  52. out = []
  53. let i = 0
  54. for (f of list) {
  55. let image = ('' + f.type).startsWith('image/')
  56. out.push({ i = '' + i name = f.name size = sizeText(f.size) image = image other = !image url = image ? window.URL.createObjectURL(f) : '' kind = kindOf(f.name) remove = removeText + ' ' + f.name })
  57. i = i + 1
  58. }
  59. return out
  60. }
  61. // the refused files of the last pick; once the host empties a list they were picked into (after its Send), they go too
  62. refusedOf(list, current) {
  63. out = []
  64. for (r of list) { if (current.length > 0 || r.kept == 0) { out.push(r) } }
  65. return out
  66. }
  67. // "2 files · 1.2 MB"
  68. pickedTextOf(list) {
  69. if (list.length == 0) { return '' }
  70. let total = 0
  71. for (f of list) { total = total + f.size }
  72. return (list.length == 1 ? '1 file' : list.length + ' files') + ' · ' + sizeText(total)
  73. }
  74. View {
  75. filesUpload { on paste(e) { emit filesPaste(e) }
  76. if (uploads) {
  77. dropZone { tabindex = "0" on dragover(e) { emit filesOver(e) } on dragleave(e) { emit filesLeave(e) } on drop(e) { emit filesDrop(e) }
  78. p { dropText }
  79. label { class = "choose" chooseText input { type = "file" multiple = "multiple" on change(e) { emit filesChosen(e) } } }
  80. }
  81. ul { class = "files" 'aria-live' = "polite" }
  82. output { class = "summary" }
  83. }
  84. if (picks) {
  85. pickArea { on dragover(e) { emit pickOver(e) } on dragleave(e) { emit filesLeave(e) } on drop(e) { emit pickDrop(e) }
  86. ul { class = "files picked" 'aria-live' = "polite"
  87. for (r of rows) {
  88. li { class = "picked" 'data-i' = r.i
  89. if (r.image) { img { class = "thumb" src = r.url alt = "" on load(e) { emit pickShown(e) } } }
  90. if (r.other) { span { class = "kind" 'aria-hidden' = "true" r.kind } }
  91. fileInfo { strong { class = "name" r.name } span { class = "size" r.size } }
  92. button { type = "button" class = "remove" 'data-i' = r.i 'aria-label' = r.remove title = r.remove Icon { name = "close" size = "1.1em" } on click(e) { emit pickRemove(e) } }
  93. }
  94. }
  95. for (r of refusedRows) {
  96. li { class = "refused" 'data-i' = r.i
  97. span { class = "kind" 'aria-hidden' = "true" "!" }
  98. fileInfo { strong { class = "name" r.name } span { class = "size" r.size } output { class = "state" r.message } }
  99. button { type = "button" class = "remove" 'data-i' = r.i 'aria-label' = r.remove title = r.remove Icon { name = "close" size = "1.1em" } on click(e) { emit pickDismiss(e) } }
  100. }
  101. }
  102. }
  103. output { class = "summary" pickedText }
  104. slot
  105. dropZone { tabindex = "0"
  106. p { pickText }
  107. label { class = "choose" chooseText input { type = "file" multiple = "multiple" accept = accept on change(e) { emit pickChosen(e) } } }
  108. label { class = "camera" cameraText input { type = "file" accept = "image/*" capture = "environment" on change(e) { emit pickChosen(e) } } }
  109. }
  110. }
  111. }
  112. }
  113. }
  114. rootOf(node) { return node.closest('files-upload') }
  115. on filesOver(e) {
  116. e.preventDefault()
  117. rootOf(e.target).classList.add('over')
  118. }
  119. on filesLeave(e) { rootOf(e.target).classList.remove('over') }
  120. on filesDrop(e) {
  121. e.preventDefault()
  122. root = rootOf(e.target)
  123. root.classList.remove('over')
  124. add(root, e.dataTransfer.files)
  125. }
  126. on filesChosen(e) {
  127. root = rootOf(e.target)
  128. input = e.target
  129. add(root, input.files)
  130. input.value = ''
  131. }
  132. // Ctrl+V anywhere in the component (in pick mode: also in the host's input area inside it): the clipboard's files — a screenshot, a copied
  133. // image or file. A paste without files (text) is left alone.
  134. on filesPaste(e) {
  135. pasted = e.clipboardData != null ? e.clipboardData.files : null
  136. if (pasted == null || pasted.length == 0) { return null }
  137. e.preventDefault()
  138. if (picks) { take(pasted) } else { add(rootOf(e.target), pasted) }
  139. }
  140. // ---- pick mode: a drag carrying files is taken anywhere in the area (text dragged into the host's field is left alone)
  141. carriesFiles(e) {
  142. let found = false
  143. if (e.dataTransfer != null) { for (t of e.dataTransfer.types) { if (t == 'Files') { found = true } } }
  144. return found
  145. }
  146. on pickOver(e) {
  147. if (!carriesFiles(e)) { return null }
  148. e.preventDefault()
  149. rootOf(e.target).classList.add('over')
  150. }
  151. on pickDrop(e) {
  152. if (!carriesFiles(e)) { return null }
  153. e.preventDefault()
  154. rootOf(e.target).classList.remove('over')
  155. take(e.dataTransfer.files)
  156. }
  157. on pickChosen(e) {
  158. input = e.target
  159. take(input.files)
  160. input.value = ''
  161. }
  162. // the host's list and the new files (a file over maxSize is refused in its own row), told to the host
  163. take(list) {
  164. next = []
  165. for (f of files) { next.push(f) }
  166. tooBig = []
  167. for (f of list) {
  168. if (maxSize > 0 && f.size > maxSize) {
  169. tooBig.push(f)
  170. } else {
  171. next.push(f)
  172. }
  173. }
  174. out = []
  175. let i = 0
  176. for (f of tooBig) {
  177. out.push({ i = '' + i name = f.name size = sizeText(f.size) message = maxText() remove = removeText + ' ' + f.name kept = next.length })
  178. i = i + 1
  179. }
  180. refused = out
  181. if (onChange != null) { onChange(next) }
  182. }
  183. on pickRemove(e) {
  184. gone = toNumber(e.target.getAttribute('data-i'))
  185. next = []
  186. let i = 0
  187. for (f of files) {
  188. if (i != gone) { next.push(f) }
  189. i = i + 1
  190. }
  191. if (onChange != null) { onChange(next) }
  192. }
  193. on pickDismiss(e) {
  194. gone = e.target.getAttribute('data-i')
  195. out = []
  196. for (r of refused) { if (r.i != gone) { out.push(r) } }
  197. refused = out
  198. }
  199. // the thumbnail is drawn: its object URL is given back (the picture stays)
  200. on pickShown(e) { window.URL.revokeObjectURL(e.target.src) }
  201. // the bar of the row being sent (one at a time)
  202. on client uploadProgress(p) {
  203. for (row of document.querySelectorAll('files-upload li.busy')) {
  204. let bar = row.querySelector('progress')
  205. bar.max = p.size > 0 ? p.size : 1
  206. bar.value = p.sent
  207. }
  208. }
  209. // one row per file, waiting; then the queue runs (unless it already does)
  210. add(root, picked) {
  211. list = root.querySelector('ul.files')
  212. for (f of picked) {
  213. let li = document.createElement('li')
  214. li.className = 'waiting'
  215. let name = document.createElement('strong')
  216. name.className = 'name'
  217. name.textContent = f.name
  218. let size = document.createElement('span')
  219. size.className = 'size'
  220. size.textContent = sizeText(f.size)
  221. let bar = document.createElement('progress')
  222. bar.max = 100
  223. bar.value = 0
  224. let state = document.createElement('output')
  225. state.className = 'state'
  226. state.textContent = 'Waiting'
  227. li.appendChild(name)
  228. li.appendChild(size)
  229. li.appendChild(bar)
  230. li.appendChild(state)
  231. li.upload = f
  232. if (maxSize > 0 && f.size > maxSize) {
  233. li.className = 'failed'
  234. li.upload = null
  235. state.textContent = maxText()
  236. }
  237. list.appendChild(li)
  238. }
  239. count(root)
  240. run(root)
  241. }
  242. // sends the waiting rows one after the other
  243. run(root) {
  244. if (root.classList.contains('busy')) { return null }
  245. root.classList.add('busy')
  246. let row = root.querySelector('li.waiting')
  247. while (row != null) {
  248. row.classList.remove('waiting')
  249. row.classList.add('busy')
  250. row.querySelector('.state').textContent = 'Uploading…'
  251. let saved = emit server uploadFile(row.upload)
  252. row.classList.remove('busy')
  253. row.upload = null
  254. let bar = row.querySelector('progress')
  255. if (saved == null) {
  256. row.classList.add('failed')
  257. row.querySelector('.state').textContent = 'Failed'
  258. } else {
  259. bar.max = 100
  260. bar.value = 100
  261. row.classList.add('done')
  262. row.querySelector('.state').textContent = 'Uploaded'
  263. }
  264. count(root)
  265. row = root.querySelector('li.waiting')
  266. }
  267. root.classList.remove('busy')
  268. }
  269. // "2 of 3 uploaded" (and how many failed)
  270. count(root) {
  271. all = root.querySelectorAll('ul.files li').length
  272. done = root.querySelectorAll('ul.files li.done').length
  273. failed = root.querySelectorAll('ul.files li.failed').length
  274. let text = all == 0 ? '' : done + ' of ' + all + ' uploaded'
  275. if (failed > 0) { text = text + ', ' + failed + ' failed' }
  276. root.querySelector('output.summary').textContent = text
  277. }
  278. // ---- look: the component's own Style, the same base look as the one-file upload (black, white, the greys #555 #999 #ddd #f2f2f2;
  279. // root README "Look"); every colour is a semantic token with its base value as fallback. No frames: the one border is the drop zone's
  280. // dashed one (it means "drop here"); rows are separated by space only. MOBILE FIRST: every button is at least 44 × 44 px (2.75rem);
  281. // the Camera button shows only on a touch screen (`pointer: coarse`)
  282. Style {
  283. 'files-upload' { display = 'block' color = 'var(--color-text, #000)' }
  284. 'files-upload drop-zone' { display = 'block' padding = '1rem' textAlign = 'center' border = '2px dashed var(--color-border, #999)' borderRadius = '.5rem' }
  285. 'files-upload.over drop-zone' { borderColor = 'var(--color-accent, #000)' background = 'var(--color-surface, #f2f2f2)' }
  286. 'files-upload drop-zone p' { margin = '0 0 .5rem' color = 'var(--color-text-muted, #555)' }
  287. 'files-upload drop-zone:focus-visible' { outline = '2px solid var(--color-accent, #555)' outlineOffset = '2px' }
  288. 'files-upload label.choose, files-upload label.camera' { display = 'inline-flex' alignItems = 'center' justifyContent = 'center' minHeight = '2.75rem' minWidth = '2.75rem' boxSizing = 'border-box' padding = '0 1rem' cursor = 'pointer' border = '0' borderRadius = '.35rem' background = 'var(--color-surface, #f2f2f2)' }
  289. 'files-upload label.choose:hover, files-upload label.camera:hover' { filter = 'brightness(.93)' }
  290. 'files-upload label.choose input, files-upload label.camera input' { display = 'none' }
  291. 'files-upload label.camera' { display = 'none' marginLeft = '.4rem' }
  292. '@media (pointer: coarse)' {
  293. 'files-upload label.camera' { display = 'inline-flex' }
  294. }
  295. 'files-upload ul.files' { listStyle = 'none' margin = '.8rem 0 0' padding = '0' }
  296. 'files-upload ul.files li' { display = 'grid' gridTemplateColumns = '1fr auto' columnGap = '.6rem' marginBottom = '.7rem' }
  297. 'files-upload ul.files .name' { overflowWrap = 'anywhere' }
  298. 'files-upload ul.files .size' { color = 'var(--color-text-muted, #555)' textAlign = 'right' }
  299. 'files-upload ul.files progress' { gridColumn = '1 / -1' width = '100%' margin = '.3rem 0' accentColor = 'var(--color-accent, #000)' }
  300. 'files-upload ul.files .state' { gridColumn = '1 / -1' fontSize = '.9rem' color = 'var(--color-text-muted, #555)' }
  301. 'files-upload ul.files li.done .state' { color = 'var(--color-success, #000)' }
  302. 'files-upload ul.files li.failed .state' { color = 'var(--color-danger, #000)' fontWeight = '600' }
  303. 'files-upload output.summary' { display = 'block' marginTop = '.3rem' fontWeight = '600' }
  304. 'files-upload pick-area' { display = 'block' borderRadius = '.5rem' }
  305. 'files-upload.over pick-area' { background = 'var(--color-surface, #f2f2f2)' }
  306. 'files-upload pick-area drop-zone' { padding = '.5rem' marginTop = '.5rem' borderWidth = '1px' }
  307. 'files-upload pick-area drop-zone p' { display = 'inline' margin = '0 .5rem 0 0' fontSize = '.9rem' }
  308. 'files-upload ul.files li.picked, files-upload ul.files li.refused' { gridTemplateColumns = '3rem minmax(0, 1fr) 2.75rem' alignItems = 'center' marginBottom = '.4rem' }
  309. 'files-upload ul.files img.thumb, files-upload ul.files .kind' { display = 'block' width = '3rem' height = '3rem' borderRadius = '.35rem' background = 'var(--color-surface, #f2f2f2)' }
  310. 'files-upload ul.files img.thumb' { objectFit = 'cover' }
  311. 'files-upload ul.files .kind' { display = 'grid' placeItems = 'center' fontSize = '.7rem' fontWeight = '700' color = 'var(--color-text-muted, #555)' background = 'var(--color-surface, #ddd)' }
  312. 'files-upload ul.files file-info' { display = 'flex' flexDirection = 'column' minWidth = '0' }
  313. 'files-upload ul.files file-info .size' { textAlign = 'left' fontSize = '.85rem' }
  314. 'files-upload ul.files li.refused .state' { color = 'var(--color-danger, #000)' fontWeight = '600' }
  315. 'files-upload button.remove' { display = 'inline-flex' alignItems = 'center' justifyContent = 'center' width = '2.75rem' height = '2.75rem' padding = '0' font = 'inherit' fontSize = '1.1rem' color = 'var(--color-text-muted, #555)' background = 'transparent' border = '0' borderRadius = '.35rem' cursor = 'pointer' }
  316. 'files-upload button.remove:hover' { color = 'var(--color-text, #000)' background = 'var(--color-surface, #f2f2f2)' }
  317. 'files-upload button.remove:focus-visible' { outline = '2px solid var(--color-accent, #555)' outlineOffset = '1px' }
  318. 'files-upload pick-area output.summary:empty' { display = 'none' }
  319. }

Branches

Latest commits

  • d74a5482sync components.hybriel 134b4e3: MdEditor builds an editor put into the page at once, redraws a surface put in anew (F5, hybriel#211)mre
  • 9d02c540sync components.hybriel 6b0936a: the 'edited' foldable drops the theme's border and marginmre
  • 22751914sync components.hybriel 8d79ff7: bubble hugs its content, quiet 'edited' label; gate checksmre
  • c8452453sync components 9a: shared Icon (icons/icons.hl), md-editor toolbar icons + single focus ring (components f4-icons ba96735)mre
  • 52ba11e8sync-components: copy icons/icons.hl + README (F4); testsmre
  • 138d198dmd-editor.hl: verbatim copy of components f3-mdvalue (an older text handed back is kept)mre
  • 18d2df60md-editor.hl: verbatim copy of components f3-mdvalue (an outside value change is shown by the editor)mre
  • f6764bedreports/H1-hydrate.mdmre
  • 1bbbf0edtests/browser.mjs: wait for hydration before clicking the login button on / (device B)mre
  • 6f503cfctests/cdp.mjs: HL_CPU_THROTTLE=<rate> optional CPU throttling for gate runsmre
  • e17e2855re-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
  • a6ddccd2re-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
  • 81e036b7docker-compose: nofile 65536 (Hybriel int 6+ boot opened >1024 files in the tracker)mre
  • 5d8d2cf6deploy: backup copies index files first and *.mpack data last (hot-copy skew, hybriel #203)mre
  • 58e8c02cmission 024: report, LOG, STATUS, e2e fixmre
  • 50778073tests/e2e-route-marker.mjs: first paint marker, comments post/edit redrawmre
  • c5203298mission 024: MainMenu marks the page from the route (layouts 0bcc629 main-menu.hl); browser-side marker removed; gate: marker in the server HTMLmre
  • b1a4c270re-vendor Hybriel master f9614007: bin/ (git-ignored) and plugins wholemre
  • 866397a0mission 023: theme from layouts 76f99f6 (current marker, menu count badge), components from 3794c59 (MdEditor keys); Inbox count as badge, current by address; gates and E2E; reportmre
  • 7ed2e5e5deploy.sh: a gate without a 'passed,' line (check-theme) no longer ends the script silently (grep under pipefail), as gitoria/layoutsmre