Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: getDirtyCount() non deterministic after load #5385

Closed
1 task done
padcom opened this issue Sep 14, 2023 · 5 comments
Closed
1 task done

BUG: getDirtyCount() non deterministic after load #5385

padcom opened this issue Sep 14, 2023 · 5 comments

Comments

@padcom
Copy link
Contributor

padcom commented Sep 14, 2023

GrapesJS version

  • I confirm to use the latest version of GrapesJS

What browser are you using?

Chrome

Reproducible demo link

https://github.com/padcom/grapesjs-bug-dirty-count-non-deterministic/blob/master/index.html

Describe the bug

Currently, when the user calls await load() the dirty count is zero. Only after the next event loop or later the count is increased. Therefore:

await editor.load()
console.log(editor.getDirtyCount())

prints out 0 but after a milisecond:

setTimeout(() => console.log(editor.getDirtyCount(), 1)

the number of changes is 1.

Expected behavior is that after awaiting the call to load(), if indeed as described in #5373 the editor is changed it should be so immediately after the call to load()


### Code of Conduct

- [X] I agree to follow this project's Code of Conduct
@artf artf closed this as completed in 0c01cf6 Sep 16, 2023
@artf
Copy link
Member

artf commented Sep 16, 2023

Added also load options in order to clear the editor internal state post load

const storageOptions = {};
const loadOptions = { clear: true };
await editor.load(storageOptions, loadOptions);
editor.getDirtyCount(); // should always be 0 post load

@padcom
Copy link
Contributor Author

padcom commented Sep 18, 2023

Thank you! Looking forward to the next release!

@artf
Copy link
Member

artf commented Sep 19, 2023

@padcom
Copy link
Contributor Author

padcom commented Sep 21, 2023

I checked the current implementation with the latest dev branch and... before the call to load() returns the storage manages to persist the changes if autosave is enabled. So the problem with saving a project that has just been loaded is still there.

The current remedy to it is to disable autoload before saving and then re-enabling it. Would you be open to making that part of the grapesjs codebase?

  async load<T extends StorageOptions>(options?: T, loadOptions: EditorLoadOptions = {}) {
    const autosave = this.Storage.config.autosave || true
    this.Storage.setAutosave(false)
    try {
      const result = await this.Storage.load(options);
      this.loadData(result);
      // Wait in order to properly update the dirty counter (#5385)
      await wait();

      if (loadOptions.clear) {
        this.UndoManager.clear();
        this.clearDirtyCount();
      }
      return result;
    } finally {
      this.Storage.setAutosave(autosave)
    }
  }

@artf
Copy link
Member

artf commented Sep 21, 2023

I guess you can simply initialize the editor with autosave: false and run editor.Storage.setAutosave(true) right after your initial load.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants