components
All repositories: gitoria
18.0 KB
// files-upload.hl — THE FILE UPLOAD FOR SEVERAL FILES (ticket #5), pure Hybriel, in two modes (`mode`).// UPLOAD AT ONCE (the default, `mode = "upload"`): a drop zone with a "Choose files" button (more than one at a time). Every dropped,// chosen or pasted file gets its own row — name, size, a progress bar, its state — and goes to the app through// `emit server uploadFile(file)` (the framework carries it over HTTP, in parts, with progress: hybriel#94), one file after the other in// 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// (an error or null = that file failed; the next one still goes). Files dropped during an upload wait in their row ("Waiting") and follow.// 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// `on client uploadProgress` paints the bar of the row that is sent.// PICK FIRST, SEND LATER (`mode = "pick"`, ticket #18 for HyperConductor's chat attachments): dropped, chosen, pasted (Ctrl+V) or// photographed files are COLLECTED, nothing is sent. THE HOST OWNS THE LIST (the Comments block's pattern, hybriel#87): it binds// `files = attached` and hands `onChange = (list) => { attached = list }`; the component calls onChange with the new list of browser// 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,// each with ✕. At send time the host passes its list to its own face (`emit server chatSend(text, attached)`) and clears it// (`attached = []`). The host's input area goes INSIDE the component (`FilesUpload { mode = "pick" … form { textarea { } button { } } }`, the// 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// 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// anywhere in the box is caught. `maxSize` (bytes, 0 = none, both modes): a bigger file gets a row with a message and is// not collected / not sent. On a touch screen a Camera button (`accept="image/*" capture`) opens the camera; "Choose files" offers the// phone's gallery and files. USE ONE PER PAGE (a composed component's members are the page's state).import Icon from '../icons/icons.hl'String mode = 'upload'Hybrid files = []onChange = nullNumber maxSize = 0String accept = ''slot = nullString dropText = 'Drop files here or'String chooseText = 'Choose files'String pickText = 'Choose files — or drop or paste them anywhere in the box'String cameraText = 'Camera'String removeText = 'Remove'String tooBigText = 'Too big — at most'Boolean uploads = mode != 'pick'Boolean picks = mode == 'pick'// pick mode: what each collected file shows, the refused files of the last pick, the line under themHybrid refused = []Hybrid rows = rowsOf(files)Hybrid refusedRows = refusedOf(refused, files)String pickedText = pickedTextOf(files)sizeText(n) {if (n < 1024) { return n + ' B' }if (n < 1048576) { return (n / 1024).toFixed(1) + ' KB' }return (n / 1048576).toFixed(1) + ' MB'}// "Too big — at most 2.0 MB" (in the browser: the server has no toFixed)maxText() { return tooBigText + ' ' + sizeText(maxSize) }// "TXT", "PDF": the box a file that is not an image shows in place of a thumbnailkindOf(name) {dot = name.lastIndexOf('.')return dot > 0 && dot < name.length - 1 ? name.slice(dot + 1, dot + 5).toUpperCase() : 'FILE'}// one row per collected file; an image gets an object URL for its thumbnail (given back once the thumbnail has loaded)rowsOf(list) {out = []let i = 0for (f of list) {let image = ('' + f.type).startsWith('image/')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 })i = i + 1}return out}// the refused files of the last pick; once the host empties a list they were picked into (after its Send), they go toorefusedOf(list, current) {out = []for (r of list) { if (current.length > 0 || r.kept == 0) { out.push(r) } }return out}// "2 files · 1.2 MB"pickedTextOf(list) {if (list.length == 0) { return '' }let total = 0for (f of list) { total = total + f.size }return (list.length == 1 ? '1 file' : list.length + ' files') + ' · ' + sizeText(total)}View {filesUpload { on paste(e) { emit filesPaste(e) }if (uploads) {dropZone { tabindex = "0" on dragover(e) { emit filesOver(e) } on dragleave(e) { emit filesLeave(e) } on drop(e) { emit filesDrop(e) }p { dropText }label { class = "choose" chooseText input { type = "file" multiple = "multiple" on change(e) { emit filesChosen(e) } } }}ul { class = "files" 'aria-live' = "polite" }output { class = "summary" }}if (picks) {pickArea { on dragover(e) { emit pickOver(e) } on dragleave(e) { emit filesLeave(e) } on drop(e) { emit pickDrop(e) }slotul { class = "files picked" 'aria-live' = "polite"for (r of rows) {li { class = "picked" 'data-i' = r.iif (r.image) { img { class = "thumb" src = r.url alt = "" on load(e) { emit pickShown(e) } } }if (r.other) { span { class = "kind" 'aria-hidden' = "true" r.kind } }fileInfo { strong { class = "name" r.name } span { class = "size" r.size } }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) } }}}for (r of refusedRows) {li { class = "refused" 'data-i' = r.ispan { class = "kind" 'aria-hidden' = "true" "!" }fileInfo { strong { class = "name" r.name } span { class = "size" r.size } output { class = "state" r.message } }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) } }}}}output { class = "summary" pickedText }label { class = "choose" title = pickText chooseText input { type = "file" multiple = "multiple" accept = accept on change(e) { emit pickChosen(e) } } }label { class = "camera" cameraText input { type = "file" accept = "image/*" capture = "environment" on change(e) { emit pickChosen(e) } } }}}}}rootOf(node) { return node.closest('files-upload') }on filesOver(e) {e.preventDefault()rootOf(e.target).classList.add('over')}on filesLeave(e) { rootOf(e.target).classList.remove('over') }on filesDrop(e) {e.preventDefault()root = rootOf(e.target)root.classList.remove('over')add(root, e.dataTransfer.files)}on filesChosen(e) {root = rootOf(e.target)input = e.targetadd(root, input.files)input.value = ''}// 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// image or file. A paste without files (text) is left alone.on filesPaste(e) {pasted = e.clipboardData != null ? e.clipboardData.files : nullif (pasted == null || pasted.length == 0) { return null }e.preventDefault()if (picks) { take(pasted) } else { add(rootOf(e.target), pasted) }}// ---- pick mode: a drag carrying files is taken anywhere in the area (text dragged into the host's field is left alone)carriesFiles(e) {let found = falseif (e.dataTransfer != null) { for (t of e.dataTransfer.types) { if (t == 'Files') { found = true } } }return found}on pickOver(e) {if (!carriesFiles(e)) { return null }e.preventDefault()rootOf(e.target).classList.add('over')}on pickDrop(e) {if (!carriesFiles(e)) { return null }e.preventDefault()rootOf(e.target).classList.remove('over')take(e.dataTransfer.files)}on pickChosen(e) {input = e.targettake(input.files)input.value = ''}// the host's list and the new files (a file over maxSize is refused in its own row), told to the hosttake(list) {next = []for (f of files) { next.push(f) }tooBig = []for (f of list) {if (maxSize > 0 && f.size > maxSize) {tooBig.push(f)} else {next.push(f)}}out = []let i = 0for (f of tooBig) {out.push({ i = '' + i name = f.name size = sizeText(f.size) message = maxText() remove = removeText + ' ' + f.name kept = next.length })i = i + 1}refused = outif (onChange != null) { onChange(next) }}on pickRemove(e) {gone = toNumber(e.target.getAttribute('data-i'))next = []let i = 0for (f of files) {if (i != gone) { next.push(f) }i = i + 1}if (onChange != null) { onChange(next) }}on pickDismiss(e) {gone = e.target.getAttribute('data-i')out = []for (r of refused) { if (r.i != gone) { out.push(r) } }refused = out}// the thumbnail is drawn: its object URL is given back (the picture stays)on pickShown(e) { window.URL.revokeObjectURL(e.target.src) }// the bar of the row being sent (one at a time)on client uploadProgress(p) {for (row of document.querySelectorAll('files-upload li.busy')) {let bar = row.querySelector('progress')bar.max = p.size > 0 ? p.size : 1bar.value = p.sent}}// one row per file, waiting; then the queue runs (unless it already does)add(root, picked) {list = root.querySelector('ul.files')for (f of picked) {let li = document.createElement('li')li.className = 'waiting'let name = document.createElement('strong')name.className = 'name'name.textContent = f.namelet size = document.createElement('span')size.className = 'size'size.textContent = sizeText(f.size)let bar = document.createElement('progress')bar.max = 100bar.value = 0let state = document.createElement('output')state.className = 'state'state.textContent = 'Waiting'li.appendChild(name)li.appendChild(size)li.appendChild(bar)li.appendChild(state)li.upload = fif (maxSize > 0 && f.size > maxSize) {li.className = 'failed'li.upload = nullstate.textContent = maxText()}list.appendChild(li)}count(root)run(root)}// sends the waiting rows one after the otherrun(root) {if (root.classList.contains('busy')) { return null }root.classList.add('busy')let row = root.querySelector('li.waiting')while (row != null) {row.classList.remove('waiting')row.classList.add('busy')row.querySelector('.state').textContent = 'Uploading…'let saved = emit server uploadFile(row.upload)row.classList.remove('busy')row.upload = nulllet bar = row.querySelector('progress')if (saved == null) {row.classList.add('failed')row.querySelector('.state').textContent = 'Failed'} else {bar.max = 100bar.value = 100row.classList.add('done')row.querySelector('.state').textContent = 'Uploaded'}count(root)row = root.querySelector('li.waiting')}root.classList.remove('busy')}// "2 of 3 uploaded" (and how many failed)count(root) {all = root.querySelectorAll('ul.files li').lengthdone = root.querySelectorAll('ul.files li.done').lengthfailed = root.querySelectorAll('ul.files li.failed').lengthlet text = all == 0 ? '' : done + ' of ' + all + ' uploaded'if (failed > 0) { text = text + ', ' + failed + ' failed' }root.querySelector('output.summary').textContent = text}// ---- look: the component's own Style, the same base look as the one-file upload (black, white, the greys #555 #999 #ddd #f2f2f2;// 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// dashed one (it means "drop here"); rows are separated by space only. MOBILE FIRST: every button is at least 44 × 44 px (2.75rem);// the Camera button shows only on a touch screen (`pointer: coarse`)Style {// the choose and camera labels share one look: a selector list has no form in the language'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)'}'files-upload label.choose:hover, files-upload label.camera:hover' { filter = 'brightness(.93)' }'files-upload label.choose input, files-upload label.camera input' { display = 'none' }// a list again: the picked and the refused row share the grid, the thumbnail and the kind badge share the box'files-upload ul.files li.picked, files-upload ul.files li.refused' {gridTemplateColumns = '3rem minmax(0, 1fr) 2.75rem'alignItems = 'center'marginBottom = '.4rem'}'files-upload ul.files img.thumb, files-upload ul.files .kind' {display = 'block'width = '3rem'height = '3rem'borderRadius = '.35rem'background = 'var(--color-surface, #f2f2f2)'}// 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// 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)// 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// 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.// The next rules reach the host's form in the slot (a child combinator, :has, :not, a list): no form in the language'files-upload pick-area:has(> form > textarea:focus, > form > md-editor:focus-within)' {outline = '2px solid var(--color-accent, #555)'outlineOffset = '-1px'}'files-upload pick-area > form' { display = 'contents' }'files-upload pick-area form > textarea, files-upload pick-area form > md-editor' {order = '1'flex = '1 1 100%'minWidth = '0'boxSizing = 'border-box'background = 'transparent'outline = 'none'}'files-upload pick-area form > textarea:focus, files-upload pick-area form > md-editor:focus-within' { outline = 'none' }'files-upload pick-area label.choose, files-upload pick-area label.camera' {order = '4'margin = '.25rem'padding = '0 .75rem'color = 'var(--color-text-muted, #555)'background = 'transparent'}'files-upload pick-area label.choose:hover, files-upload pick-area label.camera:hover' {color = 'var(--color-text, #000)'background = 'var(--color-surface, #ddd)'filter = 'none'}'files-upload pick-area form > :not(textarea):not(md-editor)' {order = '5'margin = '.25rem .5rem .25rem auto'}// the upload rows are built by the component's code, so the View has no progress element to name: no form for it'files-upload ul.files progress' {gridColumn = '1 / -1'width = '100%'margin = '.3rem 0'accentColor = 'var(--color-accent, #000)'}filesUpload {display = 'block'color = 'var(--color-text, #000)'dropZone {display = 'block'padding = '1rem'textAlign = 'center'border = '2px dashed var(--color-border, #999)'borderRadius = '.5rem'p {margin = '0 0 .5rem'color = 'var(--color-text-muted, #555)'}focusVisible {outline = '2px solid var(--color-accent, #555)'outlineOffset = '2px'}}'label.camera' {display = 'none'marginLeft = '.4rem'@media {pointer = 'coarse'display = 'inline-flex'}}'ul.files' {listStyle = 'none'margin = '.8rem 0 0'padding = '0'li {display = 'grid'gridTemplateColumns = '1fr auto'columnGap = '.6rem'marginBottom = '.7rem'}'.name' { overflowWrap = 'anywhere' }'.size' {color = 'var(--color-text-muted, #555)'textAlign = 'right'}'.state' {gridColumn = '1 / -1'fontSize = '.9rem'color = 'var(--color-text-muted, #555)'}// the row's class comes from a value (class = state): no form for li.done / li.failed / li.refused'li.done .state' { color = 'var(--color-success, #000)' }'li.failed .state' {color = 'var(--color-danger, #000)'fontWeight = '600'}'img.thumb' { objectFit = 'cover' }'.kind' {display = 'grid'placeItems = 'center'fontSize = '.7rem'fontWeight = '700'color = 'var(--color-text-muted, #555)'background = 'var(--color-surface, #ddd)'}fileInfo {display = 'flex'flexDirection = 'column'minWidth = '0''.size' {textAlign = 'left'fontSize = '.85rem'}}'li.refused .state' {color = 'var(--color-danger, #000)'fontWeight = '600'}}'output.summary' {display = 'block'marginTop = '.3rem'fontWeight = '600'}pickArea {display = 'flex'flexWrap = 'wrap'alignItems = 'center'borderRadius = '.5rem'background = 'var(--color-background-sunken, #f2f2f2)''ul.files' {order = '2'flex = '1 1 100%'margin = '0'padding = '.4rem .75rem 0'empty { display = 'none' }}'output.summary' {order = '3'flex = '1 1 100%'margin = '0'padding = '0 .75rem'fontSize = '.85rem'fontWeight = '400'color = 'var(--color-text-muted, #555)'}'label.camera' { marginLeft = '0' }'output.summary:empty' { display = 'none' }}'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'hover {color = 'var(--color-text, #000)'background = 'var(--color-surface, #f2f2f2)'}focusVisible {outline = '2px solid var(--color-accent, #555)'outlineOffset = '1px'}}}// the class is switched on the component's own element (drag over): no form for files-upload.over'files-upload.over drop-zone' {borderColor = 'var(--color-accent, #000)'background = 'var(--color-surface, #f2f2f2)'}'files-upload.over pick-area' {background = 'var(--color-surface, #ddd)'outline = '2px dashed var(--color-accent, #555)'outlineOffset = '-2px'}}
Branches
Latest commits
- 7e428ba6Re-vendor Hybriel day72 (day73 withdrawn: minifier breaks write sets)Atlas
- 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