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
client/icon pack/preview: fix the view mode switcher buttons not in sync with the current view mode when #displayMode is not set
Adorian Doranbc0a9e74df94
diff --git a/apps/client/src/widgets/FloatingButtonsDefinitions.tsx b/apps/client/src/widgets/FloatingButtonsDefinitions.tsxindex f908b4304f..4db4859a37 100644--- a/apps/client/src/widgets/FloatingButtonsDefinitions.tsx+++ b/apps/client/src/widgets/FloatingButtonsDefinitions.tsx@@ -21,9 +21,10 @@ import { createImageSrcUrl, isElectron, openInAppHelpFromUrl } from "../servicesimport { ViewTypeOptions } from "./collections/interface";import ActionButton, { ActionButtonProps } from "./react/ActionButton";import { ButtonGroup } from "./react/Button";-import { useIsNoteReadOnly, useNoteLabel, useNoteLabelBoolean, useTriliumEvent, useTriliumOption, useWindowSize } from "./react/hooks";+import { useEffectiveReadOnly, useIsNoteReadOnly, useNoteLabel, useNoteLabelBoolean, useTriliumEvent, useTriliumOption, useWindowSize } from "./react/hooks";import NoteLink from "./react/NoteLink";import RawHtml from "./react/RawHtml";+import { isSplitEditorForcedReadOnly, resolveDisplayMode } from "./type_widgets/helpers/split_editor_mode";export interface FloatingButtonContext {parentComponent: Component;@@ -112,12 +113,14 @@ function ToggleReadOnlyButton({ note, isDefaultViewMode }: FloatingButtonContext/>;}-function DisplayModeSwitcher({ note, isDefaultViewMode }: FloatingButtonContext) {+function DisplayModeSwitcher({ note, noteContext, isDefaultViewMode }: FloatingButtonContext) {const [ displayMode, setDisplayMode ] = useNoteLabel(note, "displayMode");+ const readOnly = useEffectiveReadOnly(note, noteContext);const isEnabled = (note.isMarkdown() || note.type === "mermaid" || note.isIconPack()) && note.isContentAvailable() && isDefaultViewMode;if (!isEnabled) return false;- const mode = displayMode === "source" || displayMode === "preview" ? displayMode : "split";+ // Mirror SplitEditor's mode resolution so the active button matches the actual pane.+ const mode = resolveDisplayMode(displayMode, readOnly || isSplitEditorForcedReadOnly(note));const buttons: Array<{ value: "source" | "split" | "preview"; icon: string; text: string }> = [{ value: "source", icon: "bx bx-code", text: t("display_mode.source") },{ value: "split", icon: "bx bxs-dock-left", text: t("display_mode.split") },diff --git a/apps/client/src/widgets/ribbon/NoteActionsCustom.tsx b/apps/client/src/widgets/ribbon/NoteActionsCustom.tsxindex d44590646e..f1230a6295 100644--- a/apps/client/src/widgets/ribbon/NoteActionsCustom.tsx+++ b/apps/client/src/widgets/ribbon/NoteActionsCustom.tsx@@ -17,7 +17,8 @@ import ActionButton, { ActionButtonProps } from "../react/ActionButton";import { ButtonGroup } from "../react/Button";import { FormFileUploadActionButton, FormFileUploadFormListItem, FormFileUploadProps } from "../react/FormFileUpload";import { FormListItem } from "../react/FormList";-import { useNoteLabel, useNoteLabelBoolean, useNoteProperty, useTriliumEvent, useTriliumEvents, useTriliumOption } from "../react/hooks";+import { useEffectiveReadOnly, useNoteLabel, useNoteLabelBoolean, useNoteProperty, useTriliumEvent, useTriliumEvents, useTriliumOption } from "../react/hooks";+import { isSplitEditorForcedReadOnly, resolveDisplayMode } from "../type_widgets/helpers/split_editor_mode";import { ParentComponent } from "../react/react_utils";import { buildUploadNewFileRevisionListener } from "./FilePropertiesTab";import { buildUploadNewImageRevisionListener } from "./ImagePropertiesTab";@@ -206,12 +207,14 @@ function SwitchSplitOrientationButton({ note, isReadOnly, isDefaultViewMode }: N/>;}-function DisplayModeSwitcher({ note, isDefaultViewMode }: NoteActionsCustomInnerProps) {+function DisplayModeSwitcher({ note, noteContext, isDefaultViewMode }: NoteActionsCustomInnerProps) {const [ displayMode, setDisplayMode ] = useNoteLabel(note, "displayMode");+ const readOnly = useEffectiveReadOnly(note, noteContext);const isEnabled = (note.isMarkdown() || note.type === "mermaid" || note.isIconPack()) && note.isContentAvailable() && isDefaultViewMode;if (!isEnabled) return null;- const mode = displayMode === "source" || displayMode === "preview" ? displayMode : "split";+ // Mirror SplitEditor's mode resolution so the active button matches the actual pane.+ const mode = resolveDisplayMode(displayMode, readOnly || isSplitEditorForcedReadOnly(note));const buttons: Array<{ value: "source" | "split" | "preview"; icon: string; text: string }> = [{ value: "source", icon: "bx bx-code", text: t("display_mode.source") },{ value: "split", icon: "bx bxs-dock-left", text: t("display_mode.split") },diff --git a/apps/client/src/widgets/type_widgets/helpers/SplitEditor.tsx b/apps/client/src/widgets/type_widgets/helpers/SplitEditor.tsxindex 77a052ac97..80e72433c4 100644--- a/apps/client/src/widgets/type_widgets/helpers/SplitEditor.tsx+++ b/apps/client/src/widgets/type_widgets/helpers/SplitEditor.tsx@@ -12,6 +12,7 @@ import { ExtendedAdmonition } from "../../react/Admonition";import { Badge } from "../../react/Badge";import { useEffectiveReadOnly, useNoteBlob, useNoteLabel, useTriliumOption } from "../../react/hooks";import { EditableCode, EditableCodeProps, ReadOnlyCode } from "../code/Code";+import { resolveDisplayMode } from "./split_editor_mode";export interface SplitEditorProps extends EditableCodeProps {className?: string;@@ -50,9 +51,7 @@ export default function SplitEditor({ note, noteContext, error, previewStale, spconst splitEditorOrientation = useSplitOrientation(forceOrientation);const [ displayMode ] = useNoteLabel(note, "displayMode");const readOnly = useEffectiveReadOnly(note, noteContext) || Boolean(forceReadOnly);- const mode = displayMode === "source" || displayMode === "split" || displayMode === "preview"- ? displayMode- : readOnly ? "preview" : "split";+ const mode = resolveDisplayMode(displayMode, readOnly);// Lazy-mount each pane on first need, then keep it mounted so subsequent switches stay instant.const editorMounted = useRef(mode !== "preview");diff --git a/apps/client/src/widgets/type_widgets/helpers/split_editor_mode.spec.ts b/apps/client/src/widgets/type_widgets/helpers/split_editor_mode.spec.tsnew file mode 100644index 0000000000..33a4e6fd5d--- /dev/null+++ b/apps/client/src/widgets/type_widgets/helpers/split_editor_mode.spec.ts@@ -0,0 +1,34 @@+import { describe, expect, it } from "vitest";++import { buildNote } from "../../../test/easy-froca";+import { isSplitEditorForcedReadOnly, resolveDisplayMode } from "./split_editor_mode";++describe("resolveDisplayMode", () => {+ it("uses the #displayMode label when it names a valid mode, regardless of read-only", () => {+ expect(resolveDisplayMode("source", false)).toBe("source");+ expect(resolveDisplayMode("split", true)).toBe("split");+ expect(resolveDisplayMode("preview", false)).toBe("preview");+ });++ it("falls back to preview when read-only and split otherwise if the label is absent or invalid", () => {+ expect(resolveDisplayMode(undefined, true)).toBe("preview");+ expect(resolveDisplayMode(undefined, false)).toBe("split");+ expect(resolveDisplayMode(null, true)).toBe("preview");+ expect(resolveDisplayMode("", false)).toBe("split");+ expect(resolveDisplayMode("bogus", true)).toBe("preview");+ });+});++describe("isSplitEditorForcedReadOnly", () => {+ it("is true only for file-type icon packs (which can't be edited as text)", () => {+ const filePack = buildNote({ title: "fp", type: "file", "#iconPack": "fp" });+ filePack.mime = "application/json";+ expect(isSplitEditorForcedReadOnly(filePack)).toBe(true);++ const codePack = buildNote({ title: "cp", type: "code", "#iconPack": "cp" });+ codePack.mime = "application/json";+ expect(isSplitEditorForcedReadOnly(codePack)).toBe(false);++ expect(isSplitEditorForcedReadOnly(buildNote({ title: "plain", type: "file" }))).toBe(false);+ });+});diff --git a/apps/client/src/widgets/type_widgets/helpers/split_editor_mode.ts b/apps/client/src/widgets/type_widgets/helpers/split_editor_mode.tsnew file mode 100644index 0000000000..4a201c7d76--- /dev/null+++ b/apps/client/src/widgets/type_widgets/helpers/split_editor_mode.ts@@ -0,0 +1,22 @@+import type FNote from "../../../entities/fnote";++export type DisplayMode = "source" | "split" | "preview";++/**+ * Resolves the active split-editor mode from the `#displayMode` label. When the label is unset it+ * falls back to the note's read-only state — read-only → `preview`, editable → `split`. Kept as a+ * shared pure function so the SplitEditor and its mode-switcher buttons stay in sync.+ */+export function resolveDisplayMode(displayMode: string | null | undefined, readOnly: boolean): DisplayMode {+ return displayMode === "source" || displayMode === "split" || displayMode === "preview"+ ? displayMode+ : readOnly ? "preview" : "split";+}++/**+ * File-type icon packs can't be edited as text, so their split editor is always read-only (the+ * SplitEditor gets this via its `forceReadOnly` prop; the switcher buttons derive it from the note).+ */+export function isSplitEditorForcedReadOnly(note: FNote | null | undefined) {+ return Boolean(note?.isIconPack() && note.type === "file");+}