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

Preserve the original editor observer #1006

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ var _ = Mavo.Backend = class Backend extends EventTarget {
}

addEventListener("message", evt => {
if (evt.source === this.authPopup) {
if (evt.source === this.authPopup && evt.data.backend) {
if (evt.data.backend == this.id) {
this.accessToken = localStorage[`mavo:${id}token`] = evt.data.token;
}
Expand Down
11 changes: 6 additions & 5 deletions src/mavo.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ let _ = self.Mavo = $.Class(class Mavo {
$.bind(this.element, "mv-login.mavo", evt => {
if (evt.backend == (this.source || this.storage)) {
// If last time we rendered we got nothing, maybe now we'll have better luck?
if (this.inProgress !== "loading" && !this.root.data && !this.unsavedChanges) {
if (!this.root.data && !this.unsavedChanges) {
this.load();
}
}
Expand Down Expand Up @@ -546,9 +546,6 @@ let _ = self.Mavo = $.Class(class Mavo {
return;
}

let autoSaveState = this.autoSave;
this.autoSave = false;

if (data === undefined) {
this.inProgress = "loading";

Expand Down Expand Up @@ -588,13 +585,17 @@ let _ = self.Mavo = $.Class(class Mavo {
this.inProgress = false;
}

let autoSaveState = this.autoSave;
this.autoSave = false;

this.render(data);

this.autoSave = autoSaveState;

await Mavo.defer();

this.dataLoaded.resolve();
this.element.dispatchEvent(new CustomEvent("mv-load", {detail: backend, bubbles: true}));
this.autoSave = autoSaveState;
}

async store () {
Expand Down
11 changes: 9 additions & 2 deletions src/primitive.js
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,14 @@ var _ = Mavo.Primitive = class Primitive extends Mavo.Node {
}

for (let primitive of nodes) {
primitive.originalEditorUpdated({force: true});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this line removed accidentally or we genuinely don't need it anymore?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My investigation shows that this line is the one that was added in the first bad commit and caused the regression. The only thing I did was revert the previous version of this code block.

I think I understand why this line was present—we get the same result as when we have the two added lines. However, to add to this, we destroy the original editor observer and replace it with the observer of the clone of the original editor. It works fine if the element is not in the edit mode initially. But when it is, we clone the original editor before Mavo can update expressions and handle the original editor mutations.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like we need this line since, without it, we break another test with dynamic selects. I should continue working on a proper fix.

if (primitive.defaultSource == "editor") {
primitive.default = this.originalEditor.value;
}

if (primitive.editor) {
primitive.editor = this.originalEditor.cloneNode(true);
}

primitive.setValue(primitive.value, {force: true, silent: true});
}
});
Expand All @@ -441,7 +448,7 @@ var _ = Mavo.Primitive = class Primitive extends Mavo.Node {

let editor = this.editor ?? this.originalEditor;

if (editor?.matches("select:not(.mv-options-select")) {
if (editor?.matches("select:not(.mv-options-select)")) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoa, how did this go undetected all this time?!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have it not so long as you might imagine—not longer than we have mv-options. 😉 We couldn't detect it thanks to the browser forgiveness, I believe. I checked in the console—both variants return the same result.

// This is a select menu that is not automatically generated from mv-options
// We need to update this.options

Expand Down