@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
6 lines (5 loc) • 5.1 kB
JavaScript
/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.33/esri/copyright.txt for details.
*/
import t from"../../../core/Evented.js";import{makeHandle as e}from"../../../core/handleUtils.js";import{clone as r}from"../../../core/libs/gl-matrix-2/factories/vec3f64.js";import{hasCompatibleTopology as s}from"../../../geometry/support/coordsUtils.js";import{isMeshEditGeometry as i,EditGeometry as o}from"./EditGeometry.js";import{AccumulationBehavior as a}from"./interfaces.js";import{AppendVertex as n}from"./operations/AppendVertex.js";import{UpdateVertices as p,AccumulationType as c}from"./operations/UpdateVertices.js";import{RemoveVertices as h}from"./operations/RemoveVertices.js";import{SplitEdge as d}from"./operations/SplitEdge.js";import{SetVertexPosition as u}from"./operations/SetVertexPosition.js";import{CloseComponent as m}from"./operations/CloseComponent.js";import{MoveMesh as l}from"./operations/MoveMesh.js";import{MoveVertex as S}from"./operations/MoveVertex.js";import{OffsetEdgeVertex as V}from"./operations/OffsetEdgeVertex.js";import{RotateVertex as _}from"./operations/RotateVertex.js";import{ScaleVertex as f}from"./operations/ScaleVertex.js";import{SetAllVertexPositions as y}from"./operations/SetAllVertexPositions.js";import{UndoGroup as U}from"./operations/UndoGroup.js";class E extends t{constructor(t,e){super(),this.data=t,this.viewingMode=e,this._undoStack=[],this._redoStack=[],this._listener=this.data.on("change",(t=>{t.addedVertices&&this.emit("vertex-add",{type:"vertex-add",vertices:t.addedVertices,operation:t.operation}),t.removedVertices&&this.emit("vertex-remove",{type:"vertex-remove",vertices:t.removedVertices,operation:t.operation}),t.updatedVertices&&this.emit("vertex-update",{type:"vertex-update",vertices:t.updatedVertices,operation:t.operation})}))}destroy(){this._listener.remove()}splitEdge(t,e){return this._apply(new d(this.data,t,e))}updateVertices(t,e,r=a.ACCUMULATE_STEPS){return this._apply(new p(this.data,t,e),r)}move(t,e,r,s=a.ACCUMULATE_STEPS){return i(this.data)?this._apply(new l(this.data,t,e,r),s):this.moveVertices(this.data.allVertices,t,e,r,s)}moveVertices(t,e,r,s,o=a.ACCUMULATE_STEPS){return i(this.data)?this._apply(new l(this.data,e,r,s),o):this.updateVertices(t,new S(this.data.coordinateHelper,e,r,s),o)}scale(t,e,r,s,i=a.ACCUMULATE_STEPS,o=c.CUMULATIVE){return this.scaleVertices(this.data.allVertices,t,e,r,s,i,o)}scaleVertices(t,e,r,s,i,o=a.ACCUMULATE_STEPS,n=c.CUMULATIVE){return this.updateVertices(t,new f(e,r,s,i,n),o)}rotate(t,e,r=a.ACCUMULATE_STEPS,s=c.CUMULATIVE){return this.rotateVertices(this.data.allVertices,t,e,r,s)}rotateVertices(t,e,r,s=a.ACCUMULATE_STEPS,i=c.CUMULATIVE){return this.updateVertices(t,new _(e,r,i),s)}removeVertices(t){return this._apply(new h(this.data,t,this._minNumVerticesPerType))}appendVertex(t){return 0===this.data.components.length?null:this._apply(new n(this.data,this.data.components[0],t))}setVertexPosition(t,e){return this._apply(new u(this.data,t,e))}offsetEdge(t,e,r,s=a.ACCUMULATE_STEPS){return this.updateVertices([e.leftVertex,e.rightVertex],new V(this.data.coordinateHelper,t,e,r),s)}trySetGeometry(t,e=a.ACCUMULATE_STEPS){const{data:r}=this,{coordinateHelper:n}=r;if(r.type!==t.type||!r.spatialReference.equals(t.spatialReference)||n.hasZ()!==t.hasZ||n.hasM()!==t.hasM||!s(r.geometry,t)||i(r))return;const p=Array.from(o.fromGeometry(t,this.viewingMode).iterateVertices(),(t=>t.pos));return this.setVertexPositions(p,e)}setVertexPositions(t,e=a.ACCUMULATE_STEPS){return this._apply(new y(this.data,t),e)}createResetState(){if(i(this.data))return this._createResetStateMesh();const t=this.data.geometry.clone();return e((()=>this.trySetGeometry(t)))}closeComponent(t){return this.data.components.includes(t)?this._apply(new m(this.data,t)):null}canRemoveVertex(t){return t.vertices.length>this._minNumVerticesPerType}createUndoGroup(){const t=new U;return this._apply(t),e((()=>t.close()))}undo(){if(this._undoStack.length>0){const t=this._undoStack.pop();return t.undo(),this._redoStack.push(t),t}return null}redo(){if(this._redoStack.length>0){const t=this._redoStack.pop();return t.apply(),this._undoStack.push(t),t}return null}get canUndo(){return this._undoStack.length>0}get canRedo(){return this._redoStack.length>0}get lastOperation(){return this._undoStack.length>0?this._undoStack[this._undoStack.length-1]:null}get _minNumVerticesPerType(){switch(this.data.type){case"point":return 1;case"polyline":return 2;case"polygon":return 3;default:return 0}}_apply(t,e=a.ACCUMULATE_STEPS){return e!==a.NEW_STEP&&null!=this.lastOperation&&this.lastOperation.accumulate(t)||(t.apply(),this._undoStack.push(t),this._redoStack=[]),t}_createResetStateMesh(){if(!i(this.data))return e();const t=this.data.geometry,{vertexSpace:s}=t;if(s.origin){const i=r(s.origin);return e((()=>{t.vertexSpace.origin=i}))}const o=t.vertexAttributes.clonePositional();return e((()=>{t.vertexAttributes=o,t.vertexAttributesChanged()}))}static fromGeometry(t,e){return new E(o.fromGeometry(t,e),e)}}export{E as EditGeometryOperations};