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: enable split editor for icon pack code notes
Adorian Doran96b1555884f2
diff --git a/apps/client/src/entities/fnote.ts b/apps/client/src/entities/fnote.tsindex 21dbb12bc7..9244233e52 100644--- a/apps/client/src/entities/fnote.ts+++ b/apps/client/src/entities/fnote.ts@@ -1074,6 +1074,12 @@ export default class FNote {return this.type === "code" && (this.mime === "text/markdown" || this.mime === "text/x-markdown" || this.mime === "text/x-gfm");}+ isIconPack() {+ // Icon-pack manifests exist both as JSON `code` notes (created manually per the docs) and as+ // `file` notes (produced by the icon-pack builder and shipped in distributable zips).+ return (this.type === "code" || this.type === "file") && this.mime === "application/json" && this.hasLabel("iconPack");+ }+isTriliumScript() {return this.mime.startsWith("application/javascript");}diff --git a/apps/client/src/widgets/FloatingButtonsDefinitions.tsx b/apps/client/src/widgets/FloatingButtonsDefinitions.tsxindex c21de3c166..f908b4304f 100644--- a/apps/client/src/widgets/FloatingButtonsDefinitions.tsx+++ b/apps/client/src/widgets/FloatingButtonsDefinitions.tsx@@ -114,7 +114,7 @@ function ToggleReadOnlyButton({ note, isDefaultViewMode }: FloatingButtonContextfunction DisplayModeSwitcher({ note, isDefaultViewMode }: FloatingButtonContext) {const [ displayMode, setDisplayMode ] = useNoteLabel(note, "displayMode");- const isEnabled = (note.isMarkdown() || note.type === "mermaid") && note.isContentAvailable() && isDefaultViewMode;+ const isEnabled = (note.isMarkdown() || note.type === "mermaid" || note.isIconPack()) && note.isContentAvailable() && isDefaultViewMode;if (!isEnabled) return false;const mode = displayMode === "source" || displayMode === "preview" ? displayMode : "split";diff --git a/apps/client/src/widgets/NoteDetail.tsx b/apps/client/src/widgets/NoteDetail.tsxindex 9e225f0df8..9d1c402af5 100644--- a/apps/client/src/widgets/NoteDetail.tsx+++ b/apps/client/src/widgets/NoteDetail.tsx@@ -397,6 +397,8 @@ export async function getExtendedWidgetType(note: FNote | null | undefined, noteresultingType = "sqlConsole";} else if (note.isMarkdown()) {resultingType = "markdown";+ } else if (note.isIconPack()) {+ resultingType = "iconPack";} else if (type === "code" && (await noteContext?.isReadOnly())) {resultingType = "readOnlyCode";} else if (type === "text") {diff --git a/apps/client/src/widgets/note_types.tsx b/apps/client/src/widgets/note_types.tsxindex 7921f7481c..5dbb08d3d9 100644--- a/apps/client/src/widgets/note_types.tsx+++ b/apps/client/src/widgets/note_types.tsx@@ -12,7 +12,7 @@ import { TypeWidgetProps } from "./type_widgets/type_widget";* A `NoteType` altered by the note detail widget, taking into consideration whether the note is editable or not and adding special note types such as an empty one,* for protected session or attachment information.*/-export type ExtendedNoteType = Exclude<NoteType, "launcher" | "text" | "code" | "llmChat"> | "empty" | "readOnlyCode" | "readOnlyText" | "editableText" | "editableCode" | "attachmentDetail" | "attachmentList" | "protectedSession" | "sqlConsole" | "markdown" | "llmChat";+export type ExtendedNoteType = Exclude<NoteType, "launcher" | "text" | "code" | "llmChat"> | "empty" | "readOnlyCode" | "readOnlyText" | "editableText" | "editableCode" | "attachmentDetail" | "attachmentList" | "protectedSession" | "sqlConsole" | "markdown" | "iconPack" | "llmChat";export type TypeWidget = ((props: TypeWidgetProps) => VNode | JSX.Element | undefined);type NoteTypeView = () => (Promise<{ default: TypeWidget } | TypeWidget> | TypeWidget);@@ -148,6 +148,12 @@ export const TYPE_MAPPINGS: Record<ExtendedNoteType, NoteTypeMapping> = {printable: true,isFullHeight: true},+ iconPack: {+ view: () => import("./type_widgets/icon_pack/IconPack"),+ className: "note-detail-icon-pack",+ printable: true,+ isFullHeight: true+ },spreadsheet: {view: () => import("./type_widgets/spreadsheet/Spreadsheet"),className: "note-detail-spreadsheet",diff --git a/apps/client/src/widgets/ribbon/NoteActionsCustom.tsx b/apps/client/src/widgets/ribbon/NoteActionsCustom.tsxindex c2b01877ea..d44590646e 100644--- a/apps/client/src/widgets/ribbon/NoteActionsCustom.tsx+++ b/apps/client/src/widgets/ribbon/NoteActionsCustom.tsx@@ -208,7 +208,7 @@ function SwitchSplitOrientationButton({ note, isReadOnly, isDefaultViewMode }: Nfunction DisplayModeSwitcher({ note, isDefaultViewMode }: NoteActionsCustomInnerProps) {const [ displayMode, setDisplayMode ] = useNoteLabel(note, "displayMode");- const isEnabled = (note.isMarkdown() || note.type === "mermaid") && note.isContentAvailable() && isDefaultViewMode;+ const isEnabled = (note.isMarkdown() || note.type === "mermaid" || note.isIconPack()) && note.isContentAvailable() && isDefaultViewMode;if (!isEnabled) return null;const mode = displayMode === "source" || displayMode === "preview" ? displayMode : "split";diff --git a/apps/client/src/widgets/type_widgets/helpers/SplitEditor.tsx b/apps/client/src/widgets/type_widgets/helpers/SplitEditor.tsxindex 771528901a..77a052ac97 100644--- a/apps/client/src/widgets/type_widgets/helpers/SplitEditor.tsx+++ b/apps/client/src/widgets/type_widgets/helpers/SplitEditor.tsx@@ -23,6 +23,10 @@ export interface SplitEditorProps extends EditableCodeProps {previewButtons?: ComponentChildren;editorBefore?: ComponentChildren;forceOrientation?: "horizontal" | "vertical";+ /** Forces the editor pane into read-only mode regardless of the note's own read-only state, e.g. for note types whose content isn't editable as text. */+ forceReadOnly?: boolean;+ /** Replaces the default CodeMirror editor pane, e.g. for note types whose content shouldn't be shown in a code editor (large `file` notes). Implies read-only; combine with {@link forceReadOnly} so the preview is still fed from the blob. */+ editorContent?: ComponentChildren;extraContent?: ComponentChildren;}@@ -41,11 +45,11 @@ export interface SplitEditorProps extends EditableCodeProps {* - Can display errors to the user via {@link setError}.* - Horizontal or vertical orientation for the editor/preview split, adjustable via the switch split orientation button floating button.*/-export default function SplitEditor({ note, noteContext, error, previewStale, splitOptions, previewContent, previewButtons, className, editorBefore, forceOrientation, extraContent, ...editorProps }: SplitEditorProps) {+export default function SplitEditor({ note, noteContext, error, previewStale, splitOptions, previewContent, previewButtons, className, editorBefore, forceOrientation, forceReadOnly, editorContent, extraContent, ...editorProps }: SplitEditorProps) {const containerRef = useRef<HTMLDivElement>(null);const splitEditorOrientation = useSplitOrientation(forceOrientation);const [ displayMode ] = useNoteLabel(note, "displayMode");- const readOnly = useEffectiveReadOnly(note, noteContext);+ const readOnly = useEffectiveReadOnly(note, noteContext) || Boolean(forceReadOnly);const mode = displayMode === "source" || displayMode === "split" || displayMode === "preview"? displayMode: readOnly ? "preview" : "split";@@ -73,7 +77,7 @@ export default function SplitEditor({ note, noteContext, error, previewStale, sp<div className="note-detail-split-editor-col">{editorBefore}<div className="note-detail-split-editor">- {readOnly+ {editorContent ?? (readOnly? <ReadOnlyCode note={note} noteContext={noteContext} {...editorProps} />: <EditableCodenote={note}@@ -82,7 +86,7 @@ export default function SplitEditor({ note, noteContext, error, previewStale, spupdateInterval={750} debounceUpdatenoBackgroundChange{...editorProps}- />}+ />)}</div>{extraContent}</div>diff --git a/apps/client/src/widgets/type_widgets/icon_pack/IconPack.css b/apps/client/src/widgets/type_widgets/icon_pack/IconPack.cssnew file mode 100644index 0000000000..18b65680bb--- /dev/null+++ b/apps/client/src/widgets/type_widgets/icon_pack/IconPack.css@@ -0,0 +1,13 @@+.note-detail-icon-pack .icon-pack-preview {+ display: flex;+ align-items: center;+ justify-content: center;+ height: 100%;+ padding: 1em;+ overflow: auto;+}++.note-detail-icon-pack .icon-pack-file-source {+ height: 100%;+ overflow: auto;+}diff --git a/apps/client/src/widgets/type_widgets/icon_pack/IconPack.tsx b/apps/client/src/widgets/type_widgets/icon_pack/IconPack.tsxnew file mode 100644index 0000000000..31d219cbbe--- /dev/null+++ b/apps/client/src/widgets/type_widgets/icon_pack/IconPack.tsx@@ -0,0 +1,41 @@+import "./IconPack.css";++import FNote from "../../../entities/fnote";+import { isDesktop } from "../../../services/utils";+import { useNoteBlob } from "../../react/hooks";+import { TextPreview } from "../File";+import SplitEditor from "../helpers/SplitEditor";+import { TypeWidgetProps } from "../type_widget";++export default function IconPack(props: TypeWidgetProps) {+ // Icon packs shipped in distributable zips are `file` notes, whose content isn't editable as+ // text and can be large — show a read-only, truncated source pane (no CodeMirror). Manually+ // created `code` notes stay editable in the normal code editor.+ const isFileNote = props.note.type === "file";+ return (+ <SplitEditor+ noteType="code"+ {...props}+ forceReadOnly={isFileNote}+ editorContent={isFileNote ? <FileSource note={props.note} /> : undefined}+ previewContent={<IconPackPreview />}+ forceOrientation={isDesktop() ? "horizontal" : "vertical"}+ />+ );+}++function FileSource({ note }: { note: FNote }) {+ const blob = useNoteBlob(note);+ // `TextPreview` renders the alert and the `<pre>` as siblings; wrap them so the split editor+ // pane has a single flow child and doesn't break its layout.+ return (+ <div className="icon-pack-file-source">+ <TextPreview content={blob?.content ?? ""} />+ </div>+ );+}++function IconPackPreview() {+ // TODO: parse the icon pack JSON and render its icons here.+ return <div className="icon-pack-preview" />;+}