Do not open preview for attachments that cannot be previewed

This commit is contained in:
Vid Smole 2023-08-03 18:57:12 +02:00
parent d0ba89d913
commit 21ffff3977
No known key found for this signature in database
GPG key ID: 85348A78371C84EB

View file

@ -35,24 +35,24 @@ Template.attachmentGallery.events({
}, },
'click .js-rename': Popup.open('attachmentRename'), 'click .js-rename': Popup.open('attachmentRename'),
'click .js-confirm-delete': Popup.afterConfirm('attachmentDelete', function() { 'click .js-confirm-delete': Popup.afterConfirm('attachmentDelete', function() {
Attachments.remove(this._id); Attachments.remove(this._id);
Popup.back(2); Popup.back(2);
}), }),
}); });
function getNextAttachmentId(currentAttachmentId) { function getNextAttachmentId(currentAttachmentId, offset = 0) {
const attachments = ReactiveCache.getAttachments({'meta.cardId': cardId}); const attachments = ReactiveCache.getAttachments({'meta.cardId': cardId});
let i = 0; let i = 0;
for (; i < attachments.length; i++) { for (; i < attachments.length; i++) {
if (attachments[i]._id === currentAttachmentId) { if (attachments[i]._id === currentAttachmentId) {
break; break;
}
} }
return attachments[(i + 1 + attachments.length) % attachments.length]._id; }
return attachments[(i + offset + 1 + attachments.length) % attachments.length]._id;
} }
function getPrevAttachmentId(currentAttachmentId) { function getPrevAttachmentId(currentAttachmentId, offset = 0) {
const attachments = ReactiveCache.getAttachments({'meta.cardId': cardId}); const attachments = ReactiveCache.getAttachments({'meta.cardId': cardId});
let i = 0; let i = 0;
@ -61,52 +61,70 @@ function getPrevAttachmentId(currentAttachmentId) {
break; break;
} }
} }
return attachments[(i - 1 + attachments.length) % attachments.length]._id; return attachments[(i + offset - 1 + attachments.length) % attachments.length]._id;
} }
function openAttachmentViewer(attachmentId){ function attachmentCanBeOpened(attachment) {
return (
attachment.isImage ||
attachment.isPDF ||
attachment.isText ||
attachment.isJSON ||
attachment.isVideo ||
attachment.isAudio
);
}
const attachment = ReactiveCache.getAttachment(attachmentId); function openAttachmentViewer(attachmentId) {
const attachment = ReactiveCache.getAttachment(attachmentId);
$("#attachment-name").text(attachment.name); // Check if we can open the attachment (if we have a viewer for it) and exit if not
if (!attachmentCanBeOpened(attachment)) {
return;
}
// IMPORTANT: if you ever add a new viewer, make sure you also implement /*
// cleanup in the closeAttachmentViewer() function Instructions for adding a new viewer:
switch(true){ - add a new case to the switch statement below
case (attachment.isImage): - implement cleanup in the closeAttachmentViewer() function, if necessary
$("#image-viewer").attr("src", attachment.link()); - mark attachment type as openable by adding a new condition to the attachmentCanBeOpened function
$("#image-viewer").removeClass("hidden"); */
break; switch(true){
case (attachment.isPDF): case (attachment.isImage):
$("#pdf-viewer").attr("data", attachment.link()); $("#image-viewer").attr("src", attachment.link());
$("#pdf-viewer").removeClass("hidden"); $("#image-viewer").removeClass("hidden");
break; break;
case (attachment.isVideo): case (attachment.isPDF):
// We have to create a new <source> DOM element and append it to the video $("#pdf-viewer").attr("data", attachment.link());
// element, otherwise the video won't load $("#pdf-viewer").removeClass("hidden");
let videoSource = document.createElement('source'); break;
videoSource.setAttribute('src', attachment.link()); case (attachment.isVideo):
$("#video-viewer").append(videoSource); // We have to create a new <source> DOM element and append it to the video
// element, otherwise the video won't load
let videoSource = document.createElement('source');
videoSource.setAttribute('src', attachment.link());
$("#video-viewer").append(videoSource);
$("#video-viewer").removeClass("hidden"); $("#video-viewer").removeClass("hidden");
break; break;
case (attachment.isAudio): case (attachment.isAudio):
// We have to create a new <source> DOM element and append it to the audio // We have to create a new <source> DOM element and append it to the audio
// element, otherwise the audio won't load // element, otherwise the audio won't load
let audioSource = document.createElement('source'); let audioSource = document.createElement('source');
audioSource.setAttribute('src', attachment.link()); audioSource.setAttribute('src', attachment.link());
$("#audio-viewer").append(audioSource); $("#audio-viewer").append(audioSource);
$("#audio-viewer").removeClass("hidden"); $("#audio-viewer").removeClass("hidden");
break; break;
case (attachment.isText): case (attachment.isText):
case (attachment.isJSON): case (attachment.isJSON):
$("#txt-viewer").attr("data", attachment.link()); $("#txt-viewer").attr("data", attachment.link());
$("#txt-viewer").removeClass("hidden"); $("#txt-viewer").removeClass("hidden");
break; break;
} }
$("#viewer-overlay").removeClass("hidden"); $('#attachment-name').text(attachment.name);
$('#viewer-overlay').removeClass('hidden');
} }
function closeAttachmentViewer() { function closeAttachmentViewer() {
@ -152,16 +170,36 @@ Template.attachmentViewer.events({
closeAttachmentViewer(); closeAttachmentViewer();
}, },
'click #next-attachment'(event) { 'click #next-attachment'(event) {
closeAttachmentViewer() closeAttachmentViewer();
const id = getNextAttachmentId(openAttachmentId);
openAttachmentId = id; let i = 0;
openAttachmentViewer(id); // Find an attachment that can be opened
while (true) {
const id = getNextAttachmentId(openAttachmentId, i);
const attachment = ReactiveCache.getAttachment(id);
if (attachmentCanBeOpened(attachment)) {
openAttachmentId = id;
openAttachmentViewer(id);
break;
}
i++;
}
}, },
'click #prev-attachment'(event) { 'click #prev-attachment'(event) {
closeAttachmentViewer() closeAttachmentViewer();
const id = getPrevAttachmentId(openAttachmentId);
openAttachmentId = id; let i = 0;
openAttachmentViewer(id); // Find an attachment that can be opened
while (true) {
const id = getPrevAttachmentId(openAttachmentId, i);
const attachment = ReactiveCache.getAttachment(id);
if (attachmentCanBeOpened(attachment)) {
openAttachmentId = id;
openAttachmentViewer(id);
break;
}
i--;
}
} }
}); });