amis-playground
Version:
amis-playground 是一个amis在线代码编辑器可实时预览运行效果
2 lines (1 loc) • 5.29 kB
JavaScript
import{R as d,_ as E,a as F,u as b,r as v,ap as y,j as w,k as _,f as l,p as u,q as a,s as P,h as S,e5 as x,ch as R}from"./index-7c288cb3.js";var V=d.lazy(function(){return E(()=>import("./PdfViewer-19789a2d.js"),["./PdfViewer-19789a2d.js","./index-7c288cb3.js","./index-5b9dbdb6.css"],import.meta.url)}),j=function(f){F(t,f);function t(e){var n=f.call(this,e)||this;return n.wrapper=d.createRef(),n.state={inited:!1,loading:!1,error:!1},n}return t.prototype.componentDidMount=function(){this.wrapper.current&&this.setState({width:this.wrapper.current.clientWidth-100}),this.renderPdf()},t.prototype.componentDidUpdate=function(e){var n=this,r=this.props;(b(e.src,r.src,e.data,r.data)||v(r.src,r.data,"| raw")!==v(e.src,e.data,"| raw"))&&(this.abortLoad(),setTimeout(function(){n.fetchPdf()},0)),y(r.data,r.name)&&y(e.data,e.name)!==y(r.data,r.name)&&(this.abortLoad(),this.renderPdf())},t.prototype.componentWillUnmount=function(){this.abortLoad()},t.prototype.abortLoad=function(){this.fetchCancel&&(this.fetchCancel("load canceled"),this.fetchCancel=void 0),this.reader&&(this.reader.abort(),this.reader=void 0)},t.prototype.renderPdf=function(){return w(this,void 0,void 0,function(){var e,n,r,s;return _(this,function(i){switch(i.label){case 0:return e=this.props,n=e.src,r=e.name,s=e.data,this.setState({error:!1}),n?this.file?[3,2]:[4,this.fetchPdf()]:[3,3];case 1:i.sent(),i.label=2;case 2:return[3,5];case 3:return y(s,r)?[4,this.renderFormFile()]:[3,5];case 4:i.sent(),i.label=5;case 5:return[2]}})})},t.prototype.fetchPdf=function(){return w(this,void 0,void 0,function(){var e,n,r,s,i,o,c,h,m=this;return _(this,function(p){switch(p.label){case 0:if(e=this.props,n=e.env,r=e.src,s=e.data,e.translate,r&&(o=v(r,s,"| raw"),typeof o=="string"?i=o:typeof o=="object"&&typeof o.value=="string"&&(i=o.value)),!i)return console.warn("file src is empty"),[2];this.setState({inited:!0,loading:!0,error:!1}),p.label=1;case 1:return p.trys.push([1,3,4,5]),[4,n.fetcher(i,s,{responseType:"arraybuffer",cancelExecutor:function(g){return m.fetchCancel=g}})];case 2:return c=p.sent(),this.file=c.data,this.forceUpdate(),[3,5];case 3:return h=p.sent(),this.setState({error:!0}),console.error(h),[3,5];case 4:return this.setState({loading:!1}),[7];case 5:return[2]}})})},t.prototype.renderFormFile=function(){return w(this,void 0,void 0,function(){var e,n,r,s,i,o=this;return _(this,function(c){return e=this.props,n=e.name,r=e.data,s=y(r,n),this.setState({inited:!0,loading:!0}),s instanceof File&&(i=new FileReader,i.onload=function(h){var m=i.result;o.file=m,o.setState({loading:!1}),o.forceUpdate()},i.onerror=function(h){o.setState({error:!0})},i.readAsArrayBuffer(s),this.reader=i),[2]})})},t.prototype.renderEmpty=function(){var e=this.props,n=e.src,r=e.name;return!n&&!r?d.createElement("svg",{width:"100%",height:"100",xmlns:"http://www.w3.org/2000/svg"},d.createElement("rect",{x:"0",y:"0",width:"100%",height:"100",style:{fill:"#F7F7F9"}}),d.createElement("text",{x:"50%",y:"50%",fontSize:"18",textAnchor:"middle",alignmentBaseline:"middle",fontFamily:"monospace, sans-serif",fill:"#555555"},"PDF viewer")):null},t.prototype.renderError=function(){var e=this.props,n=e.src,r=e.translate,s=this.state.error;return s&&n?d.createElement("div",null,r("loadingFailed")+" url:"+n):null},t.prototype.renderTip=function(){return d.createElement("div",null,d.createElement("p",null,"[PdfViewer]: pdfjsWorkerSrc is required, Please set the `pdfjsWorkerSrc` in setGlobalOptions function."))},t.prototype.render=function(){var e=this.props,n=e.className,r=e.classnames;e.translate;var s=e.height,i=e.background,o=x().pdfjsWorkerSrc,c=this.state,h=c.loading,m=c.inited,p=c.error,g=Math.max(this.props.width||this.state.width,300);return d.createElement("div",{ref:this.wrapper},this.renderEmpty(),o?d.createElement(R.Suspense,{fallback:d.createElement("div",null,"...")},m&&!p?d.createElement(V,{file:this.file,loading:h,className:n,classnames:r,width:g,height:s,background:i}):null):this.renderTip(),this.renderError())},l([u,a("design:type",Function),a("design:paramtypes",[]),a("design:returntype",void 0)],t.prototype,"abortLoad",null),l([u,a("design:type",Function),a("design:paramtypes",[]),a("design:returntype",Promise)],t.prototype,"renderPdf",null),l([u,a("design:type",Function),a("design:paramtypes",[]),a("design:returntype",Promise)],t.prototype,"fetchPdf",null),l([u,a("design:type",Function),a("design:paramtypes",[]),a("design:returntype",Promise)],t.prototype,"renderFormFile",null),l([u,a("design:type",Function),a("design:paramtypes",[]),a("design:returntype",void 0)],t.prototype,"renderEmpty",null),l([u,a("design:type",Function),a("design:paramtypes",[]),a("design:returntype",void 0)],t.prototype,"renderError",null),l([u,a("design:type",Function),a("design:paramtypes",[]),a("design:returntype",void 0)],t.prototype,"renderTip",null),t}(d.Component),C=function(f){F(t,f);function t(e,n){var r=f.call(this,e)||this,s=n;return s.registerComponent(r),r}return t.prototype.componentWillUnmount=function(){var e;(e=f.prototype.componentWillUnmount)===null||e===void 0||e.call(this);var n=this.context;n.unRegisterComponent(this)},t.contextType=P,t=l([S({type:"pdf-viewer"}),a("design:paramtypes",[Object,Object])],t),t}(j);export{V as PdfView,C as PdfViewerRenderer,j as default};