FEAT: Combine getDirtyCount with undo/redo
Question
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)
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 !
Ok. But that means undo and redo will always trigger autosave, which is not optimal.
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.
Issue #3193
FEAT: Change of current internal object store to allow for relation mappings instead of current indexable container implementation
What are you trying to add to GrapesJS? Currently newly instantiated editor instances are added to a indexable collection where the user mu...
Issue #1888
[Question] Add custom html inside canvas
Hi there, I'm using grapes to export a custom json with all the information i need, instead of HTML and CSS. For doing that I'm parsing the...
Issue #1047
[BUG] Model not able to change after changing value in custom Select dropdown menu.
Hey, I'm using CDN link for grapes js from unpkg.com In my project, I've used custom trait as a drop-down select menu which changes HTML us...
Issue #3577
BUG: The UndoManager hasUndo() method returns true when it shouldnt
I am trying to disable the undo button when there are no undo actions to do. To do so, I am listening to 'change:changesCount' event, and u...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.