(self.webpackChunkmattermost_webapp=self.webpackChunkmattermost_webapp||[]).push([[271],{78271:(e,t,s)=>{"use strict";s.r(t),s.d(t,{default:()=>u}),s(19601),s(88674);var n=s(45697),a=s.n(n),i=s(67294),r=s(99299),o=s.n(r),c=s(23279),d=s.n(c),l=s(3293),p=s(56640),h=s(80463);function f(e,t,s){return t in e?Object.defineProperty(e,t,{value:s,enumerable:!0,configurable:!0,writable:!0}):e[t]=s,e}class u extends i.PureComponent{constructor(e){super(e),f(this,"downloadFile",(e=>{const t=this.props.fileInfo.link||(0,l.getFileDownloadUrl)(this.props.fileInfo.id);e.preventDefault(),window.location.href=t})),f(this,"isInViewport",(e=>{const t=e.getBoundingClientRect(),s=this.container.current.scrollTop,n=s+this.container.current.parentElement.clientHeight;return t.top>=s&&t.top<=n||t.bottom>=s&&t.bottom<=n||t.top<=s&&t.bottom>=n})),f(this,"renderPDFPage",(async e=>{const t=this["pdfCanvasRef-".concat(e)].current;if(e>=3&&!this.isInViewport(t))return;if(this.pdfPagesRendered[e])return;await this.loadPage(this.state.pdf,e);const s=this.state.pdfPages[e],n=t.getContext("2d"),a=s.getViewport(this.props.scale);this["pdfCanvasRef-".concat(e)].current.height=a.height,this["pdfCanvasRef-".concat(e)].current.width=a.width;const i={canvasContext:n,viewport:a};s.render(i),this.pdfPagesRendered[e]=!0})),f(this,"getPdfDocument",(()=>{o().getDocument(this.props.fileUrl).then(this.onDocumentLoad).catch(this.onDocumentLoadError)})),f(this,"onDocumentLoad",(e=>{this.setState({pdf:e,numPages:e.numPages});for(let t=0;t<e.numPages;t++)this["pdfCanvasRef-".concat(t)]=i.createRef();this.setState({loading:!1,success:!0})})),f(this,"onDocumentLoadError",(e=>{console.log("Unable to load PDF preview: "+e),this.setState({loading:!1,success:!1})})),f(this,"loadPage",(async(e,t)=>{if(this.state.pdfPagesLoaded[t])return this.state.pdfPages[t];const s=await e.getPage(t+1),n=Object.assign({},this.state.pdfPages);n[s.pageIndex]=s;const a=Object.assign({},this.state.pdfPagesLoaded);return a[s.pageIndex]=!0,this.setState({pdfPages:n,pdfPagesLoaded:a}),s})),f(this,"handleScroll",d()((()=>{if(this.state.success)for(let e=0;e<this.state.numPages;e++)this.renderPDFPage(e)}),100)),this.pdfPagesRendered={},this.container=i.createRef(),this.state={pdf:null,pdfPages:{},pdfPagesLoaded:{},numPages:0,loading:!0,success:!1}}componentDidMount(){this.getPdfDocument(),this.container.current&&(this.parentNode=this.container.current.parentElement.parentElement,this.parentNode.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 i.createElement("div",{ref:this.container,className:"view-image__loading"},i.createElement(p.Z,null));if(!this.state.success)return i.createElement(h.Z,{fileInfo:this.props.fileInfo,fileUrl:this.props.fileUrl});const e=[];for(let t=0;t<this.state.numPages;t++)e.push(i.createElement("canvas",{ref:this["pdfCanvasRef-".concat(t)],key:"previewpdfcanvas"+t})),t<this.state.numPages-1&&this.state.numPages>1&&e.push(i.createElement("div",{key:"previewpdfspacer"+t,className:"pdf-preview-spacer"}));return i.createElement("div",{ref:this.container,className:"post-code"},e)}}f(u,"propTypes",{fileInfo:a().object.isRequired,fileUrl:a().string.isRequired,scale:a().number.isRequired})}}]);
//# sourceMappingURL=271.7760e96336b4cd3aa124.js.map