UNPKG

react-pdf-printer

Version:
3 lines (2 loc) 11 kB
"use strict";var e=require("react"),t=require("react-dom"),r=e.createContext(null),n=function(t){var n=e.useContext(r);if(!n&&!t)throw new Error("DocumentContext can only be executed under DocumentProvider");return n},i=function(t){var r=t.children;return e.createElement("section",{"data-printer-section":"footer"},r)},o=function(t){var r=t.children;return e.createElement("section",{"data-printer-section":"header"},r)},a=function(t){var r=t.children;return e.createElement("section",{"data-printer-section":"main"},r)},c=function(){return c=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},c.apply(this,arguments)};function l(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 u=e.createContext(null);function d(t){var r=e.useContext(u);if(!r&&!t)throw new Error("PrinterContext can only be executed under PrinterProvider");return r}var p=function(e,t){return Object.entries(t).reduce((function(r,n){var i,o=n[0],a=n[1];return c(c({},r),((i={})[o]="object"==typeof a?p(e[o],t[o]):a,i))}),e)},s=function(e,t){var r,n,i=t.key;switch(t.type){case"subscribe":return c(c({},e),((r={})[i]=!1,r));case"run":return c(c({},e),((n={})[i]=!0,n));case"reset":var o=c({},e);return delete o[i],o}},f=function(t){var n=t.configuration,i=void 0===n?{}:n,o=t.children,a=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"]),l=e.useReducer(s,{}),u=l[0],f=l[1],h=e.useState(!0),m=h[0],g=h[1],v=d().configuration,y=p(v,i),b=y.useAsync,E=e.useCallback((function(e){f({key:e,type:"subscribe"})}),[]),C=e.useCallback((function(e){f({key:e,type:"run"})}),[]),w=e.useCallback((function(e){g(!0),f({key:e,type:"reset"})}),[]);return e.useEffect((function(){if(b){var e=Object.values(u),t=e.some((function(e){return!e}));g(0===e.length||t)}else g(!1)}),[u,b]),e.createElement(r.Provider,{value:c({configuration:y,subscribe:E,run:C,reset:w,isPending:m},a)},o)},h={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}},m=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}},g="calc(100vh + 1px)",v=function(t){var r=t.documentType,i=t.children,o=t.isRendering,a=t.onRender,c=n(),u=c.configuration,d=c.isPending,p=u.pagination,s=u.orientation,f=u.size,v=e.useRef(null),y=function(t,r){var n=e.useMemo((function(){var e,n=0,i=0,o="";if("number"==typeof t)n=i=t,o=String(t);else if(Array.isArray(t))n=t[0],i=t[1],o="".concat(n," ").concat(i);else{var a=h[t];n=a.width,i=a.height,o=t}return"landscape"===r&&(n=(e=[i,n])[0],i=e[1]),{width:n,height:i,sizeValue:o}}),[t,r]),i=n.height,o=n.width,a=n.sizeValue;return e.useEffect((function(){var e=document.createElement("style"),t="\n @media print {\n @page {\n margin: 0;\n size: ".concat(a," ").concat(r,";\n }\n }\n ");return e.appendChild(document.createTextNode(t)),document.head.appendChild(e),function(){document.head.removeChild(e)}}),[r,a]),{width:o,height:i}}(f,s),b=y.height,E=y.width,C=e.useRef(!1);return e.useEffect((function(){if(!d&&o&&v.current){if(!C.current){var e=v.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(g,")"),n.style.top="calc(calc(".concat(t+1," * ").concat(g,") - ").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 u=b-a;if(e.previousElementSibling){var d=e.previousElementSibling.getBoundingClientRect().bottom;u+=d}for(var p=Array.from(i.querySelectorAll("[data-printer-divisible]")),s=[],f=0;f<p.length;f++){for(var h=p[f],v=Array.from(h.childNodes),y=[],E=0;E<v.length;){var C=v[E],w=C.dataset.printerSpan;if(w){var x=Math.max(parseInt(w)||1,1);if(1!==x){var P=v[Math.min(E+x,v.length)-1];y.push({parent:h,topChild:C,bottomChild:P}),E+=x}else y.push({parent:h,topChild:C,bottomChild:C}),E++}else y.push({parent:h,topChild:C,bottomChild:C}),E++}for(var R=[],S=(E=0,0);E<s.length||S<y.length;){if(E>=s.length){R.push.apply(R,y.slice(S));break}if(S>=y.length){R.push.apply(R,s.slice(E));break}for(var k=y[S].topChild.getBoundingClientRect().top,A=E;;){if(k-s[A].topChild.getBoundingClientRect().top<0)break;if(++A>=s.length)break}R.push.apply(R,l(l([],s.slice(E,A),!1),[y[S]],!1)),E=A,S++}s=R}s.forEach((function(i){var c=i.parent,l=i.topChild,d=i.bottomChild,p=m(c),s=p.top,f=p.bottom,h=p.marginTop,v=p.marginBottom;if(!(s-h>=u||f+v<=u)){var y=m(l,d),E=y.top,C=y.bottom,w=y.marginTop,x=y.marginBottom;if(E-w<u&&C+x>u){u=b-a+E,t++;var P=l.previousElementSibling,R=document.createElement("div");R.style.height="".concat(o,"px"),R.dataset.printerPlaceholder="true",c.insertBefore(R,l),P?(P.style.breakAfter="page",P.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(g,")"),S.dataset.printerClone="true",e.appendChild(S);var k=n.cloneNode(!0);k.style.top="calc(calc(".concat(t+1," * ").concat(g,") - ").concat(a,"px)"),k.dataset.printerClone="true",e.appendChild(k)}}}))}}}));var r=p.format,n=p.formatPage,i=p.formatTotal,c=p.style;return v.current.style.setProperty("--printer-pagination-total","".concat(t+1)),v.current.style.setProperty("--printer-pagination-content","'".concat(r.replaceAll(n,"'counter(printer-pagination-page, ".concat(c,")'")).replaceAll(i,"'counter(printer-pagination-total, ".concat(c,")'")),"'")),C.current=!0,a(),function(){var e,t,r;((null===(e=v.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=v.current)||void 0===t?void 0:t.querySelectorAll('[data-printer-placeholder="true"]'))||[]).forEach((function(e){e.remove()})),((null===(r=v.current)||void 0===r?void 0:r.querySelectorAll('[data-printer-clone="true"]'))||[]).forEach((function(e){e.remove()})),C.current=!1}}a()}}),[d,o,b,p,a]),e.createElement("div",{ref:v,style:{width:Math.floor(E)},"data-printer-type":"".concat(r,"-document"),"data-printer-printonly":!0},i)},y=e.forwardRef((function(t,r){var n=t.title,i=t.header,o=t.footer,a=t.children,c=t.screen,l=t.configuration,u=t.renderOnInit,p=void 0===u||u,s=t.onRender,h=void 0===s?window.print:s,m=d(),g=m.isRendering,y=m.setRendering;e.useEffect((function(){y(p)}),[y,p]),e.useEffect((function(){if(n){var e=document.title;return document.title=n,function(){document.title=e}}}),[n]);var b=e.useCallback((function(){y(!1),setTimeout((function(){return h()}),0)}),[h,y]);return e.useImperativeHandle(r,(function(){return{render:function(){y(!0)}}}),[y]),e.createElement(f,{header:i,footer:o,configuration:l},e.createElement(v,{documentType:"static",onRender:b,isRendering:g},a),e.createElement("div",{"data-printer-screenonly":"true"},e.isValidElement(c)?c:e.createElement(c,{isRendering:g})))})),b=e.forwardRef((function(r,n){var i=r.header,o=r.footer,a=r.children,c=r.configuration,l=r.onRender,u=void 0===l?window.print:l,p=d(),s=p.isRendering,h=p.setRendering,m=e.useState(null),g=m[0],y=m[1];e.useEffect((function(){var e=document.createElement("div");return document.body.appendChild(e),e.dataset.printerType="portal",y(e),function(){document.body.removeChild(e)}}),[]);var b=e.useCallback((function(){h(!1),setTimeout((function(){return u()}),0)}),[u,h]);return e.useImperativeHandle(n,(function(){return{render:function(){setTimeout((function(){return h(!0)}),0)}}}),[h]),g?t.createPortal(e.createElement(f,{header:i,footer:o,configuration:c},e.createElement(v,{documentType:"portal",onRender:b,isRendering:s},a)),g):null})),E={useAsync:!1,size:"a4",orientation:"portrait",pagination:{format:"#p / #t",formatPage:"#p",formatTotal:"#t",style:"decimal"}};exports.Document=y,exports.Page=function(t){var r=t.header,c=t.footer,l=t.children,u=n(),d=u.header,p=u.footer;return e.createElement("article",{"data-printer-article":"page"},e.createElement(o,null,r||d),e.createElement(i,null,c||p),e.createElement(a,null,l))},exports.Pages=function(t){var r=t.header,c=t.footer,l=t.children,u=n(),d=u.header,p=u.footer;return e.createElement("article",{"data-printer-article":"pages"},e.createElement(o,null,r||d),e.createElement(i,null,c||p),e.createElement(a,null,l))},exports.Pagination=function(t){return e.createElement("span",c({},t,{"data-printer-component":"pagination"}))},exports.PortalDocument=b,exports.PrinterProvider=function(t){var r=t.configuration,n=void 0===r?{}:r,i=t.children,o=p(E,n),a=e.useState(!1),c=a[0],l=a[1];return e.createElement(u.Provider,{value:{configuration:o,isRendering:c,setRendering:l}},i)},exports.usePrinter=function(t){var r=d().isRendering,i=n(!0);if(!i||!t)return{isPrinter:!!i,isRendering:r,subscribe:e.useCallback((function(){}),[]),run:e.useCallback((function(){}),[])};var o=i.subscribe,a=i.run,c=i.reset,l=e.useCallback((function(){return o(t)}),[o,t]),u=e.useCallback((function(){setTimeout((function(){return a(t)}),0)}),[a,t]);return e.useEffect((function(){return function(){c(t)}}),[c,t]),{isPrinter:!0,isRendering:r,subscribe:l,run:u}}; //# sourceMappingURL=index.js.map