UNPKG

vue-print-next

Version:

This is a directive wrapper for printed, Simple, fast, convenient, light.

2 lines (1 loc) 29.4 kB
(function(u,c){typeof exports=="object"&&typeof module!="undefined"?c(exports):typeof define=="function"&&define.amd?define(["exports"],c):(u=typeof globalThis!="undefined"?globalThis:u||self,c(u.VuePrintNext={}))})(this,function(u){"use strict";var R=Object.defineProperty,_=Object.defineProperties;var V=Object.getOwnPropertyDescriptors;var z=Object.getOwnPropertySymbols;var D=Object.prototype.hasOwnProperty,F=Object.prototype.propertyIsEnumerable;var P=(u,c,y)=>c in u?R(u,c,{enumerable:!0,configurable:!0,writable:!0,value:y}):u[c]=y,A=(u,c)=>{for(var y in c||(c={}))D.call(c,y)&&P(u,y,c[y]);if(z)for(var y of z(c))F.call(c,y)&&P(u,y,c[y]);return u},M=(u,c)=>_(u,V(c));var N=(u,c)=>{var y={};for(var f in u)D.call(u,f)&&c.indexOf(f)<0&&(y[f]=u[f]);if(u!=null&&z)for(var f of z(u))c.indexOf(f)<0&&F.call(u,f)&&(y[f]=u[f]);return y};var E=(u,c,y)=>P(u,typeof c!="symbol"?c+"":c,y);const c="[VuePrintNext]";let y=0;const f={A0:{width:"841mm",height:"1189mm"},A1:{width:"594mm",height:"841mm"},A2:{width:"420mm",height:"594mm"},A3:{width:"297mm",height:"420mm"},A4:{width:"210mm",height:"297mm"},A5:{width:"148mm",height:"210mm"},A6:{width:"105mm",height:"148mm"},A7:{width:"74mm",height:"105mm"},A8:{width:"52mm",height:"74mm"},Letter:{width:"215.9mm",height:"279.4mm"},Legal:{width:"215.9mm",height:"355.6mm"},Tabloid:{width:"279.4mm",height:"431.8mm"}},q=(p=32)=>`width: ${p}px; height: ${p}px; border: none; background: transparent; cursor: pointer; border-radius: 4px; display: flex; align-items: center; justify-content: center; transition: background-color 0.2s ease; padding: 0; outline: none;`,T=(p,e,t=32,o="")=>{const n=document.createElement("button");return n.setAttribute("class",p),n.setAttribute("style",q(t)),n.innerHTML=e,o&&n.setAttribute("title",o),n},k=(p,e)=>(p.addEventListener("mouseover",()=>{p.style.backgroundColor=e?"#444":"#f0f0f0"}),p.addEventListener("mouseout",()=>{p.style.backgroundColor="transparent"}),p),b={close:'<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="18" y1="6" x2="6" y2="18"></line><line x1="6" y1="6" x2="18" y2="18"></line></svg>',zoomIn:'<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="12" y1="5" x2="12" y2="19"></line><line x1="5" y1="12" x2="19" y2="12"></line></svg>',zoomOut:'<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="5" y1="12" x2="19" y2="12"></line></svg>',darkMode:'<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"></path></svg>',lightMode:'<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="5"></circle><line x1="12" y1="1" x2="12" y2="3"></line><line x1="12" y1="21" x2="12" y2="23"></line><line x1="4.22" y1="4.22" x2="5.64" y2="5.64"></line><line x1="18.36" y1="18.36" x2="19.78" y2="19.78"></line><line x1="1" y1="12" x2="3" y2="12"></line><line x1="21" y1="12" x2="23" y2="12"></line><line x1="4.22" y1="19.78" x2="5.64" y2="18.36"></line><line x1="18.36" y1="5.64" x2="19.78" y2="4.22"></line></svg>',fullscreen:'<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" y1="3" x2="14" y2="10"></line><line x1="3" y1="21" x2="10" y2="14"></line></svg>',exitFullscreen:'<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="8 3 3 3 3 8"></polyline><polyline points="21 8 21 3 16 3"></polyline><polyline points="3 16 3 21 8 21"></polyline><polyline points="16 21 21 21 21 16"></polyline></svg>',print:'<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="margin-right: 6px;"><polyline points="6 9 6 2 18 2 18 9"></polyline><path d="M6 18H4a2 2 0 0 1-2-2v-5a2 2 0 0 1 2-2h16a2 2 0 0 1 2 2v5a2 2 0 0 1-2 2h-2"></path><rect x="6" y="14" width="12" height="8"></rect></svg>',paper:'<svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="margin-right: 6px;"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path><polyline points="14 2 14 8 20 8"></polyline></svg>'},S=(p,e)=>{var o;if(typeof p=="number"||!isNaN(Number(p)))return`${p}${e}`;const t=(o=p.toString().match(/[a-zA-Z]+/g))==null?void 0:o[0];return!t||!(t in f)?`${p}mm`:p};function j(){try{return/^((?!chrome|android).)*safari/i.test(navigator.userAgent)&&navigator.vendor==="Apple Computer, Inc."}catch(p){return!1}}function O(p){const{immediateEvents:e=["load"],debug:t=!1}={},o=HTMLIFrameElement.prototype.addEventListener;return HTMLIFrameElement.prototype.addEventListener=function(n,r,i){var s;if(o.call(this,n,r,i),!(!j()||typeof r!="function"))try{e.includes(n)&&((s=this.contentWindow)==null?void 0:s.document.readyState)==="complete"&&(t&&console.log(`[Safari Iframe Patch] Triggering "${n}" immediately`),this.contentWindow.setTimeout(()=>{r.call(this,new Event(n))}))}catch(l){console.warn("[Safari Iframe Patch] Error:",l)}},function(){HTMLIFrameElement.prototype.addEventListener=o,t&&console.log("[Safari Iframe Patch] Restored original addEventListener")}}class ${constructor(e){E(this,"standards",{strict:"strict",loose:"loose",html5:"html5"});E(this,"iframeId","");E(this,"previewBody",null);E(this,"close",null);E(this,"scale",1);E(this,"isDarkMode",!1);E(this,"isFullscreen",!0);E(this,"settings");const C=e,{vue:t,standard:o="html5",zIndex:n=20002,previewTitle:r="打印预览",previewPrintBtnLabel:i="打印",preview:s=!1,popTitle:l=document.title,previewSize:a,darkMode:h=!1,windowMode:d=!1,defaultScale:m=1,previewTools:v={zoom:!0,theme:!0,fullscreen:!0}}=C,x=N(C,["vue","standard","zIndex","previewTitle","previewPrintBtnLabel","preview","popTitle","previewSize","darkMode","windowMode","defaultScale","previewTools"]);this.isDarkMode=h,this.isFullscreen=!d,this.scale=m,this.settings=M(A({standard:o,zIndex:n,previewTitle:r,previewPrintBtnLabel:i,preview:s,popTitle:l,previewSize:a,darkMode:h,windowMode:d,defaultScale:m,previewTools:v},x),{previewBeforeOpenCallback:()=>{var w;return(w=e.previewBeforeOpenCallback)==null?void 0:w.call(e,t)},previewOpenCallback:()=>{var w;return(w=e.previewOpenCallback)==null?void 0:w.call(e,t)},openCallback:()=>{var w;return(w=e.openCallback)==null?void 0:w.call(e,t)},closeCallback:()=>{var w;return(w=e.closeCallback)==null?void 0:w.call(e,t)},beforeOpenCallback:()=>{var w;return(w=e.beforeOpenCallback)==null?void 0:w.call(e,t)}}),this.init()}init(){this.iframeId=`printArea_${++y}`;const{el:e,url:t,asyncUrl:o}=this.settings;if(O(),e)this.handlePrintWindow("");else if(t)this.handlePrintWindow(t);else if(o)o(n=>this.handlePrintWindow(n),this.settings.vue);else throw new Error(`${c}: Either "el", "url", or "asyncUrl" parameter must be provided in the settings.`)}handlePrintWindow(e){const t=this.getPrintWindow(e);!e&&this.write(t.doc),this.settings.preview?this.previewIframeLoad():this.print(t)}addEvent(e,t,o){e&&e.addEventListener(t,o,!1)}previewIframeLoad(){var o,n;const e=document.getElementById("vue-print-next-previewBox");if(!e)return;const t=e.querySelector("iframe");(n=(o=this.settings).previewBeforeOpenCallback)==null||n.call(o),this.addEvent(t,"load",()=>{var r,i;this.previewBoxShow(),(i=(r=this.settings).previewOpenCallback)==null||i.call(r)}),this.addEvent(e.querySelector(".previewBodyUtilPrintBtn"),"click",()=>{var r,i,s,l,a,h;(i=(r=this.settings).beforeOpenCallback)==null||i.call(r),t!=null&&t.contentWindow?this.waitForFontsLoaded(t.contentWindow).then(()=>{var d,m,v,x,C;(m=(d=this.settings).openCallback)==null||m.call(d),(v=t.contentWindow)==null||v.print(),(C=(x=this.settings).closeCallback)==null||C.call(x)}).catch(()=>{var d,m,v,x,C;console.warn(`${c}: Font loading timeout in preview mode, proceeding with print`),(m=(d=this.settings).openCallback)==null||m.call(d),(v=t.contentWindow)==null||v.print(),(C=(x=this.settings).closeCallback)==null||C.call(x)}):((l=(s=this.settings).openCallback)==null||l.call(s),(h=(a=this.settings).closeCallback)==null||h.call(a))})}print(e){var r,i;const t=document.getElementById(this.iframeId)||e.f,o=t==null?void 0:t.contentWindow;if(!o)return;const n=()=>{this.waitForFontsLoaded(o).then(()=>{const s=setTimeout(()=>{var l,a,h,d;o.focus(),(a=(l=this.settings).openCallback)==null||a.call(l),o.print(),t.remove(),(d=(h=this.settings).closeCallback)==null||d.call(h),clearTimeout(s)},100)}).catch(()=>{console.warn(`${c}: Font loading timeout, proceeding with print`);const s=setTimeout(()=>{var l,a,h,d;o.focus(),(a=(l=this.settings).openCallback)==null||a.call(l),o.print(),t.remove(),(d=(h=this.settings).closeCallback)==null||d.call(h),clearTimeout(s)},100)})};(i=(r=this.settings).beforeOpenCallback)==null||i.call(r),this.addEvent(t,"load",n)}getNoPrintMediaStyle(){const{noPrintSelector:e}=this.settings;if(!e)return"";const o=(Array.isArray(e)?e:[e]).filter(n=>n.trim());return o.length?`${o.join(",")} { display: none; }`:(console.error(new TypeError(`${c}: The "noPrintSelector" must be a non-empty string or an array of strings.`)),"")}write(e){e.open(),e.write(`${this.docType()}<html lang='zh'>${this.getHead()}${this.getBody()}</html>`),e.close()}docType(){if(this.settings.standard===this.standards.html5)return"<!DOCTYPE html>";const e=this.settings.standard===this.standards.loose?" Transitional":"",t=this.settings.standard===this.standards.loose?"loose":"strict";return`<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01${e}//EN" "http://www.w3.org/TR/html4/${t}.dtd">`}getPrintMediaStyle(){const{paperSize:e,orientation:t,customSize:o}=this.settings,n="body {-webkit-print-color-adjust: exact; -moz-print-color-adjust: exact; -ms-print-color-adjust: exact; print-color-adjust: exact;}",r=this.settings.paperSize!==void 0||this.settings.orientation!==void 0||this.settings.customSize!==void 0;let i="";if(r){const s=e||"A4",l=t||"portrait";if(s==="custom"&&o){const{width:a,height:h,unit:d="mm"}=o,m=S(a,d),v=S(h,d);i=l==="portrait"?`@page { size: ${m} ${v}; }`:`@page { size: ${v} ${m}; }`}else if(s in f){const a=f[s];i=l==="portrait"?`@page { size: ${a.width} ${a.height}; }`:`@page { size: ${a.height} ${a.width}; }`}else{const a=f.A4;i=t==="portrait"?`@page { size: ${a.width} ${a.height}; }`:`@page { size: ${a.height} ${a.width}; }`}}return`${n}${i}`}getHead(){const{extraHead:e="",extraCss:t="",popTitle:o}=this.settings,n=Array.from(document.querySelectorAll('link[href$=".css"]')).map(h=>`<link type="text/css" rel="stylesheet" href='${h.href}'>`).join(""),r=Array.from(document.querySelectorAll("style")).map(h=>h.outerHTML).join(""),i=t.split(",").filter(h=>h.trim()).map(h=>`<link type="text/css" rel="stylesheet" href='${h.trim()}'>`).join(""),s=this.getPrintMediaStyle(),l=this.getNoPrintMediaStyle();return`<head><title>${o}</title>`+e+n+r+`<style type="text/css">${l}${s}</style>`+i+"</head>"}getPrintAreaDom(){const e=this.settings.el,t=typeof e=="string";if(e instanceof HTMLElement)return[e];if(!t)throw new TypeError(`${c}: The "el" property should be either a string (CSS selector) or an HTMLElement, but received type "${typeof e}".`);let o=Array.from(document.querySelectorAll(e));if(!(o!=null&&o.length))throw new Error(`${c}: No elements found matching the selector: "${e}".`);return o}getBody(){const e=this.getPrintAreaDom(),t=document.createElement("div");return e.forEach(o=>{const n=o.cloneNode(!0);this.removeScriptHandler(n),this.canvasToImgHandler(o,n),this.formDataHandler(o,n),t.appendChild(n)}),`<body>${t.innerHTML}</body>`}removeScriptHandler(e){const t=Array.from(e.querySelectorAll("script"));t.length!==0&&t.forEach(o=>{o.remove()})}canvasToImgHandler(e,t){try{const o=Array.from(e.querySelectorAll("canvas")),n=Array.from(t.querySelectorAll("canvas"));if(n.length===0)return;let r=[];n.forEach((i,s)=>{const l=o[s];if(!l){console.warn(`${c}: Canvas element mismatch, Canvas at index ${s} does not exist in the original element`);return}const a=i.parentNode;if(!a){console.warn(`${c}: Canvas parent element does not exist`);return}try{const h=l.toDataURL("image/png"),d=new Image;d.className="canvasImg",d.style.display="block",d.src=h,r.push({parent:a,newElement:d,oldElement:i})}catch(h){console.error(`${c}: Error processing Canvas:`,h);const d=document.createElement("div");d.className="canvas-error",d.style.cssText="padding: 10px; border: 1px dashed #ff6b6b; color: #ff6b6b; text-align: center;",d.textContent="Unable to load Canvas content, possibly due to cross-origin restrictions",r.push({parent:a,newElement:d,oldElement:i})}}),r.forEach(({parent:i,newElement:s,oldElement:l})=>{i.replaceChild(s,l)})}catch(o){console.error(`${c}: Error occurred while processing Canvas elements:`,o)}}formDataHandler(e,t){try{const o=Array.from(t.querySelectorAll("input,select,textarea"));if(o.length===0)return;const n=Array.from(e.querySelectorAll("select"));let r=-1;o.forEach(i=>{var s;try{const l=i.value;switch((s=i.getAttribute("type"))!=null?s:i.tagName.toLowerCase()){case"select":if(r++,r<n.length){const d=n[r].selectedIndex,m=i.options;m&&m.length>d&&d>=0?m[d].setAttribute("selected","selected"):m&&m.length>0?(m[0].setAttribute("selected","selected"),console.warn(`${c}: Select element option index out of range, defaulting to first option`)):console.warn(`${c}: Select element has no options`)}else console.warn(`${c}: Original select element not found at index ${r}`);break;case"textarea":i.innerHTML=l,i.setAttribute("html",l);break;case"radio":case"checkbox":i.checked&&i.setAttribute("checked","checked");break;default:i.value=l,i.setAttribute("value",l);break}}catch(l){console.error(`${c}: Error processing form element (${i.tagName}):`,l)}})}catch(o){console.error(`${c}: Error occurred while processing form data:`,o)}}getPrintWindow(e){var n;const t=this.createIframe(e),o=t.contentDocument||((n=t.contentWindow)==null?void 0:n.document)||t.document;if(!o)throw new Error(`${c}: Unable to find the document object within the created iframe. Please ensure the iframe is correctly created and loaded.`);return{f:t,win:t.contentWindow||t,doc:o}}previewBoxShow(){const e=document.getElementById("vue-print-next-previewBox");if(!e)return;const t=document.querySelector("html");t&&(t.style.overflow="hidden"),e.style.opacity="0",e.style.display="block",setTimeout(()=>{e.style.transition="opacity 0.3s ease",e.style.opacity="1",this.applyInitialSettings()},10)}applyInitialSettings(){this.isDarkMode&&(this.isDarkMode=!1,this.toggleTheme()),this.isFullscreen||(this.isFullscreen=!0,this.toggleFullscreen()),this.scale!==1&&this.updatePreviewScale()}previewBoxHide(){const e=document.getElementById("vue-print-next-previewBox");e&&(e.style.opacity="0",setTimeout(()=>{var n;const t=document.querySelector("html");t&&(t.style.overflow="visible");const o=e.querySelector(".paperContainer");(n=o==null?void 0:o.querySelector("iframe"))==null||n.remove(),e.style.display="none",this.resetPreviewState(e)},300))}resetPreviewState(e){const t=e.querySelector(".previewBodyUtilPrintBtn");t&&(t.style.backgroundColor="#1890ff",t.style.boxShadow="none"),this.scale=this.settings.defaultScale||1,this.isDarkMode&&this.toggleTheme(),this.isFullscreen&&(this.isFullscreen=!1,e.style.width="100%",e.style.height="100%",e.style.top="0",e.style.left="0",e.style.borderRadius="0")}previewBox(){var o;let e=document.getElementById("vue-print-next-previewBox");if(e){(o=e.querySelector("iframe"))==null||o.remove();const n=e.querySelector(".previewHeader");if(n){const s=this.createPreviewHeader();e.replaceChild(s,n),this.close=s.querySelector(".previewClose")}const r=e.querySelector(".paperContainer");r&&this.setPaperSize(r);const i=e.querySelector(".paperInfo");return i&&(i.innerHTML=b.paper+this.getPaperSizeText()),{close:this.close,previewBody:e.querySelector(".previewBody")}}e=document.createElement("div"),e.setAttribute("id","vue-print-next-previewBox"),e.setAttribute("style","position: fixed; top: 0px; left: 0px; width: 100%; height: 100%; background: rgba(250, 250, 250, 0.98); display: none; z-index: "+this.settings.zIndex+'; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; box-shadow: 0 0 20px rgba(0, 0, 0, 0.1); transition: background-color 0.3s ease;'),e.setAttribute("data-theme","light");const t=this.createPreviewHeader();return e.appendChild(t),this.previewBody=this.createPreviewBody(),e.appendChild(this.previewBody),document.body.appendChild(e),{close:this.close,previewBody:this.previewBody}}createPreviewHeader(){const e=document.createElement("div");e.setAttribute("class","previewHeader"),e.setAttribute("style","padding: 12px 24px; background: #fff; border-bottom: 1px solid #eaeaea; display: flex; align-items: center; justify-content: space-between; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05); transition: background-color 0.3s ease, border-color 0.3s ease;");const t=document.createElement("div");t.setAttribute("style","font-size: 16px; font-weight: 500; color: #333; transition: color 0.3s ease;"),t.innerHTML=this.settings.previewTitle||"";const o=document.createElement("div");o.setAttribute("style","display: flex; align-items: center; gap: 12px;");const n={zoom:!0,theme:!0,fullscreen:!0};let r;if(this.settings.previewTools===!1)r={zoom:!1,theme:!1,fullscreen:!1};else if(typeof this.settings.previewTools=="object"&&this.settings.previewTools!==null){const i=this.settings.previewTools;r={zoom:i.zoom!==!1,theme:i.theme!==!1,fullscreen:i.fullscreen!==!1}}else r=A({},n);if(r.zoom){const i=this.createZoomControls();o.appendChild(i)}if(r.theme){const i=this.createThemeToggle();o.appendChild(i)}if(r.fullscreen){const i=this.createFullscreenToggle();o.appendChild(i)}return this.close=this.createCloseButton(),e.appendChild(t),e.appendChild(o),e.appendChild(this.close),e}createIframe(e){const t=document.createElement("iframe");if(t.id=this.iframeId,t.src=e||"about:blank",t.style.cssText="position: fixed; left: -9999px; border: 0; width: 0; height: 0; opacity: 0;",!this.settings.preview)document.body.appendChild(t);else{t.style.cssText="border: 0px; width: 100%; height: 100%; background: white; transition: background-color 0.3s ease;";const{close:o,previewBody:n}=this.previewBox(),r=n==null?void 0:n.querySelector(".paperContainer"),i=r==null?void 0:r.querySelector("div");i==null||i.appendChild(t),this.updatePreviewScale(),this.addEvent(o,"click",this.previewBoxHide.bind(this)),this.addEvent(t,"load",this.applyIframeDarkMode.bind(this,t))}return t}applyIframeDarkMode(e){var t;if(this.isDarkMode)try{const o=e.contentDocument||((t=e.contentWindow)==null?void 0:t.document);if(o){const n=o.createElement("style");n.textContent="body { color-scheme: dark; }",o.head.appendChild(n)}}catch(o){console.warn("[VuePrintNext] Failed to apply dark mode style to iframe content",o)}}createCloseButton(){const e=document.createElement("div");return e.setAttribute("class","previewClose"),e.setAttribute("style","width: 32px; height: 32px; cursor: pointer; display: flex; align-items: center; justify-content: center; border-radius: 4px; transition: background-color 0.2s ease, color 0.3s ease;"),e.innerHTML=b.close,k(e,this.isDarkMode),e}createZoomControls(){const e=document.createElement("div");e.setAttribute("class","zoomControls"),e.setAttribute("style","display: flex; align-items: center; gap: 8px;");const t=T("zoomOutBtn",b.zoomOut,28);k(t,this.isDarkMode),t.addEventListener("click",()=>{this.scale=Math.max(.5,this.scale-.1),this.updatePreviewScale()});const o=document.createElement("span");o.setAttribute("class","zoomDisplay"),o.setAttribute("style","font-size: 14px; min-width: 40px; text-align: center;"),o.textContent="100%";const n=T("zoomInBtn",b.zoomIn,28);k(n,this.isDarkMode),n.addEventListener("click",()=>{this.scale=Math.min(2,this.scale+.1),this.updatePreviewScale()});const r=document.createElement("button");return r.setAttribute("class","zoomResetBtn"),r.setAttribute("style","font-size: 12px; padding: 2px 6px; border: none; background: transparent; cursor: pointer; border-radius: 4px; transition: background-color 0.2s ease;"),r.textContent="重置",k(r,this.isDarkMode),r.addEventListener("click",()=>{this.scale=this.settings.defaultScale||1,this.updatePreviewScale()}),e.appendChild(t),e.appendChild(o),e.appendChild(n),e.appendChild(r),e}updatePreviewScale(){const e=document.getElementById("vue-print-next-previewBox");if(!e)return;const t=e.querySelector(".zoomDisplay");t&&(t.textContent=`${Math.round(this.scale*100)}%`);const o=e.querySelector(".paperContainer");o&&(o.style.transform=`scale(${this.scale})`,o.style.transformOrigin="center top")}createThemeToggle(){const e=T("themeToggle",b.darkMode,32,"切换深色/浅色模式");return k(e,this.isDarkMode),e.addEventListener("click",()=>{this.toggleTheme()}),e}toggleTheme(){var C,w,I,L,H;this.isDarkMode=!this.isDarkMode;const e=document.getElementById("vue-print-next-previewBox");if(!e)return;const t=this.isDarkMode?{theme:"dark",background:"rgba(30, 30, 30, 0.98)",headerBg:"#242424",borderColor:"#333",textColor:"#fff",contentBg:"#1a1a1a",pageInfoColor:"#ccc",themeIcon:b.lightMode}:{theme:"light",background:"rgba(250, 250, 250, 0.98)",headerBg:"#fff",borderColor:"#eaeaea",textColor:"#333",contentBg:"#f5f5f5",pageInfoColor:"#666",themeIcon:b.darkMode};e.setAttribute("data-theme",t.theme),e.style.backgroundColor=t.background;const o=e.querySelector(".previewHeader");o&&(o.style.backgroundColor=t.headerBg,o.style.borderColor=t.borderColor);const n=e.querySelector(".previewHeader > div:first-child");n&&(n.style.color=t.textColor),e.querySelectorAll(".previewHeader button, .previewHeader svg, .zoomDisplay, .zoomResetBtn").forEach(g=>{g.style.color=t.textColor});const i=e.querySelector(".previewBodyUtil");i&&(i.style.backgroundColor=t.headerBg,i.style.borderColor=t.borderColor);const s=e.querySelector(".previewBody > div:last-child");s&&(s.style.backgroundColor=t.contentBg);const l=e.querySelector(".pageInfo");l&&(l.style.color=t.pageInfoColor);const a=e.querySelector(".themeToggle");if(a){const g=a.cloneNode(!0);g.innerHTML=t.themeIcon,(C=a.parentNode)==null||C.replaceChild(g,a),k(g,this.isDarkMode),g.addEventListener("click",()=>{this.toggleTheme()})}const h=e.querySelector(".zoomInBtn");if(h){const g=h.cloneNode(!0);(w=h.parentNode)==null||w.replaceChild(g,h),k(g,this.isDarkMode),g.addEventListener("click",()=>{this.scale=Math.min(2,this.scale+.1),this.updatePreviewScale()})}const d=e.querySelector(".zoomOutBtn");if(d){const g=d.cloneNode(!0);(I=d.parentNode)==null||I.replaceChild(g,d),k(g,this.isDarkMode),g.addEventListener("click",()=>{this.scale=Math.max(.5,this.scale-.1),this.updatePreviewScale()})}const m=e.querySelector(".zoomResetBtn");if(m){const g=m.cloneNode(!0);(L=m.parentNode)==null||L.replaceChild(g,m),k(g,this.isDarkMode),g.addEventListener("click",()=>{this.scale=this.settings.defaultScale||1,this.updatePreviewScale()})}const v=e.querySelector(".fullscreenToggle");if(v){const g=v.cloneNode(!0);g.innerHTML=this.isFullscreen?b.fullscreen:b.exitFullscreen,(H=v.parentNode)==null||H.replaceChild(g,v),k(g,this.isDarkMode),g.addEventListener("click",()=>{this.toggleFullscreen()})}const x=e.querySelector(".previewBodyUtilPrintBtn");x&&(x.addEventListener("mouseover",()=>{x.style.backgroundColor="#40a9ff",x.style.boxShadow="0 2px 8px rgba(24, 144, 255, 0.3)"}),x.addEventListener("mouseout",()=>{x.style.backgroundColor="#1890ff",x.style.boxShadow="none"}))}createFullscreenToggle(){const e=T("fullscreenToggle",b.fullscreen,32,"切换全屏/弹窗模式");return k(e,this.isDarkMode),e.addEventListener("click",()=>{this.toggleFullscreen()}),e}toggleFullscreen(){this.isFullscreen=!this.isFullscreen;const e=document.getElementById("vue-print-next-previewBox");if(!e)return;const t=this.isFullscreen?{width:"100%",height:"100%",top:"0",left:"0",borderRadius:"0",boxShadow:"0 0 20px rgba(0, 0, 0, 0.1)",icon:b.fullscreen}:{width:"80%",height:"80%",top:"10%",left:"10%",borderRadius:"8px",boxShadow:"0 0 30px rgba(0, 0, 0, 0.2)",icon:b.exitFullscreen};e.style.width=t.width,e.style.height=t.height,e.style.top=t.top,e.style.left=t.left,e.style.borderRadius=t.borderRadius,e.style.boxShadow=t.boxShadow;const o=e.querySelector(".fullscreenToggle");o&&(o.innerHTML=t.icon)}getPaperSizeText(){const{paperSize:e="A4",orientation:t="portrait",customSize:o,previewSize:n}=this.settings;let r="";if(n){if(typeof n=="string")r=n;else if(n.width&&n.height){const{width:i,height:s,unit:l="mm"}=n,a=S(i,l),h=S(s,l);r=`自定义 (${a} × ${h})`}}else if(e==="custom"&&o){const{width:i,height:s,unit:l="mm"}=o,a=S(i,l),h=S(s,l);r=`自定义 (${a} × ${h})`}else r=e;return`${r} - ${t==="portrait"?"纵向":"横向"}`}createPreviewBody(){const e=document.createElement("div");e.className="previewBody",e.style.cssText="display: flex; flex-direction: column; height: calc(100% - 56px);";const t=document.createElement("div");t.className="previewBodyUtil",t.style.cssText="height: 48px; background: #fff; position: relative; display: flex; align-items: center; justify-content: space-between; padding: 0 24px; border-bottom: 1px solid #eaeaea; transition: background-color 0.3s ease, border-color 0.3s ease;";const o=document.createElement("div");o.className="leftTools",o.style.cssText="display: flex; align-items: center; gap: 16px;";const n=document.createElement("button");n.className="previewBodyUtilPrintBtn",n.style.cssText="padding: 8px 16px; font-size: 14px; color: white; cursor: pointer; background: #1890ff; border: none; border-radius: 4px; transition: all 0.3s ease; outline: none; font-weight: 500; display: flex; align-items: center; justify-content: center;",n.innerHTML=b.print+(this.settings.previewPrintBtnLabel||""),n.addEventListener("mouseover",()=>{n.style.backgroundColor="#40a9ff",n.style.boxShadow="0 2px 8px rgba(24, 144, 255, 0.3)"}),n.addEventListener("mouseout",()=>{n.style.backgroundColor="#1890ff",n.style.boxShadow="none"}),o.appendChild(n);const r=document.createElement("div");r.className="paperInfo",r.style.cssText="font-size: 13px; color: #666; display: flex; align-items: center; transition: color 0.3s ease;",r.innerHTML=b.paper+this.getPaperSizeText(),o.appendChild(r),t.appendChild(o),e.appendChild(t);const i=document.createElement("div");i.className="contentContainer",i.style.cssText="flex: 1; padding: 24px; overflow: auto; background: #f5f5f5; display: flex; justify-content: center; transition: background-color 0.3s ease;";const s=document.createElement("div");s.className="paperContainer",s.style.cssText="background: white; box-shadow: 0 2px 12px rgba(0, 0, 0, 0.15); transition: box-shadow 0.3s ease; transform-origin: center top; margin-bottom: 24px;",this.setPaperSize(s);const l=document.createElement("div");return l.style.cssText="width: 100%; height: 100%; overflow: hidden;",s.appendChild(l),i.appendChild(s),e.appendChild(i),e}getPaperDimensions(){const{paperSize:e="A4",customSize:t,previewSize:o}=this.settings,n=f.A4;let r=n.width,i=n.height;if(o){if(typeof o=="string"){if(o in f){const s=f[o];r=s.width,i=s.height}}else if(o.width&&o.height){const{width:s,height:l,unit:a="mm"}=o;r=S(s,a),i=S(l,a)}}else if(e==="custom"&&t){const{width:s,height:l,unit:a="mm"}=t;r=S(s,a),i=S(l,a)}else if(e in f){const s=f[e];r=s.width,i=s.height}return{width:r,height:i}}setPaperSize(e){const{orientation:t="portrait"}=this.settings,{width:o,height:n}=this.getPaperDimensions();t==="portrait"?(e.style.width=o,e.style.height=n):(e.style.width=n,e.style.height=o)}waitForFontsLoaded(e){return new Promise((t,o)=>{const r=Date.now();if(e.document.fonts&&typeof e.document.fonts.ready=="object")Promise.race([e.document.fonts.ready,new Promise((i,s)=>{setTimeout(()=>s(new Error("Font loading timeout")),3e3)})]).then(()=>{t()}).catch(()=>{o(new Error("Font loading timeout or failed"))});else{const i=()=>{if(Date.now()-r>=3e3){o(new Error("Font loading timeout"));return}e.document.readyState==="complete"?setTimeout(()=>t(),200):setTimeout(i,50)};i()}})}}const U=(p,e,t)=>{p.addEventListener?p.addEventListener(e,t,!1):p.attachEvent?p.attachEvent("on"+e,t):p["on"+e]=t},B={directiveName:"print",mounted(p,e){let t,o={};U(p,"click",()=>{e.value?typeof e.value=="string"?t=e.value:typeof e.value=="object"&&(t=e.value.el,o=e.value):t="body",new $(M(A({},o),{el:t,vue:e.instance}))})},bind(p,e,t){e.instance=t.context,B.mounted(p,e)}},W={install(p){p.directive(B.directiveName,B)}};u.VuePrintNext=$,u.printPlugin=W,u.vPrint=B,Object.defineProperty(u,Symbol.toStringTag,{value:"Module"})});