mirror of
https://github.com/zadam/trilium.git
synced 2025-06-06 18:08:33 +02:00
restore revision with attachments
This commit is contained in:
parent
8b46d6c718
commit
c6c162cdda
@ -1569,11 +1569,9 @@ class BNote extends AbstractBeccaEntity {
|
|||||||
noteRevision.save(); // to generate noteRevisionId which is then used to save attachments
|
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 attachmentContent = noteAttachment.getContent();
|
|
||||||
|
|
||||||
const revisionAttachment = noteAttachment.copy();
|
const revisionAttachment = noteAttachment.copy();
|
||||||
revisionAttachment.parentId = noteRevision.noteRevisionId;
|
revisionAttachment.parentId = noteRevision.noteRevisionId;
|
||||||
revisionAttachment.setContent(attachmentContent, {
|
revisionAttachment.setContent(noteAttachment.getContent(), {
|
||||||
forceSave: true,
|
forceSave: true,
|
||||||
forceCold: true
|
forceCold: true
|
||||||
});
|
});
|
||||||
|
@ -5,6 +5,8 @@ const utils = require('../../services/utils');
|
|||||||
const dateUtils = require('../../services/date_utils');
|
const dateUtils = require('../../services/date_utils');
|
||||||
const becca = require('../becca');
|
const becca = require('../becca');
|
||||||
const AbstractBeccaEntity = require("./abstract_becca_entity");
|
const AbstractBeccaEntity = require("./abstract_becca_entity");
|
||||||
|
const sql = require("../../services/sql");
|
||||||
|
const BAttachment = require("./battachment");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* NoteRevision represents snapshot of note's title and content at some point in the past.
|
* NoteRevision represents snapshot of note's title and content at some point in the past.
|
||||||
@ -92,6 +94,16 @@ class BNoteRevision extends AbstractBeccaEntity {
|
|||||||
this._setContent(content, opts);
|
this._setContent(content, opts);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @returns {BAttachment[]} */
|
||||||
|
getAttachments() {
|
||||||
|
return sql.getRows(`
|
||||||
|
SELECT attachments.*
|
||||||
|
FROM attachments
|
||||||
|
WHERE parentId = ?
|
||||||
|
AND isDeleted = 0`, [this.noteRevisionId])
|
||||||
|
.map(row => new BAttachment(row));
|
||||||
|
}
|
||||||
|
|
||||||
beforeSaving() {
|
beforeSaving() {
|
||||||
super.beforeSaving();
|
super.beforeSaving();
|
||||||
|
|
||||||
|
@ -28,9 +28,8 @@ export default class AttachmentDetailTypeWidget extends TypeWidget {
|
|||||||
async doRefresh(note) {
|
async doRefresh(note) {
|
||||||
this.$wrapper.empty();
|
this.$wrapper.empty();
|
||||||
this.children = [];
|
this.children = [];
|
||||||
this.renderedAttachmentIds = new Set();
|
|
||||||
|
|
||||||
const attachment = await server.get(`attachments/${this.noteContext.viewScope.attachmentId}/?includeContent=true`);
|
const attachment = await server.get(`attachments/${this.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>");
|
||||||
@ -46,10 +45,14 @@ export default class AttachmentDetailTypeWidget extends TypeWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async entitiesReloadedEvent({loadResults}) {
|
async entitiesReloadedEvent({loadResults}) {
|
||||||
const attachmentChange = loadResults.getAttachments().find(att => att.attachmentId === this.attachment.attachmentId);
|
const attachmentChange = loadResults.getAttachments().find(att => att.attachmentId === this.attachmentId);
|
||||||
|
|
||||||
if (attachmentChange?.isDeleted) {
|
if (attachmentChange?.isDeleted) {
|
||||||
this.refresh(); // all other updates are handled within AttachmentDetailWidget
|
this.refresh(); // all other updates are handled within AttachmentDetailWidget
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get attachmentId() {
|
||||||
|
return this.noteContext.viewScope.attachmentId;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -95,11 +95,27 @@ function restoreNoteRevision(req) {
|
|||||||
if (noteRevision) {
|
if (noteRevision) {
|
||||||
const note = noteRevision.getNote();
|
const note = noteRevision.getNote();
|
||||||
|
|
||||||
note.saveNoteRevision();
|
sql.transactional(() => {
|
||||||
|
note.saveNoteRevision();
|
||||||
|
|
||||||
note.title = noteRevision.title;
|
for (const oldNoteAttachment of note.getAttachments()) {
|
||||||
note.setContent(noteRevision.getContent());
|
oldNoteAttachment.markAsDeleted();
|
||||||
note.save();
|
}
|
||||||
|
|
||||||
|
let revisionContent = noteRevision.getContent();
|
||||||
|
|
||||||
|
for (const revisionAttachment of noteRevision.getAttachments()) {
|
||||||
|
const noteAttachment = revisionAttachment.copy();
|
||||||
|
noteAttachment.parentId = note.noteId;
|
||||||
|
noteAttachment.setContent(revisionAttachment.getContent(), { forceSave: true });
|
||||||
|
|
||||||
|
// content is rewritten to point to the restored revision attachments
|
||||||
|
revisionContent = revisionContent.replaceAll(`attachments/${revisionAttachment.attachmentId}`, `attachments/${noteAttachment.attachmentId}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
note.title = noteRevision.title;
|
||||||
|
note.setContent(revisionContent, { forceSave: true });
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user