UNPKG

react-pdf-printer

Version:
3 lines (2 loc) 10.8 kB
import e,{createContext as t,useContext as r,useReducer as n,useState as i,useCallback as o,useEffect as a,useMemo as c,useRef as l,useImperativeHandle as u}from"react";import d from"react-dom";var p=t(null),f=function(e){var t=r(p);if(!t&&!e)throw new Error("DocumentContext can only be executed under DocumentProvider");return t},s=function(t){var r=t.children;return e.createElement("section",{"data-printer-section":"footer"},r)},h=function(t){var r=t.children;return e.createElement("section",{"data-printer-section":"header"},r)},m=function(t){var r=t.children;return e.createElement("section",{"data-printer-section":"main"},r)},g=function(t){var r=t.header,n=t.footer,i=t.children,o=f(),a=o.header,c=o.footer;return e.createElement("article",{"data-printer-article":"page"},e.createElement(h,null,r||a),e.createElement(s,null,n||c),e.createElement(m,null,i))},v=function(t){var r=t.header,n=t.footer,i=t.children,o=f(),a=o.header,c=o.footer;return e.createElement("article",{"data-printer-article":"pages"},e.createElement(h,null,r||a),e.createElement(s,null,n||c),e.createElement(m,null,i))},y=function(){return y=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var i in t=arguments[r])Object.prototype.hasOwnProperty.call(t,i)&&(e[i]=t[i]);return e},y.apply(this,arguments)};function b(e,t,r){if(r||2===arguments.length)for(var n,i=0,o=t.length;i<o;i++)!n&&i in t||(n||(n=Array.prototype.slice.call(t,0,i)),n[i]=t[i]);return e.concat(n||Array.prototype.slice.call(t))}"function"==typeof SuppressedError&&SuppressedError;var E=t(null);function w(e){var t=r(E);if(!t&&!e)throw new Error("PrinterContext can only be executed under PrinterProvider");return t}var C=function(e,t){return Object.entries(t).reduce((function(r,n){var i,o=n[0],a=n[1];return y(y({},r),((i={})[o]="object"==typeof a?C(e[o],t[o]):a,i))}),e)},R=function(e,t){var r,n,i=t.key;switch(t.type){case"subscribe":return y(y({},e),((r={})[i]=!1,r));case"run":return y(y({},e),((n={})[i]=!0,n));case"reset":var o=y({},e);return delete o[i],o}},S=function(t){var r=t.configuration,c=void 0===r?{}:r,l=t.children,u=function(e,t){var r={};for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&t.indexOf(n)<0&&(r[n]=e[n]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var i=0;for(n=Object.getOwnPropertySymbols(e);i<n.length;i++)t.indexOf(n[i])<0&&Object.prototype.propertyIsEnumerable.call(e,n[i])&&(r[n[i]]=e[n[i]])}return r}(t,["configuration","children"]),d=n(R,{}),f=d[0],s=d[1],h=i(!0),m=h[0],g=h[1],v=w().configuration,b=C(v,c),E=b.useAsync,S=o((function(e){s({key:e,type:"subscribe"})}),[]),P=o((function(e){s({key:e,type:"run"})}),[]),x=o((function(e){g(!0),s({key:e,type:"reset"})}),[]);return a((function(){if(E){var e=Object.values(f),t=e.some((function(e){return!e}));g(0===e.length||t)}else g(!1)}),[f,E]),e.createElement(p.Provider,{value:y({configuration:b,subscribe:S,run:P,reset:x,isPending:m},u)},l)},P={a3:{width:1122.519685,height:1587.4015748},a4:{width:793.7007874,height:1122.519685},a5:{width:559.37007874,height:793.7007874},b4:{width:944.88188976,height:1334.1732283},b5:{width:665.19685039,height:944.88188976},"jis-b4":{width:971.33858268,height:1375.7480315},"jis-b5":{width:687.87401575,height:971.33858268},letter:{width:816,height:1056},legal:{width:816,height:1344},ledger:{width:1056,height:1632}},x=function(e,t){var r=function(e,t){var r=e.getBoundingClientRect();return{top:r.top,bottom:e===t?r.bottom:t.getBoundingClientRect().bottom}}(e,t||e),n=r.top,i=r.bottom,o=function(e,t){var r=window.getComputedStyle(e),n=e.firstElementChild&&window.getComputedStyle(e.firstElementChild),i=e===t?r:window.getComputedStyle(t),o=t.lastElementChild&&window.getComputedStyle(t.lastElementChild),a=Number(r.marginTop.replace("px","")),c=Number(i.marginBottom.replace("px","")),l=Number((null==n?void 0:n.marginTop.replace("px",""))||0),u=Number((null==o?void 0:o.marginBottom.replace("px",""))||0);return{marginTop:Math.max(a,l),marginBottom:Math.max(c,u)}}(e,t||e);return{top:n,bottom:i,marginTop:o.marginTop,marginBottom:o.marginBottom}},A="calc(100vh + 1px)",B=function(t){var r=t.documentType,n=t.children,i=t.isRendering,o=t.onRender,u=f(),d=u.configuration,p=u.isPending,s=d.pagination,h=d.orientation,m=d.size,g=l(null),v=function(e,t){var r=c((function(){var r,n=0,i=0,o="";if("number"==typeof e)n=i=e,o=String(e);else if(Array.isArray(e))n=e[0],i=e[1],o="".concat(n," ").concat(i);else{var a=P[e];n=a.width,i=a.height,o=e}return"landscape"===t&&(n=(r=[i,n])[0],i=r[1]),{width:n,height:i,sizeValue:o}}),[e,t]),n=r.height,i=r.width,o=r.sizeValue;return a((function(){var e=document.createElement("style"),r="\n @media print {\n @page {\n margin: 0;\n size: ".concat(o," ").concat(t,";\n }\n }\n ");return e.appendChild(document.createTextNode(r)),document.head.appendChild(e),function(){document.head.removeChild(e)}}),[t,o]),{width:i,height:n}}(m,h),y=v.height,E=v.width,w=l(!1);return a((function(){if(!p&&i&&g.current){if(!w.current){var e=g.current.querySelectorAll('[data-printer-article="page"], [data-printer-article="pages"]'),t=-1;e.forEach((function(e){var r=e.querySelector('[data-printer-section="header"]'),n=e.querySelector('[data-printer-section="footer"]'),i=e.querySelector('[data-printer-section="main"]');if(i&&r&&n){t++;var o=r.clientHeight,a=n.clientHeight;r.style.top="calc(".concat(t," * ").concat(A,")"),n.style.top="calc(calc(".concat(t+1," * ").concat(A,") - ").concat(a,"px)");var c=document.createElement("div");if(c.style.height="".concat(o,"px"),c.dataset.printerPlaceholder="true",e.insertBefore(c,i),"page"!==e.dataset.printerArticle){var l=y-a;if(e.previousElementSibling){var u=e.previousElementSibling.getBoundingClientRect().bottom;l+=u}for(var d=Array.from(i.querySelectorAll("[data-printer-divisible]")),p=[],f=0;f<d.length;f++){for(var s=d[f],h=Array.from(s.childNodes),m=[],g=0;g<h.length;){var v=h[g],E=v.dataset.printerSpan;if(E){var w=Math.max(parseInt(E)||1,1);if(1!==w){var C=h[Math.min(g+w,h.length)-1];m.push({parent:s,topChild:v,bottomChild:C}),g+=w}else m.push({parent:s,topChild:v,bottomChild:v}),g++}else m.push({parent:s,topChild:v,bottomChild:v}),g++}for(var R=[],S=(g=0,0);g<p.length||S<m.length;){if(g>=p.length){R.push.apply(R,m.slice(S));break}if(S>=m.length){R.push.apply(R,p.slice(g));break}for(var P=m[S].topChild.getBoundingClientRect().top,B=g;;){if(P-p[B].topChild.getBoundingClientRect().top<0)break;if(++B>=p.length)break}R.push.apply(R,b(b([],p.slice(g,B),!1),[m[S]],!1)),g=B,S++}p=R}p.forEach((function(i){var c=i.parent,u=i.topChild,d=i.bottomChild,p=x(c),f=p.top,s=p.bottom,h=p.marginTop,m=p.marginBottom;if(!(f-h>=l||s+m<=l)){var g=x(u,d),v=g.top,b=g.bottom,E=g.marginTop,w=g.marginBottom;if(v-E<l&&b+w>l){l=y-a+v,t++;var C=u.previousElementSibling,R=document.createElement("div");R.style.height="".concat(o,"px"),R.dataset.printerPlaceholder="true",c.insertBefore(R,u),C?(C.style.breakAfter="page",C.dataset.printerBreak="true"):c.previousSibling?(c.previousSibling.style.breakAfter="page",c.previousSibling.dataset.printerBreak="true"):R.style.breakBefore="page";var S=r.cloneNode(!0);S.style.top="calc(".concat(t," * ").concat(A,")"),S.dataset.printerClone="true",e.appendChild(S);var P=n.cloneNode(!0);P.style.top="calc(calc(".concat(t+1," * ").concat(A,") - ").concat(a,"px)"),P.dataset.printerClone="true",e.appendChild(P)}}}))}}}));var r=s.format,n=s.formatPage,a=s.formatTotal,c=s.style;return g.current.style.setProperty("--printer-pagination-total","".concat(t+1)),g.current.style.setProperty("--printer-pagination-content","'".concat(r.replaceAll(n,"'counter(printer-pagination-page, ".concat(c,")'")).replaceAll(a,"'counter(printer-pagination-total, ".concat(c,")'")),"'")),w.current=!0,o(),function(){var e,t,r;((null===(e=g.current)||void 0===e?void 0:e.querySelectorAll('[data-printer-break="true"]'))||[]).forEach((function(e){e.style.breakAfter="auto",e.dataset.printerBreak="false"})),((null===(t=g.current)||void 0===t?void 0:t.querySelectorAll('[data-printer-placeholder="true"]'))||[]).forEach((function(e){e.remove()})),((null===(r=g.current)||void 0===r?void 0:r.querySelectorAll('[data-printer-clone="true"]'))||[]).forEach((function(e){e.remove()})),w.current=!1}}o()}}),[p,i,y,s,o]),e.createElement("div",{ref:g,style:{width:Math.floor(E)},"data-printer-type":"".concat(r,"-document"),"data-printer-printonly":!0},n)},T=e.forwardRef((function(t,r){var n=t.title,i=t.header,c=t.footer,l=t.children,d=t.screen,p=t.configuration,f=t.renderOnInit,s=void 0===f||f,h=t.onRender,m=void 0===h?window.print:h,g=w(),v=g.isRendering,y=g.setRendering;a((function(){y(s)}),[y,s]),a((function(){if(n){var e=document.title;return document.title=n,function(){document.title=e}}}),[n]);var b=o((function(){y(!1),setTimeout((function(){return m()}),0)}),[m,y]);return u(r,(function(){return{render:function(){y(!0)}}}),[y]),e.createElement(S,{header:i,footer:c,configuration:p},e.createElement(B,{documentType:"static",onRender:b,isRendering:v},l),e.createElement("div",{"data-printer-screenonly":"true"},e.isValidElement(d)?d:e.createElement(d,{isRendering:v})))})),k=e.forwardRef((function(t,r){var n=t.header,c=t.footer,l=t.children,p=t.configuration,f=t.onRender,s=void 0===f?window.print:f,h=w(),m=h.isRendering,g=h.setRendering,v=i(null),y=v[0],b=v[1];a((function(){var e=document.createElement("div");return document.body.appendChild(e),e.dataset.printerType="portal",b(e),function(){document.body.removeChild(e)}}),[]);var E=o((function(){g(!1),setTimeout((function(){return s()}),0)}),[s,g]);return u(r,(function(){return{render:function(){setTimeout((function(){return g(!0)}),0)}}}),[g]),y?d.createPortal(e.createElement(S,{header:n,footer:c,configuration:p},e.createElement(B,{documentType:"portal",onRender:E,isRendering:m},l)),y):null})),O=function(t){return e.createElement("span",y({},t,{"data-printer-component":"pagination"}))},j={useAsync:!1,size:"a4",orientation:"portrait",pagination:{format:"#p / #t",formatPage:"#p",formatTotal:"#t",style:"decimal"}},q=function(t){var r=t.configuration,n=void 0===r?{}:r,o=t.children,a=C(j,n),c=i(!1),l=c[0],u=c[1];return e.createElement(E.Provider,{value:{configuration:a,isRendering:l,setRendering:u}},o)},N=function(e){var t=w().isRendering,r=f(!0);if(!r||!e)return{isPrinter:!!r,isRendering:t,subscribe:o((function(){}),[]),run:o((function(){}),[])};var n=r.subscribe,i=r.run,c=r.reset,l=o((function(){return n(e)}),[n,e]),u=o((function(){setTimeout((function(){return i(e)}),0)}),[i,e]);return a((function(){return function(){c(e)}}),[c,e]),{isPrinter:!0,isRendering:t,subscribe:l,run:u}};export{T as Document,g as Page,v as Pages,O as Pagination,k as PortalDocument,q as PrinterProvider,N as usePrinter}; //# sourceMappingURL=index.js.map