UNPKG

@arcgis/core

Version:

ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API

3 lines (2 loc) 6.91 kB
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */ import{__addDisposableResource as e,__disposeResources as t,__decorate as r}from"tslib";import o from"../../../core/Collection.js";import{EventedAccessor as i}from"../../../core/Evented.js";import{disposable as s}from"../../../core/handleUtils.js";import n from"../../../core/ReactiveSet.js";import{deleteMany as a,addMany as p}from"../../../core/SetUtils.js";import{property as d,subclass as c}from"../../../core/accessorSupport/decorators.js";import{isCoordinate as h}from"../../../geometry/support/curves/curveUtils.js";import{UndoGroup as l}from"../../interactive/editGeometry/operations/UndoGroup.js";let y=class extends i{constructor(e){super(e),this._vertexRegistry=new Map,this._partRegistry=new Map,this._reverseVertexRegistry=new WeakMap,this._reversePartRegistry=new WeakMap,this.plugin=null,this.stepUndoStack=new o,this.stepRedoStack=new o,this.startingVertex=null,this.endVertex=null,this.vertices=new Set,this.operations=new n,this.undoneOperations=new Set,this.automaticRestart=!0,this.preserveSharedRedoAfterOperation=!1}get firstPointAddedBySession(){const{startingVertex:e}=this;return e?this._toVertexInfo(e):null}get lastPointAddedBySession(){const{endVertex:e}=this;return e?this._toVertexInfo(e):null}pushUndoStep(e){this.editGeometryOperations.resetRedoStack(),this.stepUndoStack.push(e??{}),this.stepRedoStack.length=0}peekUndoStep(){return this.stepUndoStack.at(-1)}restoreState(e){if(!e)return;const{undoSteps:t,redoSteps:r,automaticRestart:o}=e;void 0!==o&&(this.automaticRestart=o),t&&this.stepUndoStack.addMany(t),r&&this.stepRedoStack.addMany(r)}getInformationAboutPoint(e,t){const{parts:r,allVertices:o,coordinateHelper:i}=this.editGeometryOperations.data;if(!r.length||!o.length)return null;const s=r.at(t??-1),n=s?.vertices.at(e);if(!n)return null;const a=n.leftSegment?.toCurveOrCoordinate(),p=a&&!h(a)?a:void 0;return{point:i.vectorToPoint(n.pos),curveInfo:p,partIndex:n.part.index,pointIndex:n.index}}appendOrReplacePoint(e,t,r){const o=this.editGeometryOperations.data.coordinateHelper.pointToVector(t);this.appendOrReplaceCurve(e,o,r)}appendOrReplaceCurve(e,t,r){const o=e?this._vertexRegistry.get(e):null;if(o&&!r?.forceAppend)return this._applyInternal(()=>this.editGeometryOperations.setVertexPosition(o,t)),void this.notifyChange("endVertex");const i=this._applyInternal(()=>this.editGeometryOperations.appendVertex(t,this.editGeometryOperations.data.parts.at(-1),"polygon"===this.editGeometryOperations.data.type));if(!i)return;this.notifyChange("endVertex");const s=i.addedVertices.at(-1);s&&e&&(this._vertexRegistry.set(e,s),this._reverseVertexRegistry.set(s,e))}appendOrReplacePart(e,t,r){const o=e?this._partRegistry.get(e):null,i=o&&this.editGeometryOperations.data.parts.includes(o)?o:null,s=this._applyInternal(()=>this.editGeometryOperations.addOrUpdatePart(t,r?.forceAppend?null:i));if(!s)return;this.notifyChange("endVertex"),this.notifyChange("startingVertex");const n=s.addedVertices.at(-1)?.part;n&&e&&(this._partRegistry.set(e,n),this._reversePartRegistry.set(n,e))}groupGeometryChanges(e){const{operations:t}=this;try{this.editGeometryOperations.disableDefaultRedoBehavior=this.preserveSharedRedoAfterOperation;const r=this.editGeometryOperations.groupOperations(r=>t.has(r)&&(e||!(r instanceof l)));if(r){const{group:e,operations:o}=r;a(t,o),t.add(e)}}finally{this.editGeometryOperations.disableDefaultRedoBehavior=!1}}generatePreviewGeometry(e){const{endVertex:t,startingVertex:r,editGeometryOperations:o}=this,i=e.includeAutomaticConnection?r?.leftSegment?.leftVertex??r:r,s=this._resolveVertex(e?.firstIncludedPoint,i),n=this._resolveVertex(e?.lastIncludedPoint,t);return s||n?o?.data?.getPartialGeometry(s,n,e.geometryType):null}complete(){this.emit("completed",{})}sessionIsOperatingOnSameGeometry(e){return this.editGeometryOperations===e?.editGeometryOperations}eraseLastOperation(){const e=this.editGeometryOperations.lastOperation;if(!e||!this.operations.has(e))return;this._applyInternal(()=>this.editGeometryOperations.undo(),"undo")&&(this.notifyChange("endVertex"),this.notifyChange("startingVertex"))}syncVertices(e){const{vertices:t}=this,r=[];if(e)for(const s of e){const{addedVertices:e,removedVertices:o,addedParts:i,removedParts:n}=s;if(o){a(t,o);for(const e of o){const t=this._reverseVertexRegistry.get(e);t&&this._vertexRegistry.delete(t)}}if(e){p(t,e),r.push(...e);for(const t of e){const e=this._reverseVertexRegistry.get(t);e&&this._vertexRegistry.set(e,t)}}if(i)for(const t of i){const e=this._reversePartRegistry.get(t);e&&this._partRegistry.set(e,t)}if(n)for(const t of n){const e=this._reversePartRegistry.get(t);e&&this._partRegistry.delete(e)}}let o=null,i=null;for(const s of t)(!o||s.part.index<o.part.index||s.part.index===o.part.index&&s.index<=o.index)&&(o=s),(!i||s.part.index>i.part.index||s.part.index===i.part.index&&s.index>i.index)&&(i=s);return this.startingVertex=o,this.endVertex=i,r}mergeSession(e){this?.sessionIsOperatingOnSameGeometry(e)&&(p(this.vertices,e.vertices),p(this.operations,e.operations),p(this.undoneOperations,e.undoneOperations),this.syncVertices())}_applyInternal(e,t="apply"){let r=null;const o={session:this,blockApply:void 0};if(this.emit("before-apply-operation",o),o.blockApply)return null;const{editGeometryOperations:i}=this,s=u(i,()=>{try{i.disableDefaultRedoBehavior=this.automaticRestart||this.preserveSharedRedoAfterOperation||"undo"===t,r=e()}finally{i.disableDefaultRedoBehavior=!1}});r&&i.undoHas(r)&&"apply"===t&&this.operations.add(r);const n=this.syncVertices(s);return{operation:r,addedVertices:n}}_toVertexInfo(e){const t=e.leftSegment?.toCurveOrCoordinate(),r=!t||h(t)?void 0:t,o=e.part.index,i=e.index;return{point:this.editGeometryOperations.data.coordinateHelper.vectorToPoint(e.pos),curveInfo:r,partIndex:o,pointIndex:i}}_resolveVertex(e,t){return void 0!==e?.partIndex&&void 0!==e.pointIndex?this.editGeometryOperations.data.parts.at(e.partIndex)?.vertices.at(e.pointIndex)??null:t}};function u(r,o){const i={stack:[],error:void 0,hasError:!1};try{const t=[];e(i,s(r.data.on("change",e=>t.push(e))),!1);return o(),t}catch(n){i.error=n,i.hasError=!0}finally{t(i)}}r([d()],y.prototype,"plugin",void 0),r([d()],y.prototype,"stepUndoStack",void 0),r([d()],y.prototype,"stepRedoStack",void 0),r([d()],y.prototype,"startingVertex",void 0),r([d()],y.prototype,"endVertex",void 0),r([d({constructOnly:!0})],y.prototype,"editGeometryOperations",void 0),r([d()],y.prototype,"vertices",void 0),r([d()],y.prototype,"operations",void 0),r([d()],y.prototype,"undoneOperations",void 0),r([d()],y.prototype,"firstPointAddedBySession",null),r([d()],y.prototype,"lastPointAddedBySession",null),r([d()],y.prototype,"automaticRestart",void 0),r([d()],y.prototype,"preserveSharedRedoAfterOperation",void 0),y=r([c("esri.views.draw.support.EditSession")],y);export{y as EditSession,u as captureGeometryChangeEvents};