components
All repositories: gitoria
15.2 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" … textarea { } button { } }`, the// 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// 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).String mode = 'upload'Hybrid files = []onChange = nullNumber maxSize = 0String accept = ''slot = nullString dropText = 'Drop files here or'String chooseText = 'Choose files'String pickText = 'Drop or paste files here, or'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) }ul { 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 "✕" 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 "✕" on click(e) { emit pickDismiss(e) } }}}}output { class = "summary" pickedText }slotdropZone { tabindex = "0"p { pickText }label { class = "choose" 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 {'files-upload' { display = 'block' color = 'var(--color-text, #000)' }'files-upload drop-zone' { display = 'block' padding = '1rem' textAlign = 'center' border = '2px dashed var(--color-border, #999)' borderRadius = '.5rem' }'files-upload.over drop-zone' { borderColor = 'var(--color-accent, #000)' background = 'var(--color-surface, #f2f2f2)' }'files-upload drop-zone p' { margin = '0 0 .5rem' color = 'var(--color-text-muted, #555)' }'files-upload drop-zone:focus-visible' { outline = '2px solid var(--color-accent, #555)' outlineOffset = '2px' }'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' }'files-upload label.camera' { display = 'none' marginLeft = '.4rem' }'@media (pointer: coarse)' {'files-upload label.camera' { display = 'inline-flex' }}'files-upload ul.files' { listStyle = 'none' margin = '.8rem 0 0' padding = '0' }'files-upload ul.files li' { display = 'grid' gridTemplateColumns = '1fr auto' columnGap = '.6rem' marginBottom = '.7rem' }'files-upload ul.files .name' { overflowWrap = 'anywhere' }'files-upload ul.files .size' { color = 'var(--color-text-muted, #555)' textAlign = 'right' }'files-upload ul.files progress' { gridColumn = '1 / -1' width = '100%' margin = '.3rem 0' accentColor = 'var(--color-accent, #000)' }'files-upload ul.files .state' { gridColumn = '1 / -1' fontSize = '.9rem' color = 'var(--color-text-muted, #555)' }'files-upload ul.files li.done .state' { color = 'var(--color-success, #000)' }'files-upload ul.files li.failed .state' { color = 'var(--color-danger, #000)' fontWeight = '600' }'files-upload output.summary' { display = 'block' marginTop = '.3rem' fontWeight = '600' }'files-upload pick-area' { display = 'block' borderRadius = '.5rem' }'files-upload.over pick-area' { background = 'var(--color-surface, #f2f2f2)' }'files-upload pick-area drop-zone' { padding = '.5rem' marginTop = '.5rem' borderWidth = '1px' }'files-upload pick-area drop-zone p' { display = 'inline' margin = '0 .5rem 0 0' fontSize = '.9rem' }'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)' }'files-upload ul.files img.thumb' { objectFit = 'cover' }'files-upload ul.files .kind' { display = 'grid' placeItems = 'center' fontSize = '.7rem' fontWeight = '700' color = 'var(--color-text-muted, #555)' background = 'var(--color-surface, #ddd)' }'files-upload ul.files file-info' { display = 'flex' flexDirection = 'column' minWidth = '0' }'files-upload ul.files file-info .size' { textAlign = 'left' fontSize = '.85rem' }'files-upload ul.files li.refused .state' { color = 'var(--color-danger, #000)' fontWeight = '600' }'files-upload button.remove' { 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' }'files-upload button.remove:hover' { color = 'var(--color-text, #000)' background = 'var(--color-surface, #f2f2f2)' }'files-upload button.remove:focus-visible' { outline = '2px solid var(--color-accent, #555)' outlineOffset = '1px' }'files-upload pick-area output.summary:empty' { display = 'none' }}
Branches
Latest commits
- 15be2ac0comments: attach (FilesUpload pick), onPost(text, files), files per comment; md-editor files=off; README, gate fixmre
- 1f8d79d6comments (mission 009 WIP, tickets#27): attachments — attach="true" puts the form in FilesUpload pick mode, onPost(text, files), files per comment (thumbnails open full size, file rows, no url = no link); MdEditor files="off"; demo with attachments; gate m009 checks (WIP: 356/1)mre
- 13389d67mission 008: report, LOG, STATUSmre
- ab47e4a2files-upload (mission 008, #18 for hycon#46): pick-first-send-later mode — host-owned list (files + onChange), slot for the host's input area, Ctrl+V paste (both modes), thumbnails, name+size rows, ✕ remove, maxSize per file, Camera on touch screens, 44 px targets; demo /files-upload/pick; gate 343/0 (19 new, 18 fail on the old code)mre
- 172e66bemission brief 008mre
- 9db927dfpaths: projects moved into /media/STORAGE/projects/WorldAPI/mre
- 00a95329components (mission 007): re-vendor Hybriel to the shared static build 0f396652 (plugins; bin/ git-ignored), gate 324/0 old and new, real-data copy unchanged, report 007, same-output.mjs ports overridablemre
- fe50766bmission brief 007mre
- 2dce51b8components (mission 006) 4/4: docs (README comments + gate 324, STATUS, LOG), report reports/006-comments.mdmre
- 9ec7273ecomponents layouts#2 (mission 006) 3/4: comments — each text is MdEditor readonly directly in its row (hl:web builds a composed child anew when a bound value changes, so the keyed one-row list was not needed); demo 'bob edits his comment elsewhere (a push)' + gate check that a text the host changes from outside is drawn anew; theme radii (var(--radius-small/large) with 5px/10px fallbacks: layouts' check-theme 0 problems with comments.hl copied in); README 'How it is built'; gate 324/0 (34 comments checks)mre
- fd57c18bcomponents layouts#2 (mission 006) 2/3: comments block (components/comments, demo /comments) — pure Hybriel: '1 comment' / 'n comments', avatar initial, bubble with name, time, 'edited' (<details> with the earlier versions), the text drawn by MdEditor readonly (one-row list keyed by id + text, so an edit redraws), like ♥ n (aria-pressed), Edit / Delete (asks first) only where author == user, the form below in MdEditor (Comment button or Ctrl+Enter; a fresh editor after a post). Host passes comments + user and hands onPost / onEdit / onDelete / onLike (hybriel#87); demo keeps a mock thread from lib/comments.hl in the page, switches alice / bob / nobody. Home entry, routes, map line, README; base-look gate covers comments; 33 gate checks with real clicks / keysmre
- 850a376bcomponents (mission 006) 1/3: MdEditor readonly = "true" — the Markdown shown formatted and nothing else (no toolbar, link form, file rows, placeholder, source view or switch; article contenteditable false, tabindex -1, role document; no field background or focus ring); the mount only loads the value. For the comments block, which draws every comment with the editor's own renderermre
- ee9f1b57mission brief w6mre
- fe691586components (mission 005) 4/4: docs (README components + gate 290, STATUS, LOG), report reports/005-fixes-16-17-5.mdmre
- f1ebab53components #5 (mission 005) 3/4: file upload for several files (components/files-upload, demo /files-upload) — pure Hybriel, each file through emit server uploadFile (hybriel#94), drop or choose many, a row per file with live progress, one after the other in order, a drop during an upload waits, a file over uploadMax fails in its row and the next goes, 'n of m uploaded'; home entry, routes, map line, README; pagination keyboard focus ring #555 (Chrome painted its own orange one); docker-compose init: true (hybriel#156); gate 290/0 (17 #5 checks with real file drops, 1 #16 focus check)mre
- 3b73ae5bcomponents #16 (mission 005) 2/4: Pagination tells the host the target page — the host hands onPage (hybriel#87: a function, not emit), called on every click that changes the page (pointer, Enter/Space, script click; Previous/Next/numbers/first/last; not … or the current page) with the target read from the clicked button's data-page before the rebuild; demo shows what the page was told; README Data out; 15 gate checks (14 fail on the old pagination)mre
- 6b27c0f0components #17 (mission 005) 1/4: md-editor keeps backslash + r — parseInto dropped the two characters '\r' (C:\root -> C:oot) instead of a real carriage return; CR now decoded from JSON (String.fromCharCode does not parse in a member), real CRs also go from pasted plain text; 5 gate checks (3 fail on the old md-editor.hl)mre
- 1a5dc327mission brief w5mre
- a567b579components (mission 004) 3/3: docs (README Hybriel a832f73b + tables, STATUS, LOG), report reports/004-md-editor-tables.mdmre
- 70fe7ab5components (mission 004) 2/3: GitHub pipe tables in the Hybriel MdEditor (creator notes#5; behaviour of notes md-editor.js 9acc27f, no JavaScript) — parse/render (header, :--/:-:/--: alignment, \| in cells and code, inline Markdown in cells, outer pipes optional), padded writer, unchanged table byte for byte, typing | a | b | Enter |---|---| Enter makes a table, Tab/Shift+Tab, Enter new row, Enter in empty last row leaves, Backspace/Delete never merge, selection across a table empties cells, pasted HTML table -> table, thin #999 lines, wide table scrolls in its box; demo third editor 'plan'; gate 252/0 (22 new table checks, 20 fail on the old md-editor.hl)mre