Files
dtf-system/web/kanban-viewer.js
Cauê Faleiros 8b7d9f0bdb
All checks were successful
Build and deploy / Validate source (push) Successful in 9s
Build and deploy / Integration suite on a real stack (push) Successful in 4m28s
Build and deploy / Secret scan and release gate (push) Successful in 7s
Build and deploy / Publish images (push) Successful in 1m17s
feat: "Abrir e conferir", the print file full screen, in pieces (item 7)
The card's "Abrir e conferir" opens the order's print file full screen
(Dropstar's third round, item 7): zoom up to 100% real (one file pixel, one
screen pixel), the position in cm, the order's files one after the other
without closing, and the background behind the transparency light, dark or
checkered, remembered (Dropstar, 2026-10-30). Closed in Arte recebida, it asks
"Está OK?": the class of each item, prefilled with the measure, then
"Mover para Arte conferida" (the final approved, the class kept, the card
moved) or "Tratar a arte".

A 15 m film is loaded like a map: while the PNG is written, its strips of
512 rows go to storage at 100% and at each halving (app/printtiles.py,
originals/tiles/<upload>/), and the viewer (web/kanban-viewer.js) fetches
only those on screen. They go when their file expires. Storing a print file
that fails is now an attempt like a failed render, not a claim held for an
hour.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 20:10:03 -03:00

144 lines
8.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* Kanban DTF — "Abrir e conferir" (Dropstar's third round, item 7).
The print file in the browser, full screen: zoom up to 100% real (one file
pixel, one screen pixel), the position in cm, the order's files one after
the other without closing, and a background behind the transparency (light,
dark or checkered, remembered: white art vanishes on light, black on dark).
A 57 cm x 15 m film is loaded in pieces, like a map: strips of the film's
width at the zoom shown (app/printtiles.py), only those on screen.
openViewer(files, {title, onClose}): files are [{name, label, manifest,
picture}], each with its strips' manifest (and their urls), or only a
picture when it has none. */
(function(){
const BG_KEY='dtf-viewer-bg';
const BACKGROUNDS=[['light','Claro'],['dark','Escuro'],['check','Xadrez']];
const PX_PER_CM=300/2.54;
// Decoded strips kept, the least recently drawn going first.
const KEEP=24;
const el=(tag,text,cls)=>{const n=document.createElement(tag);if(text!=null)n.textContent=text;if(cls)n.className=cls;return n;};
const cm=px=>(px/PX_PER_CM).toFixed(1).replace('.',',');
let bg='check';try{bg=localStorage.getItem(BG_KEY)||'check';}catch(_){}
window.openViewer=function(files,{title='',onClose}={}){
const dlg=el('dialog',null,'viewer');dlg.setAttribute('aria-label','Abrir e conferir');
const top=el('div',null,'vtop'), name=el('b',title,'vname');
const tabs=el('div',null,'vtabs'), tools=el('div',null,'vtools');
const where=el('span','','vwhere');
const zoomOut=el('button','−','vbtn'), zoomText=el('span','','vzoom'), zoomIn=el('button','+','vbtn');
const fit=el('button','Inteira','vbtn'), real=el('button','100%','vbtn');
const bgBox=el('div',null,'vbg');
const close=el('button','Fechar','vbtn pri');
for(const b of [zoomOut,zoomIn,fit,real,close])b.type='button';
zoomOut.setAttribute('aria-label','Diminuir');zoomIn.setAttribute('aria-label','Aumentar');
for(const [key,label] of BACKGROUNDS){const b=el('button',label,'vbtn');b.type='button';b.dataset.bg=key;
b.onclick=()=>{bg=key;try{localStorage.setItem(BG_KEY,key);}catch(_){}paintBg();draw();};bgBox.append(b);}
tools.append(where,zoomOut,zoomText,zoomIn,fit,real,bgBox,close);
top.append(name,tabs,tools);
const stage=el('div',null,'vstage'), canvas=el('canvas'), note=el('p','','vnote');
stage.append(canvas,note);dlg.append(top,stage);document.body.append(dlg);
let file=null, zoom=1, x=0, y=0, cache=new Map(), loading=new Set(), picture=null, raf=0;
const paintBg=()=>{for(const b of bgBox.children)b.setAttribute('aria-pressed',String(b.dataset.bg===bg));stage.dataset.bg=bg;};
const size=()=>file?.manifest?{w:file.manifest.width,h:file.manifest.height}:picture?{w:picture.width,h:picture.height}:{w:1,h:1};
const fitZoom=()=>{const {w,h}=size();return Math.min(stage.clientWidth/w,stage.clientHeight/h,1);};
const clamp=()=>{const {w,h}=size(), vw=stage.clientWidth/zoom, vh=stage.clientHeight/zoom;
x=w<=vw?(w-vw)/2:Math.max(0,Math.min(w-vw,x));y=h<=vh?(h-vh)/2:Math.max(0,Math.min(h-vh,y));};
function setZoom(z,cx=stage.clientWidth/2,cy=stage.clientHeight/2){
const fx=x+cx/zoom, fy=y+cy/zoom;
zoom=Math.max(Math.min(fitZoom(),0.02),Math.min(4,z));
x=fx-cx/zoom;y=fy-cy/zoom;clamp();
zoomText.textContent=Math.round(zoom*100)+'%';draw();
}
// The level whose detail is at least what the zoom shows.
function level(){
const levels=file.manifest.levels;let k=0;
while(k+1<levels.length&&levels[k+1].scale>=zoom)k++;
return k;
}
async function strip(k,i){
const key=k+':'+i;
if(cache.has(key)){const v=cache.get(key);cache.delete(key);cache.set(key,v);return v;}
if(loading.has(key))return null;
loading.add(key);
try{
const url=file.manifest.urls[k][i];
const res=await fetch(url,{referrerPolicy:'no-referrer'});
if(!res.ok)throw new Error('erro '+res.status);
const bmp=await createImageBitmap(await res.blob());
if(closed||file?.manifest?.urls[k]?.[i]!==url){bmp.close();return null;}
cache.set(key,bmp);
while(cache.size>KEEP){const [old,v]=cache.entries().next().value;v.close?.();cache.delete(old);}
draw();
}catch(e){note.textContent='Não foi possível carregar um pedaço do arquivo ('+e.message+').';}
finally{loading.delete(key);}
return null;
}
function draw(){cancelAnimationFrame(raf);raf=requestAnimationFrame(paint);}
function paint(){
if(closed)return;
const ratio=devicePixelRatio||1, W=stage.clientWidth, H=stage.clientHeight;
canvas.width=Math.round(W*ratio);canvas.height=Math.round(H*ratio);canvas.style.width=W+'px';canvas.style.height=H+'px';
const g=canvas.getContext('2d');g.setTransform(ratio,0,0,ratio,0,0);g.clearRect(0,0,W,H);
g.imageSmoothingEnabled=zoom<1;
if(picture){g.drawImage(picture,-x*zoom,-y*zoom,picture.width*zoom,picture.height*zoom);return;}
if(!file?.manifest)return;
const k=level(), L=file.manifest.levels[k], s=L.scale, rows=file.manifest.strip_rows;
const first=Math.max(0,Math.floor(y*s/rows)), last=Math.min(L.strips-1,Math.floor((y+H/zoom)*s/rows));
for(let i=first;i<=last;i++){
let bmp=cache.get(k+':'+i);
if(!bmp){strip(k,i);
// Meanwhile, a coarser level that is already here.
for(let c=k+1;c<file.manifest.levels.length&&!bmp;c++){
const C=file.manifest.levels[c], j=Math.floor(i*rows/s*C.scale/rows), have=cache.get(c+':'+j);
if(have){const top=j*rows/C.scale;g.drawImage(have,-x*zoom,(top-y)*zoom,have.width/C.scale*zoom,have.height/C.scale*zoom);bmp=null;break;}
}
continue;}
const top=i*rows/s;
g.drawImage(bmp,-x*zoom,(top-y)*zoom,bmp.width/s*zoom,bmp.height/s*zoom);
}
}
async function show(f,tab){
file=f;picture=null;cache.forEach(v=>v.close?.());cache=new Map();note.textContent='';
for(const b of tabs.children)b.setAttribute('aria-pressed',String(b===tab));
name.textContent=title?title+' · '+f.name:f.name;
if(!f.manifest){
note.textContent=f.picture?'Este arquivo não tem os pedaços para 100%: veja a prévia ou baixe o arquivo.':'Este arquivo não tem prévia: baixe para conferir.';
if(f.picture){try{const r=await fetch(f.picture);picture=await createImageBitmap(await r.blob());}catch(_){}}
}
zoom=fitZoom();x=0;y=0;clamp();zoomText.textContent=Math.round(zoom*100)+'%';draw();
}
files.forEach((f,i)=>{const b=el('button',f.label||f.name,'vtab');b.type='button';b.onclick=()=>show(f,b);tabs.append(b);});
tabs.hidden=files.length<2;
// Drag to move, wheel or pinch to zoom, keys for the rest.
let drag=null;
stage.onpointerdown=e=>{drag={x:e.clientX,y:e.clientY,ox:x,oy:y};stage.setPointerCapture(e.pointerId);};
stage.onpointermove=e=>{
const r=stage.getBoundingClientRect(), fx=x+(e.clientX-r.left)/zoom, fy=y+(e.clientY-r.top)/zoom;
where.textContent=cm(Math.max(0,fx))+' × '+cm(Math.max(0,fy))+' cm';
if(!drag)return;x=drag.ox-(e.clientX-drag.x)/zoom;y=drag.oy-(e.clientY-drag.y)/zoom;clamp();draw();};
stage.onpointerup=()=>{drag=null;};
// As on a map: the wheel zooms where the pointer is.
stage.onwheel=e=>{e.preventDefault();const r=stage.getBoundingClientRect();
setZoom(zoom*Math.exp(-e.deltaY*(e.deltaMode?40:1)/400),e.clientX-r.left,e.clientY-r.top);};
zoomIn.onclick=()=>setZoom(zoom*1.5);zoomOut.onclick=()=>setZoom(zoom/1.5);
fit.onclick=()=>setZoom(fitZoom());real.onclick=()=>setZoom(1);
dlg.onkeydown=e=>{if(e.key==='+'||e.key==='=')setZoom(zoom*1.5);else if(e.key==='-')setZoom(zoom/1.5);
else if(e.key==='0')setZoom(fitZoom());else if(e.key==='1')setZoom(1);};
const resize=()=>{clamp();draw();};addEventListener('resize',resize);
// Closed by its button or Escape, and by the dialog's own close as well,
// once: some browsers never send that event.
let closed=false;
const finish=()=>{if(closed)return;closed=true;cancelAnimationFrame(raf);removeEventListener('resize',resize);
const strips=[...cache.values()];cache=new Map();strips.forEach(v=>v.close?.());
if(dlg.open)dlg.close();dlg.remove();onClose?.();};
close.onclick=finish;
dlg.addEventListener('cancel',e=>{e.preventDefault();finish();});
dlg.onclose=finish;
paintBg();dlg.showModal();
if(files.length)show(files[0],tabs.children[0]);
return {close:finish};
};
})();