Issue #2908Opened July 21, 2020by neon123450 reactions

FEAT: Combine getDirtyCount with undo/redo

Question

Code SnippetTEXT
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;
            }
        });

Answers (3)

neon12345July 27, 20200 reactions

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

artfAugust 25, 20200 reactions

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

Related Questions and Answers

Continue research with similar issue discussions.

Paid Plugins That Match This Issue

Curated by issue keywords and label relevance to help you ship faster.

View all plugins

Loading paid plugin recommendations...

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.