grapesjs icon indicating copy to clipboard operation
grapesjs copied to clipboard

FEAT: Combine getDirtyCount with undo/redo

Open neon12345 opened this issue 6 years ago • 4 comments

Currently the changesCount is only increased regardless of undo and redo. I wonder if there is an application where the actual value is used or if this is just a boolean indicator for changes.

If it is just used as boolean, it would be possible to instead add a flag to the undo state and mark the saved position. This way it is possible to signal changes only when the undo stack does not point to the saved position on undo and redo.

Something like this seems to work:

    var um = editor.UndoManager;
    var changepoint = null;
    var hasChange = false;

    function findChange() {
        var s = um.getStack();
        var idx = s.pointer;
        var old = hasChange;
        
        if(idx != -1) {
            hasChange = changepoint !== s.models[idx];
        } else {
            hasChange = changepoint !== null;
        }
        
        if(old != hasChange) {
            console.log('changed', hasChange);
        }
    } 
    
    function saveChange() {
        var s = um.getStack();
        var idx = s.pointer;

        if(idx != -1) {
            changepoint = s.models[idx];
        } else {
            changepoint = null;
        }
    }

    editor.on('undo', findChange);
    editor.on('redo', findChange);
    editor.on('load', function() {
        var i = um.getInstance();
        var add = i.constructor.prototype._addToStack;
        i.constructor.prototype._addToStack = function() {
            var ret = add.apply(this, arguments);
            findChange();
            return ret;
        }
    });

neon12345 avatar Jul 21 '20 07:07 neon12345

Hi @neon12345 !

As far as I know, the changesCount is also used in conjunction with stepsBeforeSave to trigger storing when autosave is enabled.

(cf: - https://grapesjs.com/docs/modules/Storage.html#store-and-load-templates - https://github.com/artf/grapesjs/blob/dev/src/editor/model/Editor.js#L170 )

Cheers !

mcottret avatar Jul 27 '20 09:07 mcottret

Ok. But that means undo and redo will always trigger autosave, which is not optimal.

neon12345 avatar Jul 27 '20 11:07 neon12345

getDirtyCount returns the number of changes, so it's not used as a boolean. If you have a better solution to handle undo/redo changes the PR is welcome

artf avatar Aug 25 '20 15:08 artf

This issue has been automatically closed because there has been no response to our request for more information from the original author. With only the information that is currently in the issue, we don't have enough information to take action. Please reach out if you have or find the answers we need so that we can investigate further.

no-response[bot] avatar Sep 04 '20 15:09 no-response[bot]