Dropzone: Add "Copy link" button for new uploads (#22517)

Once an attachment is successfully uploaded via Dropzone, display a
"Copy link" under the "Remove file" button.
Once the button is clicked, depending if the attachment is an image or a
file, the appropriate markup is written to the clipboard, so it can be
conveniently pasted in the description.
This commit is contained in:
Francesco Siddi 2023-01-19 06:33:40 +01:00 committed by GitHub
parent 151b1a9508
commit 9f919cf083
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -167,6 +167,21 @@ export function initGlobalDropzone() {
file.uuid = data.uuid; file.uuid = data.uuid;
const input = $(`<input id="${data.uuid}" name="files" type="hidden">`).val(data.uuid); const input = $(`<input id="${data.uuid}" name="files" type="hidden">`).val(data.uuid);
$dropzone.find('.files').append(input); $dropzone.find('.files').append(input);
// Create a "Copy Link" element, to conveniently copy the image
// or file link as Markdown to the clipboard
const copyLinkElement = document.createElement('a');
copyLinkElement.className = 'dz-remove';
copyLinkElement.href = '#';
copyLinkElement.innerHTML = '<i class="fa fa-copy"></i> Copy link';
copyLinkElement.addEventListener('click', (e) => {
e.preventDefault();
let fileMarkdown = `[${file.name}](/attachments/${file.uuid})`;
if (file.type.startsWith('image/')) {
fileMarkdown = `!${fileMarkdown}`;
}
navigator.clipboard.writeText(fileMarkdown);
});
file.previewTemplate.appendChild(copyLinkElement);
}); });
this.on('removedfile', (file) => { this.on('removedfile', (file) => {
$(`#${file.uuid}`).remove(); $(`#${file.uuid}`).remove();