UNPKG

@chief-editor/ui

Version:

UI Component for chief editor

1 lines 1.77 kB
import{guid}from"@co-hooks/util";import{FileStatus,FileType}from"../types/enum";var FileClass=function(){function t(t){this.previewUrl="",this.fileStatus=FileStatus.INIT,this.errmsg="";var e=t.id,i=t.name,r=t.data,s=t.url,p=t.fileType,o=t.file;this.id=e||guid(),this.name=i||"",this.data=r,this.url=s||"",this.fileType=p||FileType.FILE,this.progress=100,s&&(this.fileStatus=FileStatus.DONE),o&&(this.originFile=o,this.file=o,this.name=this.getName(o.name),this.progress=0,this.isImageUrl(o)&&(p||(this.fileType=FileType.IMAGE),this.fileType===FileType.IMAGE&&(this.previewUrl=window.URL.createObjectURL(o))))}return t.prototype.updateFile=function(t){for(var e in t.file&&this.file!==t.file&&(this.previewUrl=window.URL.createObjectURL(t.file)),t)Object.prototype.hasOwnProperty.call(t,e)&&(this[e]=t[e]);return this},t.prototype.getFileName=function(){return this.name?this.name:this.url},t.prototype.setStatus=function(t){this.fileStatus=t},t.prototype.getProgress=function(){return this.progress},t.prototype.setProgress=function(t){this.progress=t},t.prototype.getName=function(t){return t.split("/").pop()||""},t.prototype.getExtName=function(t){if(!t)return"";var e=(t.split("/").pop()||"").split(/[?#]/)[0];return(/\.[^./\\]*$/.exec(e)||[""])[0]},t.prototype.isImageUrl=function(t){if(0===t.type.indexOf("image/"))return!0;var e=t.name,i=this.getExtName(e);return!(!/^data:image\//.test(e)&&!/(webp|svg|png|gif|jpg|jpeg|bmp|dpg)$/i.test(i))||!/^data:/.test(e)&&!i},t.prototype.getBaseFile=function(){return{id:this.id,name:this.name,url:this.url,fileType:this.fileType,fileStatus:this.fileStatus,data:this.data,file:this.file}},t.prototype.destroy=function(){this.previewUrl&&(window.URL.revokeObjectURL(this.previewUrl),this.previewUrl="")},t}();export{FileClass};