GetmeUK / ContentTools

A JS library for building WYSIWYG editors for HTML content.
http://getcontenttools.com
MIT License
3.94k stars 393 forks source link

Saving / Stop conflict #560

Open Cweet opened 4 years ago

Cweet commented 4 years ago

This is going to be a little bit hard to explain, so if something is unclear, please tell me. There is a little conflict between the saving and stopping functions. The saving is not equipped for handling request fails.

So when I initially used editor.stop(true) when the user clicks on "save" but when the request failed, it would clear out the whole "storage" and if I the user were to try to click on "save" again, it would not do anything because now the editor thinks no changes have been made.

So I started using editor.save(true), on "save" and when the request was successful I used editor.stop(true).

The problem is if I use editor.stop(false), it will check the reverts, and give the user the "are you sure you want to revert" message. If I use editor.stop(true), it will trigger save again. which means my whole listener will be triggered again. This is because there is two events called "saved" in the "_EditorApp.prototype.save".

One of them triggers if there are no changes. So I suggest either giving it a different event name or allow the stop function to stop without checking for reverts. So maybe have a second parameter, like editor.stop(false, false); // no save, no revert

if (root.lastModified() === this._rootLastModified && passive) {
    this.dispatchEvent(this.createEvent('saved', {
        regions: {},
        passive: passive
    }));
    return;
}

Right now, my quick solution is that I renamed the duplicate event to no_save, and now I can do this:

My trigger

$('#save_edit').click(function(e){
    e.preventDefault();
    editor.save(true);
});

My save function

editor.addEventListener('saved', function (ev, passive) {

       //my ajax call : on success{
      editor.stop(true);
}

});

Cancelling editor when nothing to save

editor.addEventListener('no_save', function (ev, passive) {
        editor.stop(false);
});
anthonyjb commented 4 years ago

I think you're right they probably should have different event names here, perhaps one should be 'passive_saved' and the other just 'saved'.

I'm not sure if this helps but you can also cancel the saved process by listening for the 'save' event (instead of the saved event) and returning false if you don't wish to continue the process.