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
refactor(markdown): handle source view at split editor level
Elian Doran5fe6611a9166
diff --git a/apps/client/src/widgets/type_widgets/code/Markdown.tsx b/apps/client/src/widgets/type_widgets/code/Markdown.tsxindex 892b7fafd4..dec85ae6e1 100644--- a/apps/client/src/widgets/type_widgets/code/Markdown.tsx+++ b/apps/client/src/widgets/type_widgets/code/Markdown.tsx@@ -7,34 +7,13 @@ 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);@@ -47,7 +26,6 @@ function SplitMarkdown({ overrideMode, ...props }: TypeWidgetProps & { overrideM<SplitEditornoteType="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.tsxindex 2da4c9b11d..beb56ca8ac 100644--- a/apps/client/src/widgets/type_widgets/helpers/SplitEditor.tsx+++ b/apps/client/src/widgets/type_widgets/helpers/SplitEditor.tsx@@ -8,7 +8,7 @@ import { DEFAULT_GUTTER_SIZE } from "../../../services/resizer";import utils, { isMobile } from "../../../services/utils";import ActionButton, { ActionButtonProps } from "../../react/ActionButton";import Admonition from "../../react/Admonition";-import { useNoteBlob, useNoteLabelBoolean, useTriliumOption } from "../../react/hooks";+import { useNoteBlob, useNoteLabel, useNoteLabelBoolean, useTriliumOption } from "../../react/hooks";import { EditableCode, EditableCodeProps } from "../code/Code";export interface SplitEditorProps extends EditableCodeProps {@@ -20,13 +20,15 @@ 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";}/*** Abstract `TypeWidget` which contains a preview and editor pane, each displayed on half of the available screen.*+ * The active view is driven by the `#displayMode` label (`source`, `split`, `preview`); when unset+ * it falls back to the `#readOnly` label (truthy → preview, falsy → split). `#displayMode` always+ * wins so an explicit choice is never overridden by `#readOnly`.+ ** Features:** - The two panes are resizeable via a split, on desktop. The split can be optionally customized via {@link buildSplitExtraOptions}.@@ -34,15 +36,19 @@ export interface SplitEditorProps extends EditableCodeProps {* - Horizontal or vertical orientation for the editor/preview split, adjustable via the switch split orientation button floating button.*/export default function SplitEditor(props: SplitEditorProps) {+ const [ displayMode ] = useNoteLabel(props.note, "displayMode");const [ readOnly ] = useNoteLabelBoolean(props.note, "readOnly");- const mode = props.overrideMode ?? (readOnly ? "preview" : "split");+ const mode = displayMode === "source" || displayMode === "split" || displayMode === "preview"+ ? displayMode+ : readOnly ? "preview" : "split";+ if (mode === "source") {+ return <SourceOnlyView {...props} />;+ }if (mode === "preview") {return <ReadOnlyView {...props} />;}-return <EditorWithSplit {...props} />;-}function EditorWithSplit({ note, error, splitOptions, previewContent, previewButtons, className, editorBefore, forceOrientation, extraContent, ...editorProps }: SplitEditorProps) {@@ -99,6 +105,31 @@ function EditorWithSplit({ note, error, splitOptions, previewContent, previewBut);}+function SourceOnlyView({ note, error, className, editorBefore, extraContent, previewContent, previewButtons, splitOptions, forceOrientation, ...editorProps }: SplitEditorProps) {+ return (+ <div className={`note-detail-split note-detail-printable split-source-only ${className ?? ""}`}>+ <div className="note-detail-split-editor-col">+ {editorBefore}+ <div className="note-detail-split-editor">+ <EditableCode+ note={note}+ lineWrapping={false}+ updateInterval={750} debounceUpdate+ noBackgroundChange+ {...editorProps}+ />+ </div>+ {error && (+ <Admonition type="caution" className="note-detail-error-container">+ {error}+ </Admonition>+ )}+ {extraContent}+ </div>+ </div>+ );+}+function ReadOnlyView({ ...props }: SplitEditorProps) {const { note, onContentChanged } = props;const content = useNoteBlob(note);