2025-09-19 17:31:10 +03:00
|
|
|
import { NoteType } from "@triliumnext/commons";
|
2025-09-20 11:46:23 +03:00
|
|
|
import { useNoteContext, useTriliumEvent } from "./react/hooks"
|
2025-09-19 17:31:10 +03:00
|
|
|
import FNote from "../entities/fnote";
|
|
|
|
|
import protected_session_holder from "../services/protected_session_holder";
|
2025-09-20 11:46:23 +03:00
|
|
|
import { useEffect, useState } from "preact/hooks";
|
2025-09-19 17:31:10 +03:00
|
|
|
import NoteContext from "../components/note_context";
|
2025-09-19 18:08:21 +03:00
|
|
|
import Empty from "./type_widgets/Empty";
|
|
|
|
|
import { VNode } from "preact";
|
2025-09-19 18:32:45 +03:00
|
|
|
import Doc from "./type_widgets/Doc";
|
|
|
|
|
import { TypeWidgetProps } from "./type_widgets/type_widget";
|
2025-09-19 18:55:04 +03:00
|
|
|
import ProtectedSession from "./type_widgets/ProtectedSession";
|
2025-09-19 19:03:31 +03:00
|
|
|
import Book from "./type_widgets/Book";
|
2025-09-19 21:18:09 +03:00
|
|
|
import ContentWidget from "./type_widgets/ContentWidget";
|
2025-09-19 21:27:45 +03:00
|
|
|
import WebView from "./type_widgets/WebView";
|
2025-09-19 21:40:35 +03:00
|
|
|
import "./NoteDetail.css";
|
2025-09-19 22:22:45 +03:00
|
|
|
import File from "./type_widgets/File";
|
2025-09-19 22:41:18 +03:00
|
|
|
import Image from "./type_widgets/Image";
|
2025-09-20 09:44:36 +03:00
|
|
|
import { ReadOnlyCode, EditableCode } from "./type_widgets/code/Code";
|
2025-09-20 12:25:11 +03:00
|
|
|
import Mermaid from "./type_widgets/Mermaid";
|
2025-09-20 21:52:57 +03:00
|
|
|
import MindMap from "./type_widgets/MindMap";
|
2025-09-21 10:56:10 +03:00
|
|
|
import { AttachmentDetail, AttachmentList } from "./type_widgets/Attachment";
|
2025-09-21 20:03:28 +03:00
|
|
|
import ReadOnlyText from "./type_widgets/text/ReadOnlyText";
|
2025-09-19 17:31:10 +03:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
type ExtendedNoteType = Exclude<NoteType, "launcher" | "text" | "code"> | "empty" | "readOnlyCode" | "readOnlyText" | "editableText" | "editableCode" | "attachmentDetail" | "attachmentList" | "protectedSession" | "aiChat";
|
2025-09-19 16:53:31 +03:00
|
|
|
|
2025-09-19 17:40:24 +03:00
|
|
|
/**
|
|
|
|
|
* The note detail is in charge of rendering the content of a note, by determining its type (e.g. text, code) and using the appropriate view widget.
|
2025-09-20 11:29:28 +03:00
|
|
|
*
|
|
|
|
|
* Apart from that:
|
|
|
|
|
* - It applies a full-height style depending on the content type (e.g. canvas notes).
|
2025-09-20 11:46:23 +03:00
|
|
|
* - Focuses the content when switching tabs.
|
2025-09-19 17:40:24 +03:00
|
|
|
*/
|
2025-09-19 16:53:31 +03:00
|
|
|
export default function NoteDetail() {
|
2025-09-20 11:02:43 +03:00
|
|
|
const { note, type, noteContext, parentComponent } = useNoteInfo();
|
|
|
|
|
const { ntxId, viewScope } = noteContext ?? {};
|
2025-09-19 18:08:21 +03:00
|
|
|
const [ correspondingWidget, setCorrespondingWidget ] = useState<VNode>();
|
2025-09-19 21:40:35 +03:00
|
|
|
const isFullHeight = checkFullHeight(noteContext, type);
|
2025-09-19 17:31:10 +03:00
|
|
|
|
2025-09-19 18:32:45 +03:00
|
|
|
const props: TypeWidgetProps = {
|
|
|
|
|
note: note!,
|
|
|
|
|
viewScope,
|
2025-09-20 10:08:46 +03:00
|
|
|
ntxId,
|
2025-09-21 20:50:26 +03:00
|
|
|
parentComponent,
|
|
|
|
|
noteContext
|
2025-09-19 18:32:45 +03:00
|
|
|
};
|
|
|
|
|
useEffect(() => setCorrespondingWidget(getCorrespondingWidget(type, props)), [ note, viewScope, type ]);
|
2025-09-19 18:08:21 +03:00
|
|
|
|
2025-09-20 11:46:23 +03:00
|
|
|
// Automatically focus the editor.
|
|
|
|
|
useTriliumEvent("activeNoteChanged", () => {
|
|
|
|
|
// Restore focus to the editor when switching tabs, but only if the note tree is not already focused.
|
|
|
|
|
if (!document.activeElement?.classList.contains("fancytree-title")) {
|
|
|
|
|
parentComponent.triggerCommand("focusOnDetail", { ntxId });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2025-09-19 18:08:21 +03:00
|
|
|
return (
|
2025-09-19 21:40:35 +03:00
|
|
|
<div class={`note-detail ${isFullHeight ? "full-height" : ""}`}>
|
2025-09-19 18:08:21 +03:00
|
|
|
{correspondingWidget || <p>Note detail goes here! {note?.title} of {type}</p>}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2025-09-19 17:31:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Manages both note changes and changes to the widget type, which are asynchronous. */
|
|
|
|
|
function useNoteInfo() {
|
2025-09-20 11:02:43 +03:00
|
|
|
const { note: actualNote, noteContext, parentComponent } = useNoteContext();
|
2025-09-19 17:31:10 +03:00
|
|
|
const [ note, setNote ] = useState<FNote | null | undefined>();
|
|
|
|
|
const [ type, setType ] = useState<ExtendedNoteType>();
|
|
|
|
|
|
2025-09-20 11:55:43 +03:00
|
|
|
function refresh() {
|
2025-09-19 17:31:10 +03:00
|
|
|
getWidgetType(actualNote, noteContext).then(type => {
|
|
|
|
|
setNote(actualNote);
|
|
|
|
|
setType(type);
|
|
|
|
|
});
|
2025-09-20 11:55:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
useEffect(refresh, [ actualNote, noteContext]);
|
|
|
|
|
useTriliumEvent("readOnlyTemporarilyDisabled", ({ noteContext: eventNoteContext }) => {
|
|
|
|
|
if (eventNoteContext?.ntxId !== noteContext?.ntxId) return;
|
|
|
|
|
refresh();
|
|
|
|
|
});
|
2025-09-19 17:31:10 +03:00
|
|
|
|
2025-09-20 11:02:43 +03:00
|
|
|
return { note, type, noteContext, parentComponent };
|
2025-09-19 17:31:10 +03:00
|
|
|
}
|
|
|
|
|
|
2025-09-19 18:32:45 +03:00
|
|
|
function getCorrespondingWidget(noteType: ExtendedNoteType | undefined, props: TypeWidgetProps) {
|
2025-09-19 18:08:21 +03:00
|
|
|
switch (noteType) {
|
2025-09-19 18:55:04 +03:00
|
|
|
case "empty": return <Empty />
|
|
|
|
|
case "doc": return <Doc {...props} />
|
|
|
|
|
case "search": return <div className="note-detail-none note-detail-printable" />
|
|
|
|
|
case "protectedSession": return <ProtectedSession />
|
2025-09-19 19:03:31 +03:00
|
|
|
case "book": return <Book {...props} />
|
2025-09-19 21:18:09 +03:00
|
|
|
case "contentWidget": return <ContentWidget {...props} />
|
2025-09-19 21:27:45 +03:00
|
|
|
case "webView": return <WebView {...props} />
|
2025-09-19 22:22:45 +03:00
|
|
|
case "file": return <File {...props} />
|
2025-09-19 22:41:18 +03:00
|
|
|
case "image": return <Image {...props} />
|
2025-09-20 08:57:47 +03:00
|
|
|
case "readOnlyCode": return <ReadOnlyCode {...props} />
|
2025-09-20 09:44:36 +03:00
|
|
|
case "editableCode": return <EditableCode {...props} />
|
2025-09-20 12:25:11 +03:00
|
|
|
case "mermaid": return <Mermaid {...props} />
|
2025-09-20 21:52:57 +03:00
|
|
|
case "mindMap": return <MindMap {...props} />
|
2025-09-21 09:56:51 +03:00
|
|
|
case "attachmentList": return <AttachmentList {...props} />
|
2025-09-21 10:56:10 +03:00
|
|
|
case "attachmentDetail": return <AttachmentDetail {...props} />
|
2025-09-21 20:03:28 +03:00
|
|
|
case "readOnlyText": return <ReadOnlyText {...props} />
|
2025-09-19 18:55:04 +03:00
|
|
|
default: break;
|
2025-09-19 18:08:21 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-19 17:31:10 +03:00
|
|
|
async function getWidgetType(note: FNote | null | undefined, noteContext: NoteContext | undefined): Promise<ExtendedNoteType> {
|
|
|
|
|
if (!note) {
|
|
|
|
|
return "empty";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const type = note.type;
|
|
|
|
|
let resultingType: ExtendedNoteType;
|
|
|
|
|
|
|
|
|
|
if (noteContext?.viewScope?.viewMode === "source") {
|
|
|
|
|
resultingType = "readOnlyCode";
|
|
|
|
|
} else if (noteContext?.viewScope && noteContext.viewScope.viewMode === "attachments") {
|
|
|
|
|
resultingType = noteContext.viewScope.attachmentId ? "attachmentDetail" : "attachmentList";
|
|
|
|
|
} else if (type === "text" && (await noteContext?.isReadOnly())) {
|
|
|
|
|
resultingType = "readOnlyText";
|
|
|
|
|
} else if ((type === "code" || type === "mermaid") && (await noteContext?.isReadOnly())) {
|
|
|
|
|
resultingType = "readOnlyCode";
|
|
|
|
|
} else if (type === "text") {
|
|
|
|
|
resultingType = "editableText";
|
|
|
|
|
} else if (type === "code") {
|
|
|
|
|
resultingType = "editableCode";
|
|
|
|
|
} else if (type === "launcher") {
|
|
|
|
|
resultingType = "doc";
|
|
|
|
|
} else {
|
|
|
|
|
resultingType = type;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (note.isProtected && !protected_session_holder.isProtectedSessionAvailable()) {
|
|
|
|
|
resultingType = "protectedSession";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return resultingType;
|
2025-09-19 16:53:31 +03:00
|
|
|
}
|
2025-09-19 21:40:35 +03:00
|
|
|
|
|
|
|
|
function checkFullHeight(noteContext: NoteContext | undefined, type: ExtendedNoteType | undefined) {
|
|
|
|
|
if (!noteContext) return false;
|
|
|
|
|
|
|
|
|
|
// https://github.com/zadam/trilium/issues/2522
|
|
|
|
|
const isBackendNote = noteContext?.noteId === "_backendLog";
|
|
|
|
|
const isSqlNote = noteContext.note?.mime === "text/x-sqlite;schema=trilium";
|
|
|
|
|
const isFullHeightNoteType = ["canvas", "webView", "noteMap", "mindMap", "mermaid", "file"].includes(type ?? "");
|
|
|
|
|
return (!noteContext?.hasNoteList() && isFullHeightNoteType && !isSqlNote)
|
|
|
|
|
|| noteContext?.viewScope?.viewMode === "attachments"
|
|
|
|
|
|| isBackendNote;
|
|
|
|
|
}
|