@doegis/core
Version:
DOE GIS API
3 lines (1 loc) • 3.37 kB
JavaScript
import e from"../../../core/Evented.js";import{isNone as t}from"../../../core/maybe.js";import{EditGeometry as r}from"./EditGeometry.js";import{AccumulationBehaviour as o}from"./interfaces.js";import{AppendVertex as s}from"./operations/AppendVertex.js";import{UpdateVertices as i,AccumulationType as n}from"./operations/UpdateVertices.js";import{RemoveVertices as a}from"./operations/RemoveVertices.js";import{SplitEdge as p}from"./operations/SplitEdge.js";import{SetVertexPosition as d}from"./operations/SetVertexPosition.js";import{CloseComponent as c}from"./operations/CloseComponent.js";import{MoveVertex as h}from"./operations/MoveVertex.js";import{OffsetEdgeVertex as u}from"./operations/OffsetEdgeVertex.js";import{RotateVertex as m}from"./operations/RotateVertex.js";import{ScaleVertex as l}from"./operations/ScaleVertex.js";import{UndoGroup as _}from"./operations/UndoGroup.js";class V extends e{constructor(e){super(),this.data=e,this._undoStack=[],this._redoStack=[],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(){this._listener.remove()}splitEdge(e,t){return this._apply(new p(this.data,e,t))}updateVertices(e,t,r=o.ACCUMULATE_STEPS){return this._apply(new i(this.data,e,t),r)}moveVertices(e,t,r,s,i=o.ACCUMULATE_STEPS){return this.updateVertices(e,new h(this.data.coordinateHelper,t,r,s),i)}scaleVertices(e,t,r,s,i,a=o.ACCUMULATE_STEPS,p=n.CUMULATIVE){return this.updateVertices(e,new l(t,r,s,i,p),a)}rotateVertices(e,t,r,s=o.ACCUMULATE_STEPS,i=n.CUMULATIVE){return this.updateVertices(e,new m(t,r,i),s)}removeVertices(e){return this._apply(new a(this.data,e,this._minNumVerticesPerType))}appendVertex(e){return 0===this.data.components.length?null:this._apply(new s(this.data,this.data.components[0],e))}setVertexPosition(e,t){return this._apply(new d(this.data,e,t))}offsetEdge(e,t,r,s=o.ACCUMULATE_STEPS){return this.updateVertices([t.leftVertex,t.rightVertex],new u(this.data.coordinateHelper,e,t,r),s)}closeComponent(e){return this.data.components.includes(e)?this._apply(new c(this.data,e)):null}canRemoveVertex(){return this.data.components[0].vertices.length>this._minNumVerticesPerType}createUndoGroup(){const e=new _;return this._apply(e),{remove:()=>e.close()}}undo(){if(this._undoStack.length>0){const e=this._undoStack.pop();return e.undo(),this._redoStack.push(e),e}return null}redo(){if(this._redoStack.length>0){const e=this._redoStack.pop();return e.apply(),this._undoStack.push(e),e}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(e,r=o.ACCUMULATE_STEPS){return r!==o.NEW_STEP&&!t(this.lastOperation)&&this.lastOperation.accumulate(e)||(e.apply(),this._undoStack.push(e),this._redoStack=[]),e}static fromGeometry(e,t){return new V(r.fromGeometry(e,t))}}export{V as EditGeometryOperations};