Skip to content

Commit 9f919cf

Browse files
authored
Dropzone: Add "Copy link" button for new uploads (go-gitea#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.
1 parent 151b1a9 commit 9f919cf

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

web_src/js/features/common-global.js

+15
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,21 @@ export function initGlobalDropzone() {
167167
file.uuid = data.uuid;
168168
const input = $(`<input id="${data.uuid}" name="files" type="hidden">`).val(data.uuid);
169169
$dropzone.find('.files').append(input);
170+
// Create a "Copy Link" element, to conveniently copy the image
171+
// or file link as Markdown to the clipboard
172+
const copyLinkElement = document.createElement('a');
173+
copyLinkElement.className = 'dz-remove';
174+
copyLinkElement.href = '#';
175+
copyLinkElement.innerHTML = '<i class="fa fa-copy"></i> Copy link';
176+
copyLinkElement.addEventListener('click', (e) => {
177+
e.preventDefault();
178+
let fileMarkdown = `[${file.name}](/attachments/${file.uuid})`;
179+
if (file.type.startsWith('image/')) {
180+
fileMarkdown = `!${fileMarkdown}`;
181+
}
182+
navigator.clipboard.writeText(fileMarkdown);
183+
});
184+
file.previewTemplate.appendChild(copyLinkElement);
170185
});
171186
this.on('removedfile', (file) => {
172187
$(`#${file.uuid}`).remove();

0 commit comments

Comments
 (0)