From acf6d9dc4f807a4f1d8dc01b0afc81ffef729112 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Sat, 22 Mar 2025 02:21:24 +0200 Subject: [PATCH] refactor(split_editor): allow different attachment name --- .../type_widgets/abstract_split_type_widget.ts | 1 + .../type_widgets/abstract_svg_split_type_widget.ts | 11 +++++++++-- src/public/app/widgets/type_widgets/mermaid.ts | 4 ++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/public/app/widgets/type_widgets/abstract_split_type_widget.ts b/src/public/app/widgets/type_widgets/abstract_split_type_widget.ts index e153bc408..e313ba8a7 100644 --- a/src/public/app/widgets/type_widgets/abstract_split_type_widget.ts +++ b/src/public/app/widgets/type_widgets/abstract_split_type_widget.ts @@ -75,6 +75,7 @@ const TPL = `\ * Features: * * - The two panes are resizeable via a split, on desktop. The split can be optionally customized via {@link buildSplitExtraOptions}. + * - Can display errors to the user via {@link setError}. */ export default abstract class AbstractSplitTypeWidget extends TypeWidget { diff --git a/src/public/app/widgets/type_widgets/abstract_svg_split_type_widget.ts b/src/public/app/widgets/type_widgets/abstract_svg_split_type_widget.ts index 6b716178d..002bc941f 100644 --- a/src/public/app/widgets/type_widgets/abstract_svg_split_type_widget.ts +++ b/src/public/app/widgets/type_widgets/abstract_svg_split_type_widget.ts @@ -9,8 +9,10 @@ import AbstractSplitTypeWidget from "./abstract_split_type_widget.js"; * * This adds the following functionality: * - * - Automatic handling of the preview when content or the note changes. + * - Automatic handling of the preview when content or the note changes via {@link renderSvg}. * - Built-in pan and zoom functionality with automatic re-centering. + * - Automatically displays errors to the user if {@link renderSvg} failed. + * - Automatically saves the SVG attachment. * */ export default abstract class AbstractSvgSplitTypeWidget extends AbstractSplitTypeWidget { @@ -91,7 +93,7 @@ export default abstract class AbstractSvgSplitTypeWidget extends AbstractSplitTy #saveSvg() { const payload = { role: "image", - title: "mermaid-export.svg", + title: `${this.attachmentName}.svg`, mime: "image/svg+xml", content: this.svg, position: 0 @@ -115,6 +117,11 @@ export default abstract class AbstractSvgSplitTypeWidget extends AbstractSplitTy */ abstract renderSvg(content: string): Promise; + /** + * Called to obtain the name of the note attachment (without .svg extension) that will be used for storing the preview. + */ + abstract get attachmentName(): string; + /** * @param preservePanZoom `true` to keep the pan/zoom settings of the previous image, or `false` to re-center it. */ diff --git a/src/public/app/widgets/type_widgets/mermaid.ts b/src/public/app/widgets/type_widgets/mermaid.ts index 836a36e04..d311ee00b 100644 --- a/src/public/app/widgets/type_widgets/mermaid.ts +++ b/src/public/app/widgets/type_widgets/mermaid.ts @@ -11,6 +11,10 @@ export class MermaidTypeWidget extends AbstractSvgSplitTypeWidget { return "mermaid"; } + get attachmentName(): string { + return "mermaid-export"; + } + async renderSvg(content: string) { const mermaid = (await import("mermaid")).default; await loadElkIfNeeded(mermaid, content);