Notes/src/public/app/widgets/attachment_detail.js

165 lines
5.7 KiB
JavaScript
Raw Normal View History

2023-04-01 23:55:04 +02:00
import utils from "../services/utils.js";
import AttachmentActionsWidget from "./buttons/attachments_actions.js";
2023-03-30 23:48:26 +02:00
import BasicWidget from "./basic_widget.js";
2023-04-01 23:55:04 +02:00
import server from "../services/server.js";
2023-04-21 00:19:17 +02:00
import options from "../services/options.js";
2023-05-03 10:23:20 +02:00
import imageService from "../services/image.js";
import linkService from "../services/link.js";
2023-05-20 23:46:45 +02:00
import contentRenderer from "../services/content_renderer.js";
2023-03-30 23:48:26 +02:00
const TPL = `
<div class="attachment-detail">
<style>
.attachment-detail-wrapper {
margin-bottom: 20px;
}
.attachment-title-line {
display: flex;
align-items: baseline;
gap: 1em;
2023-03-30 23:48:26 +02:00
}
.attachment-details {
margin-left: 10px;
}
.attachment-content pre {
background: var(--accented-background-color);
padding: 10px;
margin-top: 10px;
margin-bottom: 10px;
}
2023-04-11 17:45:51 +02:00
.attachment-detail-wrapper.list-view .attachment-content pre {
max-height: 400px;
}
2023-03-30 23:48:26 +02:00
.attachment-content img {
margin: 10px;
2023-04-11 17:45:51 +02:00
}
.attachment-detail-wrapper.list-view .attachment-content img {
2023-03-30 23:48:26 +02:00
max-height: 300px;
max-width: 90%;
object-fit: contain;
}
2023-04-11 17:45:51 +02:00
.attachment-detail-wrapper.full-detail .attachment-content img {
max-width: 90%;
object-fit: contain;
}
2023-04-21 00:19:17 +02:00
.attachment-detail-wrapper.scheduled-for-deletion .attachment-content img {
filter: contrast(10%);
}
2023-03-30 23:48:26 +02:00
</style>
2023-04-01 13:58:53 +02:00
<div class="attachment-detail-wrapper">
<div class="attachment-title-line">
<div class="attachment-actions-container"></div>
2023-04-11 17:45:51 +02:00
<h4 class="attachment-title"></h4>
2023-04-01 13:58:53 +02:00
<div class="attachment-details"></div>
2023-04-01 23:55:04 +02:00
<div style="flex: 1 1;"></div>
2023-04-01 13:58:53 +02:00
</div>
2023-04-21 00:19:17 +02:00
<div class="attachment-deletion-warning alert alert-info"></div>
2023-04-01 13:58:53 +02:00
<div class="attachment-content"></div>
</div>
2023-03-30 23:48:26 +02:00
</div>`;
export default class AttachmentDetailWidget extends BasicWidget {
constructor(attachment, isFullDetail) {
2023-03-30 23:48:26 +02:00
super();
2023-04-01 23:55:04 +02:00
this.contentSized();
2023-03-30 23:48:26 +02:00
this.attachment = attachment;
this.attachmentActionsWidget = new AttachmentActionsWidget(attachment, isFullDetail);
this.isFullDetail = isFullDetail;
2023-04-01 13:58:53 +02:00
this.child(this.attachmentActionsWidget);
2023-03-30 23:48:26 +02:00
}
doRender() {
this.$widget = $(TPL);
2023-04-01 23:55:04 +02:00
this.refresh();
super.doRender();
}
async refresh() {
2023-04-01 23:55:04 +02:00
this.$widget.find('.attachment-detail-wrapper')
.empty()
.append(
$(TPL)
.find('.attachment-detail-wrapper')
.html()
);
2023-03-30 23:48:26 +02:00
this.$wrapper = this.$widget.find('.attachment-detail-wrapper');
2023-04-11 17:45:51 +02:00
this.$wrapper.addClass(this.isFullDetail ? "full-detail" : "list-view");
if (!this.isFullDetail) {
this.$wrapper.find('.attachment-title').append(
await linkService.createNoteLink(this.attachment.parentId, {
title: this.attachment.title,
viewScope: {
viewMode: 'attachments',
attachmentId: this.attachment.attachmentId
}
})
2023-04-11 17:45:51 +02:00
);
} else {
this.$wrapper.find('.attachment-title')
.text(this.attachment.title);
}
2023-04-21 00:19:17 +02:00
const $deletionWarning = this.$wrapper.find('.attachment-deletion-warning');
const {utcDateScheduledForErasureSince} = this.attachment;
if (utcDateScheduledForErasureSince) {
this.$wrapper.addClass("scheduled-for-deletion");
2023-04-20 00:11:09 +02:00
2023-04-21 00:19:17 +02:00
const scheduledSinceTimestamp = utils.parseDate(utcDateScheduledForErasureSince)?.getTime();
const intervalMs = options.getInt('eraseUnusedImageAttachmentsAfterSeconds') * 1000;
const deletionTimestamp = scheduledSinceTimestamp + intervalMs;
const willBeDeletedInMs = deletionTimestamp - Date.now();
2023-04-20 00:11:09 +02:00
2023-04-21 00:19:17 +02:00
$deletionWarning.show();
if (willBeDeletedInMs >= 60000) {
$deletionWarning.text(`This attachment will be deleted in ${utils.formatTimeInterval(willBeDeletedInMs)}`);
} else {
$deletionWarning.text(`This attachment will be deleted soon`);
}
$deletionWarning.append(", because the image attachment is not used. To prevent deletion, add the image back into the note.");
} else {
this.$wrapper.removeClass("scheduled-for-deletion");
$deletionWarning.hide();
2023-04-20 00:11:09 +02:00
}
2023-04-01 13:58:53 +02:00
this.$wrapper.find('.attachment-details')
.text(`Role: ${this.attachment.role}, Size: ${utils.formatSize(this.attachment.contentLength)}`);
this.$wrapper.find('.attachment-actions-container').append(this.attachmentActionsWidget.render());
2023-05-20 23:46:45 +02:00
this.$wrapper.find('.attachment-content').append(contentRenderer.getRenderedContent(this.attachment));
2023-03-30 23:48:26 +02:00
}
2023-04-01 23:55:04 +02:00
2023-05-03 10:23:20 +02:00
copyAttachmentReferenceToClipboard() {
imageService.copyImageReferenceToClipboard(this.$wrapper.find('.attachment-content'));
}
2023-04-03 23:47:24 +02:00
async entitiesReloadedEvent({loadResults}) {
2023-04-01 23:55:04 +02:00
const attachmentChange = loadResults.getAttachments().find(att => att.attachmentId === this.attachment.attachmentId);
if (attachmentChange) {
if (attachmentChange.isDeleted) {
this.toggleInt(false);
} else {
2023-05-20 23:46:45 +02:00
this.attachment = await server.get(`attachments/${this.attachment.attachmentId}`);
2023-04-01 23:55:04 +02:00
this.refresh();
}
}
}
2023-03-30 23:48:26 +02:00
}