@idraw/renderer 0.3.0-beta.2 → 0.3.0-beta.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/esm/index.d.ts +11 -12
- package/dist/esm/index.js +48 -50
- package/dist/esm/lib/calculate.d.ts +3 -3
- package/dist/esm/lib/calculate.js +5 -5
- package/dist/esm/lib/diff.d.ts +6 -6
- package/dist/esm/lib/diff.js +9 -7
- package/dist/esm/lib/draw/base.d.ts +5 -5
- package/dist/esm/lib/draw/circle.d.ts +2 -2
- package/dist/esm/lib/draw/circle.js +1 -1
- package/dist/esm/lib/draw/html.d.ts +2 -2
- package/dist/esm/lib/draw/image.d.ts +2 -2
- package/dist/esm/lib/draw/index.d.ts +2 -2
- package/dist/esm/lib/draw/rect.d.ts +2 -2
- package/dist/esm/lib/draw/svg.d.ts +2 -2
- package/dist/esm/lib/draw/text.d.ts +2 -2
- package/dist/esm/lib/loader-event.d.ts +5 -5
- package/dist/esm/lib/loader.d.ts +2 -2
- package/dist/esm/lib/loader.js +22 -14
- package/dist/esm/lib/parse.d.ts +2 -2
- package/dist/esm/lib/renderer-event.d.ts +8 -8
- package/dist/esm/lib/temp.d.ts +4 -4
- package/dist/esm/lib/temp.js +1 -1
- package/dist/esm/lib/transform.d.ts +4 -4
- package/dist/esm/lib/transform.js +1 -1
- package/dist/index.global.js +376 -284
- package/dist/index.global.min.js +1 -1
- package/package.json +4 -4
- package/dist/esm/names.d.ts +0 -9
- package/dist/esm/names.js +0 -9
package/dist/index.global.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var iDrawRenderer=function(){"use strict";function t(t){return"string"==typeof t&&/^\#([0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})$/i.test(t)}function e(t){return function t(e){const i=(s=e,Object.prototype.toString.call(s).replace(/[\]|\[]{1,1}/gi,"").split(" ")[1]);var s;if(["Null","Number","String","Boolean","Undefined"].indexOf(i)>=0)return e;if("Array"===i){const i=[];return e.forEach((e=>{i.push(t(e))})),i}if("Object"===i){const i={};return Object.keys(e).forEach((s=>{i[s]=t(e[s])})),i}}(t)}function i(t){return(Object.prototype.toString.call(t)||"").replace(/(\[object|\])/gi,"").trim()}const s={type(t,e){const s=i(t);return!0===e?s.toLocaleLowerCase():s},array:t=>"Array"===i(t),json:t=>"Object"===i(t),function:t=>"Function"===i(t),asyncFunction:t=>"AsyncFunction"===i(t),string:t=>"String"===i(t),number:t=>"Number"===i(t),undefined:t=>"Undefined"===i(t),null:t=>"Null"===i(t),promise:t=>"Promise"===i(t)};var o=globalThis&&globalThis.__awaiter||function(t,e,i,s){return new(i||(i=Promise))((function(o,r){function n(t){try{h(s.next(t))}catch(t){r(t)}}function a(t){try{h(s.throw(t))}catch(t){r(t)}}function h(t){var e;t.done?o(t.value):(e=t.value,e instanceof i?e:new i((function(t){t(e)}))).then(n,a)}h((s=s.apply(t,e||[])).next())}))};const{Image:r}=window;function n(t){return new Promise(((e,i)=>{const s=new r;s.crossOrigin="anonymous",s.onload=function(){e(s)},s.onabort=i,s.onerror=i,s.src=t}))}function a(t){return o(this,void 0,void 0,(function*(){const e=yield function(t){return new Promise(((e,i)=>{const s=new Blob([t],{type:"image/svg+xml;charset=utf-8"}),o=new FileReader;o.readAsDataURL(s),o.onload=function(t){var i;const s=null===(i=null==t?void 0:t.target)||void 0===i?void 0:i.result;e(s)},o.onerror=function(t){i(t)}}))}(t);return yield n(e)}))}function h(t,e){return o(this,void 0,void 0,(function*(){t=t.replace(/\&/gi,"&");const i=yield function(t,e){const{width:i,height:s}=e;return new Promise(((e,o)=>{const r=new Blob([`\n <svg xmlns="http://www.w3.org/2000/svg" width="${i||""}" height = "${s||""}">\n <foreignObject width="100%" height="100%">\n <div xmlns = "http://www.w3.org/1999/xhtml">\n ${t}\n </div>\n </foreignObject>\n </svg>\n `],{type:"image/svg+xml;charset=utf-8"}),n=new FileReader;n.readAsDataURL(r),n.onload=function(t){var i;const s=null===(i=null==t?void 0:t.target)||void 0===i?void 0:i.result;e(s)},n.onerror=function(t){o(t)}}))}(t,e);return yield n(i)}))}class l{constructor(t,e){this._opts=e,this._ctx=t,this._transform={scale:1,scrollX:0,scrollY:0}}getContext(){return this._ctx}resetSize(t){this._opts=Object.assign(Object.assign({},this._opts),t)}calcDeviceNum(t){return t*this._opts.devicePixelRatio}calcScreenNum(t){return t/this._opts.devicePixelRatio}getSize(){return{width:this._opts.width,height:this._opts.height,contextWidth:this._opts.contextWidth,contextHeight:this._opts.contextHeight,devicePixelRatio:this._opts.devicePixelRatio}}setTransform(t){this._transform=Object.assign(Object.assign({},this._transform),t)}getTransform(){return{scale:this._transform.scale,scrollX:this._transform.scrollX,scrollY:this._transform.scrollY}}setFillStyle(t){this._ctx.fillStyle=t}fill(t){return this._ctx.fill(t||"nonzero")}arc(t,e,i,s,o,r){return this._ctx.arc(this._doSize(t),this._doSize(e),this._doSize(i),s,o,r)}rect(t,e,i,s){return this._ctx.rect(this._doSize(t),this._doSize(e),this._doSize(i),this._doSize(s))}fillRect(t,e,i,s){return this._ctx.fillRect(this._doSize(t),this._doSize(e),this._doSize(i),this._doSize(s))}clearRect(t,e,i,s){return this._ctx.clearRect(this._doSize(t),this._doSize(e),this._doSize(i),this._doSize(s))}beginPath(){return this._ctx.beginPath()}closePath(){return this._ctx.closePath()}lineTo(t,e){return this._ctx.lineTo(this._doSize(t),this._doSize(e))}moveTo(t,e){return this._ctx.moveTo(this._doSize(t),this._doSize(e))}arcTo(t,e,i,s,o){return this._ctx.arcTo(this._doSize(t),this._doSize(e),this._doSize(i),this._doSize(s),this._doSize(o))}setLineWidth(t){return this._ctx.lineWidth=this._doSize(t)}setLineDash(t){return this._ctx.setLineDash(t.map((t=>this._doSize(t))))}isPointInPath(t,e){return this._ctx.isPointInPath(this._doX(t),this._doY(e))}isPointInPathWithoutScroll(t,e){return this._ctx.isPointInPath(this._doSize(t),this._doSize(e))}setStrokeStyle(t){this._ctx.strokeStyle=t}stroke(){return this._ctx.stroke()}translate(t,e){return this._ctx.translate(this._doSize(t),this._doSize(e))}rotate(t){return this._ctx.rotate(t)}drawImage(...t){const e=t[0],i=t[1],s=t[2],o=t[3],r=t[4],n=t[t.length-4],a=t[t.length-3],h=t[t.length-2],l=t[t.length-1];return 9===t.length?this._ctx.drawImage(e,this._doSize(i),this._doSize(s),this._doSize(o),this._doSize(r),this._doSize(n),this._doSize(a),this._doSize(h),this._doSize(l)):this._ctx.drawImage(e,this._doSize(n),this._doSize(a),this._doSize(h),this._doSize(l))}createPattern(t,e){return this._ctx.createPattern(t,e)}measureText(t){return this._ctx.measureText(t)}setTextAlign(t){this._ctx.textAlign=t}fillText(t,e,i,s){return void 0!==s?this._ctx.fillText(t,this._doSize(e),this._doSize(i),this._doSize(s)):this._ctx.fillText(t,this._doSize(e),this._doSize(i))}strokeText(t,e,i,s){return void 0!==s?this._ctx.strokeText(t,this._doSize(e),this._doSize(i),this._doSize(s)):this._ctx.strokeText(t,this._doSize(e),this._doSize(i))}setFont(t){const e=[];"bold"===t.fontWeight&&e.push(`${t.fontWeight}`),e.push(`${this._doSize(t.fontSize||12)}px`),e.push(`${t.fontFamily||"sans-serif"}`),this._ctx.font=`${e.join(" ")}`}setTextBaseline(t){this._ctx.textBaseline=t}setGlobalAlpha(t){this._ctx.globalAlpha=t}save(){this._ctx.save()}restore(){this._ctx.restore()}scale(t,e){this._ctx.scale(t,e)}setShadowColor(t){this._ctx.shadowColor=t}setShadowOffsetX(t){this._ctx.shadowOffsetX=this._doSize(t)}setShadowOffsetY(t){this._ctx.shadowOffsetY=this._doSize(t)}setShadowBlur(t){this._ctx.shadowBlur=this._doSize(t)}ellipse(t,e,i,s,o,r,n,a){this._ctx.ellipse(this._doSize(t),this._doSize(e),this._doSize(i),this._doSize(s),o,r,n,a)}_doSize(t){return this._opts.devicePixelRatio*t}_doX(t){const{scale:e,scrollX:i}=this._transform,s=(t-i)/e;return this._doSize(s)}_doY(t){const{scale:e,scrollY:i}=this._transform,s=(t-i)/e;return this._doSize(s)}}function c(t){return"number"==typeof t&&(t>0||t<=0)}function u(t){return"number"==typeof t&&t>=0}function d(t){return"string"==typeof t&&/^(http:\/\/|https:\/\/|\.\/|\/)/.test(`${t}`)}function f(t){return"string"==typeof t&&/^(data:image\/)/.test(`${t}`)}const _={x:function(t){return c(t)},y:function(t){return c(t)},w:u,h:function(t){return"number"==typeof t&&t>=0},angle:function(t){return"number"==typeof t&&t>=-360&&t<=360},number:c,borderWidth:function(t){return u(t)},borderRadius:function(t){return c(t)&&t>=0},color:function(e){return t(e)},imageSrc:function(t){return f(t)||d(t)},imageURL:d,imageBase64:f,svg:function(t){return"string"==typeof t&&/^(<svg[\s]{1,}|<svg>)/i.test(`${t}`.trim())&&/<\/[\s]{0,}svg>$/i.test(`${t}`.trim())},html:function(t){let e=!1;if("string"==typeof t){let i=document.createElement("div");i.innerHTML=t,i.children.length>0&&(e=!0),i=null}return e},text:function(t){return"string"==typeof t},fontSize:function(t){return c(t)&&t>0},lineHeight:function(t){return c(t)&&t>0},textAlign:function(t){return["center","left","right"].includes(t)},fontFamily:function(t){return"string"==typeof t&&t.length>0},fontWeight:function(t){return["bold"].includes(t)},strokeWidth:function(t){return c(t)&&t>0}};function g(t,e,i){const s=function(t){return{x:t.x+t.w/2,y:t.y+t.h/2}}(e);return function(t,e,i,s){e&&(i>0||i<0)&&(t.translate(e.x,e.y),t.rotate(i),t.translate(-e.x,-e.y));s(t),e&&(i>0||i<0)&&(t.translate(e.x,e.y),t.rotate(-i),t.translate(-e.x,-e.y))}(t,s,(e.angle||0)/180*Math.PI||0,i)}function m(t){t.setFillStyle("#000000"),t.setStrokeStyle("#000000"),t.setLineDash([]),t.setGlobalAlpha(1),t.setShadowColor("#00000000"),t.setShadowOffsetX(0),t.setShadowOffsetY(0),t.setShadowBlur(0)}function x(e,i,o){m(e),function(e,i){m(e),g(e,i,(()=>{if(!(i.desc.borderWidth&&i.desc.borderWidth>0))return;const s=i.desc.borderWidth;let o="#000000";!0===t(i.desc.borderColor)&&(o=i.desc.borderColor);const r=i.x-s/2,n=i.y-s/2,a=i.w+s,h=i.h+s;let l=i.desc.borderRadius||0;l=Math.min(l,a/2,h/2),l<a/2&&l<h/2&&(l+=s/2);const{desc:c}=i;void 0!==c.shadowColor&&t(c.shadowColor)&&e.setShadowColor(c.shadowColor),void 0!==c.shadowOffsetX&&_.number(c.shadowOffsetX)&&e.setShadowOffsetX(c.shadowOffsetX),void 0!==c.shadowOffsetY&&_.number(c.shadowOffsetY)&&e.setShadowOffsetY(c.shadowOffsetY),void 0!==c.shadowBlur&&_.number(c.shadowBlur)&&e.setShadowBlur(c.shadowBlur),e.beginPath(),e.setLineWidth(s),e.setStrokeStyle(o),e.moveTo(r+l,n),e.arcTo(r+a,n,r+a,n+h,l),e.arcTo(r+a,n+h,r,n+h,l),e.arcTo(r,n+h,r,n,l),e.arcTo(r,n,r+a,n,l),e.closePath(),e.stroke()}))}(e,i),m(e),g(e,i,(()=>{const{x:t,y:r,w:n,h:a}=i;let h=i.desc.borderRadius||0;h=Math.min(h,n/2,a/2),(n<2*h||a<2*h)&&(h=0),e.beginPath(),e.moveTo(t+h,r),e.arcTo(t+n,r,t+n,r+a,h),e.arcTo(t+n,r+a,t,r+a,h),e.arcTo(t,r+a,t,r,h),e.arcTo(t,r,t+n,r,h),e.closePath(),("string"==typeof o||["CanvasPattern"].includes(s.type(o)))&&e.setFillStyle(o),e.fill()}))}function S(t,e){x(t,e,e.desc.bgColor)}function w(t,e,i){const s=i.getContent(e.uuid);g(t,e,(()=>{s&&t.drawImage(s,e.x,e.y,e.w,e.h)}))}function p(t,e,i){const s=i.getContent(e.uuid);g(t,e,(()=>{s&&t.drawImage(s,e.x,e.y,e.w,e.h)}))}function y(t,e,i){const s=i.getContent(e.uuid);g(t,e,(()=>{s&&t.drawImage(s,e.x,e.y,e.w,e.h)}))}function v(e,i,s){m(e),x(e,i,i.desc.bgColor||"transparent"),g(e,i,(()=>{const s={fontSize:12,fontFamily:"sans-serif",textAlign:"center",...i.desc};e.setFillStyle(i.desc.color),e.setTextBaseline("top"),e.setFont({fontWeight:s.fontWeight,fontSize:s.fontSize,fontFamily:s.fontFamily});const o=s.text.replace(/\r\n/gi,"\n"),r=s.lineHeight||s.fontSize,n=o.split("\n"),a=[];let h=0;n.forEach(((t,s)=>{let o="";if(t.length>0){for(let l=0;l<t.length&&(e.measureText(o+(t[l]||"")).width<e.calcDeviceNum(i.w)?o+=t[l]||"":(a.push({text:o,width:e.calcScreenNum(e.measureText(o).width)}),o=t[l]||"",h++),!((h+1)*r>i.h));l++)if(t.length-1===l&&(h+1)*r<i.h){a.push({text:o,width:e.calcScreenNum(e.measureText(o).width)}),s<n.length-1&&h++;break}}else a.push({text:"",width:0})}));let l=0;a.length*r<i.h&&("top"===i.desc.verticalAlign?l=0:"bottom"===i.desc.verticalAlign?l+=i.h-a.length*r:l+=(i.h-a.length*r)/2);{const o=i.y+l;void 0!==s.textShadowColor&&t(s.textShadowColor)&&e.setShadowColor(s.textShadowColor),void 0!==s.textShadowOffsetX&&_.number(s.textShadowOffsetX)&&e.setShadowOffsetX(s.textShadowOffsetX),void 0!==s.textShadowOffsetY&&_.number(s.textShadowOffsetY)&&e.setShadowOffsetY(s.textShadowOffsetY),void 0!==s.textShadowBlur&&_.number(s.textShadowBlur)&&e.setShadowBlur(s.textShadowBlur),a.forEach(((t,n)=>{let a=i.x;"center"===s.textAlign?a=i.x+(i.w-t.width)/2:"right"===s.textAlign&&(a=i.x+(i.w-t.width)),e.fillText(t.text,a,o+r*n)})),m(e)}if(t(s.strokeColor)&&void 0!==s.strokeWidth&&s.strokeWidth>0){const t=i.y+l;a.forEach(((o,n)=>{let a=i.x;"center"===s.textAlign?a=i.x+(i.w-o.width)/2:"right"===s.textAlign&&(a=i.x+(i.w-o.width)),void 0!==s.strokeColor&&e.setStrokeStyle(s.strokeColor),void 0!==s.strokeWidth&&s.strokeWidth>0&&e.setLineWidth(s.strokeWidth),e.strokeText(o.text,a,t+r*n)}))}}))}function b(t,e){m(t),g(t,e,(t=>{const{x:i,y:s,w:o,h:r,desc:n}=e,{bgColor:a="#000000",borderColor:h="#000000",borderWidth:l=0}=n,c=o/2,u=r/2,d=i+c,f=s+u;if(l&&l>0){const e=l/2+c,i=l/2+u;t.beginPath(),t.setStrokeStyle(h),t.setLineWidth(l),t.ellipse(d,f,e,i,0,0,2*Math.PI),t.closePath(),t.stroke()}t.beginPath(),t.setFillStyle(a),t.ellipse(d,f,c,u,0,0,2*Math.PI),t.closePath(),t.fill()}))}function z(e,i,s){var o;m(e);const r=e.getSize();if(e.clearRect(0,0,r.contextWidth,r.contextHeight),"string"==typeof i.bgColor&&t(i.bgColor)&&function(t,e){const i=t.getSize();t.setFillStyle(e),t.fillRect(0,0,i.contextWidth,i.contextHeight)}(e,i.bgColor),i.elements.length>0)for(let t=0;t<i.elements.length;t++){const r=i.elements[t];if(!0!==(null==(o=null==r?void 0:r.operation)?void 0:o.invisible))switch(r.type){case"rect":S(e,r);break;case"text":v(e,r);break;case"image":w(e,r,s);break;case"svg":p(e,r,s);break;case"html":y(e,r,s);break;case"circle":b(e,r)}}}class L{constructor(){this._listeners=new Map}on(t,e){if(this._listeners.has(t)){const i=this._listeners.get(t);null==i||i.push(e),this._listeners.set(t,i||[])}else this._listeners.set(t,[e])}off(t,e){if(this._listeners.has(t)){const i=this._listeners.get(t);if(Array.isArray(i))for(let t=0;t<(null==i?void 0:i.length);t++)if(i[t]===e){i.splice(t,1);break}this._listeners.set(t,i||[])}}trigger(t,e){const i=this._listeners.get(t);return!!Array.isArray(i)&&(i.forEach((t=>{t(e)})),!0)}has(t){if(this._listeners.has(t)){const e=this._listeners.get(t);if(Array.isArray(e)&&e.length>0)return!0}return!1}}class D{constructor(t){this._currentLoadData={},this._currentUUIDQueue=[],this._storageLoadData={},this._status="free",this._waitingLoadQueue=[],this._opts=t,this._event=new L,this._waitingLoadQueue=[]}load(t,e){const[i,s]=this._resetLoadData(t,e);"free"===this._status||"complete"===this._status?(this._currentUUIDQueue=i,this._currentLoadData=s,this._loadTask()):"loading"===this._status&&i.length>0&&this._waitingLoadQueue.push({uuidQueue:i,loadData:s})}on(t,e){this._event.on(t,e)}off(t,e){this._event.off(t,e)}isComplete(){return"complete"===this._status}getContent(t){var e;return"loaded"===(null==(e=this._storageLoadData[t])?void 0:e.status)?this._storageLoadData[t].content:null}_resetLoadData(t,e){const i={},s=[],o=this._storageLoadData;for(let r=t.elements.length-1;r>=0;r--){const n=t.elements[r];["image","svg","html"].includes(n.type)&&(o[n.uuid]?e.includes(n.uuid)&&(i[n.uuid]=this._createEmptyLoadItem(n),s.push(n.uuid)):(i[n.uuid]=this._createEmptyLoadItem(n),s.push(n.uuid)))}return[s,i]}_createEmptyLoadItem(t){let i="";const s=t.type;let o=t.w,r=t.h;if("image"===t.type){i=t.desc.src||""}else if("svg"===t.type){i=t.desc.svg||""}else if("html"===t.type){const e=t;i=(e.desc.html||"").replace(/<script[\s\S]*?<\/script>/gi,""),o=e.desc.width||t.w,r=e.desc.height||t.h}return{uuid:t.uuid,type:s,status:"null",content:null,source:i,elemW:o,elemH:r,element:e(t)}}_loadTask(){if("loading"===this._status)return;if(this._status="loading",0===this._currentUUIDQueue.length){if(0===this._waitingLoadQueue.length)return this._status="complete",void this._event.trigger("complete",void 0);{const t=this._waitingLoadQueue.shift();if(t){const{uuidQueue:e,loadData:i}=t;this._currentLoadData=i,this._currentUUIDQueue=e}}}const{maxParallelNum:t}=this._opts,e=this._currentUUIDQueue.splice(0,t);e.forEach(((t,e)=>{}));const i=[],s=()=>{if(i.length>=t)return!1;if(0===e.length)return!0;for(let o=i.length;o<t;o++){const t=e.shift();if(void 0===t)break;i.push(t),this._loadElementSource(this._currentLoadData[t]).then((o=>{var r,n;i.splice(i.indexOf(t),1);const a=s();this._storageLoadData[t]={uuid:t,type:this._currentLoadData[t].type,status:"loaded",content:o,source:this._currentLoadData[t].source,elemW:this._currentLoadData[t].elemW,elemH:this._currentLoadData[t].elemH,element:this._currentLoadData[t].element},0===i.length&&0===e.length&&!0===a&&(this._status="free",this._loadTask()),this._event.trigger("load",{uuid:null==(r=this._storageLoadData[t])?void 0:r.uuid,type:this._storageLoadData[t].type,status:this._storageLoadData[t].status,content:this._storageLoadData[t].content,source:this._storageLoadData[t].source,elemW:this._storageLoadData[t].elemW,elemH:this._storageLoadData[t].elemH,element:null==(n=this._storageLoadData[t])?void 0:n.element})})).catch((o=>{var r,n,a,h,l,c,u,d,f,_,g,m;console.warn(o),i.splice(i.indexOf(t),1);const x=s();this._currentLoadData[t]&&(this._storageLoadData[t]={uuid:t,type:null==(r=this._currentLoadData[t])?void 0:r.type,status:"fail",content:null,error:o,source:null==(n=this._currentLoadData[t])?void 0:n.source,elemW:null==(a=this._currentLoadData[t])?void 0:a.elemW,elemH:null==(h=this._currentLoadData[t])?void 0:h.elemH,element:null==(l=this._currentLoadData[t])?void 0:l.element}),0===i.length&&0===e.length&&!0===x&&(this._status="free",this._loadTask()),this._currentLoadData[t]&&this._event.trigger("error",{uuid:t,type:null==(c=this._storageLoadData[t])?void 0:c.type,status:null==(u=this._storageLoadData[t])?void 0:u.status,content:null==(d=this._storageLoadData[t])?void 0:d.content,source:null==(f=this._storageLoadData[t])?void 0:f.source,elemW:null==(_=this._storageLoadData[t])?void 0:_.elemW,elemH:null==(g=this._storageLoadData[t])?void 0:g.elemH,element:null==(m=this._storageLoadData[t])?void 0:m.element})}))}return!1};s()}async _loadElementSource(t){if(t&&"image"===t.type){return await n(t.source)}if(t&&"svg"===t.type){return await a(t.source)}if(t&&"html"===t.type){return await h(t.source,{width:t.elemW,height:t.elemH})}throw Error("Element's source is not support!")}}class T{constructor(){this._listeners=new Map}on(t,e){if(this._listeners.has(t)){const i=this._listeners.get(t);null==i||i.push(e),this._listeners.set(t,i||[])}else this._listeners.set(t,[e])}off(t,e){if(this._listeners.has(t)){const i=this._listeners.get(t);if(Array.isArray(i))for(let t=0;t<(null==i?void 0:i.length);t++)if(i[t]===e){i.splice(t,1);break}this._listeners.set(t,i||[])}}trigger(t,e){const i=this._listeners.get(t);return!!Array.isArray(i)&&(i.forEach((t=>{t(e)})),!0)}has(t){if(this._listeners.has(t)){const e=this._listeners.get(t);if(Array.isArray(e)&&e.length>0)return!0}return!1}}const P=Symbol("_queue"),O=Symbol("_ctx"),C=Symbol("_status"),W=Symbol("_loader"),k=Symbol("_opts"),A=Symbol("_freeze"),I=Symbol("_drawFrame"),R=Symbol("_retainQueueOneItem");var F,$,H;const{requestAnimationFrame:B}=window;class E extends T{constructor(t){super(),this[F]=[],this[$]=null,this[H]="null",this[k]=t,this[W]=new D({maxParallelNum:6}),this[W].on("load",(t=>{this[I](),this.trigger("load",{element:t.element})})),this[W].on("error",(t=>{this.trigger("error",{element:t.element,error:t.error})})),this[W].on("complete",(()=>{this.trigger("loadComplete",{t:Date.now()})}))}render(t,i,s){const{changeResourceUUIDs:o=[]}=s||{};this[C]="free";const r=e(i);if(Array.isArray(r.elements)&&r.elements.forEach((t=>{"string"==typeof t.uuid&&t.uuid||(t.uuid=function(){function t(){return(65536*(1+Math.random())|0).toString(16).substring(1)}return`${t()}${t()}-${t()}-${t()}-${t()}-${t()}${t()}${t()}`}())})),!this[O])if(this[k]&&"[object HTMLCanvasElement]"===Object.prototype.toString.call(t)){const{width:e,height:i,contextWidth:s,contextHeight:o,devicePixelRatio:r}=this[k],n=t;n.width=e*r,n.height=i*r;const a=n.getContext("2d");this[O]=new l(a,{width:e,height:i,contextWidth:s||e,contextHeight:o||i,devicePixelRatio:r})}else t&&(this[O]=t);if(["freeze"].includes(this[C]))return;const n=e({data:r});this[P].push(n),this[I](),this[W].load(r,o||[])}getContext(){return this[O]}thaw(){this[C]="free"}[(F=P,$=O,H=C,A)](){this[C]="freeze"}[I](){"freeze"!==this[C]&&B((()=>{if("freeze"===this[C])return;const t=this[O];let e=this[P][0],i=!1;this[P].length>1?e=this[P].shift():i=!0,!0!==this[W].isComplete()?(this[I](),e&&t&&z(t,e.data,this[W])):e&&t?(z(t,e.data,this[W]),this[R](),i?this[C]="free":this[I]()):this[C]="free",this.trigger("drawFrame",{t:Date.now()}),!0===this[W].isComplete()&&1===this[P].length&&"free"===this[C]&&(t&&this[P][0]&&this[P][0].data&&z(t,this[P][0].data,this[W]),this.trigger("drawFrameComplete",{t:Date.now()}),this[A]())}))}[R](){if(this[P].length<=1)return;const t=e(this[P][this[P].length-1]);this[P]=[t]}}return E}();
|
|
1
|
+
var iDrawRenderer=function(){"use strict";function t(t){return"string"==typeof t&&/^\#([0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})$/i.test(t)}function e(t){return function t(e){var o,i=(o=e,Object.prototype.toString.call(o).replace(/[\]|\[]{1,1}/gi,"").split(" ")[1]);if(["Null","Number","String","Boolean","Undefined"].indexOf(i)>=0)return e;if("Array"===i){var n=[];return e.forEach((function(e){n.push(t(e))})),n}if("Object"===i){var r={};return Object.keys(e).forEach((function(o){r[o]=t(e[o])})),r}}(t)}function o(t){return(Object.prototype.toString.call(t)||"").replace(/(\[object|\])/gi,"").trim()}var i={type:function(t,e){var i=o(t);return!0===e?i.toLocaleLowerCase():i},array:function(t){return"Array"===o(t)},json:function(t){return"Object"===o(t)},function:function(t){return"Function"===o(t)},asyncFunction:function(t){return"AsyncFunction"===o(t)},string:function(t){return"String"===o(t)},number:function(t){return"Number"===o(t)},undefined:function(t){return"Undefined"===o(t)},null:function(t){return"Null"===o(t)},promise:function(t){return"Promise"===o(t)}},n=function(){return n=Object.assign||function(t){for(var e,o=1,i=arguments.length;o<i;o++)for(var n in e=arguments[o])Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n]);return t},n.apply(this,arguments)};function r(t,e,o,i){return new(o||(o=Promise))((function(n,r){function s(t){try{h(i.next(t))}catch(t){r(t)}}function a(t){try{h(i.throw(t))}catch(t){r(t)}}function h(t){var e;t.done?n(t.value):(e=t.value,e instanceof o?e:new o((function(t){t(e)}))).then(s,a)}h((i=i.apply(t,e||[])).next())}))}function s(t,e){var o,i,n,r,s={label:0,sent:function(){if(1&n[0])throw n[1];return n[1]},trys:[],ops:[]};return r={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(r[Symbol.iterator]=function(){return this}),r;function a(r){return function(a){return function(r){if(o)throw new TypeError("Generator is already executing.");for(;s;)try{if(o=1,i&&(n=2&r[0]?i.return:r[0]?i.throw||((n=i.return)&&n.call(i),0):i.next)&&!(n=n.call(i,r[1])).done)return n;switch(i=0,n&&(r=[2&r[0],n.value]),r[0]){case 0:case 1:n=r;break;case 4:return s.label++,{value:r[1],done:!1};case 5:s.label++,i=r[1],r=[0];continue;case 7:r=s.ops.pop(),s.trys.pop();continue;default:if(!(n=s.trys,(n=n.length>0&&n[n.length-1])||6!==r[0]&&2!==r[0])){s=0;continue}if(3===r[0]&&(!n||r[1]>n[0]&&r[1]<n[3])){s.label=r[1];break}if(6===r[0]&&s.label<n[1]){s.label=n[1],n=r;break}if(n&&s.label<n[2]){s.label=n[2],s.ops.push(r);break}n[2]&&s.ops.pop(),s.trys.pop();continue}r=e.call(t,s)}catch(t){r=[6,t],i=0}finally{o=n=0}if(5&r[0])throw r[1];return{value:r[0]?r[1]:void 0,done:!0}}([r,a])}}}function a(t,e){var o=e.width,i=e.height;return new Promise((function(e,n){var r='\n <svg xmlns="http://www.w3.org/2000/svg" width="'.concat(o||"",'" height = "').concat(i||"",'">\n <foreignObject width="100%" height="100%">\n <div xmlns = "http://www.w3.org/1999/xhtml">\n ').concat(t,"\n </div>\n </foreignObject>\n </svg>\n "),s=new Blob([r],{type:"image/svg+xml;charset=utf-8"}),a=new FileReader;a.readAsDataURL(s),a.onload=function(t){var o,i=null===(o=null==t?void 0:t.target)||void 0===o?void 0:o.result;e(i)},a.onerror=function(t){n(t)}}))}function h(t){return new Promise((function(e,o){var i=new Blob([t],{type:"image/svg+xml;charset=utf-8"}),n=new FileReader;n.readAsDataURL(i),n.onload=function(t){var o,i=null===(o=null==t?void 0:t.target)||void 0===o?void 0:o.result;e(i)},n.onerror=function(t){o(t)}}))}var u=window.Image;function c(t){return new Promise((function(e,o){var i=new u;i.onload=function(){e(i)},i.onabort=o,i.onerror=o,i.src=t}))}var l=function(){function t(t,e){this._opts=e,this._ctx=t,this._transform={scale:1,scrollX:0,scrollY:0}}return t.prototype.getContext=function(){return this._ctx},t.prototype.resetSize=function(t){this._opts=n(n({},this._opts),t)},t.prototype.calcDeviceNum=function(t){return t*this._opts.devicePixelRatio},t.prototype.calcScreenNum=function(t){return t/this._opts.devicePixelRatio},t.prototype.getSize=function(){return{width:this._opts.width,height:this._opts.height,contextWidth:this._opts.contextWidth,contextHeight:this._opts.contextHeight,devicePixelRatio:this._opts.devicePixelRatio}},t.prototype.setTransform=function(t){this._transform=n(n({},this._transform),t)},t.prototype.getTransform=function(){return{scale:this._transform.scale,scrollX:this._transform.scrollX,scrollY:this._transform.scrollY}},t.prototype.setFillStyle=function(t){this._ctx.fillStyle=t},t.prototype.fill=function(t){return this._ctx.fill(t||"nonzero")},t.prototype.arc=function(t,e,o,i,n,r){return this._ctx.arc(this._doSize(t),this._doSize(e),this._doSize(o),i,n,r)},t.prototype.rect=function(t,e,o,i){return this._ctx.rect(this._doSize(t),this._doSize(e),this._doSize(o),this._doSize(i))},t.prototype.fillRect=function(t,e,o,i){return this._ctx.fillRect(this._doSize(t),this._doSize(e),this._doSize(o),this._doSize(i))},t.prototype.clearRect=function(t,e,o,i){return this._ctx.clearRect(this._doSize(t),this._doSize(e),this._doSize(o),this._doSize(i))},t.prototype.beginPath=function(){return this._ctx.beginPath()},t.prototype.closePath=function(){return this._ctx.closePath()},t.prototype.lineTo=function(t,e){return this._ctx.lineTo(this._doSize(t),this._doSize(e))},t.prototype.moveTo=function(t,e){return this._ctx.moveTo(this._doSize(t),this._doSize(e))},t.prototype.arcTo=function(t,e,o,i,n){return this._ctx.arcTo(this._doSize(t),this._doSize(e),this._doSize(o),this._doSize(i),this._doSize(n))},t.prototype.setLineWidth=function(t){return this._ctx.lineWidth=this._doSize(t)},t.prototype.setLineDash=function(t){var e=this;return this._ctx.setLineDash(t.map((function(t){return e._doSize(t)})))},t.prototype.isPointInPath=function(t,e){return this._ctx.isPointInPath(this._doX(t),this._doY(e))},t.prototype.isPointInPathWithoutScroll=function(t,e){return this._ctx.isPointInPath(this._doSize(t),this._doSize(e))},t.prototype.setStrokeStyle=function(t){this._ctx.strokeStyle=t},t.prototype.stroke=function(){return this._ctx.stroke()},t.prototype.translate=function(t,e){return this._ctx.translate(this._doSize(t),this._doSize(e))},t.prototype.rotate=function(t){return this._ctx.rotate(t)},t.prototype.drawImage=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];var o=t[0],i=t[1],n=t[2],r=t[3],s=t[4],a=t[t.length-4],h=t[t.length-3],u=t[t.length-2],c=t[t.length-1];return 9===t.length?this._ctx.drawImage(o,this._doSize(i),this._doSize(n),this._doSize(r),this._doSize(s),this._doSize(a),this._doSize(h),this._doSize(u),this._doSize(c)):this._ctx.drawImage(o,this._doSize(a),this._doSize(h),this._doSize(u),this._doSize(c))},t.prototype.createPattern=function(t,e){return this._ctx.createPattern(t,e)},t.prototype.measureText=function(t){return this._ctx.measureText(t)},t.prototype.setTextAlign=function(t){this._ctx.textAlign=t},t.prototype.fillText=function(t,e,o,i){return void 0!==i?this._ctx.fillText(t,this._doSize(e),this._doSize(o),this._doSize(i)):this._ctx.fillText(t,this._doSize(e),this._doSize(o))},t.prototype.strokeText=function(t,e,o,i){return void 0!==i?this._ctx.strokeText(t,this._doSize(e),this._doSize(o),this._doSize(i)):this._ctx.strokeText(t,this._doSize(e),this._doSize(o))},t.prototype.setFont=function(t){var e=[];"bold"===t.fontWeight&&e.push("".concat(t.fontWeight)),e.push("".concat(this._doSize(t.fontSize||12),"px")),e.push("".concat(t.fontFamily||"sans-serif")),this._ctx.font="".concat(e.join(" "))},t.prototype.setTextBaseline=function(t){this._ctx.textBaseline=t},t.prototype.setGlobalAlpha=function(t){this._ctx.globalAlpha=t},t.prototype.save=function(){this._ctx.save()},t.prototype.restore=function(){this._ctx.restore()},t.prototype.scale=function(t,e){this._ctx.scale(t,e)},t.prototype.setShadowColor=function(t){this._ctx.shadowColor=t},t.prototype.setShadowOffsetX=function(t){this._ctx.shadowOffsetX=this._doSize(t)},t.prototype.setShadowOffsetY=function(t){this._ctx.shadowOffsetY=this._doSize(t)},t.prototype.setShadowBlur=function(t){this._ctx.shadowBlur=this._doSize(t)},t.prototype.ellipse=function(t,e,o,i,n,r,s,a){this._ctx.ellipse(this._doSize(t),this._doSize(e),this._doSize(o),this._doSize(i),n,r,s,a)},t.prototype._doSize=function(t){return this._opts.devicePixelRatio*t},t.prototype._doX=function(t){var e=this._transform,o=e.scale,i=(t-e.scrollX)/o;return this._doSize(i)},t.prototype._doY=function(t){var e=this._transform,o=e.scale,i=(t-e.scrollY)/o;return this._doSize(i)},t}();function d(t){return"number"==typeof t&&(t>0||t<=0)}function f(t){return"number"==typeof t&&t>=0}function _(t){return"string"==typeof t&&/^(http:\/\/|https:\/\/|\.\/|\/)/.test("".concat(t))}function p(t){return"string"==typeof t&&/^(data:image\/)/.test("".concat(t))}var g={x:function(t){return d(t)},y:function(t){return d(t)},w:f,h:function(t){return"number"==typeof t&&t>=0},angle:function(t){return"number"==typeof t&&t>=-360&&t<=360},number:d,borderWidth:function(t){return f(t)},borderRadius:function(t){return d(t)&&t>=0},color:function(e){return t(e)},imageSrc:function(t){return p(t)||_(t)},imageURL:_,imageBase64:p,svg:function(t){return"string"==typeof t&&/^(<svg[\s]{1,}|<svg>)/i.test("".concat(t).trim())&&/<\/[\s]{0,}svg>$/i.test("".concat(t).trim())},html:function(t){var e=!1;if("string"==typeof t){var o=document.createElement("div");o.innerHTML=t,o.children.length>0&&(e=!0),o=null}return e},text:function(t){return"string"==typeof t},fontSize:function(t){return d(t)&&t>0},lineHeight:function(t){return d(t)&&t>0},textAlign:function(t){return["center","left","right"].includes(t)},fontFamily:function(t){return"string"==typeof t&&t.length>0},fontWeight:function(t){return["bold"].includes(t)},strokeWidth:function(t){return d(t)&&t>0}};function m(t,e,o){const i=function(t){return{x:t.x+t.w/2,y:t.y+t.h/2}}(e);return function(t,e,o,i){e&&(o>0||o<0)&&(t.translate(e.x,e.y),t.rotate(o),t.translate(-e.x,-e.y));i(t),e&&(o>0||o<0)&&(t.translate(e.x,e.y),t.rotate(-o),t.translate(-e.x,-e.y))}(t,i,(e.angle||0)/180*Math.PI||0,o)}function y(t){t.setFillStyle("#000000"),t.setStrokeStyle("#000000"),t.setLineDash([]),t.setGlobalAlpha(1),t.setShadowColor("#00000000"),t.setShadowOffsetX(0),t.setShadowOffsetY(0),t.setShadowBlur(0)}function x(e,o,n){y(e),function(e,o){y(e),m(e,o,(()=>{if(!(o.desc.borderWidth&&o.desc.borderWidth>0))return;const i=o.desc.borderWidth;let n="#000000";!0===t(o.desc.borderColor)&&(n=o.desc.borderColor);const r=o.x-i/2,s=o.y-i/2,a=o.w+i,h=o.h+i;let u=o.desc.borderRadius||0;u=Math.min(u,a/2,h/2),u<a/2&&u<h/2&&(u+=i/2);const{desc:c}=o;void 0!==c.shadowColor&&t(c.shadowColor)&&e.setShadowColor(c.shadowColor),void 0!==c.shadowOffsetX&&g.number(c.shadowOffsetX)&&e.setShadowOffsetX(c.shadowOffsetX),void 0!==c.shadowOffsetY&&g.number(c.shadowOffsetY)&&e.setShadowOffsetY(c.shadowOffsetY),void 0!==c.shadowBlur&&g.number(c.shadowBlur)&&e.setShadowBlur(c.shadowBlur),e.beginPath(),e.setLineWidth(i),e.setStrokeStyle(n),e.moveTo(r+u,s),e.arcTo(r+a,s,r+a,s+h,u),e.arcTo(r+a,s+h,r,s+h,u),e.arcTo(r,s+h,r,s,u),e.arcTo(r,s,r+a,s,u),e.closePath(),e.stroke()}))}(e,o),y(e),m(e,o,(()=>{const{x:t,y:r,w:s,h:a}=o;let h=o.desc.borderRadius||0;h=Math.min(h,s/2,a/2),(s<2*h||a<2*h)&&(h=0),e.beginPath(),e.moveTo(t+h,r),e.arcTo(t+s,r,t+s,r+a,h),e.arcTo(t+s,r+a,t,r+a,h),e.arcTo(t,r+a,t,r,h),e.arcTo(t,r,t+s,r,h),e.closePath(),("string"==typeof n||["CanvasPattern"].includes(i.type(n)))&&e.setFillStyle(n),e.fill()}))}function w(t,e){x(t,e,e.desc.bgColor)}function S(t,e,o){const i=o.getContent(e.uuid);m(t,e,(()=>{i&&t.drawImage(i,e.x,e.y,e.w,e.h)}))}function v(t,e,o){const i=o.getContent(e.uuid);m(t,e,(()=>{i&&t.drawImage(i,e.x,e.y,e.w,e.h)}))}function b(t,e,o){const i=o.getContent(e.uuid);m(t,e,(()=>{i&&t.drawImage(i,e.x,e.y,e.w,e.h)}))}function z(e,o,i){y(e),x(e,o,o.desc.bgColor||"transparent"),m(e,o,(()=>{const i={fontSize:12,fontFamily:"sans-serif",textAlign:"center",...o.desc};e.setFillStyle(o.desc.color),e.setTextBaseline("top"),e.setFont({fontWeight:i.fontWeight,fontSize:i.fontSize,fontFamily:i.fontFamily});const n=i.text.replace(/\r\n/gi,"\n"),r=i.lineHeight||i.fontSize,s=n.split("\n"),a=[];let h=0;s.forEach(((t,i)=>{let n="";if(t.length>0){for(let u=0;u<t.length&&(e.measureText(n+(t[u]||"")).width<e.calcDeviceNum(o.w)?n+=t[u]||"":(a.push({text:n,width:e.calcScreenNum(e.measureText(n).width)}),n=t[u]||"",h++),!((h+1)*r>o.h));u++)if(t.length-1===u&&(h+1)*r<o.h){a.push({text:n,width:e.calcScreenNum(e.measureText(n).width)}),i<s.length-1&&h++;break}}else a.push({text:"",width:0})}));let u=0;a.length*r<o.h&&("top"===o.desc.verticalAlign?u=0:"bottom"===o.desc.verticalAlign?u+=o.h-a.length*r:u+=(o.h-a.length*r)/2);{const n=o.y+u;void 0!==i.textShadowColor&&t(i.textShadowColor)&&e.setShadowColor(i.textShadowColor),void 0!==i.textShadowOffsetX&&g.number(i.textShadowOffsetX)&&e.setShadowOffsetX(i.textShadowOffsetX),void 0!==i.textShadowOffsetY&&g.number(i.textShadowOffsetY)&&e.setShadowOffsetY(i.textShadowOffsetY),void 0!==i.textShadowBlur&&g.number(i.textShadowBlur)&&e.setShadowBlur(i.textShadowBlur),a.forEach(((t,s)=>{let a=o.x;"center"===i.textAlign?a=o.x+(o.w-t.width)/2:"right"===i.textAlign&&(a=o.x+(o.w-t.width)),e.fillText(t.text,a,n+r*s)})),y(e)}if(t(i.strokeColor)&&void 0!==i.strokeWidth&&i.strokeWidth>0){const t=o.y+u;a.forEach(((n,s)=>{let a=o.x;"center"===i.textAlign?a=o.x+(o.w-n.width)/2:"right"===i.textAlign&&(a=o.x+(o.w-n.width)),void 0!==i.strokeColor&&e.setStrokeStyle(i.strokeColor),void 0!==i.strokeWidth&&i.strokeWidth>0&&e.setLineWidth(i.strokeWidth),e.strokeText(n.text,a,t+r*s)}))}}))}function L(t,e){y(t),m(t,e,(t=>{const{x:o,y:i,w:n,h:r,desc:s}=e,{bgColor:a="#000000",borderColor:h="#000000",borderWidth:u=0}=s,c=n/2,l=r/2,d=o+c,f=i+l;if(u&&u>0){const e=u/2+c,o=u/2+l;t.beginPath(),t.setStrokeStyle(h),t.setLineWidth(u),t.ellipse(d,f,e,o,0,0,2*Math.PI),t.closePath(),t.stroke()}t.beginPath(),t.setFillStyle(a),t.ellipse(d,f,c,l,0,0,2*Math.PI),t.closePath(),t.fill()}))}function D(e,o,i){var n;y(e);const r=e.getSize();if(e.clearRect(0,0,r.contextWidth,r.contextHeight),"string"==typeof o.bgColor&&t(o.bgColor)&&function(t,e){const o=t.getSize();t.setFillStyle(e),t.fillRect(0,0,o.contextWidth,o.contextHeight)}(e,o.bgColor),o.elements.length>0)for(let t=0;t<o.elements.length;t++){const r=o.elements[t];if(!0!==(null==(n=null==r?void 0:r.operation)?void 0:n.invisible))switch(r.type){case"rect":w(e,r);break;case"text":z(e,r);break;case"image":S(e,r,i);break;case"svg":v(e,r,i);break;case"html":b(e,r,i);break;case"circle":L(e,r)}}}class k{constructor(){this._listeners=new Map}on(t,e){if(this._listeners.has(t)){const o=this._listeners.get(t);null==o||o.push(e),this._listeners.set(t,o||[])}else this._listeners.set(t,[e])}off(t,e){if(this._listeners.has(t)){const o=this._listeners.get(t);if(Array.isArray(o))for(let t=0;t<(null==o?void 0:o.length);t++)if(o[t]===e){o.splice(t,1);break}this._listeners.set(t,o||[])}}trigger(t,e){const o=this._listeners.get(t);return!!Array.isArray(o)&&(o.forEach((t=>{t(e)})),!0)}has(t){if(this._listeners.has(t)){const e=this._listeners.get(t);if(Array.isArray(e)&&e.length>0)return!0}return!1}}class P{constructor(t){this._currentLoadData={},this._currentUUIDQueue=[],this._storageLoadData={},this._status="free",this._waitingLoadQueue=[],this._opts=t,this._event=new k,this._waitingLoadQueue=[]}load(t,e){const[o,i]=this._resetLoadData(t,e);"free"===this._status||"complete"===this._status?(this._currentUUIDQueue=o,this._currentLoadData=i,this._loadTask()):"loading"===this._status&&o.length>0&&this._waitingLoadQueue.push({uuidQueue:o,loadData:i})}on(t,e){this._event.on(t,e)}off(t,e){this._event.off(t,e)}isComplete(){return"complete"===this._status}getContent(t){var e;return"loaded"===(null==(e=this._storageLoadData[t])?void 0:e.status)?this._storageLoadData[t].content:null}_resetLoadData(t,e){const o={},i=[],n=this._storageLoadData;for(let r=t.elements.length-1;r>=0;r--){const s=t.elements[r];["image","svg","html"].includes(s.type)&&(n[s.uuid]?e.includes(s.uuid)&&(o[s.uuid]=this._createEmptyLoadItem(s),i.push(s.uuid)):(o[s.uuid]=this._createEmptyLoadItem(s),i.push(s.uuid)))}return[i,o]}_createEmptyLoadItem(t){let o="";const i=t.type;let n=t.w,r=t.h;if("image"===t.type){o=t.desc.src||""}else if("svg"===t.type){o=t.desc.svg||""}else if("html"===t.type){const e=t;o=(e.desc.html||"").replace(/<script[\s\S]*?<\/script>/gi,""),n=e.desc.width||t.w,r=e.desc.height||t.h}return{uuid:t.uuid,type:i,status:"null",content:null,source:o,elemW:n,elemH:r,element:e(t)}}_loadTask(){if("loading"===this._status)return;if(this._status="loading",0===this._currentUUIDQueue.length){if(0===this._waitingLoadQueue.length)return this._status="complete",void this._event.trigger("complete",void 0);{const t=this._waitingLoadQueue.shift();if(t){const{uuidQueue:e,loadData:o}=t;this._currentLoadData=o,this._currentUUIDQueue=e}}}const{maxParallelNum:t}=this._opts,e=this._currentUUIDQueue.splice(0,t);e.forEach(((t,e)=>{}));const o=[],i=()=>{if(o.length>=t)return!1;if(0===e.length)return!0;for(let n=o.length;n<t;n++){const t=e.shift();if(void 0===t)break;o.push(t),this._loadElementSource(this._currentLoadData[t]).then((n=>{var r,s;o.splice(o.indexOf(t),1);const a=i();this._storageLoadData[t]={uuid:t,type:this._currentLoadData[t].type,status:"loaded",content:n,source:this._currentLoadData[t].source,elemW:this._currentLoadData[t].elemW,elemH:this._currentLoadData[t].elemH,element:this._currentLoadData[t].element},0===o.length&&0===e.length&&!0===a&&(this._status="free",this._loadTask()),this._event.trigger("load",{uuid:null==(r=this._storageLoadData[t])?void 0:r.uuid,type:this._storageLoadData[t].type,status:this._storageLoadData[t].status,content:this._storageLoadData[t].content,source:this._storageLoadData[t].source,elemW:this._storageLoadData[t].elemW,elemH:this._storageLoadData[t].elemH,element:null==(s=this._storageLoadData[t])?void 0:s.element})})).catch((n=>{var r,s,a,h,u,c,l,d,f,_,p,g;console.warn(n),o.splice(o.indexOf(t),1);const m=i();this._currentLoadData[t]&&(this._storageLoadData[t]={uuid:t,type:null==(r=this._currentLoadData[t])?void 0:r.type,status:"fail",content:null,error:n,source:null==(s=this._currentLoadData[t])?void 0:s.source,elemW:null==(a=this._currentLoadData[t])?void 0:a.elemW,elemH:null==(h=this._currentLoadData[t])?void 0:h.elemH,element:null==(u=this._currentLoadData[t])?void 0:u.element}),0===o.length&&0===e.length&&!0===m&&(this._status="free",this._loadTask()),this._currentLoadData[t]&&this._event.trigger("error",{uuid:t,type:null==(c=this._storageLoadData[t])?void 0:c.type,status:null==(l=this._storageLoadData[t])?void 0:l.status,content:null==(d=this._storageLoadData[t])?void 0:d.content,source:null==(f=this._storageLoadData[t])?void 0:f.source,elemW:null==(_=this._storageLoadData[t])?void 0:_.elemW,elemH:null==(p=this._storageLoadData[t])?void 0:p.elemH,element:null==(g=this._storageLoadData[t])?void 0:g.element})}))}return!1};i()}async _loadElementSource(t){if(t&&"image"===t.type){return await c(t.source)}if(t&&"svg"===t.type){return await function(t){return r(this,void 0,void 0,(function(){return s(this,(function(e){switch(e.label){case 0:return[4,h(t)];case 1:return[4,c(e.sent())];case 2:return[2,e.sent()]}}))}))}(t.source)}if(t&&"html"===t.type){return await function(t,e){return r(this,void 0,void 0,(function(){return s(this,(function(o){switch(o.label){case 0:return[4,a(t,e)];case 1:return[4,c(o.sent())];case 2:return[2,o.sent()]}}))}))}(t.source,{width:t.elemW,height:t.elemH})}throw Error("Element's source is not support!")}}class T{constructor(){this._listeners=new Map}on(t,e){if(this._listeners.has(t)){const o=this._listeners.get(t);null==o||o.push(e),this._listeners.set(t,o||[])}else this._listeners.set(t,[e])}off(t,e){if(this._listeners.has(t)){const o=this._listeners.get(t);if(Array.isArray(o))for(let t=0;t<(null==o?void 0:o.length);t++)if(o[t]===e){o.splice(t,1);break}this._listeners.set(t,o||[])}}trigger(t,e){const o=this._listeners.get(t);return!!Array.isArray(o)&&(o.forEach((t=>{t(e)})),!0)}has(t){if(this._listeners.has(t)){const e=this._listeners.get(t);if(Array.isArray(e)&&e.length>0)return!0}return!1}}const{requestAnimationFrame:C}=window;return class extends T{constructor(t){super(),this._queue=[],this._ctx=null,this._status="null",this._opts=t,this._loader=new P({maxParallelNum:6}),this._loader.on("load",(t=>{this._drawFrame(),this.trigger("load",{element:t.element})})),this._loader.on("error",(t=>{this.trigger("error",{element:t.element,error:t.error})})),this._loader.on("complete",(()=>{this.trigger("loadComplete",{t:Date.now()})}))}render(t,o,i){const{changeResourceUUIDs:n=[]}=i||{};this._status="free";const r=e(o);if(Array.isArray(r.elements)&&r.elements.forEach((t=>{"string"==typeof t.uuid&&t.uuid||(t.uuid=function(){function t(){return(65536*(1+Math.random())|0).toString(16).substring(1)}return"".concat(t()).concat(t(),"-").concat(t(),"-").concat(t(),"-").concat(t(),"-").concat(t()).concat(t()).concat(t())}())})),!this._ctx)if(this._opts&&"[object HTMLCanvasElement]"===Object.prototype.toString.call(t)){const{width:e,height:o,contextWidth:i,contextHeight:n,devicePixelRatio:r}=this._opts,s=t;s.width=e*r,s.height=o*r;const a=s.getContext("2d");this._ctx=new l(a,{width:e,height:o,contextWidth:i||e,contextHeight:n||o,devicePixelRatio:r})}else t&&(this._ctx=t);if(["freeze"].includes(this._status))return;const s=e({data:r});this._queue.push(s),this._drawFrame(),this._loader.load(r,n||[])}getContext(){return this._ctx}thaw(){this._status="free"}_freeze(){this._status="freeze"}_drawFrame(){"freeze"!==this._status&&C((()=>{if("freeze"===this._status)return;const t=this._ctx;let e=this._queue[0],o=!1;this._queue.length>1?e=this._queue.shift():o=!0,!0!==this._loader.isComplete()?(this._drawFrame(),e&&t&&D(t,e.data,this._loader)):e&&t?(D(t,e.data,this._loader),this._retainQueueOneItem(),o?this._status="free":this._drawFrame()):this._status="free",this.trigger("drawFrame",{t:Date.now()}),!0===this._loader.isComplete()&&1===this._queue.length&&"free"===this._status&&(t&&this._queue[0]&&this._queue[0].data&&D(t,this._queue[0].data,this._loader),this.trigger("drawFrameComplete",{t:Date.now()}),this._freeze())}))}_retainQueueOneItem(){if(this._queue.length<=1)return;const t=e(this._queue[this._queue.length-1]);this._queue=[t]}}}();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@idraw/renderer",
|
|
3
|
-
"version": "0.3.0-beta.
|
|
3
|
+
"version": "0.3.0-beta.6",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/esm/index.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
@@ -21,13 +21,13 @@
|
|
|
21
21
|
"author": "chenshenhai",
|
|
22
22
|
"license": "MIT",
|
|
23
23
|
"devDependencies": {
|
|
24
|
-
"@idraw/types": "^0.3.0-beta.
|
|
24
|
+
"@idraw/types": "^0.3.0-beta.6"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@idraw/util": "^0.3.0-beta.
|
|
27
|
+
"@idraw/util": "^0.3.0-beta.6"
|
|
28
28
|
},
|
|
29
29
|
"publishConfig": {
|
|
30
30
|
"access": "public"
|
|
31
31
|
},
|
|
32
|
-
"gitHead": "
|
|
32
|
+
"gitHead": "afbadcfac219cd8e1897a080385b730374c980ea"
|
|
33
33
|
}
|
package/dist/esm/names.d.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
declare const _queue: unique symbol;
|
|
2
|
-
declare const _ctx: unique symbol;
|
|
3
|
-
declare const _status: unique symbol;
|
|
4
|
-
declare const _loader: unique symbol;
|
|
5
|
-
declare const _opts: unique symbol;
|
|
6
|
-
declare const _freeze: unique symbol;
|
|
7
|
-
declare const _drawFrame: unique symbol;
|
|
8
|
-
declare const _retainQueueOneItem: unique symbol;
|
|
9
|
-
export { _queue, _ctx, _status, _loader, _opts, _freeze, _drawFrame, _retainQueueOneItem };
|
package/dist/esm/names.js
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
const _queue = Symbol('_queue');
|
|
2
|
-
const _ctx = Symbol('_ctx');
|
|
3
|
-
const _status = Symbol('_status');
|
|
4
|
-
const _loader = Symbol('_loader');
|
|
5
|
-
const _opts = Symbol('_opts');
|
|
6
|
-
const _freeze = Symbol('_freeze');
|
|
7
|
-
const _drawFrame = Symbol('_drawFrame');
|
|
8
|
-
const _retainQueueOneItem = Symbol('_retainQueueOneItem');
|
|
9
|
-
export { _queue, _ctx, _status, _loader, _opts, _freeze, _drawFrame, _retainQueueOneItem };
|