Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Fix: UXW-564 CMD-Z on an existing Document removes all content (+ other edge case) (#62692)

Brad Anderson54e84ef141d4
diff --git a/e2e/test/scenarios/documents/documents.cy.spec.ts b/e2e/test/scenarios/documents/documents.cy.spec.ts
index a16cd98699a..893eb72dfff 100644
--- a/e2e/test/scenarios/documents/documents.cy.spec.ts
+++ b/e2e/test/scenarios/documents/documents.cy.spec.ts
@@ -227,6 +227,34 @@ H.describeWithSnowplowEE("documents", () => {
});
});
});
+
+ it("should handle undo/redo properly, resetting the history whenever a different document is viewed", () => {
+ const isMac = Cypress.platform === "darwin";
+ const metaKey = isMac ? "Meta" : "Control";
+ cy.get("@documentId").then((id) => cy.visit(`/document/${id}`));
+ H.getDocumentCard("Orders").should("exist");
+ H.documentContent().within(() => {
+ const originalText = "Lorem Ipsum and some more words";
+ const originalExact = new RegExp(`^${originalText}$`);
+ cy.contains(originalExact).click();
+ cy.realPress([metaKey, "z"]);
+ cy.contains(originalExact);
+
+ const modification = " etc.";
+ const modifiedExact = new RegExp(`^${originalText}${modification}$`);
+ H.addToDocument(modification, false);
+ cy.contains(modifiedExact);
+ cy.realPress([metaKey, "z"]);
+ cy.contains(originalExact);
+ cy.realPress(["Shift", metaKey, "z"]);
+ cy.contains(modifiedExact);
+ cy.realPress([metaKey, "z"]); // revert to prevent "unsaved changes" dialog
+ });
+ H.newButton("Document").click();
+ H.documentContent().should("have.text", "");
+ cy.realPress([metaKey, "z"]);
+ H.documentContent().should("have.text", "");
+ });
});
});
diff --git a/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/Editor.tsx b/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/Editor.tsx
index adbea023c76..c3c9bb9b4f5 100644
--- a/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/Editor.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/Editor.tsx
@@ -164,7 +164,11 @@ export const Editor: React.FC<EditorProps> = ({
if (editor && initialContent !== undefined) {
// Use Promise.resolve() to avoid flushSync warning
Promise.resolve().then(() => {
- editor.commands.setContent(initialContent || "");
+ editor
+ .chain()
+ .setMeta("addToHistory", false)
+ .setContent(initialContent || "")
+ .run();
});
}
}, [editor, initialContent]);