"use strict";(self.webpackChunk_mattermost_webapp=self.webpackChunk_mattermost_webapp||[]).push([[271],{78271:(e,t,s)=>{s.r(t),s.d(t,{default:()=>p}),s(88674),s(33948),s(19601);var n=s(45697),a=s.n(n),i=s(67294),r=s(23279),o=s.n(r),c=s(76960),d=s(56640),l=s(80463);function h(e,t,s){return t in e?Object.defineProperty(e,t,{value:s,enumerable:!0,configurable:!0,writable:!0}):e[t]=s,e}class p extends i.PureComponent{constructor(e){super(e),h(this,"downloadFile",(e=>{const t=this.props.fileInfo.link||(0,c.gN)(this.props.fileInfo.id);e.preventDefault(),window.location.href=t})),h(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})),h(this,"renderPDFPage",(async e=>{const t=this["pdfCanvasRef-".concat(e)].current;if(!t)return;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})),h(this,"getPdfDocument",(()=>{Promise.all([s.e(299),s.e(143)]).then(s.t.bind(s,99299,23)).then((e=>{e.disableWorker=!0,e.getDocument(this.props.fileUrl).then(this.onDocumentLoad).catch(this.onDocumentLoadError)}))})),h(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})})),h(this,"onDocumentLoadError",(e=>{console.log("Unable to load PDF preview: "+e),this.setState({loading:!1,success:!1})})),h(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})),h(this,"handleScroll",o()((()=>{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(d.Z,null));if(!this.state.success)return i.createElement(l.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",onClick:this.props.handleBgClose},e)}}h(p,"propTypes",{fileInfo:a().object.isRequired,fileUrl:a().string.isRequired,scale:a().number.isRequired,handleBgClose:a().func.isRequired})}}]);
//# sourceMappingURL=271.f265a942df6ae63507d6.js.map