From 115c3bbeb03cd5fd0fe72e0a16b23162c6d120ff Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Tue, 4 Mar 2025 22:59:46 +0200 Subject: [PATCH] chore(client/ts): port read_only_text --- src/public/app/types.d.ts | 17 +++++++++++++++++ src/public/app/widgets/mermaid.ts | 2 +- .../{read_only_text.js => read_only_text.ts} | 17 +++++++++++------ 3 files changed, 29 insertions(+), 7 deletions(-) rename src/public/app/widgets/type_widgets/{read_only_text.js => read_only_text.ts} (90%) diff --git a/src/public/app/types.d.ts b/src/public/app/types.d.ts index a37df7f74..6e05c9fb1 100644 --- a/src/public/app/types.d.ts +++ b/src/public/app/types.d.ts @@ -5,6 +5,7 @@ import utils from "./services/utils.ts"; import appContext from "./components/app_context.ts"; import server from "./services/server.ts"; import library_loader, { Library } from "./services/library_loader.ts"; +import type { init } from "i18next"; interface ElectronProcess { type: string; @@ -139,10 +140,26 @@ declare global { } interface MermaidLoader { + } + interface MermaidChartConfig { + useMaxWidth: boolean; + } + interface MermaidConfig { + theme: string; + securityLevel: "antiscript", + flow: MermaidChartConfig; + sequence: MermaidChartConfig; + gantt: MermaidChartConfig; + class: MermaidChartConfig; + state: MermaidChartConfig; + pie: MermaidChartConfig; + journey: MermaidChartConfig; + git: MermaidChartConfig; } var mermaid: { mermaidAPI: MermaidApi; registerLayoutLoaders(loader: MermaidLoader); + init(config: MermaidConfig, el: HTMLElement | JQuery); parse(content: string, opts: { suppressErrors: true }): Promise<{ diff --git a/src/public/app/widgets/mermaid.ts b/src/public/app/widgets/mermaid.ts index 1f4d72bf7..417400c03 100644 --- a/src/public/app/widgets/mermaid.ts +++ b/src/public/app/widgets/mermaid.ts @@ -143,7 +143,7 @@ export default class MermaidWidget extends NoteContextAwareWidget { } } -export function getMermaidConfig() { +export function getMermaidConfig(): MermaidConfig { const documentStyle = window.getComputedStyle(document.documentElement); const mermaidTheme = documentStyle.getPropertyValue("--mermaid-theme"); diff --git a/src/public/app/widgets/type_widgets/read_only_text.js b/src/public/app/widgets/type_widgets/read_only_text.ts similarity index 90% rename from src/public/app/widgets/type_widgets/read_only_text.js rename to src/public/app/widgets/type_widgets/read_only_text.ts index 0cd683673..20e12fd63 100644 --- a/src/public/app/widgets/type_widgets/read_only_text.js +++ b/src/public/app/widgets/type_widgets/read_only_text.ts @@ -2,6 +2,8 @@ import AbstractTextTypeWidget from "./abstract_text_type_widget.js"; import libraryLoader from "../../services/library_loader.js"; import { applySyntaxHighlight } from "../../services/syntax_highlight.js"; import { getMermaidConfig } from "../mermaid.js"; +import type FNote from "../../entities/fnote.js"; +import type { EventData } from "../../components/app_context.js"; const TPL = `
@@ -71,6 +73,9 @@ const TPL = ` `; export default class ReadOnlyTextTypeWidget extends AbstractTextTypeWidget { + + private $content!: JQuery; + static getType() { return "readOnlyText"; } @@ -89,7 +94,7 @@ export default class ReadOnlyTextTypeWidget extends AbstractTextTypeWidget { this.$content.html(""); } - async doRefresh(note) { + async doRefresh(note: FNote) { // we load CKEditor also for read only notes because they contain content styles required for correct rendering of even read only notes // we could load just ckeditor-content.css but that causes CSS conflicts when both build CSS and this content CSS is loaded at the same time // (see https://github.com/zadam/trilium/issues/1590 for example of such conflict) @@ -97,13 +102,13 @@ export default class ReadOnlyTextTypeWidget extends AbstractTextTypeWidget { const blob = await note.getBlob(); - this.$content.html(blob.content); + this.$content.html(blob?.content ?? ""); - this.$content.find("a.reference-link").each(async (_, el) => { + this.$content.find("a.reference-link").each((_, el) => { this.loadReferenceLinkTitle($(el)); }); - this.$content.find("section").each(async (_, el) => { + this.$content.find("section").each((_, el) => { const noteId = $(el).attr("data-note-id"); this.loadIncludedNote(noteId, $(el)); @@ -135,11 +140,11 @@ export default class ReadOnlyTextTypeWidget extends AbstractTextTypeWidget { mermaid.init(getMermaidConfig(), this.$content.find(".mermaid-diagram")); } - async refreshIncludedNoteEvent({ noteId }) { + async refreshIncludedNoteEvent({ noteId }: EventData<"refreshIncludedNote">) { this.refreshIncludedNote(this.$content, noteId); } - async executeWithContentElementEvent({ resolve, ntxId }) { + async executeWithContentElementEvent({ resolve, ntxId }: EventData<"executeWithContentElement">) { if (!this.isNoteContext(ntxId)) { return; }