UNPKG

@arcgis/core

Version:

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

3 lines (2 loc) 6.89 kB
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */ import{__decorate as t}from"tslib";import e from"../../../../core/Collection.js";import s from"../../../../core/Error.js";import{EventedAccessor as o}from"../../../../core/Evented.js";import{destroyMaybe as n}from"../../../../core/maybe.js";import{property as i}from"../../../../core/accessorSupport/decorators/property.js";import{subclass as r}from"../../../../core/accessorSupport/decorators/subclass.js";import{EditSession as a,captureGeometryChangeEvents as d}from"../EditSession.js";import{EditGeometry as p}from"../../../interactive/editGeometry/EditGeometry.js";import{EditGeometryOperations as c}from"../../../interactive/editGeometry/EditGeometryOperations.js";let l=class extends o{constructor(t){super(t),this._sessionStack=new e,this._pluginSessionLookup=new WeakMap,this._sharedEditGeometry=null}destroy(){this._sharedEditGeometry=n(this._sharedEditGeometry),this._sessionStack.drain(t=>t.destroy())}get _lastOperableSessionIndex(){const t=this._sessionStack.items.findLastIndex(t=>t.editGeometryOperations===this._sharedEditGeometry);return t>=0?t:null}get editGeometryOperations(){return this._sharedEditGeometry}get outputGeometry(){return this._sharedEditGeometry?.data.geometry}get canUndo(){const t=this._sharedEditGeometry?.canUndo??!1;return this._sessionStack.some(e=>(e.stepUndoStack.length>0||e.operations.size>0&&t)&&!e.automaticRestart)}get canRedo(){return this._sharedEditGeometry?.canRedo||this._sessionStack.some(t=>t.stepRedoStack.length>0)}get test(){return{sessionStack:this._sessionStack}}startSession(t,e,s,o){const n=e?this._sharedEditGeometry??=this._setupEditGeometryOperations(s,this):this._setupEditGeometryOperations(s,t),i=new a({editGeometryOperations:n,plugin:t});return i.restoreState(o),i.addHandles([i.on("completed",()=>{this.completeSession(i)}),i.on("before-apply-operation",t=>{const e=t.session;if(t.blockApply=!0,e.editGeometryOperations!==this._sharedEditGeometry)return void(t.blockApply=!1);const s=this._lastOperableSessionIndex;this._sessionStack.indexOf(e)===s&&(t.blockApply=!1)})]),this._sessionStack.push(i),this._pluginSessionLookup.set(t,i),i}cancelSession(t){const e=this._pluginSessionLookup.get(t);if(!e)return;const s=this._sessionStack.indexOf(e);if(!(s<0))for(let o=this._sessionStack.length-1;o>=s;o--){const t=this._sessionStack.at(o);this._discardSessionOperations(t),this._cleanUpSession(t),this._sessionStack.remove(t)}}completeSession(t){const e=this._sessionStack;for(;e.length>0;){const s=e.pop(),o=e.at(-1);if(o?.mergeSession(s),this._cleanUpSession(s),s===t)break}}getSession(t){return this._pluginSessionLookup.get(t)??null}undo(){const t=this._undoAndGetRestartInfo([]);t.length>0&&this._restartSession(t.toReversed())}redo(){const t=this._sharedEditGeometry;if(!t||!this.canRedo||0===this._sessionStack.length)return;const e=t.nextOperation,s=[],o=[];let n=null,i=!1;for(let a=0;a<this._sessionStack.length;a++){const t=this._sessionStack.at(a);if(e&&t.undoneOperations.has(e)){n=a,i=!0;break}t.stepRedoStack.length>0&&null===n&&(n=a)}if(null===n)return;for(let a=this._sessionStack.length-1;a>n;a--){const t=this._sessionStack.at(a);if(t.automaticRestart){this._discardSessionOperations(t);const e=this._requestCancelSession(t,"undo",!1);e&&s.push(e)}else o.push(t)}const r=this._sessionStack.at(n);if(i)this._redoOperation(r,t),o.push(r);else{const t=this._requestCancelSession(r,"redo",!0);t&&s.push(t)}s.length>0&&this._restartSession(s.toReversed());for(const{plugin:a}of o)a?.afterUndoRedo?.("redo")}generatePreviewGeometryForPlugins(t){const e=this.editGeometryOperations,s=e?.data.type;if(!s||"mesh"===s)return null;const o=t.map(t=>this._pluginSessionLookup.get(t)).filter(t=>t?.editGeometryOperations===e),n=o.findLast(t=>t?.endVertex)?.endVertex,i=o.find(t=>t?.startingVertex)?.startingVertex;return i&&n?this.editGeometryOperations?.data.getPartialGeometry(i,n,s):null}_discardSessionOperations(t){const e=t.editGeometryOperations;if(!e)return;let s=e.lastOperation;try{for(e.disableDefaultRedoBehavior=!0;s&&t.operations.has(s);)this._undoOperation(t,e),s=e.lastOperation}finally{e.disableDefaultRedoBehavior=!1}}_undoAndGetRestartInfo(t){const e=this._sharedEditGeometry;if(!e||!this.canUndo||0===this._sessionStack.length)return t;const s=e.lastOperation;let o=!1;for(let n=this._sessionStack.length-1;n>=0;n--){const i=this._sessionStack.at(n),r=s&&i.operations.has(s);if(i.automaticRestart){this._discardSessionOperations(i);const e=this._requestCancelSession(i,"undo",!1);e&&t.push(e)}else{if(i.stepUndoStack.length>0){const e=this._requestCancelSession(i,"undo",!0);e&&t.push(e),o=!i.stepUndoStack.length;break}if(r){this._undoOperation(i,e),i.plugin?.afterUndoRedo?.("undo");break}}if(r){o=!0;break}}return o&&this._undoAndGetRestartInfo(t),t}_setupEditGeometryOperations(t,e){const{coordinateHelper:o,viewingMode:n}=this.getContext?.()??{};if(!o||!n)throw new s("esri-drawtool:edit-session-missing-context","Failed to initialize edit session");const i=new c(new p(t,o),n,!0);return e.addHandles([i.data.on("change",()=>{this.notifyChange("outputGeometry")}),i.on("can-redo-change",()=>{this.notifyChange("canRedo")}),i.on("can-undo-change",()=>{this.notifyChange("canUndo")}),i.on("redo-reset",()=>{this._sessionStack.forEach(t=>t.undoneOperations.clear()),this.notifyChange("canRedo")})]),i}_requestCancelSession(t,e,s){try{const o=t.plugin;if(!o)return null;const n=o.configuration,i=o?.suspend?.();let r=null;const{stepUndoStack:a,stepRedoStack:d,automaticRestart:p}=t;"undo"===e&&!t.automaticRestart&&s?(r=a.pop(),r&&d.push(r)):"redo"===e&&s&&(r=d.pop(),r&&a.push(r));const c={undoSteps:[...a],redoSteps:[...d],automaticRestart:p,isConnected:t.editGeometryOperations===this._sharedEditGeometry,geometryType:t.editGeometryOperations.data.geometry?.type};return this.emit("cancelPlugin",{plugin:o}),n?{pluginConfiguration:n,pluginState:i,sessionState:c,currentInternalStep:a.at(-1),pluginInstance:o}:null}finally{this._cleanUpSession(t)}}_restartSession(t){this.emit("restartPlugin",{restartInfo:t}),this.notifyChange("outputGeometry")}_cleanUpSession(t){t.plugin=null,t.destroy()}_undoOperation(t,e){let s=null;const o=d(e,()=>{s=e.undo()});return t&&(t.syncVertices(o),s&&(t.operations.delete(s),t.undoneOperations.add(s))),s}_redoOperation(t,e){let s=null;const o=d(e,()=>{s=e.redo()});return t?.syncVertices(o),t&&s&&(t.operations.add(s),t.undoneOperations.delete(s)),s}};t([i()],l.prototype,"_sharedEditGeometry",void 0),t([i()],l.prototype,"_lastOperableSessionIndex",null),t([i()],l.prototype,"editGeometryOperations",null),t([i()],l.prototype,"outputGeometry",null),t([i()],l.prototype,"getContext",void 0),t([i()],l.prototype,"canUndo",null),t([i()],l.prototype,"canRedo",null),t([i()],l.prototype,"test",null),l=t([r("esri.views.draw.support.managers.EditSessionManager")],l);export{l as EditSessionManager};