Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(ribbon): tick only one of Code and Markdown in the note type dropdown
Elian Doran1799ff7e57e0
diff --git a/apps/client/src/services/note_types.spec.ts b/apps/client/src/services/note_types.spec.tsindex eeb56a1bd6..00b343c7c8 100644--- a/apps/client/src/services/note_types.spec.ts+++ b/apps/client/src/services/note_types.spec.ts@@ -17,7 +17,7 @@ vi.mock("./experimental_features.js", () => ({}));import { isExperimentalFeatureEnabled } from "./experimental_features.js";-import noteTypesService from "./note_types";+import noteTypesService, { isCurrentNoteType } from "./note_types";const llmFlag = vi.mocked(isExperimentalFeatureEnabled);@@ -409,3 +409,30 @@ describe("new template badges", () => {}});});++describe("isCurrentNoteType", () => {+ const CODE = { type: "code", mime: "text/plain" } as const;+ const MARKDOWN = { type: "code", mime: "text/x-markdown" } as const;++ it("tells the two code entries apart, which share a type", () => {+ // A JavaScript note is the plain code entry, never the Markdown one.+ const script = buildNote({ title: "script", type: "code", mime: "application/javascript" });+ expect(isCurrentNoteType(CODE, script)).toBe(true);+ expect(isCurrentNoteType(MARKDOWN, script)).toBe(false);++ // Every mime `isMarkdown()` accepts picks the Markdown entry, `text/x-gfm` included.+ for (const mime of [ "text/x-markdown", "text/markdown", "text/x-gfm" ]) {+ const note = buildNote({ title: mime, type: "code", mime });+ expect(isCurrentNoteType(MARKDOWN, note)).toBe(true);+ expect(isCurrentNoteType(CODE, note)).toBe(false);+ }+ });++ it("matches every other type on the type alone, and nothing without a note", () => {+ const text = buildNote({ title: "prose", type: "text", mime: "text/html" });+ expect(isCurrentNoteType({ type: "text", mime: "text/html" }, text)).toBe(true);+ expect(isCurrentNoteType({ type: "canvas", mime: "application/json" }, text)).toBe(false);+ expect(isCurrentNoteType(CODE, text)).toBe(false);+ expect(isCurrentNoteType(CODE, null)).toBe(false);+ });+});diff --git a/apps/client/src/services/note_types.ts b/apps/client/src/services/note_types.tsindex 961b5ac062..f8f53d9a71 100644--- a/apps/client/src/services/note_types.ts+++ b/apps/client/src/services/note_types.ts@@ -27,6 +27,9 @@ export interface NoteTypeMapping {static?: boolean;}+/** The mime carried by the Markdown entry, which shares `type: "code"` with the plain code one. */+export const MARKDOWN_NOTE_TYPE_MIME = "text/x-markdown";+export const NOTE_TYPES: NoteTypeMapping[] = [// The suggested note type ordering method: insert the item into the corresponding group,// then ensure the items within the group are ordered alphabetically.@@ -55,7 +58,7 @@ export const NOTE_TYPES: NoteTypeMapping[] = [// Code notes{ type: "code", mime: "text/plain", title: t("note_types.code"), icon: "bx-code" },- { type: "code", mime: "text/x-markdown", title: t("note_types.markdown"), icon: "bxl-markdown", isNew: true },+ { type: "code", mime: MARKDOWN_NOTE_TYPE_MIME, title: t("note_types.markdown"), icon: "bxl-markdown", isNew: true },// Reserved types (cannot be created by the user){ type: "contentWidget", mime: "", title: t("note_types.widget"), reserved: true },@@ -65,6 +68,18 @@ export const NOTE_TYPES: NoteTypeMapping[] = [{ type: "launcher", mime: "", title: t("note_types.launcher"), reserved: true },];+/**+ * Whether a {@link NOTE_TYPES} entry names the type `note` already has, so a menu can tick it.+ *+ * Code and Markdown are both `type: "code"`, so comparing types alone marks both of them for any+ * code note; the note's mime is what tells the two entries apart.+ */+export function isCurrentNoteType(entry: Pick<NoteTypeMapping, "type" | "mime">, note: FNote | null | undefined) {+ if (!note || entry.type !== note.type) return false;+ if (entry.type !== "code") return true;+ return note.isMarkdown() === (entry.mime === MARKDOWN_NOTE_TYPE_MIME);+}+/*** One thing a new note can be made from: a blank note type, or a note carrying `#template`.*diff --git a/apps/client/src/widgets/ribbon/BasicPropertiesTab.tsx b/apps/client/src/widgets/ribbon/BasicPropertiesTab.tsxindex 54e073e89f..f612da5e74 100644--- a/apps/client/src/widgets/ribbon/BasicPropertiesTab.tsx+++ b/apps/client/src/widgets/ribbon/BasicPropertiesTab.tsx@@ -11,7 +11,7 @@ import { isExperimentalFeatureEnabled } from "../../services/experimental_featurimport { getAvailableLocales, getLocaleById, t } from "../../services/i18n";import { resolveContentLanguage } from "../../utils/formatters";import mime_types from "../../services/mime_types";-import { NOTE_TYPES } from "../../services/note_types";+import { isCurrentNoteType, NOTE_TYPES } from "../../services/note_types";import protected_session from "../../services/protected_session";import server from "../../services/server";import sync from "../../services/sync";@@ -111,7 +111,7 @@ export function NoteTypeDropdownContent({ currentNoteType, currentNoteMime, note});}- const checked = (type === currentNoteType);+ const checked = isCurrentNoteType({ type, mime }, note);if (noCodeNotes || type !== "code") {return (<FormListItem