Skip to content

Build your personal knowledge base with Trilium Notes

client: fix an issue

Adorian Doran07daa1c8362e
diff --git a/apps/client/src/widgets/type_widgets/options/content_languages_card.spec.tsx b/apps/client/src/widgets/type_widgets/options/content_languages_card.spec.tsx
index 6040eb5ff7..b2bc14dc10 100644
--- a/apps/client/src/widgets/type_widgets/options/content_languages_card.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/options/content_languages_card.spec.tsx
@@ -105,14 +105,30 @@ describe("ContentLanguagesCard", () => {
});
});
- it("removes a language with its button", async () => {
+ it("removes a language with its button, handing its focus to the nearest one left", async () => {
+ options.set("languages", JSON.stringify([ "ro", "de", "fr", "it" ]));
const container = draw();
- const remove = segment(container, "Français")?.querySelector(".content-languages-remove");
- expect(remove).toBeInstanceOf(HTMLElement);
-
- await click(remove ?? null);
- expect(stored()).toEqual([ "ro", "de" ]);
+ const removeButton = (name: string) => segment(container, name)
+ ?.querySelector<HTMLElement>(".content-languages-remove") ?? null;
+ expect(removeButton("Français")).toBeInstanceOf(HTMLElement);
+
+ // Without the focus, there is none to hand on.
+ await click(removeButton("Français"));
+ expect(stored()).toEqual([ "ro", "de", "it" ]);
+ expect(names(container)).toEqual([ "Deutsch", "Italiano", "Română" ]);
+ expect(document.activeElement).toBe(document.body);
+
+ act(() => removeButton("Italiano")?.focus());
+ await click(removeButton("Italiano"));
expect(names(container)).toEqual([ "Deutsch", "Română" ]);
+ expect(document.activeElement).toBe(removeButton("Română"));
+
+ await click(removeButton("Română"));
+ expect(document.activeElement).toBe(removeButton("Deutsch"));
+
+ await click(removeButton("Deutsch"));
+ expect(stored()).toEqual([]);
+ expect(document.activeElement).toBe(container.querySelector(".tn-card-adder"));
});
});
diff --git a/apps/client/src/widgets/type_widgets/options/content_languages_card.tsx b/apps/client/src/widgets/type_widgets/options/content_languages_card.tsx
index 7f0f415eaa..b3e6210179 100644
--- a/apps/client/src/widgets/type_widgets/options/content_languages_card.tsx
+++ b/apps/client/src/widgets/type_widgets/options/content_languages_card.tsx
@@ -85,7 +85,12 @@ export function ContentLanguagesCard({ heading, actions }: ContentLanguagesCardP
className="content-languages-remove"
icon="bx bx-x"
text={t("content_language.remove")}
- onClick={() => remove(language.id)}
+ onClick={(event) => {
+ if (document.activeElement === event.currentTarget) {
+ focusNearestLanguage(event.currentTarget);
+ }
+ remove(language.id);
+ }}
/>
</CardSection>
))}
@@ -105,3 +110,17 @@ export function ContentLanguagesCard({ heading, actions }: ContentLanguagesCardP
function readLanguages() {
return (options.getJson("languages") as string[] | null) ?? [];
}
+
+/**
+ * Moves the focus off a remove button whose language is about to go: to the next language's remove
+ * button, else the previous one's, else the add button. The rows are keyed, so the one focused here
+ * stays in the document when the other is removed.
+ */
+function focusNearestLanguage(removeButton: HTMLElement) {
+ const row = removeButton.closest(".content-languages-item");
+ const nearest = [ row?.nextElementSibling, row?.previousElementSibling ]
+ .find((sibling) => sibling?.classList.contains("content-languages-item"));
+ const target = nearest?.querySelector<HTMLElement>(".content-languages-remove")
+ ?? row?.parentElement?.querySelector<HTMLElement>(".tn-card-adder");
+ target?.focus();
+}