feat: Minhas folhas on the Site: save a cart item to the account, order it again with the files already on the server, edit it without sending them again (Rodada 5, B9)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Cauê Faleiros
2026-11-30 16:09:52 -03:00
parent 5671235681
commit 3428ff68be
5 changed files with 156 additions and 8 deletions

View File

@@ -364,3 +364,77 @@ function tracoDaArte(a){
// imagem → proporção da imagem aplicada à largura do filme
// PDF → página única lida pelo PDF.js, com boxes, rotação e UserUnit
// TIFF, PSD, AI, CDR → o navegador não abre; o cliente informa o comprimento
// Minhas folhas: "Editar" opens the product with ?folha=<id>. The files come
// from the server, so nothing is sent again, and the saved measures go back
// onto the new cards.
const folhaSalvaId=new URLSearchParams(location.search).get('folha');
let folhaSalvaAberta=false;
function mensagemFolhaSalva(texto){
const el=$('aoVivo');
if(el){el.className='aoVivo'; el.textContent=texto;}
}
function seguraMensagemFolhaSalva(){
// The live check repaints aoVivo a moment after opening; while the saved
// sheet comes, that line belongs to the loading message.
try{ if(tAval){clearTimeout(tAval);tAval=null;} }catch(_){}
mensagemFolhaSalva('Abrindo a folha salva…');
}
async function abrirFolhaSalva(id){
seguraMensagemFolhaSalva();
try{
const {sheets}=await window.dtfApi('/account/sheets');
const folha=(sheets||[]).find(s=>String(s.id)===String(id));
if(!folha){ mensagemFolhaSalva('Folha não encontrada.'); return; }
seguraMensagemFolhaSalva();
const {files}=await window.dtfApi('/account/sheets/'+encodeURIComponent(id)+'/files',{});
const item=folha.item||{};
const guardadas=item.partes?.length
? item.partes.flatMap(p=>p.savedFiles||[])
: (item.savedFiles||[]);
const fontes=item.partes?.length
? item.partes.flatMap(p=>p.production?.sources||[])
: (item.production?.sources||[]);
const porId=new Map((files||[]).map(f=>[f.upload_id,f]));
const lista=guardadas.length
? guardadas.map(meta=>({meta,arquivo:porId.get(meta.upload_id)})).filter(x=>x.arquivo)
: (files||[]).map(arquivo=>({meta:null,arquivo}));
const baixados=[];
seguraMensagemFolhaSalva();
for(const {meta,arquivo} of lista){
const resposta=await fetch(arquivo.url);
if(!resposta.ok) throw new Error('download');
const blob=await resposta.blob();
const file=new File([blob],meta?.name||arquivo.name||'arquivo',
{type:meta?.type||blob.type||'',lastModified:meta?.lastModified||Date.now()});
window.dtfEnvio?.conhecido(file,arquivo.upload_id);
baixados.push(file);
}
if(!baixados.length) throw new Error('download');
const antesArtes=artes.length, antesFolhas=folhas.length;
await sel(baixados);
if(ehFolha()){
const novas=folhas.slice(antesFolhas);
novas.forEach((x,i)=>{const fonte=fontes[i]; if(fonte) x.rep=fonte.copies||1;});
pintaFolha();
}else{
const novas=artes.slice(0,Math.max(0,artes.length-antesArtes));
novas.forEach((a,i)=>{const fonte=fontes[i]; if(fonte){
a.cm=fonte.width_cm; a.q=fonte.copies;
a.giro=fonte.rotation_degrees?90:0; a.esp=!!fonte.mirrored;
}});
pintaArtes();
}
const url=new URL(location.href); url.searchParams.delete('folha');
history.replaceState(history.state,'',url.pathname+url.search+url.hash);
}catch(error){
mensagemFolhaSalva(error?.status===409
? error.message : 'Não foi possível abrir a folha salva. Tente de novo.');
}
}
addEventListener('dtf-page-changed',()=>{
if(folhaSalvaAberta || !folhaSalvaId || !modo ||
document.documentElement.dataset.rota!=='produto') return;
folhaSalvaAberta=true;
abrirFolhaSalva(folhaSalvaId);
});