UNPKG

@arcgis/core

Version:

ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API

6 lines (5 loc) 3.88 kB
/* All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://js.arcgis.com/4.33/esri/copyright.txt for details. */ import{_ as t}from"../../chunks/tslib.es6.js";import e from"../../request.js";import r from"../../core/Error.js";import{getImageData as o}from"../../core/imageUtils.js";import{isAbsolute as i,isDataProtocol as n,isBlobProtocol as s,dataToBlob as a,join as m}from"../../core/urlUtils.js";import{generateUUID as l}from"../../core/uuid.js";import{property as c}from"../../core/accessorSupport/decorators/property.js";import"../../core/has.js";import"../../core/Logger.js";import"../../core/RandomLCG.js";import{reader as p}from"../../core/accessorSupport/decorators/reader.js";import{subclass as u}from"../../core/accessorSupport/decorators/subclass.js";import{writer as h}from"../../core/accessorSupport/decorators/writer.js";import d from"./MediaElementBase.js";import{mediaElementUrlProperty as g,mediaTypeProperty as f}from"./mediaUtils.js";import{getResourceContentExtension as y}from"../../portal/support/resourceExtension.js";import{ImageElementSymbol as v}from"../../support/mediaLayerUtils.js";import{f as j,t as I,i as b,M as w}from"../../chunks/persistableUrlUtils.js";var E;let U=class extends d{static{E=v}constructor(t){super(t),this.animationOptions=null,this.content=null,this.image=null,this.type="image",this[E]=!0,this.image=null}load(){const t=this.image;if("string"==typeof t){const e=o(t).then((t=>{this._set("content",t)}));this.addResolvingPromise(e)}else if(t instanceof HTMLImageElement){const e=t.decode().then((()=>{this._set("content",t)}));this.addResolvingPromise(e)}else t?this._set("content",t):this.addResolvingPromise(Promise.reject(new r("image-element:invalid-image-type","Invalid image type",{image:t})));return Promise.resolve(this)}get contentWidth(){return null==this.content?0:this.content instanceof HTMLImageElement?this.content.naturalWidth:this.content.width}get contentHeight(){return null==this.content?0:this.content instanceof HTMLImageElement?this.content.naturalHeight:this.content.height}readImage(t,e,r){return j(e.url,r)}writeImage(t,e,r,o){if(null==t)return;const n=o?.portalItem,s=o?.resources;if(!n||!s)return void("string"==typeof t&&(e[r]=I(t,o)));const a=H(t)?t:null;if(a){if(null==b(a))return void(e[r]=a);const t=I(a,{...o,verifyItemRelativeUrls:o?.verifyItemRelativeUrls?{writtenUrls:o.verifyItemRelativeUrls.writtenUrls,rootPath:void 0}:void 0},w.NO);if(n&&t&&!i(t))return s.toKeep.push({resource:n.resourceFromPath(t),compress:!1}),void(e[r]=t)}e[r]="<pending>",s.pendingOperations.push(L(t).then((t=>{const o=P(t,n);e[r]=o.itemRelativeUrl,s.toAdd.push({resource:o,content:{type:"blob",blob:t},compress:!1,finish:t=>{this.image=t.url}})})))}};function H(t){return"string"==typeof t&&!n(t)&&!s(t)}async function L(t){if("string"==typeof t){if(n(t))return a(t);return(await e(t,{responseType:"blob"})).data}return new Promise((e=>M(t).toBlob(e)))}function M(t){if(t instanceof HTMLCanvasElement)return t;const e=t instanceof HTMLImageElement?t.naturalWidth:t.width,r=t instanceof HTMLImageElement?t.naturalHeight:t.height,o=document.createElement("canvas"),i=o.getContext("2d");return o.width=e,o.height=r,t instanceof HTMLImageElement?i.drawImage(t,0,0,t.width,t.height):t instanceof ImageData&&i.putImageData(t,0,0),o}function P(t,e){const r=l(),o=`${m("media",r)}.${y({type:"blob",blob:t})}`;return e.resourceFromPath(o)}t([c()],U.prototype,"animationOptions",void 0),t([c({readOnly:!0})],U.prototype,"content",void 0),t([c({readOnly:!0})],U.prototype,"contentWidth",null),t([c({readOnly:!0})],U.prototype,"contentHeight",null),t([c(g)],U.prototype,"image",void 0),t([p("image",["url"])],U.prototype,"readImage",null),t([h("image")],U.prototype,"writeImage",null),t([c(f)],U.prototype,"type",void 0),U=t([u("esri.layers.support.ImageElement")],U);export{U as default};