gitoria

components

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit8d79ff798d79ff79comments 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 checksmre8d79ff79/components/files-upload/README.md

7.4 KB

  1. # File upload, several files
  2. A dashed drop zone with a "Choose files" button that takes several files at once. Every dropped or chosen file gets its own
  3. row — name, size, a progress bar and its state (Waiting, Uploading…, Uploaded, Failed) — and the line under the rows counts
  4. them: "3 of 3 uploaded". Files go one after the other in the order they came; files dropped while others are still uploading
  5. wait in their row and follow. A failed file does not stop the others. Live demo: https://components.hybriel.worldapi.org/files-upload
  6. **Two modes** (`mode`): **upload at once** (the default, `mode = "upload"`, all of the above) and **pick first, send later**
  7. (`mode = "pick"`, ticket #18, for chat attachments — HyperConductor hycon#46): dropped, chosen, pasted (Ctrl+V) or photographed
  8. files are only collected; the host sends them with its own message and empties the list. Live demo:
  9. https://components.hybriel.worldapi.org/files-upload/pick
  10. ## Files
  11. Copy the folder `components/files-upload/`: `files-upload.hl` is the component (both modes; it carries its own `Style`). `demo.hl`
  12. and `demo-pick.hl` are this site's demo pages, not needed in an app.
  13. ## Use it in an app
  14. Copy the folder `components/files-upload/` (`files-upload.hl`; it carries its own `Style`, colours are semantic tokens with a
  15. black/white/grey fallback, the one border is the drop zone's dashed one), compose it, and write the face that stores a file:
  16. ```
  17. import FilesUpload from './files-upload.hl'
  18. View { FilesUpload { } }
  19. on server uploadFile(file) {
  20. // file.name, file.type, file.size, file.bytes — store it where you want (called once per file)
  21. writeFile('storage/uploads/' + name, file.bytes)
  22. return { ok = true }
  23. }
  24. ```
  25. - The component sends each file with `emit server uploadFile(file)`; the framework carries it over HTTP in 1 MiB parts with
  26. progress (hybriel#94). Pure Hybriel: no `.js`, no network calls of its own.
  27. - The face decides where the file goes and how big it may be (`uploadMax` of the WebFramework; this site: 5 MiB). Answer anything
  28. but `null` when it is stored; `null`, an error or a file over `uploadMax` shows "Failed" in that row.
  29. - Face names are global to the app (`uploadFile`; the one-file upload uses `saveFile`): one face per app for each.
  30. - `dropText`, `chooseText` are the texts. One component per page (composed members are the page's state).
  31. - Progress comes from `on client uploadProgress`, shown by the component itself in the row being sent.
  32. - A **Ctrl+V** of files (a screenshot, a copied image or file) on the focused drop zone uploads them like a drop. A text paste does nothing.
  33. - `maxSize` (bytes, default 0 = none): a bigger file gets its row with "Too big — at most 2.0 MB" and is not sent (the app's
  34. `uploadMax` still holds for the rest).
  35. ## Pick first, send later (`mode = "pick"`)
  36. Files are **collected, not sent**: an image shows as a thumbnail, any other file as a row with its kind ("PDF"), name and size,
  37. each with a **✕** that takes it away. A line under them says "3 files · 1.2 MB". **The host owns the list** — the pattern of the
  38. Comments block (hybriel#87): it binds its list into `files`, hears every change through the function `onChange`, and at its Send
  39. passes the list to its own face and empties it. **Its input area goes inside the component** (the slot): a paste (Ctrl+V) or a
  40. drop there is caught too, while text pastes and text drags stay in the field.
  41. ### Full host example: a chat input (text + attachments + Send)
  42. ```
  43. import FilesUpload from './files-upload/files-upload.hl'
  44. Hybrid attached = [] // the collected files (browser Files), owned by this page
  45. Hybrid messages = []
  46. picked = (list) => { attached = list } // the component tells every change: a pick, a paste, a ✕
  47. View {
  48. chat {
  49. ol { for (m of messages) { li { m.text } } }
  50. FilesUpload { mode = "pick" files = attached onChange = picked maxSize = 10485760 accept = "image/*,.pdf,.txt,.md"
  51. form { on submit(e) { emit chatSubmit(e) }
  52. textarea { name = "text" placeholder = "Write a message…" }
  53. button { type = "submit" "Send" }
  54. }
  55. }
  56. }
  57. }
  58. on chatSubmit(e) {
  59. e.preventDefault()
  60. form = e.target
  61. text = form.querySelector('textarea').value
  62. if (text.trim() == '' && attached.length == 0) { return null }
  63. message = emit server chatSend(text, attached) // the framework uploads every file first (hybriel#94), then the face runs
  64. if (message == null) { return null } // refused (e.g. a file over uploadMax): the files stay, the user can retry
  65. messages.push(message)
  66. attached = [] // clears the component's rows
  67. form.reset()
  68. }
  69. on server chatSend(text, files) {
  70. // files: a list of { name, type, size, bytes } in the order shown; store them where you want
  71. for (f of files) { writeFile('storage/attachments/' + safeName(f.name), f.bytes) }
  72. return { text = text files = files.length }
  73. }
  74. ```
  75. ### Interface
  76. | Member | Meaning |
  77. |---|---|
  78. | `mode` | `"pick"` (default `"upload"`) |
  79. | `files` | **in**: the host's list of browser `File`s, shown in this order (bind the host's member; `[]` = no rows) |
  80. | `onChange(list)` | **out**: called with the NEW list after every drop, choose, paste, photo and ✕; the host stores it (`attached = list`) — without it nothing is collected |
  81. | `maxSize` | bytes, 0 = none: a bigger file gets a row "Too big — at most …" with its own ✕, and is not handed to the host |
  82. | `accept` | the "Choose files" input's `accept` (default all files) |
  83. | texts | `pickText` ("Drop or paste files here, or"), `chooseText` ("Choose files"), `cameraText` ("Camera"), `removeText` ("Remove", the ✕'s label: "Remove photo.png"), `tooBigText` ("Too big — at most") |
  84. - **Send** = the host's own face with the list as an argument: `emit server chatSend(text, attached)`. A list of Files in an emit is
  85. uploaded by the framework in 1 MiB parts before the face runs; the face gets `{ name, type, size, bytes }` for each. **Clear** =
  86. `attached = []`. Nothing is sent by the component itself; it never calls a face in this mode.
  87. - The refused rows belong to the last pick: a new pick replaces them, their ✕ dismisses one, and they go when the host empties a
  88. list that had files (after its Send).
  89. - **Phone:** "Choose files" offers the phone's own choice (gallery, camera, files); on a touch screen (`pointer: coarse`) a
  90. **Camera** button (`accept="image/*" capture="environment"`) opens the camera straight away. Every button is 44 × 44 px or more.
  91. - Thumbnails are object URLs of the Files (pure Hybriel: `window.URL.createObjectURL`), given back (`revokeObjectURL`) once drawn.
  92. ## Markup and states
  93. `<files-upload>` holds `<drop-zone>` (class `over` while files are dragged over; the hidden `<input type="file" multiple>` in
  94. `label.choose`), `<ul class="files">` with one `<li>` per file (`strong.name`, `span.size`, `<progress>`, `<output class="state">`;
  95. class `waiting`, `busy`, `done` or `failed`) and `<output class="summary">`. Class `busy` on `<files-upload>` while it sends.
  96. Pick mode: `<files-upload>` > `<pick-area>` (class `over` on `<files-upload>` while files are dragged over it) with
  97. `<ul class="files picked">` — `li.picked[data-i]` (`img.thumb` or `span.kind`, `<file-info>` with `strong.name` + `span.size`,
  98. `button.remove`) and `li.refused` (`span.kind` "!", `<file-info>` with `strong.name`, `span.size`, `output.state`, `button.remove`) —,
  99. `<output class="summary">`, the host's content, and `<drop-zone>` with `p`, `label.choose` and `label.camera` (each holding its hidden input).

Branches

Latest commits

  • 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
  • d17f8543mission 012: re-vendor Hybriel master f9614007 (plugins), fixes comments push redraw; gate 480/0mre
  • 5f95de05mission 012: re-vendor Hybriel ce6f2e0a (plugins), report: gate 478/2 on comments push redrawmre
  • 3794c591mission 011: report, LOG, STATUS, md-keys-run widths, cdp-blank helpermre
  • 77a93c16mission 011: gate hook for md-keys (480/0), flatten() runs one at a time (removeChild race), surface also built on first focus / pointer-down (background tab), md-bg test, README keysmre
  • 3c906718mission 011: md-editor key tests (real keys) and fixes: nested p/ul flatten, bare br, link change, Ctrl+E, code block exit (WIP)mre
  • 1442e583mission 010: report, LOG, STATUSmre
  • 6f27ad31md-editor: phone toolbar row contained (inline-size) so it never widens a grid parentmre
  • d702d76dmd-editor look: root class md-empty (a host .empty rule hit it), rows = surface height, one phone toolbar row with 44 px targets; comment form rows 3; gate 360/0mre