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) { constructor(row) {
super(); super();
/** @type {string} */ if (!row.noteId) {
this.noteAttachmentId = row.noteAttachmentId; 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} */ /** @type {string} */
this.noteId = row.noteId; this.noteId = row.noteId;
/** @type {string} */ /** @type {string} */
@ -82,6 +90,9 @@ class BNoteAttachment extends AbstractBeccaEntity {
} }
setContent(content) { setContent(content) {
this.contentCheckSum = this.calculateCheckSum(content);
this.save();
const pojo = { const pojo = {
noteAttachmentId: this.noteAttachmentId, noteAttachmentId: this.noteAttachmentId,
content: content, content: content,
@ -99,14 +110,12 @@ class BNoteAttachment extends AbstractBeccaEntity {
sql.upsert("note_attachment_contents", "noteAttachmentId", pojo); sql.upsert("note_attachment_contents", "noteAttachmentId", pojo);
this.contentCheckSum = this.calculateCheckSum(pojo.content);
entityChangesService.addEntityChange({ entityChangesService.addEntityChange({
entityName: 'note_attachment_contents', entityName: 'note_attachment_contents',
entityId: this.noteAttachmentId, entityId: this.noteAttachmentId,
hash: this.contentCheckSum, hash: this.contentCheckSum,
isErased: false, isErased: false,
utcDateChanged: this.getUtcDateChanged(), utcDateChanged: pojo.utcDateModified,
isSynced: true isSynced: true
}); });
} }
@ -134,6 +143,7 @@ class BNoteAttachment extends AbstractBeccaEntity {
name: this.name, name: this.name,
mime: this.mime, mime: this.mime,
isProtected: !!this.isProtected, isProtected: !!this.isProtected,
contentCheckSum: this.contentCheckSum,
isDeleted: false, isDeleted: false,
utcDateModified: this.utcDateModified, utcDateModified: this.utcDateModified,
content: this.content, content: this.content,

View File

@ -1,6 +1,7 @@
import libraryLoader from "../services/library_loader.js"; import libraryLoader from "../services/library_loader.js";
import NoteContextAwareWidget from "./note_context_aware_widget.js"; import NoteContextAwareWidget from "./note_context_aware_widget.js";
import froca from "../services/froca.js"; import froca from "../services/froca.js";
import server from "../services/server.js";
const TPL = `<div class="mermaid-widget"> const TPL = `<div class="mermaid-widget">
<style> <style>
@ -76,6 +77,14 @@ export default class MermaidWidget extends NoteContextAwareWidget {
const renderedSvg = await this.renderSvg(); const renderedSvg = await this.renderSvg();
this.$display.html(renderedSvg); 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; await wheelZoomLoaded;
this.$display.attr("id", `mermaid-render-${idCounter}`); this.$display.attr("id", `mermaid-render-${idCounter}`);

View File

@ -127,6 +127,19 @@ function setNoteTypeMime(req) {
note.save(); 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) { function getRelationMap(req) {
const {relationMapNoteId, noteIds} = req.body; const {relationMapNoteId, noteIds} = req.body;
@ -340,5 +353,6 @@ module.exports = {
eraseDeletedNotesNow, eraseDeletedNotesNow,
getDeleteNotesPreview, getDeleteNotesPreview,
uploadModifiedFile, 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/sort-children', notesApiRoute.sortChildNotes);
apiRoute(PUT, '/api/notes/:noteId/protect/:isProtected', notesApiRoute.protectNote); apiRoute(PUT, '/api/notes/:noteId/protect/:isProtected', notesApiRoute.protectNote);
apiRoute(PUT, '/api/notes/:noteId/type', notesApiRoute.setNoteTypeMime); 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(GET, '/api/notes/:noteId/revisions', noteRevisionsApiRoute.getNoteRevisions);
apiRoute(DELETE, '/api/notes/:noteId/revisions', noteRevisionsApiRoute.eraseAllNoteRevisions); apiRoute(DELETE, '/api/notes/:noteId/revisions', noteRevisionsApiRoute.eraseAllNoteRevisions);
apiRoute(GET, '/api/notes/:noteId/revisions/:noteRevisionId', noteRevisionsApiRoute.getNoteRevision); apiRoute(GET, '/api/notes/:noteId/revisions/:noteRevisionId', noteRevisionsApiRoute.getNoteRevision);