import{isEmpty,promptFiles}from"../../base/helpers.mjs";import{View}from"../../view/base.mjs";import{ImageView,BackgroundImageView}from"../../view/image.mjs";import{ImageEditorNodeView}from"./base.mjs";import{NoImageView}from"./common.mjs";class ImageEditorImageNodeView extends ImageEditorNodeView{static hideHeader=!0;static nodeTypeName="Image";static allFitModes=["actual","stretch","cover","contain"];static allAnchorModes=["top-left","top-center","top-right","center-left","center-center","center-right","bottom-left","bottom-center","bottom-right"];static className="image-editor-image-node-view";static nodeButtons={...ImageEditorNodeView.nodeButtons,"replace-image":{icon:"fa-solid fa-upload",tooltip:"Replace Image",shortcut:"c",callback:function(){this.replaceImage()}},"mirror-x":{icon:"fa-solid fa-left-right",tooltip:"Mirror Horizontally",shortcut:"z",callback:function(){this.mirrorHorizontally()}},"mirror-y":{icon:"fa-solid fa-up-down",tooltip:"Mirror Vertically",shortcut:"y",callback:function(){this.mirrorVertically()}},"rotate-clockwise":{icon:"fa-solid fa-rotate-right",tooltip:"Rotate Clockwise",shortcut:"r",callback:function(){this.rotateClockwise()}},"rotate-counter-clockwise":{icon:"fa-solid fa-rotate-left",tooltip:"Rotate Counter-Clockwise",shortcut:"w",callback:function(){this.rotateCounterClockwise()}}};async updateOptions(t){this.updateFit(t.fit),this.updateAnchor(t.anchor)}async updateFit(t){this.fit=t,this.content.fit=t;for(let t of this.constructor.allFitModes)this.content.removeClass(`fit-${t}`);isEmpty(t)||this.content.addClass(`fit-${t}`)}async updateAnchor(t){this.anchor=t,this.content.anchor=t;for(let t of this.constructor.allAnchorModes)this.content.removeClass(`anchor-${t}`);isEmpty(t)||this.content.addClass(`anchor-${t}`)}async replaceImage(){let t;try{t=await promptFiles("image/*")}catch(t){}if(!isEmpty(t)){let e=new FileReader;e.addEventListener("load",(async()=>{let t=new BackgroundImageView(this.config,e.result,!1);await this.setContent(t),this.updateFit(this.fit),this.updateAnchor(this.anchor)})),e.readAsDataURL(t)}}mirrorHorizontally(){return this.content.mirrorHorizontally()}mirrorVertically(){return this.content.mirrorVertically()}rotateClockwise(){return this.content.rotateClockwise()}rotateCounterClockwise(){return this.content.rotateCounterClockwise()}getState(t=!0){let e=super.getState(t);return e.src=t?this.content.src:null,e.anchor=this.anchor||null,e.fit=this.fit||null,e}async setState(t){await super.setState(t),isEmpty(t.src)?await this.setContent(new NoImageView(this.config)):await this.setContent(new BackgroundImageView(this.config,t.src,!1)),await this.updateAnchor(t.anchor),await this.updateFit(t.fit)}async getCanvasScaleSize(){return isEmpty(this.content.src)?await super.getCanvasScaleSize():(await this.content.waitForLoad(),[8*Math.floor(this.content.width/8),8*Math.floor(this.content.height/8)])}static getDefaultState(){return{classname:this.name}}}export{ImageEditorImageNodeView};
