gitoria

components

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit3f0a72293f0a7229Month view, date picker, range picker: names follow the page language (English by default, not the browser's), weekday/month names formatted in UTC (a UTC+12 zone showed Tue first), 44 px day cells and buttons, Send row air; gate checks at 390 px for every page, popup and dialog (MC)mre3f0a7229/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

  • 3f0a7229Month view, date picker, range picker: names follow the page language (English by default, not the browser's), weekday/month names formatted in UTC (a UTC+12 zone showed Tue first), 44 px day cells and buttons, Send row air; gate checks at 390 px for every page, popup and dialog (MC)mre
  • 29eb07eaPhone: 44 px targets on the site (brand, home links, buttons), comments, modal and pagination; the header is the brand alone on a phone; comments demo: labelled 'You are' group with the chosen user pressed (MC)mre
  • d1cc83e2reports/E2-toolbar.mdmre
  • 214c3183MdEditor on a phone: toolbar as two even rows of 44 px targets (no clipped icon, no swipe), 'Markdown source' label 15 px, table head 16 px; gate checks (E2)mre
  • 9215bbaareports/F5-empty.mdmre
  • 134b4e3eMdEditor: an editor the framework puts into the page is built at once (page observer), a surface put in anew under a built editor is redrawn; gates for both (hybriel#211)mre
  • 0dce2899reports/K1-bubble.mdmre
  • 6b0936a0comments: 'edited' foldable also drops the theme's border, margin and radius (display: contents alone left them computed)mre
  • 8d79ff79comments bubble hugs its content: a readonly md-editor surface gets no inline min-height (it beat the stylesheet's min-height 0); 'edited' is a quiet label beside the time, versions unframed; gate checksmre
  • ef253fecreports/F4-icons.mdmre
  • ba96735ficons: README, md-editor README note, gate checks for the single focus ring and toolbar iconsmre
  • 96c97691icons: shared Icon component; md-editor toolbar and files-upload remove use it; no inner focus ring in md-editormre
  • 0d6863d6reports/F3-mdvalue.mdmre
  • d58ed98bMdEditor: an older text of the editor handed back is not an outside change (kept surface, written back); gate for itmre
  • c2d0de59MdEditor: show a value that changes from outside (data-value observer), demo button and gatemre
  • c4272fa3re-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
  • 2e22618etests: cdp-blank/md-bg/md-keys-run Chrome range and base port from env (MDK_CHROME, MDK_PORT), defaults unchangedmre
  • a5d2384bre-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
  • 52902d23docker-compose: nofile 65536 (Hybriel int 6+ boot opened >1024 files in the tracker)mre
  • 022b943fdeploy: backup copies index files first and *.mpack data last (hot-copy skew, hybriel #203)mre