This commit is contained in:
zadam 2023-04-17 23:21:28 +02:00
parent 0e4a040ed8
commit e28fbf4617
6 changed files with 29 additions and 14 deletions

View File

@ -1545,7 +1545,7 @@ class BNote extends AbstractBeccaEntity {
*/ */
saveNoteRevision() { saveNoteRevision() {
return sql.transactional(() => { return sql.transactional(() => {
const content = this.getContent(); let noteContent = this.getContent();
const contentMetadata = this.getContentMetadata(); const contentMetadata = this.getContentMetadata();
const noteRevision = new BNoteRevision({ const noteRevision = new BNoteRevision({
@ -1566,19 +1566,24 @@ class BNote extends AbstractBeccaEntity {
dateCreated: dateUtils.localNowDateTime() dateCreated: dateUtils.localNowDateTime()
}, true); }, true);
noteRevision.setContent(content, { forceSave: true }); noteRevision.save(); // to generate noteRevisionId which is then used to save attachments
for (const noteAttachment of this.getAttachments()) { for (const noteAttachment of this.getAttachments()) {
const content = noteAttachment.getContent(); const attachmentContent = noteAttachment.getContent();
const revisionAttachment = noteAttachment.copy(); const revisionAttachment = noteAttachment.copy();
revisionAttachment.parentId = noteRevision.noteRevisionId; revisionAttachment.parentId = noteRevision.noteRevisionId;
revisionAttachment.setContent(content, { revisionAttachment.setContent(attachmentContent, {
forceSave: true, forceSave: true,
forceCold: true forceCold: true
}); });
// content is rewritten to point to the revision attachments
noteContent = noteContent.replaceAll(`attachments/${noteAttachment.attachmentId}`, `attachments/${revisionAttachment.attachmentId}`);
} }
noteRevision.setContent(noteContent, { forceSave: true });
return noteRevision; return noteRevision;
}); });
} }

View File

@ -4,6 +4,7 @@ import FAttribute from "../entities/fattribute.js";
import server from "./server.js"; import server from "./server.js";
import appContext from "../components/app_context.js"; import appContext from "../components/app_context.js";
import FNoteComplement from "../entities/fnote_complement.js"; import FNoteComplement from "../entities/fnote_complement.js";
import FAttachment from "../entities/fattachment.js";
/** /**
* Froca (FROntend CAche) keeps a read only cache of note tree structure in frontend's memory. * Froca (FROntend CAche) keeps a read only cache of note tree structure in frontend's memory.
@ -314,6 +315,12 @@ class Froca {
return child.parentToBranch[parentNoteId]; return child.parentToBranch[parentNoteId];
} }
async getAttachment(attachmentId) {
const attachmentRow = await server.get(`attachments/${attachmentId}`);
return attachmentRow ? new FAttachment(this, attachmentRow) : null;
}
/** /**
* // FIXME * // FIXME
* @returns {Promise<FNoteComplement>} * @returns {Promise<FNoteComplement>}

View File

@ -123,7 +123,7 @@ export default class AttachmentDetailWidget extends BasicWidget {
if (attachmentChange.isDeleted) { if (attachmentChange.isDeleted) {
this.toggleInt(false); this.toggleInt(false);
} else { } else {
this.attachment = await server.get(`notes/${this.attachment.parentId}/attachments/${this.attachment.attachmentId}?includeContent=true`); this.attachment = await server.get(`attachments/${this.attachment.attachmentId}?includeContent=true`);
this.refresh(); this.refresh();
} }

View File

@ -48,7 +48,7 @@ export default class AttachmentActionsWidget extends BasicWidget {
async deleteAttachmentCommand() { async deleteAttachmentCommand() {
if (await dialogService.confirm(`Are you sure you want to delete attachment '${this.attachment.title}'?`)) { if (await dialogService.confirm(`Are you sure you want to delete attachment '${this.attachment.title}'?`)) {
await server.remove(`notes/${this.attachment.parentId}/attachments/${this.attachment.attachmentId}`); await server.remove(`attachments/${this.attachment.attachmentId}`);
toastService.showMessage(`Attachment '${this.attachment.title}' has been deleted.`); toastService.showMessage(`Attachment '${this.attachment.title}' has been deleted.`);
} }
@ -56,7 +56,7 @@ export default class AttachmentActionsWidget extends BasicWidget {
async convertAttachmentIntoNoteCommand() { async convertAttachmentIntoNoteCommand() {
if (await dialogService.confirm(`Are you sure you want to convert attachment '${this.attachment.title}' into a separate note?`)) { if (await dialogService.confirm(`Are you sure you want to convert attachment '${this.attachment.title}' into a separate note?`)) {
const {note: newNote} = await server.post(`notes/${this.attachment.parentId}/attachments/${this.attachment.attachmentId}/convert-to-note`) const {note: newNote} = await server.post(`attachments/${this.attachment.attachmentId}/convert-to-note`)
toastService.showMessage(`Attachment '${this.attachment.title}' has been converted to note.`); toastService.showMessage(`Attachment '${this.attachment.title}' has been converted to note.`);

View File

@ -23,8 +23,8 @@ export default class AbstractTextTypeWidget extends TypeWidget {
}); });
} }
openImageInCurrentTab($img) { async openImageInCurrentTab($img) {
const { noteId, viewScope } = this.parseFromImage($img); const { noteId, viewScope } = await this.parseFromImage($img);
if (noteId) { if (noteId) {
appContext.tabManager.getActiveContext().setNote(noteId, { viewScope }); appContext.tabManager.getActiveContext().setNote(noteId, { viewScope });
@ -43,7 +43,7 @@ export default class AbstractTextTypeWidget extends TypeWidget {
} }
} }
parseFromImage($img) { async parseFromImage($img) {
let noteId, viewScope; let noteId, viewScope;
const imgSrc = $img.prop("src"); const imgSrc = $img.prop("src");
@ -56,13 +56,16 @@ export default class AbstractTextTypeWidget extends TypeWidget {
} }
} }
const attachmentMatch = imgSrc.match(/\/api\/notes\/([A-Za-z0-9_]+)\/images\/([A-Za-z0-9_]+)\//); const attachmentMatch = imgSrc.match(/\/api\/attachments\/([A-Za-z0-9_]+)\/image\//);
if (attachmentMatch) { if (attachmentMatch) {
const attachmentId = attachmentMatch[1];
const attachment = await froca.getAttachment(attachmentId);
return { return {
noteId: attachmentMatch[1], noteId: attachment.parentId,
viewScope: { viewScope: {
viewMode: 'attachments', viewMode: 'attachments',
attachmentId: attachmentMatch[2] attachmentId: attachmentId
} }
} }
} }

View File

@ -30,7 +30,7 @@ export default class AttachmentDetailTypeWidget extends TypeWidget {
this.children = []; this.children = [];
this.renderedAttachmentIds = new Set(); this.renderedAttachmentIds = new Set();
const attachment = await server.get(`notes/${this.noteId}/attachments/${this.noteContext.viewScope.attachmentId}/?includeContent=true`); const attachment = await server.get(`attachments/${this.noteContext.viewScope.attachmentId}/?includeContent=true`);
if (!attachment) { if (!attachment) {
this.$wrapper.html("<strong>This attachment has been deleted.</strong>"); this.$wrapper.html("<strong>This attachment has been deleted.</strong>");