GrapesJS / grapesjs

Free and Open source Web Builder Framework. Next generation tool for building templates without coding
https://grapesjs.com
BSD 3-Clause "New" or "Revised" License
22.37k stars 4.05k forks source link

BUG: `getDirtyCount()` returns `1` after loading #5373

Closed padcom closed 1 year ago

padcom commented 1 year ago

GrapesJS version

What browser are you using?

Chrome latest

Reproducible demo link

https://github.com/padcom/grapes-bug-dirty-after-loading/blob/master/src/App.vue#L37

Describe the bug

  1. Clone and start the project
    git clone https://github.com/padcom/grapes-bug-dirty-after-loading
    cd grapes-bug-dirty-after-loading
    npm i
    npm start
  2. Open console

Current behavior: number of changes is 1 Expected behavior: number of changes is 0

Additional note: when using autoload number of changes stays at 0

Code of Conduct

padcom commented 1 year ago

Please note, that if you enable autoload:

    storageManager: {
      type: 'example',
      autoload: true,
      autosave: false,
    },

and disable loading:

  // console.log('App.vue - before load', editor.getDirtyCount())
  // const result = await editor.load({})
  // console.log('App.vue - after load', editor.getDirtyCount(), result)

number of changes is 0, even though the result is the same - the page is loaded. Since in my project I desperately need to be able to load the page at a specific time this is messing everything up. I also tried clearing the number of changes after loading, but since the code that sets it does so in a setTimeout() callback it's not possible to do it deterministically.

artf commented 1 year ago

Hi @padcom this is an expected behavior as the load itself updates the state of the editor. You can simply clear the dirty counter via editor.clearDirtyCount() right after your manual load.

padcom commented 1 year ago

I beg your pardon? Are you quite serious that it is the "expected" behavior? Have you seen that in any other editor out there that the project is automatically modified upon loading and then automatically saved? I find that hard to believe.

artf commented 1 year ago

I beg your pardon? Are you quite serious that it is the "expected" behavior?

You have the initial state, you load manually a new project data, the state is changed and you have autosave: false, so yeah my friend, I'm quite serious...

Have you seen that in any other editor out there that the project is automatically modified upon loading and then automatically saved?

You're loading the state manually so it's a bit different. You've seen how with autoload: true the behavior is not the same and I told you already how to reset the dirty count manually in case you need it.

padcom commented 1 year ago

With all due respect, this is madness. Had that at least been deterministic (await load()...) and not done in setTimeout() callback outside of the regular async processing that'd be at least a workaround. With the increase to dirty count outside of the normal promise (which it currently is) one doesn't simply do

await editor.load(...)
clearDirtyCount()

because in the end the number of changes is still 1.

That is not ok, it's confusing, nowhere described and point blank a bug. It so violates the principle of least surprisse so vital in providing a predictable behavior.

padcom commented 1 year ago

On top of that, using autoload: false and autosave: true (which is completely valid from the definition standpoint as those are two separate fields) makes the code load and then save for no apparent reason. I know I can increase the number of changes before autosave occurs but that's clearly a workaround rather than proper behavior.