Skip to content

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

Allow `return` to focus body of Documents (#65732)

Maz Amelifd033ce0bf1b
diff --git a/e2e/test/scenarios/documents/documents.cy.spec.ts b/e2e/test/scenarios/documents/documents.cy.spec.ts
index 220a89e9610..14db7def573 100644
--- a/e2e/test/scenarios/documents/documents.cy.spec.ts
+++ b/e2e/test/scenarios/documents/documents.cy.spec.ts
@@ -156,6 +156,27 @@ H.describeWithSnowplowEE("documents", () => {
.should("have.attr", "contenteditable", "false");
});
+ it("should focus the start of the document body when pressing Enter on the title input", () => {
+ cy.visit("/document/new");
+
+ cy.log("Type a title");
+ cy.findByRole("textbox", { name: "Document Title" })
+ .should("be.focused")
+ .type("Doc Title{enter}");
+
+ cy.log("Add some content to the document body");
+ H.addToDocument("One{enter}Two");
+
+ cy.log("Click back on the title to focus it and hit Enter");
+ cy.findByRole("textbox", { name: "Document Title" })
+ .click()
+ .type("{enter}");
+
+ cy.log("Focus should be placed at the beginning of the document body");
+ cy.realType("NEW: ");
+ H.documentContent().should("have.text", "NEW: OneTwo");
+ });
+
it("should handle navigating from /new to /new gracefully", () => {
cy.visit("/");
H.newButton("Document").click();
diff --git a/enterprise/frontend/src/metabase-enterprise/documents/components/DocumentHeader.tsx b/enterprise/frontend/src/metabase-enterprise/documents/components/DocumentHeader.tsx
index 0b3bc1314cc..9f88e982ed6 100644
--- a/enterprise/frontend/src/metabase-enterprise/documents/components/DocumentHeader.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/documents/components/DocumentHeader.tsx
@@ -45,6 +45,7 @@ interface DocumentHeaderProps {
showSaveButton: boolean;
isBookmarked: boolean;
onTitleChange: (title: string) => void;
+ onTitleSubmit?: () => void;
onSave: () => void;
onMove: () => void;
onToggleBookmark: () => void;
@@ -60,6 +61,7 @@ export const DocumentHeader = ({
showSaveButton,
isBookmarked,
onTitleChange,
+ onTitleSubmit,
onSave,
onMove,
onToggleBookmark,
@@ -106,6 +108,15 @@ export const DocumentHeader = ({
readOnly={!canWrite}
maxLength={DOCUMENT_TITLE_MAX_LENGTH}
classNames={{ input: S.titleInput }}
+ onKeyDown={(event) => {
+ if (event.key === "Enter") {
+ event.preventDefault();
+
+ if (canWrite) {
+ onTitleSubmit?.();
+ }
+ }
+ }}
/>
{document && (
<Flex gap="md">
diff --git a/enterprise/frontend/src/metabase-enterprise/documents/components/DocumentPage.tsx b/enterprise/frontend/src/metabase-enterprise/documents/components/DocumentPage.tsx
index e90023b05da..6009d9dd6df 100644
--- a/enterprise/frontend/src/metabase-enterprise/documents/components/DocumentPage.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/documents/components/DocumentPage.tsx
@@ -353,6 +353,10 @@ export const DocumentPage = ({
],
);
+ const focusEditorBody = useCallback(() => {
+ editorInstance?.commands.focus("start");
+ }, [editorInstance]);
+
const handleUpdate = async (payload: {
collection_id?: CollectionId | null;
archived?: boolean;
@@ -420,6 +424,7 @@ export const DocumentPage = ({
showSaveButton={showSaveButton ?? false}
isBookmarked={isBookmarked}
onTitleChange={setDocumentTitle}
+ onTitleSubmit={focusEditorBody}
onSave={() => {
if (isNewDocument) {
setCollectionPickerMode("save");