@agile-team/mach-report 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,6 @@
1
+ "use strict";const e=require("vue"),N=require("@agile-team/mach-report"),E=require("./plugin-Cl-IqCqx.cjs");function Z(t,n,r={}){const o=r.pxPerMm??3.7795275590551185,a=r.gapPx??18,s=e.ref(0),c=e.ref(800),v=e.computed(()=>(t.value?.pages??[]).map(l=>l.pageHeightMm*o)),b=e.computed(()=>{const l=t.value?.pages??[];let p=0,i=0;for(const d of l)p+=d.pageHeightMm*o+a,i=Math.max(i,d.pageWidthMm*o);return{w:i,h:p}}),x=e.computed(()=>N.computePageWindow({pageHeightsPx:v.value,viewportHeightPx:c.value/n.value,scrollTopPx:s.value/n.value,overscan:1,gapPx:a})),P=e.computed(()=>{const l=t.value;if(!l)return[];const{start:p,end:i}=x.value;return i<p?[]:l.pages.slice(p,i+1).map((d,m)=>({page:d,index:p+m}))});function u(l,p){let i=0;return d=>{i||(i=requestAnimationFrame(()=>{if(i=0,s.value=d.scrollTop,c.value=d.clientHeight||800,p.value<=0)return;const m=d.querySelectorAll(".mrp-page-holder");if(!m.length)return;const M=d.getBoundingClientRect().top+56;let y=1;m.forEach(k=>{k.getBoundingClientRect().top<=M&&(y=Number(k.dataset.page??1))}),l.value=Math.max(1,Math.min(p.value,y))}))}}return{pageHeightsPx:v,contentSize:b,windowRange:x,visiblePages:P,scrollTop:s,viewportHeight:c,makeScrollHandler:u,refreshViewport(l){l&&(c.value=l.clientHeight||800)}}}function X(t){const n=e.ref(1),r=e.ref("fit");function o(){requestAnimationFrame(()=>{const s=t();!s||!s.viewport||(n.value=Math.min(1,Math.max(.3,(s.viewport.clientWidth-48)/Math.max(1,s.pageWidthPx))))})}function a(s){s==="fit"?(r.value="fit",o()):(r.value="raw",n.value=s/100)}return{zoom:n,zoomMode:r,applyFitZoom:o,setZoom:a}}const K=8;function Y(){return new Promise(t=>setTimeout(t,0))}function H(t){return`p${Math.round(t.pageWidthMm)}x${Math.round(t.pageHeightMm)}`}function D(t){const n=new Map;t.pages.forEach(c=>{n.has(H(c))||n.set(H(c),c)});const a=`<!doctype html><html><head><meta charset="utf-8"><style>${`.mr-page{margin:0 auto;page-break-after:always;}
2
+ .mr-page tr{break-inside:avoid;page-break-inside:avoid;}
3
+ .mr-comp{break-inside:avoid;page-break-inside:avoid;}
4
+ ${[...n.values()].map(c=>`@page ${H(c)}{size:${Math.round(c.pageWidthMm)}mm ${Math.round(c.pageHeightMm)}mm;margin:0;}`).join(`
5
+ `)}
6
+ body{margin:0;background:#fff;}`}</style></head><body>`,s=t.pages.map(c=>{const v=N.renderPage(c,96,{},document);return v.style.page=H(c),v.outerHTML});return{head:a,pageChunks:s}}function J(t,n){let r=null,o=!1,a=null;function s(){r?.remove(),r=null}function c(u,l){const p=URL.createObjectURL(u),i=document.createElement("a");i.href=p,i.download=l,i.click(),setTimeout(()=>URL.revokeObjectURL(p),1e4)}async function v(){if(o)return;const u=t.value;if(!u||u.pages.length===0)return;o=!0,s();const{head:l,pageChunks:p}=D(u);r=document.createElement("iframe"),r.style.cssText="position:fixed;right:0;bottom:0;width:0;height:0;border:0;visibility:hidden;",document.body.appendChild(r);const i=r;try{const d=i.contentDocument;d.open(),d.write(l);for(let m=0;m<p.length;m++)if(d.write(p[m]),m%K===K-1&&await Y(),i!==r)return;d.write("</body></html>"),d.close(),await Y(),i.contentWindow?.focus(),i.contentWindow?.print()}catch{n.onError("调用打印失败,请改用导出 PDF")}finally{o=!1}}async function b(){if(a)return a;const u=t.value;if(!(!u||u.pages.length===0))return a=(async()=>{try{const l=await n.getPdfExporter()(u);c(new Blob([l],{type:"application/pdf"}),"mach-report.pdf")}catch(l){n.onError(l instanceof Error?`PDF 导出失败: ${l.message}`:"PDF 导出失败")}finally{a=null}})(),a}function x(u){const l=t.value;if(!l||l.pages.length===0)return;if(u==="pdf"){b();return}const{head:p,pageChunks:i}=D(l),d=`${p}${i.join("")}</body></html>`;c(new Blob([d],{type:"text/html;charset=utf-8"}),`mach-report.${u==="html"?"html":u}`)}function P(){const u=t.value;if(!u||u.pages.length===0)return;const{head:l,pageChunks:p}=D(u),i=`${l}${p.join("")}</body></html>`,d=new Blob([i],{type:"text/html;charset=utf-8"}),m=URL.createObjectURL(d),M=window.open(m,"_blank");if(!M){n.onError("浏览器拦截了弹窗,请允许弹窗后重试"),URL.revokeObjectURL(m);return}setTimeout(()=>M.print(),400),setTimeout(()=>URL.revokeObjectURL(m),1e4)}return{print:v,exportAs:x,openPdfWindow:P,releasePrintFrame:s}}const be={class:"mrp-toolbar"},ye={class:"mrp-title"},Ee=["disabled"],xe={class:"mrp-pageinfo"},ke=["disabled"],Be=e.defineComponent({__name:"ReportToolbar",props:{pageCount:{},currentPage:{},zoomMode:{},zoom:{},showExport:{type:Boolean},showPrint:{type:Boolean},showPdfWindow:{type:Boolean},messages:{}},emits:["goto","zoom","export","print","pdf-window"],setup(t){function n(r,o,a){return r.replace("{cur}",String(o)).replace("{total}",String(a))}return(r,o)=>(e.openBlock(),e.createElementBlock("div",be,[e.createElementVNode("span",ye,e.toDisplayString(t.messages.title),1),t.pageCount>0?(e.openBlock(),e.createElementBlock(e.Fragment,{key:0},[e.createElementVNode("button",{class:"mrp-nav",type:"button",disabled:t.currentPage<=1,onClick:o[0]||(o[0]=a=>r.$emit("goto",t.currentPage-1))},e.toDisplayString(t.messages.prevPage),9,Ee),e.createElementVNode("span",xe,e.toDisplayString(n(t.messages.pageInfo,t.currentPage,t.pageCount)),1),e.createElementVNode("button",{class:"mrp-nav",type:"button",disabled:t.currentPage>=t.pageCount,onClick:o[1]||(o[1]=a=>r.$emit("goto",t.currentPage+1))},e.toDisplayString(t.messages.nextPage),9,ke)],64)):e.createCommentVNode("",!0),o[9]||(o[9]=e.createElementVNode("span",{class:"mrp-spacer"},null,-1)),e.createElementVNode("button",{class:e.normalizeClass(["mrp-nav",{"mrp-active":t.zoomMode==="fit"}]),type:"button",onClick:o[2]||(o[2]=a=>r.$emit("zoom","fit"))},e.toDisplayString(t.messages.fitWidth),3),e.createElementVNode("button",{class:e.normalizeClass(["mrp-nav",{"mrp-active":t.zoomMode==="raw"&&Math.round(t.zoom*100)===100}]),type:"button",onClick:o[3]||(o[3]=a=>r.$emit("zoom",100))}," 100% ",2),e.createElementVNode("button",{class:e.normalizeClass(["mrp-nav",{"mrp-active":t.zoomMode==="raw"&&Math.round(t.zoom*100)===150}]),type:"button",onClick:o[4]||(o[4]=a=>r.$emit("zoom",150))}," 150% ",2),t.showExport?(e.openBlock(),e.createElementBlock("button",{key:1,class:"mrp-nav",type:"button",onClick:o[5]||(o[5]=a=>r.$emit("export","html"))},e.toDisplayString(t.messages.exportHtml),1)):e.createCommentVNode("",!0),t.showExport?(e.openBlock(),e.createElementBlock("button",{key:2,class:"mrp-nav",type:"button",onClick:o[6]||(o[6]=a=>r.$emit("export","pdf"))},e.toDisplayString(t.messages.exportPdf),1)):e.createCommentVNode("",!0),t.showPrint?(e.openBlock(),e.createElementBlock("button",{key:3,class:"mrp-nav",type:"button",onClick:o[7]||(o[7]=a=>r.$emit("print"))},e.toDisplayString(t.messages.print),1)):e.createCommentVNode("",!0),t.showPdfWindow?(e.openBlock(),e.createElementBlock("button",{key:4,class:"mrp-nav",type:"button",onClick:o[8]||(o[8]=a=>r.$emit("pdf-window"))},e.toDisplayString(t.messages.pdfWindow),1)):e.createCommentVNode("",!0)]))}}),Q=(t,n)=>{const r=t.__vccOpts||t;for(const[o,a]of n)r[o]=a;return r},_=Q(Be,[["__scopeId","data-v-2bfb2c61"]]),ee={title:"报表预览",prevPage:"‹ 上一页",nextPage:"下一页 ›",pageInfo:"{cur} / {total}",fitWidth:"适宽",exportHtml:"导出 HTML",exportPdf:"导出 PDF",print:"打印",pdfWindow:"PDF 窗口",loading:"报表渲染中…",empty:"暂无预览数据",errorPrefix:"",retry:"重试"},te={shellBg:"#525659",shellFg:"#e8e8e8",toolbarBg:"#323639",toolbarBorder:"#22252a",toolbarFg:"#e8e8e8",btnFg:"#cfcfcf",btnBorder:"#4a4d52",btnHoverBg:"#414549",btnHoverFg:"#ffffff",btnActiveBg:"#2d5fb8"};function Me(t){return t}function Re(t){return t}const S={showExport:!0,showPrint:!0,showPdfWindow:!0,gapPx:18,pdfFontUrl:"/simhei.ttf"};function oe(t,n){const r=n?t?.presets?.[n]:t?.defaultPreset?t?.presets?.[t.defaultPreset]:void 0,o={...t?.defaults,...r};return{showExport:o.showExport??S.showExport,showPrint:o.showPrint??S.showPrint,showPdfWindow:o.showPdfWindow??S.showPdfWindow,gapPx:o.gapPx??S.gapPx,pdfFontUrl:o.pdfFontUrl??S.pdfFontUrl,theme:{...te,...o.theme},messages:{...ee,...o.messages}}}function ne(t){const n={};return t.shellBg!=null&&(n["--mrp-shell-bg"]=t.shellBg),t.shellFg!=null&&(n["--mrp-shell-fg"]=t.shellFg),t.toolbarBg!=null&&(n["--mrp-toolbar-bg"]=t.toolbarBg),t.toolbarBorder!=null&&(n["--mrp-toolbar-border"]=t.toolbarBorder),t.toolbarFg!=null&&(n["--mrp-toolbar-fg"]=t.toolbarFg),t.btnFg!=null&&(n["--mrp-btn-fg"]=t.btnFg),t.btnBorder!=null&&(n["--mrp-btn-border"]=t.btnBorder),t.btnHoverBg!=null&&(n["--mrp-btn-hover-bg"]=t.btnHoverBg),t.btnHoverFg!=null&&(n["--mrp-btn-hover-fg"]=t.btnHoverFg),t.btnActiveBg!=null&&(n["--mrp-btn-active-bg"]=t.btnActiveBg),n}function Ce(t){const n=e.inject(E.MACH_REPORT_CONFIG_KEY,null),r=e.computed(()=>{const o=n?.value??null,a="defaults"in t&&"presets"in t?t:{defaults:t,presets:{}};return o?{defaults:{...o.defaults,...a.defaults},presets:{...o.presets,...a.presets},defaultPreset:a.defaultPreset??o.defaultPreset}:a});e.provide(E.MACH_REPORT_CONFIG_KEY,r)}function re(){return e.inject(E.MACH_REPORT_CONFIG_KEY,e.computed(()=>null))}class F extends Error{constructor(n,r,o){super(r),this.name="MachReportError",this.code=n,this.cause=o}}function ae(t){return t instanceof F?{code:t.code,cause:t.cause}:{code:"render",cause:t}}const $e={key:0,class:"mrp-state"},Se={key:1,class:"mrp-state mrp-error"},Fe={key:2,class:"mrp-state"},Te=["data-page"],ze={class:"mrp-overlay"},W=96/25.4,He=e.defineComponent({__name:"ReportPreview",props:{tempId:{type:[String,Array],default:null},furnitureTempId:{type:String,default:""},params:{type:Object,default:()=>({})},height:{type:String,default:"100vh"},autoLoad:{type:Boolean,default:!0},showExport:{type:Boolean,default:void 0},showPrint:{type:Boolean,default:void 0},showPdfWindow:{type:Boolean,default:void 0},gapPx:{type:Number,default:void 0},messages:{type:Object,default:void 0},preset:{type:String,default:void 0},fetcher:{type:Function,default:null}},emits:["loaded","error"],setup(t,{expose:n,emit:r}){e.useCssVars(h=>({v8289d092:u.value+"px"}));const o=t,a=r,s=e.shallowRef(null),c=e.ref(!1),v=e.ref(""),b=e.ref(1),x=e.ref(null),P=e.ref(null),u=e.computed(()=>Math.max(0,ce.value)),l=e.inject(E.MACH_REPORT_FETCHER_KEY,null),i=e.inject(E.MACH_REPORT_PDF_EXPORTER_KEY,null)??E.createDefaultPdfExporter({fontUrl:"/simhei.ttf"}),d=e.computed(()=>E.normalizeTempIds(o.tempId)),m=e.computed(()=>s.value?.pages.length??0),M=re(),y=e.computed(()=>oe(M.value,o.preset)),k=e.computed(()=>({...y.value.messages,...o.messages})),se=e.computed(()=>o.showExport??y.value.showExport),ie=e.computed(()=>o.showPrint??y.value.showPrint),ue=e.computed(()=>o.showPdfWindow??y.value.showPdfWindow),ce=e.computed(()=>o.gapPx??y.value.gapPx),de=e.computed(()=>({height:o.height,...ne(y.value.theme)})),{zoom:R,zoomMode:pe,applyFitZoom:U,setZoom:fe}=X(()=>({viewport:P.value,pageWidthPx:(s.value?.pages[0]?.pageWidthMm??210)*W})),me=Z(s,R,{pxPerMm:W,gapPx:u.value}),{contentSize:L,windowRange:T,visiblePages:ge,makeScrollHandler:ve,refreshViewport:G}=me,he=ve(b,m);function V(){s.value=null,b.value=1}let z=0;async function C(){const h=++z,w=o.fetcher??l;if(d.value.length===0){v.value="缺少报表模板 ID",V(),a("error",v.value,{code:"param"});return}if(!w){v.value="未提供渲染数据源 fetcher",V(),a("error",v.value,{code:"config"});return}c.value=!0,v.value="",V();try{const f=await w({tempIds:d.value,furnitureTempId:o.furnitureTempId||void 0,params:{...o.params}});if(h!==z)return;const g=N.validateRenderPlan(f);if(!g.ok){const B=g.errors.slice(0,3).map($=>`${$.path}: ${$.message}`).join("; ");throw new F("validate",`渲染计划校验失败(${g.errors.length} 处): ${B}`)}g.warnings.length>0&&console.warn("[mach-report] 渲染计划告警:",g.warnings),s.value=f,b.value=1,P.value&&(P.value.scrollTop=0),U(),a("loaded",f.pages.length)}catch(f){if(h!==z)return;const g=f instanceof F?f:new F("fetch",f instanceof Error?f.message:"预览加载失败",f);v.value=g.message,a("error",g.message,ae(g))}finally{h===z&&(c.value=!1)}}function A(h){if(m.value<=0)return;const w=Math.max(1,Math.min(m.value,h));b.value=w;const f=P.value;if(!f)return;let g=0;for(let B=0;B<w-1;B++)g+=(s.value?.pages[B]?.pageHeightMm??0)*W+u.value;f.scrollTop=g*R.value}const{print:O,exportAs:I,openPdfWindow:j,releasePrintFrame:we}=J(s,{getPdfExporter:()=>i,onError:h=>a("error",h,{code:"print"})}),Pe={reload:C,print:O,exportAs:I,openPdfWindow:j,gotoPage:A};e.provide(E.MACH_REPORT_CONTROLLER_KEY,Pe),e.watchEffect(()=>{const h=s.value;if(!h)return;const w=x.value;if(!w)return;w.querySelectorAll(".mrp-page-holder").forEach(g=>{const B=Number(g.dataset.page??0)-1,$=h.pages[B];!$||g.childElementCount>0||(g.style.width=`${$.pageWidthMm*W}px`,g.appendChild(N.renderPage($,96,{},document)))})},{flush:"post"});function q(){G(P.value),U()}return e.onMounted(()=>{window.addEventListener("resize",q),G(P.value),o.autoLoad&&C()}),e.onUnmounted(()=>{window.removeEventListener("resize",q),we()}),e.watch(()=>[o.tempId,o.furnitureTempId,JSON.stringify(o.params)],()=>{o.autoLoad&&C()}),e.watch(()=>o.fetcher??l,()=>{o.autoLoad&&C()}),n({reload:C,print:O,exportAs:I,openPdfWindow:j,gotoPage:A}),(h,w)=>(e.openBlock(),e.createElementBlock("div",{class:"mach-report-preview",style:e.normalizeStyle(de.value)},[e.createVNode(_,{"page-count":m.value,"current-page":b.value,"zoom-mode":e.unref(pe),zoom:e.unref(R),"show-export":se.value,"show-print":ie.value,"show-pdf-window":ue.value,messages:k.value,onGoto:A,onZoom:e.unref(fe),onExport:w[0]||(w[0]=f=>e.unref(I)(f)),onPrint:e.unref(O),onPdfWindow:e.unref(j)},null,8,["page-count","current-page","zoom-mode","zoom","show-export","show-print","show-pdf-window","messages","onZoom","onPrint","onPdfWindow"]),e.createElementVNode("div",{ref_key:"viewportRef",ref:P,class:"mrp-body",onScrollPassive:w[1]||(w[1]=f=>e.unref(he)(f.target))},[c.value?(e.openBlock(),e.createElementBlock("div",$e,e.toDisplayString(k.value.loading),1)):v.value?(e.openBlock(),e.createElementBlock("div",Se,[e.createTextVNode(e.toDisplayString(v.value)+" ",1),e.createElementVNode("button",{class:"mrp-retry",type:"button",onClick:C},e.toDisplayString(k.value.retry),1)])):!s.value||m.value===0?(e.openBlock(),e.createElementBlock("div",Fe,e.toDisplayString(k.value.empty),1)):(e.openBlock(),e.createElementBlock("div",{key:3,class:"mrp-scale",style:e.normalizeStyle({width:`${e.unref(L).w*e.unref(R)}px`,height:`${e.unref(L).h*e.unref(R)}px`})},[e.createElementVNode("div",{ref_key:"containerRef",ref:x,class:"mrp-scale-inner",style:e.normalizeStyle({transform:`scale(${e.unref(R)})`,transformOrigin:"top left",width:`${e.unref(L).w}px`})},[e.unref(T).padTopPx>0?(e.openBlock(),e.createElementBlock("div",{key:0,class:"mrp-spacer-top",style:e.normalizeStyle({height:`${e.unref(T).padTopPx}px`}),"aria-hidden":"true"},null,4)):e.createCommentVNode("",!0),(e.openBlock(!0),e.createElementBlock(e.Fragment,null,e.renderList(e.unref(ge),f=>(e.openBlock(),e.createElementBlock("div",{key:f.index,class:"mrp-page-holder","data-page":f.index+1},null,8,Te))),128)),e.unref(T).padBottomPx>0?(e.openBlock(),e.createElementBlock("div",{key:1,class:"mrp-spacer-bottom",style:e.normalizeStyle({height:`${e.unref(T).padBottomPx}px`}),"aria-hidden":"true"},null,4)):e.createCommentVNode("",!0)],4)],4))],544),e.createElementVNode("div",ze,[e.renderSlot(h.$slots,"default",{},void 0,!0)])],4))}}),le=Q(He,[["__scopeId","data-v-6e07f84a"]]),We=Object.freeze(Object.defineProperty({__proto__:null,default:le},Symbol.toStringTag,{value:"Module"}));exports.DEFAULT_MESSAGES=ee;exports.DEFAULT_THEME=te;exports.MachReportError=F;exports.ReportPreview=le;exports.ReportPreview$1=We;exports.ReportToolbar=_;exports.buildPrintDocument=D;exports.defineMachReportConfig=Me;exports.defineMachReportPreset=Re;exports.provideMachReportConfig=Ce;exports.resolveConfig=oe;exports.themeToCssVars=ne;exports.toErrorDetail=ae;exports.useMachReportConfig=re;exports.usePageWindow=Z;exports.usePrintExport=J;exports.useZoom=X;
@@ -0,0 +1,559 @@
1
+ import { ref as k, computed as h, defineComponent as ae, openBlock as w, createElementBlock as b, createElementVNode as E, toDisplayString as x, Fragment as se, createCommentVNode as T, normalizeClass as X, inject as N, provide as le, useCssVars as xe, shallowRef as Ee, watchEffect as Me, onMounted as Re, onUnmounted as $e, watch as oe, normalizeStyle as L, createVNode as ke, unref as m, createTextVNode as Ce, renderList as Fe, renderSlot as Be } from "vue";
2
+ import { computePageWindow as Te, renderPage as ie, validateRenderPlan as ze } from "@agile-team/mach-report";
3
+ import { a as Q, b as We, c as Se, n as He, M as Le, e as Ae } from "./plugin-Dp3kKWiQ.js";
4
+ function Oe(e, o, n = {}) {
5
+ const t = n.pxPerMm ?? 3.7795275590551185, r = n.gapPx ?? 18, s = k(0), u = k(800), v = h(
6
+ () => (e.value?.pages ?? []).map((a) => a.pageHeightMm * t)
7
+ ), R = h(() => {
8
+ const a = e.value?.pages ?? [];
9
+ let c = 0, l = 0;
10
+ for (const d of a)
11
+ c += d.pageHeightMm * t + r, l = Math.max(l, d.pageWidthMm * t);
12
+ return { w: l, h: c };
13
+ }), C = h(
14
+ () => Te({
15
+ pageHeightsPx: v.value,
16
+ viewportHeightPx: u.value / o.value,
17
+ scrollTopPx: s.value / o.value,
18
+ overscan: 1,
19
+ gapPx: r
20
+ })
21
+ ), M = h(() => {
22
+ const a = e.value;
23
+ if (!a) return [];
24
+ const { start: c, end: l } = C.value;
25
+ return l < c ? [] : a.pages.slice(c, l + 1).map((d, f) => ({ page: d, index: c + f }));
26
+ });
27
+ function i(a, c) {
28
+ let l = 0;
29
+ return (d) => {
30
+ l || (l = requestAnimationFrame(() => {
31
+ if (l = 0, s.value = d.scrollTop, u.value = d.clientHeight || 800, c.value <= 0) return;
32
+ const f = d.querySelectorAll(".mrp-page-holder");
33
+ if (!f.length) return;
34
+ const z = d.getBoundingClientRect().top + 56;
35
+ let $ = 1;
36
+ f.forEach((F) => {
37
+ F.getBoundingClientRect().top <= z && ($ = Number(F.dataset.page ?? 1));
38
+ }), a.value = Math.max(1, Math.min(c.value, $));
39
+ }));
40
+ };
41
+ }
42
+ return {
43
+ pageHeightsPx: v,
44
+ contentSize: R,
45
+ windowRange: C,
46
+ visiblePages: M,
47
+ scrollTop: s,
48
+ viewportHeight: u,
49
+ makeScrollHandler: i,
50
+ refreshViewport(a) {
51
+ a && (u.value = a.clientHeight || 800);
52
+ }
53
+ };
54
+ }
55
+ function Ie(e) {
56
+ const o = k(1), n = k("fit");
57
+ function t() {
58
+ requestAnimationFrame(() => {
59
+ const s = e();
60
+ !s || !s.viewport || (o.value = Math.min(
61
+ 1,
62
+ Math.max(0.3, (s.viewport.clientWidth - 48) / Math.max(1, s.pageWidthPx))
63
+ ));
64
+ });
65
+ }
66
+ function r(s) {
67
+ s === "fit" ? (n.value = "fit", t()) : (n.value = "raw", o.value = s / 100);
68
+ }
69
+ return { zoom: o, zoomMode: n, applyFitZoom: t, setZoom: r };
70
+ }
71
+ const ne = 8;
72
+ function re() {
73
+ return new Promise((e) => setTimeout(e, 0));
74
+ }
75
+ function j(e) {
76
+ return `p${Math.round(e.pageWidthMm)}x${Math.round(e.pageHeightMm)}`;
77
+ }
78
+ function J(e) {
79
+ const o = /* @__PURE__ */ new Map();
80
+ e.pages.forEach((u) => {
81
+ o.has(j(u)) || o.set(j(u), u);
82
+ });
83
+ const r = `<!doctype html><html><head><meta charset="utf-8"><style>${`.mr-page{margin:0 auto;page-break-after:always;}
84
+ .mr-page tr{break-inside:avoid;page-break-inside:avoid;}
85
+ .mr-comp{break-inside:avoid;page-break-inside:avoid;}
86
+ ${[...o.values()].map(
87
+ (u) => `@page ${j(u)}{size:${Math.round(u.pageWidthMm)}mm ${Math.round(u.pageHeightMm)}mm;margin:0;}`
88
+ ).join(`
89
+ `)}
90
+ body{margin:0;background:#fff;}`}</style></head><body>`, s = e.pages.map((u) => {
91
+ const v = ie(u, 96, {}, document);
92
+ return v.style.page = j(u), v.outerHTML;
93
+ });
94
+ return { head: r, pageChunks: s };
95
+ }
96
+ function je(e, o) {
97
+ let n = null, t = !1, r = null;
98
+ function s() {
99
+ n?.remove(), n = null;
100
+ }
101
+ function u(i, a) {
102
+ const c = URL.createObjectURL(i), l = document.createElement("a");
103
+ l.href = c, l.download = a, l.click(), setTimeout(() => URL.revokeObjectURL(c), 1e4);
104
+ }
105
+ async function v() {
106
+ if (t) return;
107
+ const i = e.value;
108
+ if (!i || i.pages.length === 0) return;
109
+ t = !0, s();
110
+ const { head: a, pageChunks: c } = J(i);
111
+ n = document.createElement("iframe"), n.style.cssText = "position:fixed;right:0;bottom:0;width:0;height:0;border:0;visibility:hidden;", document.body.appendChild(n);
112
+ const l = n;
113
+ try {
114
+ const d = l.contentDocument;
115
+ d.open(), d.write(a);
116
+ for (let f = 0; f < c.length; f++)
117
+ if (d.write(c[f]), f % ne === ne - 1 && await re(), l !== n) return;
118
+ d.write("</body></html>"), d.close(), await re(), l.contentWindow?.focus(), l.contentWindow?.print();
119
+ } catch {
120
+ o.onError("调用打印失败,请改用导出 PDF");
121
+ } finally {
122
+ t = !1;
123
+ }
124
+ }
125
+ async function R() {
126
+ if (r) return r;
127
+ const i = e.value;
128
+ if (!(!i || i.pages.length === 0))
129
+ return r = (async () => {
130
+ try {
131
+ const a = await o.getPdfExporter()(i);
132
+ u(
133
+ new Blob([a], { type: "application/pdf" }),
134
+ "mach-report.pdf"
135
+ );
136
+ } catch (a) {
137
+ o.onError(a instanceof Error ? `PDF 导出失败: ${a.message}` : "PDF 导出失败");
138
+ } finally {
139
+ r = null;
140
+ }
141
+ })(), r;
142
+ }
143
+ function C(i) {
144
+ const a = e.value;
145
+ if (!a || a.pages.length === 0) return;
146
+ if (i === "pdf") {
147
+ R();
148
+ return;
149
+ }
150
+ const { head: c, pageChunks: l } = J(a), d = `${c}${l.join("")}</body></html>`;
151
+ u(new Blob([d], { type: "text/html;charset=utf-8" }), `mach-report.${i === "html" ? "html" : i}`);
152
+ }
153
+ function M() {
154
+ const i = e.value;
155
+ if (!i || i.pages.length === 0) return;
156
+ const { head: a, pageChunks: c } = J(i), l = `${a}${c.join("")}</body></html>`, d = new Blob([l], { type: "text/html;charset=utf-8" }), f = URL.createObjectURL(d), z = window.open(f, "_blank");
157
+ if (!z) {
158
+ o.onError("浏览器拦截了弹窗,请允许弹窗后重试"), URL.revokeObjectURL(f);
159
+ return;
160
+ }
161
+ setTimeout(() => z.print(), 400), setTimeout(() => URL.revokeObjectURL(f), 1e4);
162
+ }
163
+ return {
164
+ print: v,
165
+ exportAs: C,
166
+ openPdfWindow: M,
167
+ releasePrintFrame: s
168
+ };
169
+ }
170
+ const Ue = { class: "mrp-toolbar" }, De = { class: "mrp-title" }, Ne = ["disabled"], Ve = { class: "mrp-pageinfo" }, Ge = ["disabled"], Ke = /* @__PURE__ */ ae({
171
+ __name: "ReportToolbar",
172
+ props: {
173
+ pageCount: {},
174
+ currentPage: {},
175
+ zoomMode: {},
176
+ zoom: {},
177
+ showExport: { type: Boolean },
178
+ showPrint: { type: Boolean },
179
+ showPdfWindow: { type: Boolean },
180
+ messages: {}
181
+ },
182
+ emits: ["goto", "zoom", "export", "print", "pdf-window"],
183
+ setup(e) {
184
+ function o(n, t, r) {
185
+ return n.replace("{cur}", String(t)).replace("{total}", String(r));
186
+ }
187
+ return (n, t) => (w(), b("div", Ue, [
188
+ E("span", De, x(e.messages.title), 1),
189
+ e.pageCount > 0 ? (w(), b(se, { key: 0 }, [
190
+ E("button", {
191
+ class: "mrp-nav",
192
+ type: "button",
193
+ disabled: e.currentPage <= 1,
194
+ onClick: t[0] || (t[0] = (r) => n.$emit("goto", e.currentPage - 1))
195
+ }, x(e.messages.prevPage), 9, Ne),
196
+ E("span", Ve, x(o(e.messages.pageInfo, e.currentPage, e.pageCount)), 1),
197
+ E("button", {
198
+ class: "mrp-nav",
199
+ type: "button",
200
+ disabled: e.currentPage >= e.pageCount,
201
+ onClick: t[1] || (t[1] = (r) => n.$emit("goto", e.currentPage + 1))
202
+ }, x(e.messages.nextPage), 9, Ge)
203
+ ], 64)) : T("", !0),
204
+ t[9] || (t[9] = E("span", { class: "mrp-spacer" }, null, -1)),
205
+ E("button", {
206
+ class: X(["mrp-nav", { "mrp-active": e.zoomMode === "fit" }]),
207
+ type: "button",
208
+ onClick: t[2] || (t[2] = (r) => n.$emit("zoom", "fit"))
209
+ }, x(e.messages.fitWidth), 3),
210
+ E("button", {
211
+ class: X(["mrp-nav", { "mrp-active": e.zoomMode === "raw" && Math.round(e.zoom * 100) === 100 }]),
212
+ type: "button",
213
+ onClick: t[3] || (t[3] = (r) => n.$emit("zoom", 100))
214
+ }, " 100% ", 2),
215
+ E("button", {
216
+ class: X(["mrp-nav", { "mrp-active": e.zoomMode === "raw" && Math.round(e.zoom * 100) === 150 }]),
217
+ type: "button",
218
+ onClick: t[4] || (t[4] = (r) => n.$emit("zoom", 150))
219
+ }, " 150% ", 2),
220
+ e.showExport ? (w(), b("button", {
221
+ key: 1,
222
+ class: "mrp-nav",
223
+ type: "button",
224
+ onClick: t[5] || (t[5] = (r) => n.$emit("export", "html"))
225
+ }, x(e.messages.exportHtml), 1)) : T("", !0),
226
+ e.showExport ? (w(), b("button", {
227
+ key: 2,
228
+ class: "mrp-nav",
229
+ type: "button",
230
+ onClick: t[6] || (t[6] = (r) => n.$emit("export", "pdf"))
231
+ }, x(e.messages.exportPdf), 1)) : T("", !0),
232
+ e.showPrint ? (w(), b("button", {
233
+ key: 3,
234
+ class: "mrp-nav",
235
+ type: "button",
236
+ onClick: t[7] || (t[7] = (r) => n.$emit("print"))
237
+ }, x(e.messages.print), 1)) : T("", !0),
238
+ e.showPdfWindow ? (w(), b("button", {
239
+ key: 4,
240
+ class: "mrp-nav",
241
+ type: "button",
242
+ onClick: t[8] || (t[8] = (r) => n.$emit("pdf-window"))
243
+ }, x(e.messages.pdfWindow), 1)) : T("", !0)
244
+ ]));
245
+ }
246
+ }), ue = (e, o) => {
247
+ const n = e.__vccOpts || e;
248
+ for (const [t, r] of o)
249
+ n[t] = r;
250
+ return n;
251
+ }, Ze = /* @__PURE__ */ ue(Ke, [["__scopeId", "data-v-2bfb2c61"]]), qe = {
252
+ title: "报表预览",
253
+ prevPage: "‹ 上一页",
254
+ nextPage: "下一页 ›",
255
+ pageInfo: "{cur} / {total}",
256
+ fitWidth: "适宽",
257
+ exportHtml: "导出 HTML",
258
+ exportPdf: "导出 PDF",
259
+ print: "打印",
260
+ pdfWindow: "PDF 窗口",
261
+ loading: "报表渲染中…",
262
+ empty: "暂无预览数据",
263
+ errorPrefix: "",
264
+ retry: "重试"
265
+ }, Ye = {
266
+ shellBg: "#525659",
267
+ shellFg: "#e8e8e8",
268
+ toolbarBg: "#323639",
269
+ toolbarBorder: "#22252a",
270
+ toolbarFg: "#e8e8e8",
271
+ btnFg: "#cfcfcf",
272
+ btnBorder: "#4a4d52",
273
+ btnHoverBg: "#414549",
274
+ btnHoverFg: "#ffffff",
275
+ btnActiveBg: "#2d5fb8"
276
+ };
277
+ function ct(e) {
278
+ return e;
279
+ }
280
+ function pt(e) {
281
+ return e;
282
+ }
283
+ const A = {
284
+ showExport: !0,
285
+ showPrint: !0,
286
+ showPdfWindow: !0,
287
+ gapPx: 18,
288
+ pdfFontUrl: "/simhei.ttf"
289
+ };
290
+ function Xe(e, o) {
291
+ const n = o ? e?.presets?.[o] : e?.defaultPreset ? e?.presets?.[e.defaultPreset] : void 0, t = { ...e?.defaults, ...n };
292
+ return {
293
+ showExport: t.showExport ?? A.showExport,
294
+ showPrint: t.showPrint ?? A.showPrint,
295
+ showPdfWindow: t.showPdfWindow ?? A.showPdfWindow,
296
+ gapPx: t.gapPx ?? A.gapPx,
297
+ pdfFontUrl: t.pdfFontUrl ?? A.pdfFontUrl,
298
+ theme: { ...Ye, ...t.theme },
299
+ messages: { ...qe, ...t.messages }
300
+ };
301
+ }
302
+ function Je(e) {
303
+ const o = {};
304
+ return e.shellBg != null && (o["--mrp-shell-bg"] = e.shellBg), e.shellFg != null && (o["--mrp-shell-fg"] = e.shellFg), e.toolbarBg != null && (o["--mrp-toolbar-bg"] = e.toolbarBg), e.toolbarBorder != null && (o["--mrp-toolbar-border"] = e.toolbarBorder), e.toolbarFg != null && (o["--mrp-toolbar-fg"] = e.toolbarFg), e.btnFg != null && (o["--mrp-btn-fg"] = e.btnFg), e.btnBorder != null && (o["--mrp-btn-border"] = e.btnBorder), e.btnHoverBg != null && (o["--mrp-btn-hover-bg"] = e.btnHoverBg), e.btnHoverFg != null && (o["--mrp-btn-hover-fg"] = e.btnHoverFg), e.btnActiveBg != null && (o["--mrp-btn-active-bg"] = e.btnActiveBg), o;
305
+ }
306
+ function ft(e) {
307
+ const o = N(Q, null), n = h(() => {
308
+ const t = o?.value ?? null, r = "defaults" in e && "presets" in e ? e : { defaults: e, presets: {} };
309
+ return t ? {
310
+ defaults: { ...t.defaults, ...r.defaults },
311
+ presets: { ...t.presets, ...r.presets },
312
+ defaultPreset: r.defaultPreset ?? t.defaultPreset
313
+ } : r;
314
+ });
315
+ le(Q, n);
316
+ }
317
+ function Qe() {
318
+ return N(Q, h(() => null));
319
+ }
320
+ class D extends Error {
321
+ constructor(o, n, t) {
322
+ super(n), this.name = "MachReportError", this.code = o, this.cause = t;
323
+ }
324
+ }
325
+ function _e(e) {
326
+ return e instanceof D ? { code: e.code, cause: e.cause } : { code: "render", cause: e };
327
+ }
328
+ const et = {
329
+ key: 0,
330
+ class: "mrp-state"
331
+ }, tt = {
332
+ key: 1,
333
+ class: "mrp-state mrp-error"
334
+ }, ot = {
335
+ key: 2,
336
+ class: "mrp-state"
337
+ }, nt = ["data-page"], rt = { class: "mrp-overlay" }, U = 96 / 25.4, at = /* @__PURE__ */ ae({
338
+ __name: "ReportPreview",
339
+ props: {
340
+ tempId: {
341
+ type: [String, Array],
342
+ default: null
343
+ },
344
+ furnitureTempId: { type: String, default: "" },
345
+ params: {
346
+ type: Object,
347
+ default: () => ({})
348
+ },
349
+ height: { type: String, default: "100vh" },
350
+ autoLoad: { type: Boolean, default: !0 },
351
+ /** 以下显隐/间距不传时走配置中心(preset > defaults > 内置缺省) */
352
+ showExport: { type: Boolean, default: void 0 },
353
+ showPrint: { type: Boolean, default: void 0 },
354
+ showPdfWindow: { type: Boolean, default: void 0 },
355
+ /** 页间距(px);不传走配置中心 */
356
+ gapPx: { type: Number, default: void 0 },
357
+ /** 工具栏/状态文案覆写(不传走配置中心 messages) */
358
+ messages: { type: Object, default: void 0 },
359
+ /** 启用的 preset 名(不传用配置中心 defaultPreset) */
360
+ preset: { type: String, default: void 0 },
361
+ fetcher: {
362
+ type: Function,
363
+ default: null
364
+ }
365
+ },
366
+ emits: ["loaded", "error"],
367
+ setup(e, { expose: o, emit: n }) {
368
+ xe((P) => ({
369
+ v8289d092: i.value + "px"
370
+ }));
371
+ const t = e, r = n, s = Ee(null), u = k(!1), v = k(""), R = k(1), C = k(null), M = k(null), i = h(() => Math.max(0, fe.value)), a = N(We, null), l = N(Se, null) ?? Ae({ fontUrl: "/simhei.ttf" }), d = h(() => He(t.tempId)), f = h(() => s.value?.pages.length ?? 0), z = Qe(), $ = h(() => Xe(z.value, t.preset)), F = h(() => ({
372
+ ...$.value.messages,
373
+ ...t.messages
374
+ })), de = h(() => t.showExport ?? $.value.showExport), ce = h(() => t.showPrint ?? $.value.showPrint), pe = h(() => t.showPdfWindow ?? $.value.showPdfWindow), fe = h(() => t.gapPx ?? $.value.gapPx), me = h(() => ({
375
+ height: t.height,
376
+ ...Je($.value.theme)
377
+ })), { zoom: W, zoomMode: ge, applyFitZoom: _, setZoom: ve } = Ie(() => ({
378
+ viewport: M.value,
379
+ pageWidthPx: (s.value?.pages[0]?.pageWidthMm ?? 210) * U
380
+ })), he = Oe(s, W, {
381
+ pxPerMm: U,
382
+ gapPx: i.value
383
+ }), { contentSize: V, windowRange: O, visiblePages: we, makeScrollHandler: be, refreshViewport: ee } = he, Pe = be(R, f);
384
+ function G() {
385
+ s.value = null, R.value = 1;
386
+ }
387
+ let I = 0;
388
+ async function S() {
389
+ const P = ++I, y = t.fetcher ?? a;
390
+ if (d.value.length === 0) {
391
+ v.value = "缺少报表模板 ID", G(), r("error", v.value, { code: "param" });
392
+ return;
393
+ }
394
+ if (!y) {
395
+ v.value = "未提供渲染数据源 fetcher", G(), r("error", v.value, { code: "config" });
396
+ return;
397
+ }
398
+ u.value = !0, v.value = "", G();
399
+ try {
400
+ const p = await y({
401
+ tempIds: d.value,
402
+ furnitureTempId: t.furnitureTempId || void 0,
403
+ params: { ...t.params }
404
+ });
405
+ if (P !== I) return;
406
+ const g = ze(p);
407
+ if (!g.ok) {
408
+ const B = g.errors.slice(0, 3).map((H) => `${H.path}: ${H.message}`).join("; ");
409
+ throw new D(
410
+ "validate",
411
+ `渲染计划校验失败(${g.errors.length} 处): ${B}`
412
+ );
413
+ }
414
+ g.warnings.length > 0 && console.warn("[mach-report] 渲染计划告警:", g.warnings), s.value = p, R.value = 1, M.value && (M.value.scrollTop = 0), _(), r("loaded", p.pages.length);
415
+ } catch (p) {
416
+ if (P !== I) return;
417
+ const g = p instanceof D ? p : new D(
418
+ "fetch",
419
+ p instanceof Error ? p.message : "预览加载失败",
420
+ p
421
+ );
422
+ v.value = g.message, r("error", g.message, _e(g));
423
+ } finally {
424
+ P === I && (u.value = !1);
425
+ }
426
+ }
427
+ function K(P) {
428
+ if (f.value <= 0) return;
429
+ const y = Math.max(1, Math.min(f.value, P));
430
+ R.value = y;
431
+ const p = M.value;
432
+ if (!p) return;
433
+ let g = 0;
434
+ for (let B = 0; B < y - 1; B++)
435
+ g += (s.value?.pages[B]?.pageHeightMm ?? 0) * U + i.value;
436
+ p.scrollTop = g * W.value;
437
+ }
438
+ const { print: Z, exportAs: q, openPdfWindow: Y, releasePrintFrame: ye } = je(s, {
439
+ getPdfExporter: () => l,
440
+ onError: (P) => r("error", P, { code: "print" })
441
+ });
442
+ le(Le, { reload: S, print: Z, exportAs: q, openPdfWindow: Y, gotoPage: K }), Me(() => {
443
+ const P = s.value;
444
+ if (!P) return;
445
+ const y = C.value;
446
+ if (!y) return;
447
+ y.querySelectorAll(".mrp-page-holder").forEach((g) => {
448
+ const B = Number(g.dataset.page ?? 0) - 1, H = P.pages[B];
449
+ !H || g.childElementCount > 0 || (g.style.width = `${H.pageWidthMm * U}px`, g.appendChild(ie(H, 96, {}, document)));
450
+ });
451
+ }, { flush: "post" });
452
+ function te() {
453
+ ee(M.value), _();
454
+ }
455
+ return Re(() => {
456
+ window.addEventListener("resize", te), ee(M.value), t.autoLoad && S();
457
+ }), $e(() => {
458
+ window.removeEventListener("resize", te), ye();
459
+ }), oe(
460
+ () => [t.tempId, t.furnitureTempId, JSON.stringify(t.params)],
461
+ () => {
462
+ t.autoLoad && S();
463
+ }
464
+ ), oe(
465
+ () => t.fetcher ?? a,
466
+ () => {
467
+ t.autoLoad && S();
468
+ }
469
+ ), o({ reload: S, print: Z, exportAs: q, openPdfWindow: Y, gotoPage: K }), (P, y) => (w(), b("div", {
470
+ class: "mach-report-preview",
471
+ style: L(me.value)
472
+ }, [
473
+ ke(Ze, {
474
+ "page-count": f.value,
475
+ "current-page": R.value,
476
+ "zoom-mode": m(ge),
477
+ zoom: m(W),
478
+ "show-export": de.value,
479
+ "show-print": ce.value,
480
+ "show-pdf-window": pe.value,
481
+ messages: F.value,
482
+ onGoto: K,
483
+ onZoom: m(ve),
484
+ onExport: y[0] || (y[0] = (p) => m(q)(p)),
485
+ onPrint: m(Z),
486
+ onPdfWindow: m(Y)
487
+ }, null, 8, ["page-count", "current-page", "zoom-mode", "zoom", "show-export", "show-print", "show-pdf-window", "messages", "onZoom", "onPrint", "onPdfWindow"]),
488
+ E("div", {
489
+ ref_key: "viewportRef",
490
+ ref: M,
491
+ class: "mrp-body",
492
+ onScrollPassive: y[1] || (y[1] = (p) => m(Pe)(p.target))
493
+ }, [
494
+ u.value ? (w(), b("div", et, x(F.value.loading), 1)) : v.value ? (w(), b("div", tt, [
495
+ Ce(x(v.value) + " ", 1),
496
+ E("button", {
497
+ class: "mrp-retry",
498
+ type: "button",
499
+ onClick: S
500
+ }, x(F.value.retry), 1)
501
+ ])) : !s.value || f.value === 0 ? (w(), b("div", ot, x(F.value.empty), 1)) : (w(), b("div", {
502
+ key: 3,
503
+ class: "mrp-scale",
504
+ style: L({ width: `${m(V).w * m(W)}px`, height: `${m(V).h * m(W)}px` })
505
+ }, [
506
+ E("div", {
507
+ ref_key: "containerRef",
508
+ ref: C,
509
+ class: "mrp-scale-inner",
510
+ style: L({ transform: `scale(${m(W)})`, transformOrigin: "top left", width: `${m(V).w}px` })
511
+ }, [
512
+ m(O).padTopPx > 0 ? (w(), b("div", {
513
+ key: 0,
514
+ class: "mrp-spacer-top",
515
+ style: L({ height: `${m(O).padTopPx}px` }),
516
+ "aria-hidden": "true"
517
+ }, null, 4)) : T("", !0),
518
+ (w(!0), b(se, null, Fe(m(we), (p) => (w(), b("div", {
519
+ key: p.index,
520
+ class: "mrp-page-holder",
521
+ "data-page": p.index + 1
522
+ }, null, 8, nt))), 128)),
523
+ m(O).padBottomPx > 0 ? (w(), b("div", {
524
+ key: 1,
525
+ class: "mrp-spacer-bottom",
526
+ style: L({ height: `${m(O).padBottomPx}px` }),
527
+ "aria-hidden": "true"
528
+ }, null, 4)) : T("", !0)
529
+ ], 4)
530
+ ], 4))
531
+ ], 544),
532
+ E("div", rt, [
533
+ Be(P.$slots, "default", {}, void 0, !0)
534
+ ])
535
+ ], 4));
536
+ }
537
+ }), st = /* @__PURE__ */ ue(at, [["__scopeId", "data-v-6e07f84a"]]), mt = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
538
+ __proto__: null,
539
+ default: st
540
+ }, Symbol.toStringTag, { value: "Module" }));
541
+ export {
542
+ qe as D,
543
+ D as M,
544
+ st as R,
545
+ Ye as a,
546
+ Ze as b,
547
+ J as c,
548
+ ct as d,
549
+ pt as e,
550
+ _e as f,
551
+ Oe as g,
552
+ je as h,
553
+ Ie as i,
554
+ mt as j,
555
+ ft as p,
556
+ Xe as r,
557
+ Je as t,
558
+ Qe as u
559
+ };