Skip to content

Commit

Permalink
Dropzone: Add "Copy link" button for new uploads (#22517)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
fsiddi authored Jan 19, 2023
1 parent 151b1a9 commit 9f919cf
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions web_src/js/features/common-global.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,21 @@ export function initGlobalDropzone() {
file.uuid = data.uuid;
const input = $(`<input id="${data.uuid}" name="files" type="hidden">`).val(data.uuid);
$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) => {
$(`#${file.uuid}`).remove();
Expand Down

0 comments on commit 9f919cf

Please sign in to comment.