gitoria

components

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Main branchmaina06fc778Reset virtual-list window when the host changes resetKeyAtlasmain/components/files-upload/files-upload.hl

18.0 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" … form { textarea { } button { } } }`, the
  15. // slot) and the whole thing is ONE BOX: the host's field on top, the picked files as thumbnails / rows right under it, then a footer with
  16. // the small "Choose files" (and Camera) button at the left and the host's own buttons — its Send — at the right end. A drop or a paste
  17. // anywhere in the box is caught. `maxSize` (bytes, 0 = none, both modes): a bigger file gets a row with a message and is
  18. // not collected / not sent. On a touch screen a Camera button (`accept="image/*" capture`) opens the camera; "Choose files" offers the
  19. // phone's gallery and files. USE ONE PER PAGE (a composed component's members are the page's state).
  20. import Icon from '../icons/icons.hl'
  21. String mode = 'upload'
  22. Hybrid files = []
  23. onChange = null
  24. Number maxSize = 0
  25. String accept = ''
  26. slot = null
  27. String dropText = 'Drop files here or'
  28. String chooseText = 'Choose files'
  29. String pickText = 'Choose files — or drop or paste them anywhere in the box'
  30. String cameraText = 'Camera'
  31. String removeText = 'Remove'
  32. String tooBigText = 'Too big — at most'
  33. Boolean uploads = mode != 'pick'
  34. Boolean picks = mode == 'pick'
  35. // pick mode: what each collected file shows, the refused files of the last pick, the line under them
  36. Hybrid refused = []
  37. Hybrid rows = rowsOf(files)
  38. Hybrid refusedRows = refusedOf(refused, files)
  39. String pickedText = pickedTextOf(files)
  40. sizeText(n) {
  41. if (n < 1024) { return n + ' B' }
  42. if (n < 1048576) { return (n / 1024).toFixed(1) + ' KB' }
  43. return (n / 1048576).toFixed(1) + ' MB'
  44. }
  45. // "Too big — at most 2.0 MB" (in the browser: the server has no toFixed)
  46. maxText() { return tooBigText + ' ' + sizeText(maxSize) }
  47. // "TXT", "PDF": the box a file that is not an image shows in place of a thumbnail
  48. kindOf(name) {
  49. dot = name.lastIndexOf('.')
  50. return dot > 0 && dot < name.length - 1 ? name.slice(dot + 1, dot + 5).toUpperCase() : 'FILE'
  51. }
  52. // one row per collected file; an image gets an object URL for its thumbnail (given back once the thumbnail has loaded)
  53. rowsOf(list) {
  54. out = []
  55. let i = 0
  56. for (f of list) {
  57. let image = ('' + f.type).startsWith('image/')
  58. 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 })
  59. i = i + 1
  60. }
  61. return out
  62. }
  63. // the refused files of the last pick; once the host empties a list they were picked into (after its Send), they go too
  64. refusedOf(list, current) {
  65. out = []
  66. for (r of list) { if (current.length > 0 || r.kept == 0) { out.push(r) } }
  67. return out
  68. }
  69. // "2 files · 1.2 MB"
  70. pickedTextOf(list) {
  71. if (list.length == 0) { return '' }
  72. let total = 0
  73. for (f of list) { total = total + f.size }
  74. return (list.length == 1 ? '1 file' : list.length + ' files') + ' · ' + sizeText(total)
  75. }
  76. View {
  77. filesUpload { on paste(e) { emit filesPaste(e) }
  78. if (uploads) {
  79. dropZone { tabindex = "0" on dragover(e) { emit filesOver(e) } on dragleave(e) { emit filesLeave(e) } on drop(e) { emit filesDrop(e) }
  80. p { dropText }
  81. label { class = "choose" chooseText input { type = "file" multiple = "multiple" on change(e) { emit filesChosen(e) } } }
  82. }
  83. ul { class = "files" 'aria-live' = "polite" }
  84. output { class = "summary" }
  85. }
  86. if (picks) {
  87. pickArea { on dragover(e) { emit pickOver(e) } on dragleave(e) { emit filesLeave(e) } on drop(e) { emit pickDrop(e) }
  88. slot
  89. ul { class = "files picked" 'aria-live' = "polite"
  90. for (r of rows) {
  91. li { class = "picked" 'data-i' = r.i
  92. if (r.image) { img { class = "thumb" src = r.url alt = "" on load(e) { emit pickShown(e) } } }
  93. if (r.other) { span { class = "kind" 'aria-hidden' = "true" r.kind } }
  94. fileInfo { strong { class = "name" r.name } span { class = "size" r.size } }
  95. 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) } }
  96. }
  97. }
  98. for (r of refusedRows) {
  99. li { class = "refused" 'data-i' = r.i
  100. span { class = "kind" 'aria-hidden' = "true" "!" }
  101. fileInfo { strong { class = "name" r.name } span { class = "size" r.size } output { class = "state" r.message } }
  102. 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) } }
  103. }
  104. }
  105. }
  106. output { class = "summary" pickedText }
  107. label { class = "choose" title = pickText 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. rootOf(node) { return node.closest('files-upload') }
  114. on filesOver(e) {
  115. e.preventDefault()
  116. rootOf(e.target).classList.add('over')
  117. }
  118. on filesLeave(e) { rootOf(e.target).classList.remove('over') }
  119. on filesDrop(e) {
  120. e.preventDefault()
  121. root = rootOf(e.target)
  122. root.classList.remove('over')
  123. add(root, e.dataTransfer.files)
  124. }
  125. on filesChosen(e) {
  126. root = rootOf(e.target)
  127. input = e.target
  128. add(root, input.files)
  129. input.value = ''
  130. }
  131. // 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
  132. // image or file. A paste without files (text) is left alone.
  133. on filesPaste(e) {
  134. pasted = e.clipboardData != null ? e.clipboardData.files : null
  135. if (pasted == null || pasted.length == 0) { return null }
  136. e.preventDefault()
  137. if (picks) { take(pasted) } else { add(rootOf(e.target), pasted) }
  138. }
  139. // ---- pick mode: a drag carrying files is taken anywhere in the area (text dragged into the host's field is left alone)
  140. carriesFiles(e) {
  141. let found = false
  142. if (e.dataTransfer != null) { for (t of e.dataTransfer.types) { if (t == 'Files') { found = true } } }
  143. return found
  144. }
  145. on pickOver(e) {
  146. if (!carriesFiles(e)) { return null }
  147. e.preventDefault()
  148. rootOf(e.target).classList.add('over')
  149. }
  150. on pickDrop(e) {
  151. if (!carriesFiles(e)) { return null }
  152. e.preventDefault()
  153. rootOf(e.target).classList.remove('over')
  154. take(e.dataTransfer.files)
  155. }
  156. on pickChosen(e) {
  157. input = e.target
  158. take(input.files)
  159. input.value = ''
  160. }
  161. // the host's list and the new files (a file over maxSize is refused in its own row), told to the host
  162. take(list) {
  163. next = []
  164. for (f of files) { next.push(f) }
  165. tooBig = []
  166. for (f of list) {
  167. if (maxSize > 0 && f.size > maxSize) {
  168. tooBig.push(f)
  169. } else {
  170. next.push(f)
  171. }
  172. }
  173. out = []
  174. let i = 0
  175. for (f of tooBig) {
  176. out.push({ i = '' + i name = f.name size = sizeText(f.size) message = maxText() remove = removeText + ' ' + f.name kept = next.length })
  177. i = i + 1
  178. }
  179. refused = out
  180. if (onChange != null) { onChange(next) }
  181. }
  182. on pickRemove(e) {
  183. gone = toNumber(e.target.getAttribute('data-i'))
  184. next = []
  185. let i = 0
  186. for (f of files) {
  187. if (i != gone) { next.push(f) }
  188. i = i + 1
  189. }
  190. if (onChange != null) { onChange(next) }
  191. }
  192. on pickDismiss(e) {
  193. gone = e.target.getAttribute('data-i')
  194. out = []
  195. for (r of refused) { if (r.i != gone) { out.push(r) } }
  196. refused = out
  197. }
  198. // the thumbnail is drawn: its object URL is given back (the picture stays)
  199. on pickShown(e) { window.URL.revokeObjectURL(e.target.src) }
  200. // the bar of the row being sent (one at a time)
  201. on client uploadProgress(p) {
  202. for (row of document.querySelectorAll('files-upload li.busy')) {
  203. let bar = row.querySelector('progress')
  204. bar.max = p.size > 0 ? p.size : 1
  205. bar.value = p.sent
  206. }
  207. }
  208. // one row per file, waiting; then the queue runs (unless it already does)
  209. add(root, picked) {
  210. list = root.querySelector('ul.files')
  211. for (f of picked) {
  212. let li = document.createElement('li')
  213. li.className = 'waiting'
  214. let name = document.createElement('strong')
  215. name.className = 'name'
  216. name.textContent = f.name
  217. let size = document.createElement('span')
  218. size.className = 'size'
  219. size.textContent = sizeText(f.size)
  220. let bar = document.createElement('progress')
  221. bar.max = 100
  222. bar.value = 0
  223. let state = document.createElement('output')
  224. state.className = 'state'
  225. state.textContent = 'Waiting'
  226. li.appendChild(name)
  227. li.appendChild(size)
  228. li.appendChild(bar)
  229. li.appendChild(state)
  230. li.upload = f
  231. if (maxSize > 0 && f.size > maxSize) {
  232. li.className = 'failed'
  233. li.upload = null
  234. state.textContent = maxText()
  235. }
  236. list.appendChild(li)
  237. }
  238. count(root)
  239. run(root)
  240. }
  241. // sends the waiting rows one after the other
  242. run(root) {
  243. if (root.classList.contains('busy')) { return null }
  244. root.classList.add('busy')
  245. let row = root.querySelector('li.waiting')
  246. while (row != null) {
  247. row.classList.remove('waiting')
  248. row.classList.add('busy')
  249. row.querySelector('.state').textContent = 'Uploading…'
  250. let saved = emit server uploadFile(row.upload)
  251. row.classList.remove('busy')
  252. row.upload = null
  253. let bar = row.querySelector('progress')
  254. if (saved == null) {
  255. row.classList.add('failed')
  256. row.querySelector('.state').textContent = 'Failed'
  257. } else {
  258. bar.max = 100
  259. bar.value = 100
  260. row.classList.add('done')
  261. row.querySelector('.state').textContent = 'Uploaded'
  262. }
  263. count(root)
  264. row = root.querySelector('li.waiting')
  265. }
  266. root.classList.remove('busy')
  267. }
  268. // "2 of 3 uploaded" (and how many failed)
  269. count(root) {
  270. all = root.querySelectorAll('ul.files li').length
  271. done = root.querySelectorAll('ul.files li.done').length
  272. failed = root.querySelectorAll('ul.files li.failed').length
  273. let text = all == 0 ? '' : done + ' of ' + all + ' uploaded'
  274. if (failed > 0) { text = text + ', ' + failed + ' failed' }
  275. root.querySelector('output.summary').textContent = text
  276. }
  277. // ---- look: the component's own Style, the same base look as the one-file upload (black, white, the greys #555 #999 #ddd #f2f2f2;
  278. // 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
  279. // dashed one (it means "drop here"); rows are separated by space only. MOBILE FIRST: every button is at least 44 × 44 px (2.75rem);
  280. // the Camera button shows only on a touch screen (`pointer: coarse`)
  281. Style {
  282. // the choose and camera labels share one look: a selector list has no form in the language
  283. 'files-upload label.choose, files-upload label.camera' {
  284. display = 'inline-flex'
  285. alignItems = 'center'
  286. justifyContent = 'center'
  287. minHeight = '2.75rem'
  288. minWidth = '2.75rem'
  289. boxSizing = 'border-box'
  290. padding = '0 1rem'
  291. cursor = 'pointer'
  292. border = '0'
  293. borderRadius = '.35rem'
  294. background = 'var(--color-surface, #f2f2f2)'
  295. }
  296. 'files-upload label.choose:hover, files-upload label.camera:hover' { filter = 'brightness(.93)' }
  297. 'files-upload label.choose input, files-upload label.camera input' { display = 'none' }
  298. // a list again: the picked and the refused row share the grid, the thumbnail and the kind badge share the box
  299. 'files-upload ul.files li.picked, files-upload ul.files li.refused' {
  300. gridTemplateColumns = '3rem minmax(0, 1fr) 2.75rem'
  301. alignItems = 'center'
  302. marginBottom = '.4rem'
  303. }
  304. 'files-upload ul.files img.thumb, files-upload ul.files .kind' {
  305. display = 'block'
  306. width = '3rem'
  307. height = '3rem'
  308. borderRadius = '.35rem'
  309. background = 'var(--color-surface, #f2f2f2)'
  310. }
  311. // pick mode is ONE box: the host's input (its textarea / editor) on top, the picked files right under it, and at the foot the small attach
  312. // buttons at the left and the host's own buttons (its Send) at the right end. The host's <form> dissolves into the box (display: contents)
  313. // so its field, its buttons and the component's own parts share one wrapping row layout; `order` puts them in that sequence. The box is one
  314. // sunken field: the host's field inside it has no background and no ring of its own, the box shows the focus ring and takes a drop anywhere.
  315. // The next rules reach the host's form in the slot (a child combinator, :has, :not, a list): no form in the language
  316. 'files-upload pick-area:has(> form > textarea:focus, > form > md-editor:focus-within)' {
  317. outline = '2px solid var(--color-accent, #555)'
  318. outlineOffset = '-1px'
  319. }
  320. 'files-upload pick-area > form' { display = 'contents' }
  321. 'files-upload pick-area form > textarea, files-upload pick-area form > md-editor' {
  322. order = '1'
  323. flex = '1 1 100%'
  324. minWidth = '0'
  325. boxSizing = 'border-box'
  326. background = 'transparent'
  327. outline = 'none'
  328. }
  329. 'files-upload pick-area form > textarea:focus, files-upload pick-area form > md-editor:focus-within' { outline = 'none' }
  330. 'files-upload pick-area label.choose, files-upload pick-area label.camera' {
  331. order = '4'
  332. margin = '.25rem'
  333. padding = '0 .75rem'
  334. color = 'var(--color-text-muted, #555)'
  335. background = 'transparent'
  336. }
  337. 'files-upload pick-area label.choose:hover, files-upload pick-area label.camera:hover' {
  338. color = 'var(--color-text, #000)'
  339. background = 'var(--color-surface, #ddd)'
  340. filter = 'none'
  341. }
  342. 'files-upload pick-area form > :not(textarea):not(md-editor)' {
  343. order = '5'
  344. margin = '.25rem .5rem .25rem auto'
  345. }
  346. // the upload rows are built by the component's code, so the View has no progress element to name: no form for it
  347. 'files-upload ul.files progress' {
  348. gridColumn = '1 / -1'
  349. width = '100%'
  350. margin = '.3rem 0'
  351. accentColor = 'var(--color-accent, #000)'
  352. }
  353. filesUpload {
  354. display = 'block'
  355. color = 'var(--color-text, #000)'
  356. dropZone {
  357. display = 'block'
  358. padding = '1rem'
  359. textAlign = 'center'
  360. border = '2px dashed var(--color-border, #999)'
  361. borderRadius = '.5rem'
  362. p {
  363. margin = '0 0 .5rem'
  364. color = 'var(--color-text-muted, #555)'
  365. }
  366. focusVisible {
  367. outline = '2px solid var(--color-accent, #555)'
  368. outlineOffset = '2px'
  369. }
  370. }
  371. 'label.camera' {
  372. display = 'none'
  373. marginLeft = '.4rem'
  374. @media {
  375. pointer = 'coarse'
  376. display = 'inline-flex'
  377. }
  378. }
  379. 'ul.files' {
  380. listStyle = 'none'
  381. margin = '.8rem 0 0'
  382. padding = '0'
  383. li {
  384. display = 'grid'
  385. gridTemplateColumns = '1fr auto'
  386. columnGap = '.6rem'
  387. marginBottom = '.7rem'
  388. }
  389. '.name' { overflowWrap = 'anywhere' }
  390. '.size' {
  391. color = 'var(--color-text-muted, #555)'
  392. textAlign = 'right'
  393. }
  394. '.state' {
  395. gridColumn = '1 / -1'
  396. fontSize = '.9rem'
  397. color = 'var(--color-text-muted, #555)'
  398. }
  399. // the row's class comes from a value (class = state): no form for li.done / li.failed / li.refused
  400. 'li.done .state' { color = 'var(--color-success, #000)' }
  401. 'li.failed .state' {
  402. color = 'var(--color-danger, #000)'
  403. fontWeight = '600'
  404. }
  405. 'img.thumb' { objectFit = 'cover' }
  406. '.kind' {
  407. display = 'grid'
  408. placeItems = 'center'
  409. fontSize = '.7rem'
  410. fontWeight = '700'
  411. color = 'var(--color-text-muted, #555)'
  412. background = 'var(--color-surface, #ddd)'
  413. }
  414. fileInfo {
  415. display = 'flex'
  416. flexDirection = 'column'
  417. minWidth = '0'
  418. '.size' {
  419. textAlign = 'left'
  420. fontSize = '.85rem'
  421. }
  422. }
  423. 'li.refused .state' {
  424. color = 'var(--color-danger, #000)'
  425. fontWeight = '600'
  426. }
  427. }
  428. 'output.summary' {
  429. display = 'block'
  430. marginTop = '.3rem'
  431. fontWeight = '600'
  432. }
  433. pickArea {
  434. display = 'flex'
  435. flexWrap = 'wrap'
  436. alignItems = 'center'
  437. borderRadius = '.5rem'
  438. background = 'var(--color-background-sunken, #f2f2f2)'
  439. 'ul.files' {
  440. order = '2'
  441. flex = '1 1 100%'
  442. margin = '0'
  443. padding = '.4rem .75rem 0'
  444. empty { display = 'none' }
  445. }
  446. 'output.summary' {
  447. order = '3'
  448. flex = '1 1 100%'
  449. margin = '0'
  450. padding = '0 .75rem'
  451. fontSize = '.85rem'
  452. fontWeight = '400'
  453. color = 'var(--color-text-muted, #555)'
  454. }
  455. 'label.camera' { marginLeft = '0' }
  456. 'output.summary:empty' { display = 'none' }
  457. }
  458. 'button.remove' {
  459. display = 'inline-flex'
  460. alignItems = 'center'
  461. justifyContent = 'center'
  462. width = '2.75rem'
  463. height = '2.75rem'
  464. padding = '0'
  465. font = 'inherit'
  466. fontSize = '1.1rem'
  467. color = 'var(--color-text-muted, #555)'
  468. background = 'transparent'
  469. border = '0'
  470. borderRadius = '.35rem'
  471. cursor = 'pointer'
  472. hover {
  473. color = 'var(--color-text, #000)'
  474. background = 'var(--color-surface, #f2f2f2)'
  475. }
  476. focusVisible {
  477. outline = '2px solid var(--color-accent, #555)'
  478. outlineOffset = '1px'
  479. }
  480. }
  481. }
  482. // the class is switched on the component's own element (drag over): no form for files-upload.over
  483. 'files-upload.over drop-zone' {
  484. borderColor = 'var(--color-accent, #000)'
  485. background = 'var(--color-surface, #f2f2f2)'
  486. }
  487. 'files-upload.over pick-area' {
  488. background = 'var(--color-surface, #ddd)'
  489. outline = '2px dashed var(--color-accent, #555)'
  490. outlineOffset = '-2px'
  491. }
  492. }

Branches

Latest commits

  • a06fc778Reset virtual-list window when the host changes resetKeyAtlas
  • 3aab598cRe-vendor complete Hybriel day73 set for virtual-list resetAtlas
  • 337ca075MD1 gate check measures frames in the page, not milliseconds from outside, and takes the middle load (components#30)mre
  • f5f131c5Record AP29 autoplay comparisons and passing gatesmre
  • 51c38187Gate media autoplay on first load and after client mountmre
  • a3f2223dRecord successful B44 deployment and live proofmre
  • 06c1e021Record passing B44 browser rerunmre
  • c9861533Record B44 component proof and browser gate blockermre
  • 1b7413beIgnore detached media events and space demo option outputsmre
  • ca3b4959Re-vendor complete Hybriel plugins and runtime to day44mre
  • df9e7df9Last-item checks run in the browser gate (components#26 fixed on day41); media-player loop is a plain Booleanmre
  • 895a5afcRe-vendor complete Hybriel plugins and runtime to day41mre
  • 16006bb3Backup tar: --hard-dereference, so mpackdb sidecars are never stored as hard links (Tracker's finding)mre
  • 88397fc3Re-vendor complete Hybriel plugins and runtime to day39mre
  • 9b872cedRecord IC28 deployment and live icon verificationmre
  • 46fefaa7Count the icons entry in the offline home-page checkmre
  • c7e14cabRecord IC28 rerun and unchanged-main baseline comparisonmre
  • 5e090ffdRecord IC28 review evidence and gate resultsmre
  • adfe2c4dAdd shuffle, repeat and repeatOne shared iconsmre
  • c96709c8media-player: skip = false and repeat = 'one' options (jukebox#19)mre