fix saving mermaid attachment

This commit is contained in:
zadam 2023-01-24 23:09:00 +01:00
parent 3c57f08ef7
commit 0bfb2631df
4 changed files with 40 additions and 6 deletions

View File

@ -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,

View File

@ -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}`);

View File

@ -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
};

View File

@ -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);