mirror of
https://github.com/zadam/trilium.git
synced 2026-02-28 09:33:38 +01:00
fix(mermaid) diagrams not saving content and SVG attachment (#8220)
This commit is contained in:
parent
b1dc0e234f
commit
53e1fa1047
@ -29,6 +29,7 @@ export default function Mermaid(props: TypeWidgetProps) {
|
|||||||
<SvgSplitEditor
|
<SvgSplitEditor
|
||||||
attachmentName="mermaid-export"
|
attachmentName="mermaid-export"
|
||||||
renderSvg={renderSvg}
|
renderSvg={renderSvg}
|
||||||
|
noteType="mermaid"
|
||||||
{...props}
|
{...props}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
import "./code.css";
|
import "./code.css";
|
||||||
|
|
||||||
import { default as VanillaCodeMirror, getThemeById } from "@triliumnext/codemirror";
|
import { default as VanillaCodeMirror, getThemeById } from "@triliumnext/codemirror";
|
||||||
|
import { NoteType } from "@triliumnext/commons";
|
||||||
import { useEffect, useRef, useState } from "preact/hooks";
|
import { useEffect, useRef, useState } from "preact/hooks";
|
||||||
|
|
||||||
import appContext, { CommandListenerData } from "../../../components/app_context";
|
import appContext, { CommandListenerData } from "../../../components/app_context";
|
||||||
@ -24,6 +25,7 @@ export interface EditableCodeProps extends TypeWidgetProps, Omit<CodeEditorProps
|
|||||||
debounceUpdate?: boolean;
|
debounceUpdate?: boolean;
|
||||||
lineWrapping?: boolean;
|
lineWrapping?: boolean;
|
||||||
updateInterval?: number;
|
updateInterval?: number;
|
||||||
|
noteType?: NoteType;
|
||||||
/** Invoked when the content of the note is changed, such as a different revision or a note switch. */
|
/** Invoked when the content of the note is changed, such as a different revision or a note switch. */
|
||||||
onContentChanged?: (content: string) => void;
|
onContentChanged?: (content: string) => void;
|
||||||
/** Invoked after the content of the note has been uploaded to the server, using a spaced update. */
|
/** Invoked after the content of the note has been uploaded to the server, using a spaced update. */
|
||||||
@ -72,14 +74,14 @@ function formatViewSource(note: FNote, content: string) {
|
|||||||
return content;
|
return content;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function EditableCode({ note, ntxId, noteContext, debounceUpdate, parentComponent, updateInterval, onContentChanged, dataSaved, ...editorProps }: EditableCodeProps) {
|
export function EditableCode({ note, ntxId, noteContext, debounceUpdate, parentComponent, updateInterval, noteType = "code", onContentChanged, dataSaved, ...editorProps }: EditableCodeProps) {
|
||||||
const editorRef = useRef<VanillaCodeMirror>(null);
|
const editorRef = useRef<VanillaCodeMirror>(null);
|
||||||
const containerRef = useRef<HTMLPreElement>(null);
|
const containerRef = useRef<HTMLPreElement>(null);
|
||||||
const [ vimKeymapEnabled ] = useTriliumOptionBool("vimKeymapEnabled");
|
const [ vimKeymapEnabled ] = useTriliumOptionBool("vimKeymapEnabled");
|
||||||
const mime = useNoteProperty(note, "mime");
|
const mime = useNoteProperty(note, "mime");
|
||||||
const spacedUpdate = useEditorSpacedUpdate({
|
const spacedUpdate = useEditorSpacedUpdate({
|
||||||
note,
|
note,
|
||||||
noteType: "code",
|
noteType,
|
||||||
noteContext,
|
noteContext,
|
||||||
getData: () => ({ content: editorRef.current?.getText() ?? "" }),
|
getData: () => ({ content: editorRef.current?.getText() ?? "" }),
|
||||||
onContentChange: (content) => {
|
onContentChange: (content) => {
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import { useEffect, useRef, useState } from "preact/hooks";
|
import { useCallback, useEffect, useRef, useState } from "preact/hooks";
|
||||||
import { t } from "../../../services/i18n";
|
import { t } from "../../../services/i18n";
|
||||||
import SplitEditor, { PreviewButton, SplitEditorProps } from "./SplitEditor";
|
import SplitEditor, { PreviewButton, SplitEditorProps } from "./SplitEditor";
|
||||||
import { RawHtmlBlock } from "../../react/RawHtml";
|
import { RawHtmlBlock } from "../../react/RawHtml";
|
||||||
@ -55,7 +55,9 @@ export default function SvgSplitEditor({ ntxId, note, attachmentName, renderSvg,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Save as attachment.
|
// Save as attachment.
|
||||||
function onSave() {
|
const onSave = useCallback(() => {
|
||||||
|
if (!svg) return; // Don't save if SVG hasn't been rendered yet
|
||||||
|
|
||||||
const payload = {
|
const payload = {
|
||||||
role: "image",
|
role: "image",
|
||||||
title: `${attachmentName}.svg`,
|
title: `${attachmentName}.svg`,
|
||||||
@ -65,16 +67,18 @@ export default function SvgSplitEditor({ ntxId, note, attachmentName, renderSvg,
|
|||||||
};
|
};
|
||||||
|
|
||||||
server.post(`notes/${note.noteId}/attachments?matchBy=title`, payload);
|
server.post(`notes/${note.noteId}/attachments?matchBy=title`, payload);
|
||||||
}
|
}, [ svg, attachmentName, note.noteId ]);
|
||||||
|
|
||||||
// Save the SVG when entering a note only when it does not have an attachment.
|
// Save the SVG when entering a note only when it does not have an attachment.
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
if (!svg) return; // Wait until SVG is rendered
|
||||||
|
|
||||||
note?.getAttachments().then((attachments) => {
|
note?.getAttachments().then((attachments) => {
|
||||||
if (!attachments.find((a) => a.title === `${attachmentName}.svg`)) {
|
if (!attachments.find((a) => a.title === `${attachmentName}.svg`)) {
|
||||||
onSave();
|
onSave();
|
||||||
}
|
}
|
||||||
});
|
}).catch(e => console.error("Failed to get attachments for SVGSplitEditor", e));
|
||||||
}, [ note ]);
|
}, [ note, svg, attachmentName, onSave ]);
|
||||||
|
|
||||||
// Import/export
|
// Import/export
|
||||||
useTriliumEvent("exportSvg", async({ ntxId: eventNtxId }) => {
|
useTriliumEvent("exportSvg", async({ ntxId: eventNtxId }) => {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user