Skip to content

Build your personal knowledge base with Trilium Notes

fix saving mermaid attachment

zadam0bfb2631df0a
diff --git a/src/becca/entities/bnote_attachment.js b/src/becca/entities/bnote_attachment.js
index 5ec9cddabb..e72faaf6a6 100644
--- a/src/becca/entities/bnote_attachment.js
+++ b/src/becca/entities/bnote_attachment.js
@@ -22,8 +22,16 @@ class BNoteAttachment extends AbstractBeccaEntity {
constructor(row) {
super();
- /** @type {string} */
- this.noteAttachmentId = row.noteAttachmentId;
+ if (!row.noteId) {
+ throw new Error("'noteId' must be given to initialize a NoteAttachment entity");
+ }
+
+ if (!row.name) {
+ throw new Error("'name' must be given to initialize a NoteAttachment entity");
+ }
+
+ /** @type {string} needs to be set at the initialization time since it's used in the .setContent() */
+ this.noteAttachmentId = row.noteAttachmentId || `${this.noteId}_${this.name}`;
/** @type {string} */
this.noteId = row.noteId;
/** @type {string} */
@@ -82,6 +90,9 @@ class BNoteAttachment extends AbstractBeccaEntity {
}
setContent(content) {
+ this.contentCheckSum = this.calculateCheckSum(content);
+ this.save();
+
const pojo = {
noteAttachmentId: this.noteAttachmentId,
content: content,
@@ -99,14 +110,12 @@ class BNoteAttachment extends AbstractBeccaEntity {
sql.upsert("note_attachment_contents", "noteAttachmentId", pojo);
- this.contentCheckSum = this.calculateCheckSum(pojo.content);
-
entityChangesService.addEntityChange({
entityName: 'note_attachment_contents',
entityId: this.noteAttachmentId,
hash: this.contentCheckSum,
isErased: false,
- utcDateChanged: this.getUtcDateChanged(),
+ utcDateChanged: pojo.utcDateModified,
isSynced: true
});
}
@@ -134,6 +143,7 @@ class BNoteAttachment extends AbstractBeccaEntity {
name: this.name,
mime: this.mime,
isProtected: !!this.isProtected,
+ contentCheckSum: this.contentCheckSum,
isDeleted: false,
utcDateModified: this.utcDateModified,
content: this.content,
diff --git a/src/public/app/widgets/mermaid.js b/src/public/app/widgets/mermaid.js
index ad8e94eb8a..b4aa7f4f7b 100644
--- a/src/public/app/widgets/mermaid.js
+++ b/src/public/app/widgets/mermaid.js
@@ -1,6 +1,7 @@
import libraryLoader from "../services/library_loader.js";
import NoteContextAwareWidget from "./note_context_aware_widget.js";
import froca from "../services/froca.js";
+import server from "../services/server.js";
const TPL = `<div class="mermaid-widget">
<style>
@@ -76,6 +77,14 @@ export default class MermaidWidget extends NoteContextAwareWidget {
const renderedSvg = await this.renderSvg();
this.$display.html(renderedSvg);
+ // not awaiting intentionally
+ // this is pretty hacky since we update attachment on render
+ // but if nothing changed this should not trigger DB write and sync
+ server.put(`notes/${note.noteId}/attachments/mermaidSvg`, {
+ mime: 'image/svg+xml',
+ content: renderedSvg
+ });
+
await wheelZoomLoaded;
this.$display.attr("id", `mermaid-render-${idCounter}`);
diff --git a/src/routes/api/notes.js b/src/routes/api/notes.js
index 5f62e5ae84..11601c4816 100644
--- a/src/routes/api/notes.js
+++ b/src/routes/api/notes.js
@@ -127,6 +127,19 @@ function setNoteTypeMime(req) {
note.save();
}
+function saveNoteAttachment(req) {
+ const {noteId, name} = req.params;
+ const {mime, content} = req.body;
+
+ const note = becca.getNote(noteId);
+
+ if (!note) {
+ throw new NotFoundError(`Note '${noteId}' doesn't exist.`);
+ }
+
+ note.saveNoteAttachment(name, mime, content);
+}
+
function getRelationMap(req) {
const {relationMapNoteId, noteIds} = req.body;
@@ -340,5 +353,6 @@ module.exports = {
eraseDeletedNotesNow,
getDeleteNotesPreview,
uploadModifiedFile,
- forceSaveNoteRevision
+ forceSaveNoteRevision,
+ saveNoteAttachment
};
diff --git a/src/routes/routes.js b/src/routes/routes.js
index cdca32975f..f12cbb02bf 100644
--- a/src/routes/routes.js
+++ b/src/routes/routes.js
@@ -126,6 +126,7 @@ function register(app) {
apiRoute(PUT, '/api/notes/:noteId/sort-children', notesApiRoute.sortChildNotes);
apiRoute(PUT, '/api/notes/:noteId/protect/:isProtected', notesApiRoute.protectNote);
apiRoute(PUT, '/api/notes/:noteId/type', notesApiRoute.setNoteTypeMime);
+ apiRoute(PUT, '/api/notes/:noteId/attachments/:name', notesApiRoute.saveNoteAttachment);
apiRoute(GET, '/api/notes/:noteId/revisions', noteRevisionsApiRoute.getNoteRevisions);
apiRoute(DELETE, '/api/notes/:noteId/revisions', noteRevisionsApiRoute.eraseAllNoteRevisions);
apiRoute(GET, '/api/notes/:noteId/revisions/:noteRevisionId', noteRevisionsApiRoute.getNoteRevision);