Search code examples
javascriptarcgisundo-redo

Unable to add a correct undo/redo operation for move/scale/rotate using arcgis


I have the following code by which I am reading when a graphic is being moved (scaling and rotation use similar code).

//Obtaining the graphic before it is moved
moveToolbar.on("graphic-move-start", function (evt) {
oldGraphicMove = evt.graphic;
});

//Updating the graphic on move end
moveToolbar.on("graphic-move-stop", function (evt) {

  //Creating the operation to add to the undomanager
  var operation = new Update({
  featureLayer: evt.graphic._graphicsLayer, //The layer that will contain the modified graphic
  preUpdatedGraphics: [oldGraphicMove], //The graphic before the changes are created
  postUpdatedGraphics: [evt.graphic] //The graphic after the changes are made
});

//Adding the undo/redo operation
undoManager.add(operation);
//Updating the graphic
evt.graphic._graphicsLayer.applyEdits(null, [evt.graphic], null);

});

For some reason the old graphic before is always staying equal to the new graphic after so when the operation is added there is nothing to undo/redo as the graphic is marked as equal before and after.

I have no idea what I might be doing wrong, any clues about this?


Solution

  • Apparently the oldGraphicMove variable on the start of editing was being overwritten by the evt.graphic from the editing end. This was resolved by creating a new graphic with the specifications of the graphic before it was edited rather than assigning the graphic directly to the variable :

    //Obtaining the graphic before it is scaled
    moveToolbar.on("scale-first-move", function (evt) {
         oldGraphicScale = new esri.Graphic(evt.graphic.geometry,evt.graphic.symbol,evt.graphic.attributes);
                                                        });
    
    //Updating the graphic on scale end
    moveToolbar.on("scale-stop", function (evt) {
    
        newGraphicScale = new esri.Graphic(evt.graphic.geometry, evt.graphic.symbol, evt.graphic.attributes);
    
       //Creating the operation to add to the undomanager
       var operation = new Update({
             featureLayer: evt.graphic._graphicsLayer, //The layer that will contain the modified graphic  
             preUpdatedGraphics: [oldGraphicScale], //The graphic before the changes are created
             postUpdatedGraphics: [newGraphicScale] //The graphic after the changes are made
                                                            });
    
    //Adding the undo/redo operation
    undoManager.add(operation);
    
    //Updating the graphic
    evt.graphic._graphicsLayer.applyEdits(null, [evt.graphic], null);
                                                        });
    

    There are still a few issues with undoing/redoing movement, however this method works for undoing/redoing scale and rotation.