Skip to content

Build your personal knowledge base with Trilium Notes

client/options/spell check: switch to the new card component

Adorian Doranc04a152b2354
diff --git a/apps/client/src/widgets/type_widgets/options/components/OptionsPageHeader.css b/apps/client/src/widgets/type_widgets/options/components/OptionsPageHeader.css
index 88b8438745..9daad61a10 100644
--- a/apps/client/src/widgets/type_widgets/options/components/OptionsPageHeader.css
+++ b/apps/client/src/widgets/type_widgets/options/components/OptionsPageHeader.css
@@ -69,6 +69,12 @@
justify-content: flex-end;
}
+/* A page whose settings all hang off one switch carries it here rather than in a card of its own.
+ It is a card option without a card, so it needs the room a segment would have given it. */
+.options-header-switch {
+ padding-block: 4px;
+}
+
/* A full-width bar that sticks to the top of the scrolling content, so cards scroll cleanly beneath
it. The background is painted per context (below) so the sticky bar matches its surroundings. */
.options-page-header {
diff --git a/apps/client/src/widgets/type_widgets/options/llm.css b/apps/client/src/widgets/type_widgets/options/llm.css
index cb5046de67..3b2c2e3bf4 100644
--- a/apps/client/src/widgets/type_widgets/options/llm.css
+++ b/apps/client/src/widgets/type_widgets/options/llm.css
@@ -1,9 +1,3 @@
-/* The page's own switch, standing on the header's second row rather than in a card: it carries the
- card option's layout without a card's frame, so it needs the room a segment would have given it. */
-.llm-master-switch {
- padding-block: 4px;
-}
-
.llm-provider-name {
display: inline-flex;
align-items: center;
diff --git a/apps/client/src/widgets/type_widgets/options/llm.tsx b/apps/client/src/widgets/type_widgets/options/llm.tsx
index c1459c8187..e6ad10db1b 100644
--- a/apps/client/src/widgets/type_widgets/options/llm.tsx
+++ b/apps/client/src/widgets/type_widgets/options/llm.tsx
@@ -31,7 +31,7 @@ export default function LlmSettings() {
helpUrl="GBBMSlVSOIGP"
below={
<CardOption
- className="llm-master-switch"
+ className="options-header-switch"
name="ai-enabled"
label={t("llm.enabled")}
description={t("llm.enabled_description")}
diff --git a/apps/client/src/widgets/type_widgets/options/spellcheck.page.spec.tsx b/apps/client/src/widgets/type_widgets/options/spellcheck.page.spec.tsx
new file mode 100644
index 0000000000..cae40488ee
--- /dev/null
+++ b/apps/client/src/widgets/type_widgets/options/spellcheck.page.spec.tsx
@@ -0,0 +1,146 @@
+import { render } from "preact";
+import { act } from "preact/test-utils";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+const mocks = vi.hoisted(() => ({
+ electron: true,
+ enabled: true,
+ languageCode: "en-US",
+ saved: [] as [ string, string | boolean ][],
+ triggerCommand: vi.fn(),
+ spellcheck: {
+ setSpellCheckerEnabled: vi.fn(),
+ setSpellCheckerLanguages: vi.fn(),
+ getAvailableSpellCheckerLanguages: vi.fn(() => [ "de-DE", "en-US" ]),
+ addWordToDictionary: vi.fn()
+ }
+}));
+
+vi.mock("../../../services/i18n", () => ({ t: (key: string) => key }));
+
+// The page is desktop-only; a server build has no spell checker of its own to configure.
+vi.mock("../../../services/utils", async (importOriginal) => ({
+ ...(await importOriginal<typeof import("../../../services/utils")>()),
+ isElectron: () => mocks.electron
+}));
+
+vi.mock("../../../components/app_context", () => ({
+ default: { triggerCommand: mocks.triggerCommand }
+}));
+
+vi.mock("../../react/hooks", async (importOriginal) => ({
+ ...(await importOriginal<typeof import("../../react/hooks")>()),
+ useTriliumOption: (name: string) => [
+ mocks.languageCode,
+ (value: string) => void mocks.saved.push([ name, value ])
+ ],
+ useTriliumOptionBool: (name: string) => [
+ mocks.enabled,
+ (value: boolean) => void mocks.saved.push([ name, value ])
+ ]
+}));
+
+vi.mock("./components/OptionsPageHeader", () => ({
+ default: ({ below }: { below?: preact.ComponentChildren }) => <div className="header-stub">{below}</div>
+}));
+
+import SpellcheckSettings from "./spellcheck";
+
+let host: HTMLElement;
+
+beforeEach(() => {
+ mocks.electron = true;
+ mocks.enabled = true;
+ mocks.languageCode = "en-US";
+ mocks.saved = [];
+ (window as unknown as { electronApi: unknown }).electronApi = { spellcheck: mocks.spellcheck };
+ host = document.body.appendChild(document.createElement("div"));
+});
+
+afterEach(() => {
+ render(null, host);
+ document.body.innerHTML = "";
+ delete (window as unknown as { electronApi?: unknown }).electronApi;
+ vi.clearAllMocks();
+});
+
+function open() {
+ act(() => {
+ render(null, host);
+ render(<SpellcheckSettings />, host);
+ });
+}
+
+const cardHeadings = () =>
+ [ ...host.querySelectorAll(".tn-card-heading") ].map((heading) => heading.firstChild?.textContent);
+const masterSwitch = () => host.querySelector<HTMLInputElement>(".header-stub input.switch-toggle");
+
+describe("the spellcheck page on a server build", () => {
+ it("says the page has nothing to offer here, rather than showing settings that do nothing", () => {
+ mocks.electron = false;
+ open();
+
+ expect(host.querySelector(".no-items")).not.toBeNull();
+ expect(host.querySelector(".tn-card")).toBeNull();
+ });
+});
+
+describe("the master switch", () => {
+ it("stands in the header, since everything on the page hangs off it", () => {
+ open();
+
+ expect(masterSwitch()).not.toBeNull();
+ expect(masterSwitch()?.closest(".tn-card")).toBeNull();
+ });
+
+ it("brings out the languages and the dictionary only once spellchecking is on", () => {
+ open();
+ expect(cardHeadings()).toEqual([
+ "spellcheck.language_code_label",
+ "spellcheck.custom_dictionary_title"
+ ]);
+
+ mocks.enabled = false;
+ open();
+ // The switch is the page: nothing stands in for what it turns off.
+ expect(cardHeadings()).toEqual([]);
+ expect(masterSwitch()).not.toBeNull();
+ });
+});
+
+describe("choosing the languages", () => {
+ it("offers what the checker actually has, named in the reader's own language and sorted by it", () => {
+ open();
+
+ const labels = [ ...host.querySelectorAll(".spellcheck-languages label") ].map((label) => label.textContent?.trim());
+ expect(labels).toHaveLength(2);
+ expect(labels).toEqual([ ...labels ].sort((a, b) => (a ?? "").localeCompare(b ?? "")));
+ });
+
+ it("reads the stored codes as a list, whatever spacing they were written with", () => {
+ mocks.languageCode = " en-US , de-DE ";
+ open();
+
+ const ticked = [ ...host.querySelectorAll<HTMLInputElement>(".spellcheck-languages input") ]
+ .filter((box) => box.checked);
+ expect(ticked).toHaveLength(2);
+ });
+
+ it("stores nothing at all when the checker offers no languages", () => {
+ mocks.spellcheck.getAvailableSpellCheckerLanguages.mockReturnValueOnce([]);
+ open();
+
+ expect(host.querySelectorAll(".spellcheck-languages input")).toHaveLength(0);
+ });
+});
+
+describe("the custom dictionary", () => {
+ it("opens the note it is kept in, rather than editing it here", () => {
+ open();
+
+ const button = host.querySelector<HTMLButtonElement>("button[name='open-custom-dictionary']");
+ act(() => button?.click());
+
+ expect(mocks.triggerCommand).toHaveBeenCalledWith("openInPopup", { noteIdOrPath: "_customDictionary" });
+ });
+});
diff --git a/apps/client/src/widgets/type_widgets/options/spellcheck.spec.tsx b/apps/client/src/widgets/type_widgets/options/spellcheck.spec.tsx
index 8d0bce20b0..5c889cd6bb 100644
--- a/apps/client/src/widgets/type_widgets/options/spellcheck.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/options/spellcheck.spec.tsx
@@ -26,8 +26,11 @@ vi.mock("../../../services/utils", async (importActual) => ({
isElectron: vi.fn(() => true)
}));
+// Stubbed for the note context it would otherwise need, but both its slots are rendered: the page's
+// master switch lives on the row below the title.
vi.mock("./components/OptionsPageHeader", () => ({
- default: ({ actions }: { actions?: ComponentChildren }) => <div className="header-stub">{actions}</div>
+ default: ({ actions, below }: { actions?: ComponentChildren; below?: ComponentChildren }) =>
+ <div className="header-stub">{actions}{below}</div>
}));
vi.mock("../../react/FormToggle", () => ({
diff --git a/apps/client/src/widgets/type_widgets/options/spellcheck.tsx b/apps/client/src/widgets/type_widgets/options/spellcheck.tsx
index 3b141e5903..be44281644 100644
--- a/apps/client/src/widgets/type_widgets/options/spellcheck.tsx
+++ b/apps/client/src/widgets/type_widgets/options/spellcheck.tsx
@@ -4,13 +4,12 @@ import appContext from "../../../components/app_context";
import { t } from "../../../services/i18n";
import { isElectron } from "../../../services/utils";
import Button from "../../react/Button";
-import FormText from "../../react/FormText";
+import { Card, CardOption, CardSection } from "../../react/Card";
+import FormToggle from "../../react/FormToggle";
import { useTriliumOption, useTriliumOptionBool } from "../../react/hooks";
import NoItems from "../../react/NoItems";
import CheckboxList from "./components/CheckboxList";
import OptionsPageHeader from "./components/OptionsPageHeader";
-import OptionsRow, { OptionsRowWithToggle } from "./components/OptionsRow";
-import OptionsSection from "./components/OptionsSection";
export default function SpellcheckSettings() {
if (isElectron()) {
@@ -41,17 +40,20 @@ function ElectronSpellcheckSettings() {
return (
<>
- <OptionsPageHeader />
-
- <OptionsSection>
- <OptionsRowWithToggle
- name="spellcheck-enabled"
- label={t("spellcheck.enable")}
- description={t("spellcheck.enable_description")}
- currentValue={spellCheckEnabled}
- onChange={onToggle}
- />
- </OptionsSection>
+ {/* The switch governs the whole page rather than anything on it, so it belongs to the
+ header — on the row below the title, clear of the dialog's own close button. */}
+ <OptionsPageHeader
+ below={
+ <CardOption
+ className="options-header-switch"
+ name="spellcheck-enabled"
+ label={t("spellcheck.enable")}
+ description={t("spellcheck.enable_description")}
+ >
+ <FormToggle currentValue={spellCheckEnabled} onChange={onToggle} />
+ </CardOption>
+ }
+ />
{spellCheckEnabled && (
<>
@@ -97,15 +99,19 @@ function SpellcheckLanguages() {
}, []);
return (
- <OptionsSection title={t("spellcheck.language_code_label")}>
- <CheckboxList
- values={availableLanguages}
- keyProperty="code" titleProperty="name"
- currentValue={selectedCodes}
- onChange={setSelectedCodes}
- columnWidth="200px"
- />
- </OptionsSection>
+ <div className="options-section spellcheck-languages">
+ <Card heading={t("spellcheck.language_code_label")}>
+ <CardSection>
+ <CheckboxList
+ values={availableLanguages}
+ keyProperty="code" titleProperty="name"
+ currentValue={selectedCodes}
+ onChange={setSelectedCodes}
+ columnWidth="200px"
+ />
+ </CardSection>
+ </Card>
+ </div>
);
}
@@ -115,28 +121,35 @@ function CustomDictionary() {
}
return (
- <OptionsSection title={t("spellcheck.custom_dictionary_title")}>
- <FormText>{t("spellcheck.custom_dictionary_description")}</FormText>
-
- <OptionsRow name="custom-dictionary" label={t("spellcheck.custom_dictionary_edit")} description={t("spellcheck.custom_dictionary_edit_description")}>
- <Button
- name="open-custom-dictionary"
- text={t("spellcheck.custom_dictionary_open")}
- icon="bx bx-edit"
- onClick={openDictionary}
- />
- </OptionsRow>
- </OptionsSection>
+ <div className="options-section">
+ <Card
+ heading={t("spellcheck.custom_dictionary_title")}
+ description={t("spellcheck.custom_dictionary_description")}
+ >
+ <CardOption
+ label={t("spellcheck.custom_dictionary_edit")}
+ description={t("spellcheck.custom_dictionary_edit_description")}
+ >
+ <Button
+ name="open-custom-dictionary"
+ text={t("spellcheck.custom_dictionary_open")}
+ icon="bx-edit"
+ size="micro"
+ onClick={openDictionary}
+ />
+ </CardOption>
+ </Card>
+ </div>
);
}
function WebSpellcheckSettings() {
return (
- <OptionsSection>
+ <div className="options-section">
<NoItems
text={t("spellcheck.description")}
icon="bx bx-check-double"
/>
- </OptionsSection>
+ </div>
);
}