components
All repositories: gitoria
7.9 KB
# File upload, several filesA dashed drop zone with a "Choose files" button that takes several files at once. Every dropped or chosen file gets its ownrow — name, size, a progress bar and its state (Waiting, Uploading…, Uploaded, Failed) — and the line under the rows countsthem: "3 of 3 uploaded". Files go one after the other in the order they came; files dropped while others are still uploadingwait in their row and follow. A failed file does not stop the others. Live demo: https://components.hybriel.worldapi.org/files-upload**Two modes** (`mode`): **upload at once** (the default, `mode = "upload"`, all of the above) and **pick first, send later**(`mode = "pick"`, ticket #18, for chat attachments — HyperConductor hycon#46): dropped, chosen, pasted (Ctrl+V) or photographedfiles are only collected; the host sends them with its own message and empties the list. Live demo:https://components.hybriel.worldapi.org/files-upload/pick## FilesCopy the folder `components/files-upload/`: `files-upload.hl` is the component (both modes; it carries its own `Style`). `demo.hl`and `demo-pick.hl` are this site's demo pages, not needed in an app.## Use it in an appCopy the folder `components/files-upload/` (`files-upload.hl`; it carries its own `Style`, colours are semantic tokens with ablack/white/grey fallback, the one border is the drop zone's dashed one), compose it, and write the face that stores a file:```import FilesUpload from './files-upload.hl'View { FilesUpload { } }on server uploadFile(file) {// file.name, file.type, file.size, file.bytes — store it where you want (called once per file)writeFile('storage/uploads/' + name, file.bytes)return { ok = true }}```- The component sends each file with `emit server uploadFile(file)`; the framework carries it over HTTP in 1 MiB parts withprogress (hybriel#94). Pure Hybriel: no `.js`, no network calls of its own.- The face decides where the file goes and how big it may be (`uploadMax` of the WebFramework; this site: 5 MiB). Answer anythingbut `null` when it is stored; `null`, an error or a file over `uploadMax` shows "Failed" in that row.- Face names are global to the app (`uploadFile`; the one-file upload uses `saveFile`): one face per app for each.- `dropText`, `chooseText` are the texts. One component per page (composed members are the page's state).- Progress comes from `on client uploadProgress`, shown by the component itself in the row being sent.- 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.- `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`uploadMax` still holds for the rest).## Pick first, send later (`mode = "pick"`)Files are **collected, not sent**: an image shows as a thumbnail, any other file as a row with its kind ("PDF"), name and size,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 theComments block (hybriel#87): it binds its list into `files`, hears every change through the function `onChange`, and at its Sendpasses the list to its own face and empties it. **Its input area goes inside the component** (the slot), and everything is**one box**: the host's field on top, the picked files right under it, and a footer with the small "Choose files" (and, on a touchscreen, "Camera") button at the left and the host's own buttons — its Send — at the right end. A drop or a paste (Ctrl+V)anywhere in the box is caught, while text pastes and text drags stay in the field. The host's `<form>` dissolves into the box(`display: contents`): its first field (a `textarea` or an `md-editor`) is the top row, every other direct child of the form(a button, a button group) goes to the right end of the footer.### Full host example: a chat input (text + attachments + Send)```import FilesUpload from './files-upload/files-upload.hl'Hybrid attached = [] // the collected files (browser Files), owned by this pageHybrid messages = []picked = (list) => { attached = list } // the component tells every change: a pick, a paste, a ✕View {chat {ol { for (m of messages) { li { m.text } } }FilesUpload { mode = "pick" files = attached onChange = picked maxSize = 10485760 accept = "image/*,.pdf,.txt,.md"form { on submit(e) { emit chatSubmit(e) }textarea { name = "text" placeholder = "Write a message…" }button { type = "submit" "Send" }}}}}on chatSubmit(e) {e.preventDefault()form = e.targettext = form.querySelector('textarea').valueif (text.trim() == '' && attached.length == 0) { return null }message = emit server chatSend(text, attached) // the framework uploads every file first (hybriel#94), then the face runsif (message == null) { return null } // refused (e.g. a file over uploadMax): the files stay, the user can retrymessages.push(message)attached = [] // clears the component's rowsform.reset()}on server chatSend(text, files) {// files: a list of { name, type, size, bytes } in the order shown; store them where you wantfor (f of files) { writeFile('storage/attachments/' + safeName(f.name), f.bytes) }return { text = text files = files.length }}```### Interface| Member | Meaning ||---|---|| `mode` | `"pick"` (default `"upload"`) || `files` | **in**: the host's list of browser `File`s, shown in this order (bind the host's member; `[]` = no rows) || `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 || `maxSize` | bytes, 0 = none: a bigger file gets a row "Too big — at most …" with its own ✕, and is not handed to the host || `accept` | the "Choose files" input's `accept` (default all files) || texts | `pickText` (the tooltip of "Choose files"), `chooseText` ("Choose files"), `cameraText` ("Camera"), `removeText` ("Remove", the ✕'s label: "Remove photo.png"), `tooBigText` ("Too big — at most") |- **Send** = the host's own face with the list as an argument: `emit server chatSend(text, attached)`. A list of Files in an emit isuploaded by the framework in 1 MiB parts before the face runs; the face gets `{ name, type, size, bytes }` for each. **Clear** =`attached = []`. Nothing is sent by the component itself; it never calls a face in this mode.- The refused rows belong to the last pick: a new pick replaces them, their ✕ dismisses one, and they go when the host empties alist that had files (after its Send).- **Phone:** "Choose files" offers the phone's own choice (gallery, camera, files); on a touch screen (`pointer: coarse`) a**Camera** button (`accept="image/*" capture="environment"`) opens the camera straight away. Every button is 44 × 44 px or more.- Thumbnails are object URLs of the Files (pure Hybriel: `window.URL.createObjectURL`), given back (`revokeObjectURL`) once drawn.## Markup and states`<files-upload>` holds `<drop-zone>` (class `over` while files are dragged over; the hidden `<input type="file" multiple>` in`label.choose`), `<ul class="files">` with one `<li>` per file (`strong.name`, `span.size`, `<progress>`, `<output class="state">`;class `waiting`, `busy`, `done` or `failed`) and `<output class="summary">`. Class `busy` on `<files-upload>` while it sends.Pick mode: `<files-upload>` > `<pick-area>` (class `over` on `<files-upload>` while files are dragged over it) with`<ul class="files picked">` — `li.picked[data-i]` (`img.thumb` or `span.kind`, `<file-info>` with `strong.name` + `span.size`,`button.remove`) and `li.refused` (`span.kind` "!", `<file-info>` with `strong.name`, `span.size`, `output.state`, `button.remove`) —,`<output class="summary">`, the host's content (first, in the DOM), and the footer's `label.choose` and `label.camera` (each holding its hidden input; there is no drop zone in pick mode — the whole box takes the drop).
Branches
- b27b
- mainmain branch
- vendor-day72
- vl27-baseline
- vl27-resetkey
Latest commits
- 3d43c187ident-access: server-side check of an ident access key (Bearer or Basic password), asks ident on every call, README, gate tests/ident-access.mjs (ident#27 app side)mre
- 59704ce1Merge branch 'n5b-pipes'mre
- 23b9164aMdEditor: a clipboard whose text is only the names or file:// paths of its files (a file manager copy) is about the files, as before; any other text wins (tickets#49); gate casesmre
- b0f1ec28MdEditor: a clipboard with text always pastes the text, also when a picture is on it too (a spreadsheet or office app), files on or off; a picture alone still goes to the files; real Ctrl+V gate tests/md-paste-files.mjs (tickets#49)mre
- ad4e929bMdEditor: pipe rows without a delimiter row are a table (first row = header, short rows padded, written back without an invented | --- | line) (notes#5)mre
- 4bd11128MdEditor: the Table button's empty table is written as a real pipe table (empty cells), so it survives the source switch, a save and a reload (notes#7)mre
- 7437e32bMdEditor draws its surface at once when claimed from server HTML: 1 ms mount animation, the first editor mounts all editors of the pagemre
- 5de0264cRe-vendor onto Hybriel master acaceaf3 (set-day27b): binary, lib and plugins; exit 1 on a startup fault; .gitignore binmre
- 79c77e7dMerge sb3-standby: components deploys through its standbymre
- c5442043components: no jobs switch needed in the standbymre
- 8d2fb24ccomponents: a standby container (port 45016, profile standby); deploy hands over through standby-swap.sh without a gapmre
- c2834dd3Merge mr-fixes: media player state after a queue pick, Range handling, cover-less audio, accessibility labelsmre
- 354fe3c6Media player: audio without a cover starts and shows its time (the mount tick sits on the seek bar, which every item has); the speed button names the current speed, the seek slider says its time in words; a gate page for the first render on cover-less audio and a fourth demo queue item without a covermre
- ee4aee1eMedia player: a new queue item shows its own speed, mute, play state and time and starts when asked, also for two videos in a row; the media route reads suffix ranges and ignores range lists and garbage instead of answering 500; stronger gate checks (the 10 percent drag, the played bar, the queue tap)mre
- 9a4f9b31Merge mp-player: media served from the committed filesmre
- ab96e686Media route: serve the demo's media from the committed files in components/media-player (storage/ is not in git, the deploy gate found no files)mre
- b2980734Merge mp-player: the media player (components/media-player), its demo and the media iconsmre
- 1c41293eMediaPlayer: back / forward 10 s as replay / forward 10 icons (circular arrow with a 10), the play button keeps its surface background on audio and video, the media route's range end is a letmre
- 9e1ab228MP: report, LOG line, STATUS entrymre
- dd003e5dMediaPlayer: audio and video in one phone-first player (components/media-player), its demo /media-player with a queue, the media icons in components/icons, the demo's media files served with ranges (lib/media.hl), 35 gate checksmre