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.32/esri/copyright.txt for details. */ import{_ as t}from"../../chunks/tslib.es6.js";import e from"../../request.js";import o from"../../core/Error.js";import{getImageData as r}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{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=r(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 o("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,o){return j(e.url,o)}writeImage(t,e,o,r){if(null==t)return;const n=r?.portalItem,s=r?.resources;if(!n||!s)return void("string"==typeof t&&(e[o]=I(t,r)));const a=L(t)?t:null;if(a){if(null==b(a))return void(e[o]=a);const t=I(a,{...r,verifyItemRelativeUrls:r?.verifyItemRelativeUrls?{writtenUrls:r.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[o]=t)}e[o]="<pending>",s.pendingOperations.push(M(t).then((t=>{const r=R(t,n);e[o]=r.itemRelativeUrl,s.toAdd.push({resource:r,content:{type:"blob",blob:t},compress:!1,finish:t=>{this.image=t.url}})})))}};E=v,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);const H=U;function L(t){return"string"==typeof t&&!n(t)&&!s(t)}async function M(t){if("string"==typeof t){if(n(t))return a(t);return(await e(t,{responseType:"blob"})).data}return new Promise((e=>P(t).toBlob(e)))}function P(t){if(t instanceof HTMLCanvasElement)return t;const e=t instanceof HTMLImageElement?t.naturalWidth:t.width,o=t instanceof HTMLImageElement?t.naturalHeight:t.height,r=document.createElement("canvas"),i=r.getContext("2d");return r.width=e,r.height=o,t instanceof HTMLImageElement?i.drawImage(t,0,0,t.width,t.height):t instanceof ImageData&&i.putImageData(t,0,0),r}function R(t,e){const o=l(),r=`${m("media",o)}.${y({type:"blob",blob:t})}`;return e.resourceFromPath(r)}export{H as default};