Skip to content
Merged
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
56 changes: 56 additions & 0 deletions src/gui/ChoiceBuilder/builderInitialFocus.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";

// FormatPreviewField -> formatter graph pulls obsidian-dataview's CJS require.
vi.mock("obsidian-dataview", () => ({ getAPI: vi.fn() }));

import { App, Modal } from "obsidian";
import type QuickAdd from "../../main";
import { CaptureChoice } from "../../types/choices/CaptureChoice";
import { MacroChoice } from "../../types/choices/MacroChoice";
import { TemplateChoice } from "../../types/choices/TemplateChoice";
import { MacroBuilder } from "../MacroGUIs/MacroBuilder";
import { CaptureChoiceBuilder } from "./captureChoiceBuilder";
import { TemplateChoiceBuilder } from "./templateChoiceBuilder";

const plugin = {
getTemplateFiles: () => [],
settings: { choices: [] },
} as unknown as QuickAdd;

// The builders opened with focus on different elements: the Macro builder's
// title rename button (a focus ring around the title), the Template/Capture
// builders' footer Done. Cause: Obsidian's Modal.open() focuses the first
// focusable element in modalEl, and only the Macro builder builds its content
// before open(). The stub's open() does not autofocus, so emulate it here;
// otherwise a fix that focuses Done before open() would pass the test but lose
// to open() in Obsidian.
describe("choice builder initial focus", () => {
beforeEach(() => {
const open = Modal.prototype.open;
vi.spyOn(Modal.prototype, "open").mockImplementation(function (
this: InstanceType<typeof Modal>,
) {
open.call(this);
this.modalEl
.querySelector<HTMLElement>("button, input, select, textarea, [tabindex]")
?.focus();
});
});

afterEach(() => {
vi.restoreAllMocks();
document.body.replaceChildren();
});

it.each([
["Macro", () => new MacroBuilder(new App(), plugin, new MacroChoice("Macro"), [])],
["Template", () => new TemplateChoiceBuilder(new App(), new TemplateChoice("Template"), plugin)],
["Capture", () => new CaptureChoiceBuilder(new App(), new CaptureChoice("Capture"), plugin)],
])("%s builder opens with focus on the footer's Done", (_type, openBuilder) => {
const modal = openBuilder();

const done = modal.modalEl.querySelector(".qa-builder-footer button.mod-cta");
expect(done?.textContent).toBe("Done");
expect(document.activeElement).toBe(done);
});
});
3 changes: 2 additions & 1 deletion src/gui/ChoiceBuilder/choiceBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ export abstract class ChoiceBuilder extends Modal {
});

this.containerEl.addClass("quickAddModal");
this.open();
// Installed here, not in display(): display() runs from the subclass
// constructor and is re-run by builders that rebuild their content.
// After open() so the footer's Done keeps the initial focus.
addAutosaveFooter(this, "choice");
this.open();
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/gui/ChoiceBuilder/components/autosaveFooter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ import type { Modal } from "obsidian";
* content cannot drop it — MacroBuilder.reload() empties contentEl. Idempotent
* for the same reason.
*
* Also gives Done the initial focus, so every builder opens the same way. Call
* it AFTER `modal.open()`: open() autofocuses the first focusable element in the
* modal, which was the title's rename button in the Macro builder (built before
* open) but Done in the Template/Capture builders (their forms mount after open).
* Done is the safe target: Enter only closes, and closing saves.
*
* @param subject What the modal edits, e.g. "choice" or "macro".
*/
export function addAutosaveFooter(modal: Modal, subject: string): void {
Expand All @@ -33,4 +39,5 @@ export function addAutosaveFooter(modal: Modal, subject: string): void {

const done = footer.createEl("button", { type: "button", cls: "mod-cta", text: "Done" });
done.addEventListener("click", () => modal.close());
done.focus();
}
3 changes: 2 additions & 1 deletion src/gui/MacroGUIs/MacroBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,11 @@ export class MacroBuilder extends Modal {
);

this.display();
this.open();
// Installed here, not in display(): reload() re-runs display(), which
// empties contentEl. The footer lives on modalEl and survives that.
// After open() so the footer's Done, not the title, gets initial focus.
addAutosaveFooter(this, "macro");
this.open();
}

onClose() {
Expand Down
Loading