Skip to content

Build your personal knowledge base with Trilium Notes

feat(markdown): add a display mode switcher

Elian Doran8760d683a360
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index f498dc856f..e284c16b78 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -2076,6 +2076,11 @@
"unlock-editing": "Unlock editing",
"lock-editing": "Lock editing"
},
+ "display_mode": {
+ "source": "Source view",
+ "split": "Split view",
+ "preview": "Preview"
+ },
"png_export_button": {
"button_title": "Export diagram as PNG"
},
diff --git a/apps/client/src/widgets/FloatingButtonsDefinitions.tsx b/apps/client/src/widgets/FloatingButtonsDefinitions.tsx
index 32d3cfd3fa..2c1b63d0c8 100644
--- a/apps/client/src/widgets/FloatingButtonsDefinitions.tsx
+++ b/apps/client/src/widgets/FloatingButtonsDefinitions.tsx
@@ -20,7 +20,7 @@ import tree from "../services/tree";
import { createImageSrcUrl, openInAppHelpFromUrl } from "../services/utils";
import { ViewTypeOptions } from "./collections/interface";
import ActionButton, { ActionButtonProps } from "./react/ActionButton";
-import { useIsNoteReadOnly, useNoteLabelBoolean, useTriliumEvent, useTriliumOption, useWindowSize } from "./react/hooks";
+import { useIsNoteReadOnly, useNoteLabel, useNoteLabelBoolean, useTriliumEvent, useTriliumOption, useWindowSize } from "./react/hooks";
import NoteLink from "./react/NoteLink";
import RawHtml from "./react/RawHtml";
@@ -49,6 +49,7 @@ export const DESKTOP_FLOATING_BUTTONS: FloatingButtonsList = [
RefreshBackendLogButton,
SwitchSplitOrientationButton,
ToggleReadOnlyButton,
+ DisplayModeSwitcher,
EditButton,
ShowTocWidgetButton,
ShowHighlightsListWidgetButton,
@@ -94,7 +95,7 @@ function SwitchSplitOrientationButton({ note, isReadOnly, isDefaultViewMode }: F
function ToggleReadOnlyButton({ note, isDefaultViewMode }: FloatingButtonContext) {
const [ isReadOnly, setReadOnly ] = useNoteLabelBoolean(note, "readOnly");
const isSavedSqlite = note.isTriliumSqlite() && !note.isHiddenCompletely();
- const isEnabled = ([ "mermaid", "mindMap", "canvas" ].includes(note.type) || isSavedSqlite || note.isMarkdown())
+ const isEnabled = ([ "mermaid", "mindMap", "canvas" ].includes(note.type) || isSavedSqlite)
&& note.isContentAvailable() && isDefaultViewMode;
return isEnabled && <FloatingButton
@@ -104,6 +105,32 @@ function ToggleReadOnlyButton({ note, isDefaultViewMode }: FloatingButtonContext
/>;
}
+function DisplayModeSwitcher({ note, isDefaultViewMode }: FloatingButtonContext) {
+ const [ displayMode, setDisplayMode ] = useNoteLabel(note, "displayMode");
+ if (!note.isMarkdown() || !note.isContentAvailable() || !isDefaultViewMode) return false;
+
+ const mode = displayMode === "source" || displayMode === "preview" ? displayMode : "split";
+ 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") },
+ { value: "preview", icon: "bx bx-show", text: t("display_mode.preview") }
+ ];
+
+ return (
+ <>
+ {buttons.map(({ value, icon, text }) => (
+ <FloatingButton
+ key={value}
+ icon={icon}
+ text={text}
+ active={mode === value}
+ onClick={() => setDisplayMode(value)}
+ />
+ ))}
+ </>
+ );
+}
+
function EditButton({ note, noteContext }: FloatingButtonContext) {
const [animationClass, setAnimationClass] = useState("");
const {isReadOnly, enableEditing} = useIsNoteReadOnly(note, noteContext);
diff --git a/apps/client/src/widgets/ribbon/NoteActionsCustom.tsx b/apps/client/src/widgets/ribbon/NoteActionsCustom.tsx
index c868014557..96c07d0cf9 100644
--- a/apps/client/src/widgets/ribbon/NoteActionsCustom.tsx
+++ b/apps/client/src/widgets/ribbon/NoteActionsCustom.tsx
@@ -73,6 +73,7 @@ export default function NoteActionsCustom(props: NoteActionsCustomProps) {
<RunActiveNoteButton {...innerProps } />
<SwitchSplitOrientationButton {...innerProps} />
<ToggleReadOnlyButton {...innerProps} />
+ <DisplayModeSwitcher {...innerProps} />
<SaveToNoteButton {...innerProps} />
<RefreshButton {...innerProps} />
<CopyReferenceToClipboardButton {...innerProps} />
@@ -203,7 +204,7 @@ function SwitchSplitOrientationButton({ note, isReadOnly, isDefaultViewMode }: N
export function ToggleReadOnlyButton({ note, isDefaultViewMode }: NoteActionsCustomInnerProps) {
const [ isReadOnly, setReadOnly ] = useNoteLabelBoolean(note, "readOnly");
const isSavedSqlite = note.isTriliumSqlite() && !note.isHiddenCompletely();
- const isEnabled = ([ "mermaid", "mindMap", "canvas", "spreadsheet" ].includes(note.type) || isSavedSqlite || note.isMarkdown())
+ const isEnabled = ([ "mermaid", "mindMap", "canvas", "spreadsheet" ].includes(note.type) || isSavedSqlite)
&& note.isContentAvailable() && isDefaultViewMode;
return isEnabled && <NoteAction
@@ -213,6 +214,32 @@ export function ToggleReadOnlyButton({ note, isDefaultViewMode }: NoteActionsCus
/>;
}
+function DisplayModeSwitcher({ note, isDefaultViewMode }: NoteActionsCustomInnerProps) {
+ const [ displayMode, setDisplayMode ] = useNoteLabel(note, "displayMode");
+ if (!note.isMarkdown() || !note.isContentAvailable() || !isDefaultViewMode) return null;
+
+ const mode = displayMode === "source" || displayMode === "preview" ? displayMode : "split";
+ 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") },
+ { value: "preview", icon: "bx bx-show", text: t("display_mode.preview") }
+ ];
+
+ return (
+ <>
+ {buttons.map(({ value, icon, text }) => (
+ <NoteAction
+ key={value}
+ icon={icon}
+ text={text}
+ active={mode === value}
+ onClick={() => setDisplayMode(value)}
+ />
+ ))}
+ </>
+ );
+}
+
function RunActiveNoteButton({ noteMime }: NoteActionsCustomInnerProps) {
const isEnabled = noteMime.startsWith("application/javascript") || noteMime === "text/x-sqlite;schema=trilium";
return isEnabled && <NoteAction
@@ -268,12 +295,12 @@ function AddChildButton({ parentComponent, noteType, ntxId, isReadOnly }: NoteAc
}
//#endregion
-function NoteAction({ text, ...props }: Pick<ActionButtonProps, "text" | "icon" | "disabled" | "triggerCommand"> & {
+function NoteAction({ text, active, ...props }: Pick<ActionButtonProps, "text" | "icon" | "disabled" | "triggerCommand" | "active"> & {
onClick?: ((e: MouseEvent) => void) | undefined;
}) {
return (cachedIsMobile
? <FormListItem {...props}>{text}</FormListItem>
- : <ActionButton text={text} {...props} />
+ : <ActionButton text={text} active={active} {...props} />
);
}
diff --git a/apps/client/src/widgets/type_widgets/code/Markdown.tsx b/apps/client/src/widgets/type_widgets/code/Markdown.tsx
index dec85ae6e1..892b7fafd4 100644
--- a/apps/client/src/widgets/type_widgets/code/Markdown.tsx
+++ b/apps/client/src/widgets/type_widgets/code/Markdown.tsx
@@ -7,13 +7,34 @@ import { Marked } from "marked";
import { RefObject } from "preact";
import { useEffect, useMemo, useRef, useState } from "preact/hooks";
+import { useNoteLabel } from "../../react/hooks";
import SplitEditor from "../helpers/SplitEditor";
import { ReadOnlyTextContent } from "../text/ReadOnlyText";
import { TypeWidgetProps } from "../type_widget";
+import { EditableCode } from "./Code";
const marked = new Marked({ breaks: true, gfm: true });
export default function Markdown(props: TypeWidgetProps) {
+ const [ displayMode ] = useNoteLabel(props.note, "displayMode");
+ const mode = displayMode === "source" || displayMode === "preview" ? displayMode : "split";
+
+ if (mode === "source") {
+ return (
+ <EditableCode
+ noteType="code"
+ lineWrapping={false}
+ updateInterval={750} debounceUpdate
+ noBackgroundChange
+ {...props}
+ />
+ );
+ }
+
+ return <SplitMarkdown {...props} overrideMode={mode} />;
+}
+
+function SplitMarkdown({ overrideMode, ...props }: TypeWidgetProps & { overrideMode: "preview" | "split" }) {
const [ content, setContent ] = useState("");
const html = useMemo(() => renderWithSourceLines(content), [ content ]);
const previewRef = useRef<HTMLDivElement>(null);
@@ -26,6 +47,7 @@ export default function Markdown(props: TypeWidgetProps) {
<SplitEditor
noteType="code"
{...props}
+ overrideMode={overrideMode}
editorRef={editorRef}
onContentChanged={setContent}
previewContent={(
diff --git a/apps/client/src/widgets/type_widgets/helpers/SplitEditor.tsx b/apps/client/src/widgets/type_widgets/helpers/SplitEditor.tsx
index bf6d331ea5..2da4c9b11d 100644
--- a/apps/client/src/widgets/type_widgets/helpers/SplitEditor.tsx
+++ b/apps/client/src/widgets/type_widgets/helpers/SplitEditor.tsx
@@ -20,6 +20,8 @@ export interface SplitEditorProps extends EditableCodeProps {
editorBefore?: ComponentChildren;
forceOrientation?: "horizontal" | "vertical";
extraContent?: ComponentChildren;
+ /** When set, overrides the view chosen by the `readOnly` label. */
+ overrideMode?: "preview" | "split";
}
/**
@@ -33,8 +35,9 @@ export interface SplitEditorProps extends EditableCodeProps {
*/
export default function SplitEditor(props: SplitEditorProps) {
const [ readOnly ] = useNoteLabelBoolean(props.note, "readOnly");
+ const mode = props.overrideMode ?? (readOnly ? "preview" : "split");
- if (readOnly) {
+ if (mode === "preview") {
return <ReadOnlyView {...props} />;
}
diff --git a/packages/commons/src/lib/attribute_names.ts b/packages/commons/src/lib/attribute_names.ts
index 9b53e784e3..e5081f6c39 100644
--- a/packages/commons/src/lib/attribute_names.ts
+++ b/packages/commons/src/lib/attribute_names.ts
@@ -70,6 +70,7 @@ type Labels = {
webViewSrc: string;
"disabled:webViewSrc": string;
readOnly: boolean;
+ displayMode: string;
tabWidth: number;
indentWithTabs: boolean;
wrapLines: boolean;