Skip to content

Build your personal knowledge base with Trilium Notes

feat(mobile): pick a code language from a filterable submenu

Elian Doran80956b001e2d
diff --git a/apps/client/src/widgets/mobile_widgets/mobile_detail_menu.css b/apps/client/src/widgets/mobile_widgets/mobile_detail_menu.css
index 80dcda4b54..588e4ef0bf 100644
--- a/apps/client/src/widgets/mobile_widgets/mobile_detail_menu.css
+++ b/apps/client/src/widgets/mobile_widgets/mobile_detail_menu.css
@@ -1,7 +1,3 @@
-.code-note-switcher-modal .dropdown-menu {
- background: none !important;
-}
-
/* The modal is the frame here, so the list keeps neither the cap nor the padding it is given for the
ribbon's tab (see SimilarNotesTab.css). Kept to the new layout, as it was when it lived in
StatusBar.css — the old mobile look is not this change's to alter. */
diff --git a/apps/client/src/widgets/mobile_widgets/mobile_detail_menu.tsx b/apps/client/src/widgets/mobile_widgets/mobile_detail_menu.tsx
index be46fd908a..d5df2bf126 100644
--- a/apps/client/src/widgets/mobile_widgets/mobile_detail_menu.tsx
+++ b/apps/client/src/widgets/mobile_widgets/mobile_detail_menu.tsx
@@ -13,8 +13,9 @@ import ActionButton from "../react/ActionButton";
import type { DropdownHandle } from "../react/Dropdown";
import { FormDropdownDivider, FormDropdownSubmenu, FormListItem } from "../react/FormList";
import { useNoteContext, useNoteProperty } from "../react/hooks";
+import { MenuItemRows } from "../react/Menu";
import Modal from "../react/Modal";
-import { NoteTypeCodeNoteList, useLanguageSwitcher, useMimeTypes } from "../ribbon/BasicPropertiesTab";
+import { codeLanguageItems, useLanguageSwitcher, useMimeTypes } from "../ribbon/BasicPropertiesTab";
import { NoteContextMenu } from "../ribbon/NoteActions";
import NoteActionsCustom from "../ribbon/NoteActionsCustom";
import { NotePathsWidget, useSortedNotePaths } from "../ribbon/NotePathsTab";
@@ -30,7 +31,6 @@ export default function MobileDetailMenu() {
const [ notePathsModalShown, setNotePathsModalShown ] = useState(false);
const [ noteInfoModalShown, setNoteInfoModalShown ] = useState(false);
const [ similarNotesModalShown, setSimilarNotesModalShown ] = useState(false);
- const [ codeNoteSwitcherModalShown, setCodeNoteSwitcherModalShown ] = useState(false);
const sortedNotePaths = useSortedNotePaths(note, hoistedNoteId);
const backlinksCount = useBacklinkCount(note, viewScope?.viewMode === "default");
@@ -94,7 +94,7 @@ export default function MobileDetailMenu() {
</>}
itemsNearNoteSettings={<>
{note.type === "text" && <ContentLanguageSelector note={note} />}
- {note.type === "code" && <FormListItem icon={"bx bx-code"} onClick={() => setCodeNoteSwitcherModalShown(true)}>{t("status_bar.code_note_switcher")}</FormListItem>}
+ {note.type === "code" && <CodeLanguageSelector note={note} />}
<FormListItem icon="bx bx-info-circle" onClick={() => setNoteInfoModalShown(true)}>{t("note_info_widget.title")}</FormListItem>
<FormListItem icon="bx bx-bar-chart" onClick={() => setSimilarNotesModalShown(true)}>{t("similar_notes.title")}</FormListItem>
<FormDropdownDivider />
@@ -114,7 +114,6 @@ export default function MobileDetailMenu() {
<NotePathsModal note={note} modalShown={notePathsModalShown} notePath={noteContext?.notePath} sortedNotePaths={sortedNotePaths} setModalShown={setNotePathsModalShown} />
<NoteInfoModal note={note} modalShown={noteInfoModalShown} setModalShown={setNoteInfoModalShown} />
<SimilarNotesModal note={note} modalShown={similarNotesModalShown} setModalShown={setSimilarNotesModalShown} />
- <CodeNoteSwitcherModal note={note} modalShown={codeNoteSwitcherModalShown} setModalShown={setCodeNoteSwitcherModalShown} />
</>
), document.body)}
</div>
@@ -146,6 +145,22 @@ function ContentLanguageSelector({ note }: { note: FNote | null | undefined }) {
);
}
+/** The code languages to switch the note to, filtered as the reader types. */
+function CodeLanguageSelector({ note }: { note: FNote }) {
+ const currentNoteMime = useNoteProperty(note, "mime");
+ const { enabledMimeTypes } = useMimeTypes();
+
+ return (
+ <FormDropdownSubmenu icon="bx bx-code" title={t("status_bar.code_note_switcher")} filterable>
+ <MenuItemRows items={codeLanguageItems({
+ currentMimeType: currentNoteMime,
+ mimeTypes: enabledMimeTypes,
+ changeNoteType: (type, mime) => void server.put(`notes/${note.noteId}/type`, { type, mime })
+ })} />
+ </FormDropdownSubmenu>
+ );
+}
+
interface WithModal {
modalShown: boolean;
setModalShown: (shown: boolean) => void;
@@ -211,29 +226,3 @@ function SimilarNotesModal({ note, modalShown, setModalShown }: { note: FNote |
</Modal>
);
}
-
-function CodeNoteSwitcherModal({ note, modalShown, setModalShown }: { note: FNote | null | undefined } & WithModal) {
- const currentNoteMime = useNoteProperty(note, "mime");
- const { enabledMimeTypes } = useMimeTypes();
-
- return (
- <Modal
- className="code-note-switcher-modal"
- size="md"
- title={t("status_bar.code_note_switcher")}
- show={modalShown}
- onHidden={() => setModalShown(false)}
- >
- <div className="dropdown-menu static show">
- {note && <NoteTypeCodeNoteList
- currentMimeType={currentNoteMime}
- mimeTypes={enabledMimeTypes}
- changeNoteType={(type, mime) => {
- server.put(`notes/${note.noteId}/type`, { type, mime });
- setModalShown(false);
- }}
- />}
- </div>
- </Modal>
- );
-}
diff --git a/apps/client/src/widgets/react/Dropdown.spec.tsx b/apps/client/src/widgets/react/Dropdown.spec.tsx
index 8ea88c8115..614a3021f7 100644
--- a/apps/client/src/widgets/react/Dropdown.spec.tsx
+++ b/apps/client/src/widgets/react/Dropdown.spec.tsx
@@ -4,6 +4,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import Dropdown, { type DropdownHandle, DropdownPanel, type DropdownPanelProps } from "./Dropdown";
import { FormDropdownSubmenu, FormListItem } from "./FormList";
+import { MenuItemRows } from "./Menu";
// A dialog's focus trap would pull focus out of a menu portaled over it.
const focusTraps = vi.hoisted(() => ({ suspend: vi.fn(() => () => {}) }));
@@ -115,6 +116,43 @@ describe("Dropdown", () => {
expect(picked).toEqual([ "JavaScript" ]);
});
+ it("filters the rows a filterable submenu of components draws from data, on a desktop and on a phone", async () => {
+ for (const onMobile of [ false, true ]) {
+ layout.onMobile = onMobile;
+ const picked: string[] = [];
+ const row = (title: string) => ({ title, handler: () => picked.push(title) });
+ const { toggle } = renderDropdown({}, (
+ <FormDropdownSubmenu title="Language" filterable>
+ <MenuItemRows items={[ row("Python"), row("JavaScript") ]} />
+ </FormDropdownSubmenu>
+ ));
+ const key = (name: string) => {
+ const event = new KeyboardEvent("keydown", { key: name, bubbles: true, cancelable: true });
+ (document.activeElement ?? document.body).dispatchEvent(event);
+ };
+ const input = () => popup()?.querySelector<HTMLInputElement>("input.tn-menu-filter-input") ?? null;
+ const titles = () => [ ...popup()?.querySelectorAll("li.dropdown-item:not(.dropdown-submenu)") ?? [] ]
+ .map((item) => item.querySelector(".tn-menu-filter-title")?.textContent ?? item.textContent);
+
+ click(toggle);
+ await vi.waitFor(() => expect(document.activeElement).toBe(popup()));
+ key("ArrowDown");
+ // Each key waits for the one before it to render, as a key press arrives in a task of its own.
+ await vi.waitFor(() => expect(popup()?.getAttribute("aria-activedescendant")).toBeTruthy());
+ key(onMobile ? "Enter" : "ArrowRight");
+ await vi.waitFor(() => expect(input()).not.toBeNull());
+ expect(titles()).toEqual([ "Python", "JavaScript" ]);
+
+ key("j");
+ await vi.waitFor(() => expect(document.activeElement).toBe(input()));
+ expect(titles()).toEqual([ "JavaScript" ]);
+ key("Enter");
+ expect(picked).toEqual([ "JavaScript" ]);
+ await vi.waitFor(() => expect(popup()).toBeNull());
+ render(null, host);
+ }
+ });
+
it("opens a popup below its toggle on a click, and closes it on another", async () => {
const onShown = vi.fn();
const onHidden = vi.fn();
diff --git a/apps/client/src/widgets/ribbon/BasicPropertiesTab.tsx b/apps/client/src/widgets/ribbon/BasicPropertiesTab.tsx
index 777b297143..c1e095cdad 100644
--- a/apps/client/src/widgets/ribbon/BasicPropertiesTab.tsx
+++ b/apps/client/src/widgets/ribbon/BasicPropertiesTab.tsx
@@ -146,13 +146,6 @@ interface CodeLanguageListProps {
onConfigure?(): void;
}
-/** The code languages to switch a note to, as rows of a menu or a list. See {@link codeLanguageItems}. */
-export function NoteTypeCodeNoteList({ setModalShown, ...props }: Omit<CodeLanguageListProps, "onConfigure"> & {
- setModalShown?(shown: boolean): void;
-}) {
- return <MenuItemRows items={codeLanguageItems({ ...props, onConfigure: setModalShown && (() => setModalShown(true)) })} />;
-}
-
/** The code languages to switch a note to, the current one ticked, as menu items. */
export function codeLanguageItems({ currentMimeType, mimeTypes, changeNoteType, onConfigure }: CodeLanguageListProps) {
const items: MenuItem<unknown>[] = mimeTypes.map(({ title, mime }) => ({