@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
3 lines (2 loc) • 6.08 kB
JavaScript
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */
import{Evented as e}from"../../../core/Evented.js";import{makeHandle as t}from"../../../core/handleUtils.js";import{clone as r}from"../../../core/libs/gl-matrix-2/factories/vec3f64.js";import{isMeshEditGeometry as o,Part as s,EditGeometry as i,haveCompatibleTopologies as a}from"./EditGeometry.js";import{AppendVertex as n}from"./operations/AppendVertex.js";import{UpdateVertices as d}from"./operations/UpdateVertices.js";import{RemoveVertices as h}from"./operations/RemoveVertices.js";import{SplitSegment as c}from"./operations/SplitSegment.js";import{SetVertexPosition as p}from"./operations/SetVertexPosition.js";import{ClosePart as u}from"./operations/ClosePart.js";import{AddOrUpdatePart as l}from"./operations/AddOrUpdatePart.js";import{MoveMesh as m}from"./operations/MoveMesh.js";import{MoveVertex as g}from"./operations/MoveVertex.js";import{OffsetEdgeVertex as f}from"./operations/OffsetEdgeVertex.js";import{RotateVertex as v}from"./operations/RotateVertex.js";import{ScaleVertex as y}from"./operations/ScaleVertex.js";import{SetAllVertexPositions as _}from"./operations/SetAllVertexPositions.js";import{UndoGroup as V}from"./operations/UndoGroup.js";class S extends e{constructor(e,t,r=!1){super(),this.data=e,this.viewingMode=t,this.allowCurves=r,this._undoStack=[],this._redoStack=[],this.disableDefaultRedoBehavior=!1,this._listener=this.data.on("change",e=>{e.addedVertices&&this.emit("vertex-add",{type:"vertex-add",vertices:e.addedVertices,operation:e.operation}),e.removedVertices&&this.emit("vertex-remove",{type:"vertex-remove",vertices:e.removedVertices,operation:e.operation}),e.updatedVertices&&this.emit("vertex-update",{type:"vertex-update",vertices:e.updatedVertices,operation:e.operation})})}destroy(){super.destroy(),this._listener.remove()}splitSegment(e,t){return this._apply(new c(this.data,e,t))}updateVertices(e,t,r=1){return this._apply(new d(this.data,e,t),r)}move(e,t,r,s=1){return o(this.data)?this._apply(new m(this.data,e,t,r),s):this.moveVertices(this.data.allVerticesUnordered,e,t,r,s)}moveVertices(e,t,r,s,i=1){return o(this.data)?this._apply(new m(this.data,t,r,s),i):this.updateVertices(e,new g(this.data.coordinateHelper,t,r,s),i)}scale(e,t,r,o,s=1,i=0){return this.scaleVertices(this.data.allVerticesUnordered,e,t,r,o,s,i)}scaleVertices(e,t,r,o,s,i=1,a=0){return this.updateVertices(e,new y(t,r,o,s,a),i)}rotate(e,t,r=1,o=0){return this.rotateVertices(this.data.allVerticesUnordered,e,t,r,o)}rotateVertices(e,t,r,o=1,s=0){return this.updateVertices(e,new v(t,r,s),o)}removeVertices(e){return this._apply(new h(this.data,e,this._minNumVerticesPerType))}appendVertex(e,t,r){return t??=new s(this.data.spatialReference,this.viewingMode,r),this._apply(new n(this.data,t,e))}appendVertexToFirstPart(e){const t=this.data.parts.at(0);return t?this._apply(new n(this.data,t,e)):null}addOrUpdatePart(e,t){return this._apply(new l(this.data,e,t,this.viewingMode))}setVertexPosition(e,t){return this._apply(new p(this.data,e,t))}offsetEdge(e,t,r,o=1){return this.updateVertices([t.leftVertex,t.rightVertex],new f(this.data.coordinateHelper,e,t,r),o)}trySetGeometry(e,t=1){const{data:r}=this,{coordinateHelper:s}=r;if(r.type!==e.type||!r.spatialReference.equals(e.spatialReference)||s.hasZ()!==e.hasZ||s.hasM()!==e.hasM||o(r))return;const n=i.fromGeometry(e,this.viewingMode,{allowCurves:this.allowCurves});return a(r,n)?this.overwriteGeometryDataInPlace(n,t):void 0}overwriteGeometryDataInPlace(e,t=1){return this._apply(new _(this.data,e),t)}createResetState(){if(o(this.data))return this._createResetStateMesh();const e=this.data.geometry.clone();return t(()=>this.trySetGeometry(e))}closePart(e){return this.data.parts.includes(e)?this._apply(new u(this.data,e)):null}canRemoveVertex(e){return e.vertices.length>this._minNumVerticesPerType}createUndoGroup(){const e=new V;return this._apply(e),t(()=>e.close())}groupOperations(e){const t=[],{_undoStack:r}=this;for(;r.length>0;){const o=r.at(-1);if(!o||!e(o))break;r.pop(),t.push(o)}if(t.length>0){const e=new V(t.reverse());return this.recordUndo(e,!0),{group:e,operations:t}}return null}recordUndo(e,t){const r=this.canUndo;this._undoStack.push(e),t&&this.resetRedoStack(),this.emit("can-redo-change",{type:"can-redo-change",canRedo:this.canRedo}),r!==this.canUndo&&this.emit("can-undo-change",{type:"can-undo-change",canUndo:this.canUndo})}recordRedo(e){if(this.disableDefaultRedoBehavior)return;const t=this.canRedo;this._redoStack.push(e),t||this.emit("can-redo-change",{type:"can-redo-change",canRedo:!0})}undo(){if(this._undoStack.length>0){const e=this.canUndo,t=this._undoStack.pop();return t.undo(),this.recordRedo(t),e!==this.canUndo&&this.emit("can-undo-change",{type:"can-undo-change",canUndo:this.canUndo}),t}return null}redo(){if(this._redoStack.length>0){const e=this._redoStack.pop();return e.apply(),this.recordUndo(e,!1),e}return null}resetRedoStack(){this.disableDefaultRedoBehavior||(this._redoStack=[],this.emit("redo-reset",{type:"redo-reset"}))}undoHas(e){return this._undoStack.includes(e)}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 nextOperation(){return this._redoStack.length>0?this._redoStack[this._redoStack.length-1]:null}get test(){return{undoLength:this._undoStack.length,redoLength:this._redoStack.length}}get _minNumVerticesPerType(){switch(this.data.type){case"point":return 1;case"polyline":return 2;case"polygon":return 3;default:return 0}}_apply(e,t=1){return 0!==t&&this.lastOperation?.accumulate(e)||(e.apply(),this.recordUndo(e,!0)),e}_createResetStateMesh(){if(!o(this.data))return t();const e=this.data.geometry,{vertexSpace:s}=e;if(s.origin){const o=r(s.origin);return t(()=>{e.vertexSpace.origin=o})}const i=e.vertexAttributes.clonePositional();return t(()=>{e.vertexAttributes=i,e.vertexAttributesChanged()})}static fromGeometry(e,t,r){return new S(i.fromGeometry(e,t,r),t,r?.allowCurves)}}export{S as EditGeometryOperations};