"use strict";(self.webpackChunkmattermost_webapp=self.webpackChunkmattermost_webapp||[]).push([[5660],{25660:(e,t,s)=>{s.r(t),s.d(t,{default:()=>g});var i=s(62688),n=s.n(i),a=(s(43813),s(29206),s(4290),s(46177)),r=s.n(a),o=s(97792),l=(s(23100),s(36418)),d=s.n(l),c=s(31655),p=s(92600),f=s(53654),h=s(63155);function u(e,t,s){return(t=function(e){var t=function(e){if("object"!=typeof e||!e)return e;var t=e[Symbol.toPrimitive];if(void 0!==t){var s=t.call(e,"string");if("object"!=typeof s)return s;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"==typeof t?t:t+""}(t))in e?Object.defineProperty(e,t,{value:s,enumerable:!0,configurable:!0,writable:!0}):e[t]=s,e}class g extends d().PureComponent{constructor(e){super(e),u(this,"parentNode",null),u(this,"pdfCanvasRef",{}),u(this,"downloadFile",(e=>{const t=this.props.fileInfo.link||(0,c.S1)(this.props.fileInfo.id);e.preventDefault(),window.location.href=t})),u(this,"isInViewport",(e=>{var t,s,i,n;const a=e.getBoundingClientRect(),r=null!==(t=null===(s=this.container.current)||void 0===s?void 0:s.scrollTop)&&void 0!==t?t:0,o=r+(null!==(i=null===(n=this.parentNode)||void 0===n?void 0:n.clientHeight)&&void 0!==i?i:0);return a.top>=r&&a.top<=o||a.bottom>=r&&a.bottom<=o||a.top<=r&&a.bottom>=o})),u(this,"renderPDFPage",(async e=>{const t=this.pdfCanvasRef["pdfCanvasRef-".concat(e)].current;if(!t)return;if(e>=3&&!this.isInViewport(t))return;if(this.pdfPagesRendered[e])return;const s=await this.loadPage(this.state.pdf,e),i=t.getContext("2d"),n=s.getViewport({scale:this.props.scale});t.height=n.height,t.width=n.width;const a={canvasContext:i,viewport:n};await s.render(a).promise,this.pdfPagesRendered[e]=!0})),u(this,"getPdfDocument",(async()=>{try{const e=await o.YE({url:this.props.fileUrl,cMapUrl:(0,h.vV)()+"/static/cmaps/",cMapPacked:!0}).promise;this.onDocumentLoad(e)}catch(e){this.onDocumentLoadError(e)}})),u(this,"onDocumentLoad",(e=>{this.setState({pdf:e,numPages:e.numPages});for(let t=0;t<e.numPages;t++)this.pdfCanvasRef["pdfCanvasRef-".concat(t)]=d().createRef();this.setState({loading:!1,success:!0})})),u(this,"onDocumentLoadError",(e=>{console.log("Unable to load PDF preview: "+e),this.setState({loading:!1,success:!1})})),u(this,"loadPage",(async(e,t)=>{if(this.state.pdfPagesLoaded[t])return this.state.pdfPages[t];const s=await e.getPage(t+1),i=Object.assign({},this.state.pdfPages);i[t]=s;const n=Object.assign({},this.state.pdfPagesLoaded);return n[t]=!0,this.setState({pdfPages:i,pdfPagesLoaded:n}),s})),u(this,"handleScroll",r()((()=>{if(this.state.success)for(let e=0;e<this.state.numPages;e++)this.renderPDFPage(e)}),100)),this.pdfPagesRendered={},this.container=d().createRef(),this.state={pdf:null,pdfPages:[],pdfPagesLoaded:{},numPages:0,loading:!0,success:!1,prevFileUrl:""}}componentDidMount(){var e;this.getPdfDocument(),this.container.current&&(this.parentNode=this.container.current.parentElement,null===(e=this.parentNode)||void 0===e||e.addEventListener("scroll",this.handleScroll))}componentWillUnmount(){this.parentNode&&this.parentNode.removeEventListener("scroll",this.handleScroll)}static getDerivedStateFromProps(e,t){return e.fileUrl!==t.prevFileUrl?{pdf:null,pdfPages:{},pdfPagesLoaded:{},numPages:0,loading:!0,success:!1,prevFileUrl:e.fileUrl}:null}componentDidUpdate(e,t){if(this.props.fileUrl!==e.fileUrl&&(this.getPdfDocument(),this.pdfPagesRendered={}),this.props.scale!==e.scale&&(this.pdfPagesRendered={},this.state.success))for(let e=0;e<this.state.numPages;e++)this.renderPDFPage(e);if(!t.success&&this.state.success)for(let e=0;e<this.state.numPages;e++)this.renderPDFPage(e)}render(){if(this.state.loading)return d().createElement("div",{ref:this.container,className:"view-image__loading"},d().createElement(f.A,null));if(!this.state.success)return d().createElement(p.A,{fileInfo:this.props.fileInfo,fileUrl:this.props.fileUrl});const e=[];for(let t=0;t<this.state.numPages;t++)e.push(d().createElement("canvas",{ref:this.pdfCanvasRef["pdfCanvasRef-".concat(t)],key:"previewpdfcanvas"+t})),t<this.state.numPages-1&&this.state.numPages>1&&e.push(d().createElement("div",{key:"previewpdfspacer"+t,className:"pdf-preview-spacer"}));return d().createElement("div",{ref:this.container,className:"post-code",onClick:this.props.handleBgClose},e)}}u(g,"propTypes",{fileUrl:n().string.isRequired,scale:n().number.isRequired,handleBgClose:n().func.isRequired})}}]);
//# sourceMappingURL=5660.2b54aa680a48301089e4.js.map