Skip to content

Commit a88f718

Browse files
authored
Refactor dropzone (#31482)
Refactor the legacy code and remove some jQuery calls.
1 parent 35ce7a5 commit a88f718

File tree

8 files changed

+184
-184
lines changed

8 files changed

+184
-184
lines changed

web_src/js/features/comp/ComboMarkdownEditor.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {initTextExpander} from './TextExpander.js';
1111
import {showErrorToast} from '../../modules/toast.js';
1212
import {POST} from '../../modules/fetch.js';
1313
import {initTextareaMarkdown} from './EditorMarkdown.js';
14+
import {initDropzone} from '../dropzone.js';
1415

1516
let elementIdCounter = 0;
1617

@@ -47,7 +48,7 @@ class ComboMarkdownEditor {
4748
this.prepareEasyMDEToolbarActions();
4849
this.setupContainer();
4950
this.setupTab();
50-
this.setupDropzone();
51+
await this.setupDropzone(); // textarea depends on dropzone
5152
this.setupTextarea();
5253

5354
await this.switchToUserPreference();
@@ -114,13 +115,30 @@ class ComboMarkdownEditor {
114115
}
115116
}
116117

117-
setupDropzone() {
118+
async setupDropzone() {
118119
const dropzoneParentContainer = this.container.getAttribute('data-dropzone-parent-container');
119120
if (dropzoneParentContainer) {
120121
this.dropzone = this.container.closest(this.container.getAttribute('data-dropzone-parent-container'))?.querySelector('.dropzone');
122+
if (this.dropzone) this.attachedDropzoneInst = await initDropzone(this.dropzone);
121123
}
122124
}
123125

126+
dropzoneGetFiles() {
127+
if (!this.dropzone) return null;
128+
return Array.from(this.dropzone.querySelectorAll('.files [name=files]'), (el) => el.value);
129+
}
130+
131+
dropzoneReloadFiles() {
132+
if (!this.dropzone) return;
133+
this.attachedDropzoneInst.emit('reload');
134+
}
135+
136+
dropzoneSubmitReload() {
137+
if (!this.dropzone) return;
138+
this.attachedDropzoneInst.emit('submit');
139+
this.attachedDropzoneInst.emit('reload');
140+
}
141+
124142
setupTab() {
125143
const tabs = this.container.querySelectorAll('.tabular.menu > .item');
126144

web_src/js/features/dropzone.js

Lines changed: 109 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,134 @@
1-
import $ from 'jquery';
21
import {svg} from '../svg.js';
32
import {htmlEscape} from 'escape-goat';
43
import {clippie} from 'clippie';
54
import {showTemporaryTooltip} from '../modules/tippy.js';
6-
import {POST} from '../modules/fetch.js';
5+
import {GET, POST} from '../modules/fetch.js';
76
import {showErrorToast} from '../modules/toast.js';
7+
import {createElementFromHTML, createElementFromAttrs} from '../utils/dom.js';
88

99
const {csrfToken, i18n} = window.config;
1010

11-
export async function createDropzone(el, opts) {
11+
async function createDropzone(el, opts) {
1212
const [{Dropzone}] = await Promise.all([
1313
import(/* webpackChunkName: "dropzone" */'dropzone'),
1414
import(/* webpackChunkName: "dropzone" */'dropzone/dist/dropzone.css'),
1515
]);
1616
return new Dropzone(el, opts);
1717
}
1818

19-
export function initGlobalDropzone() {
20-
for (const el of document.querySelectorAll('.dropzone')) {
21-
initDropzone(el);
22-
}
19+
function addCopyLink(file) {
20+
// Create a "Copy Link" element, to conveniently copy the image or file link as Markdown to the clipboard
21+
// The "<a>" element has a hardcoded cursor: pointer because the default is overridden by .dropzone
22+
const copyLinkEl = createElementFromHTML(`
23+
<div class="tw-text-center">
24+
<a href="#" class="tw-cursor-pointer">${svg('octicon-copy', 14)} Copy link</a>
25+
</div>`);
26+
copyLinkEl.addEventListener('click', async (e) => {
27+
e.preventDefault();
28+
let fileMarkdown = `[${file.name}](/attachments/${file.uuid})`;
29+
if (file.type?.startsWith('image/')) {
30+
fileMarkdown = `!${fileMarkdown}`;
31+
} else if (file.type?.startsWith('video/')) {
32+
fileMarkdown = `<video src="/attachments/${htmlEscape(file.uuid)}" title="${htmlEscape(file.name)}" controls></video>`;
33+
}
34+
const success = await clippie(fileMarkdown);
35+
showTemporaryTooltip(e.target, success ? i18n.copy_success : i18n.copy_error);
36+
});
37+
file.previewTemplate.append(copyLinkEl);
2338
}
2439

25-
export function initDropzone(el) {
26-
const $dropzone = $(el);
27-
const _promise = createDropzone(el, {
28-
url: $dropzone.data('upload-url'),
40+
/**
41+
* @param {HTMLElement} dropzoneEl
42+
*/
43+
export async function initDropzone(dropzoneEl) {
44+
const listAttachmentsUrl = dropzoneEl.closest('[data-attachment-url]')?.getAttribute('data-attachment-url');
45+
const removeAttachmentUrl = dropzoneEl.getAttribute('data-remove-url');
46+
const attachmentBaseLinkUrl = dropzoneEl.getAttribute('data-link-url');
47+
48+
let disableRemovedfileEvent = false; // when resetting the dropzone (removeAllFiles), disable the "removedfile" event
49+
let fileUuidDict = {}; // to record: if a comment has been saved, then the uploaded files won't be deleted from server when clicking the Remove in the dropzone
50+
const opts = {
51+
url: dropzoneEl.getAttribute('data-upload-url'),
2952
headers: {'X-Csrf-Token': csrfToken},
30-
maxFiles: $dropzone.data('max-file'),
31-
maxFilesize: $dropzone.data('max-size'),
32-
acceptedFiles: (['*/*', ''].includes($dropzone.data('accepts'))) ? null : $dropzone.data('accepts'),
53+
acceptedFiles: ['*/*', ''].includes(dropzoneEl.getAttribute('data-accepts')) ? null : dropzoneEl.getAttribute('data-accepts'),
3354
addRemoveLinks: true,
34-
dictDefaultMessage: $dropzone.data('default-message'),
35-
dictInvalidFileType: $dropzone.data('invalid-input-type'),
36-
dictFileTooBig: $dropzone.data('file-too-big'),
37-
dictRemoveFile: $dropzone.data('remove-file'),
55+
dictDefaultMessage: dropzoneEl.getAttribute('data-default-message'),
56+
dictInvalidFileType: dropzoneEl.getAttribute('data-invalid-input-type'),
57+
dictFileTooBig: dropzoneEl.getAttribute('data-file-too-big'),
58+
dictRemoveFile: dropzoneEl.getAttribute('data-remove-file'),
3859
timeout: 0,
3960
thumbnailMethod: 'contain',
4061
thumbnailWidth: 480,
4162
thumbnailHeight: 480,
42-
init() {
43-
this.on('success', (file, data) => {
44-
file.uuid = data.uuid;
45-
const $input = $(`<input id="${data.uuid}" name="files" type="hidden">`).val(data.uuid);
46-
$dropzone.find('.files').append($input);
47-
// Create a "Copy Link" element, to conveniently copy the image
48-
// or file link as Markdown to the clipboard
49-
const copyLinkElement = document.createElement('div');
50-
copyLinkElement.className = 'tw-text-center';
51-
// The a element has a hardcoded cursor: pointer because the default is overridden by .dropzone
52-
copyLinkElement.innerHTML = `<a href="#" style="cursor: pointer;">${svg('octicon-copy', 14, 'copy link')} Copy link</a>`;
53-
copyLinkElement.addEventListener('click', async (e) => {
54-
e.preventDefault();
55-
let fileMarkdown = `[${file.name}](/attachments/${file.uuid})`;
56-
if (file.type.startsWith('image/')) {
57-
fileMarkdown = `!${fileMarkdown}`;
58-
} else if (file.type.startsWith('video/')) {
59-
fileMarkdown = `<video src="/attachments/${file.uuid}" title="${htmlEscape(file.name)}" controls></video>`;
60-
}
61-
const success = await clippie(fileMarkdown);
62-
showTemporaryTooltip(e.target, success ? i18n.copy_success : i18n.copy_error);
63-
});
64-
file.previewTemplate.append(copyLinkElement);
65-
});
66-
this.on('removedfile', (file) => {
67-
$(`#${file.uuid}`).remove();
68-
if ($dropzone.data('remove-url')) {
69-
POST($dropzone.data('remove-url'), {
70-
data: new URLSearchParams({file: file.uuid}),
71-
});
72-
}
73-
});
74-
this.on('error', function (file, message) {
75-
showErrorToast(message);
76-
this.removeFile(file);
77-
});
78-
},
63+
};
64+
if (dropzoneEl.hasAttribute('data-max-file')) opts.maxFiles = Number(dropzoneEl.getAttribute('data-max-file'));
65+
if (dropzoneEl.hasAttribute('data-max-size')) opts.maxFilesize = Number(dropzoneEl.getAttribute('data-max-size'));
66+
67+
// there is a bug in dropzone: if a non-image file is uploaded, then it tries to request the file from server by something like:
68+
// "http://localhost:3000/owner/repo/issues/[object%20Event]"
69+
// the reason is that the preview "callback(dataURL)" is assign to "img.onerror" then "thumbnail" uses the error object as the dataURL and generates '<img src="[object Event]">'
70+
const dzInst = await createDropzone(dropzoneEl, opts);
71+
dzInst.on('success', (file, data) => {
72+
file.uuid = data.uuid;
73+
fileUuidDict[file.uuid] = {submitted: false};
74+
const input = createElementFromAttrs('input', {name: 'files', type: 'hidden', id: `dropzone-file-${data.uuid}`, value: data.uuid});
75+
dropzoneEl.querySelector('.files').append(input);
76+
addCopyLink(file);
77+
});
78+
79+
dzInst.on('removedfile', async (file) => {
80+
if (disableRemovedfileEvent) return;
81+
document.querySelector(`#dropzone-file-${file.uuid}`)?.remove();
82+
// when the uploaded file number reaches the limit, there is no uuid in the dict, and it doesn't need to be removed from server
83+
if (removeAttachmentUrl && fileUuidDict[file.uuid] && !fileUuidDict[file.uuid].submitted) {
84+
await POST(removeAttachmentUrl, {data: new URLSearchParams({file: file.uuid})});
85+
}
86+
});
87+
88+
dzInst.on('submit', () => {
89+
for (const fileUuid of Object.keys(fileUuidDict)) {
90+
fileUuidDict[fileUuid].submitted = true;
91+
}
7992
});
93+
94+
dzInst.on('reload', async () => {
95+
try {
96+
const resp = await GET(listAttachmentsUrl);
97+
const respData = await resp.json();
98+
// do not trigger the "removedfile" event, otherwise the attachments would be deleted from server
99+
disableRemovedfileEvent = true;
100+
dzInst.removeAllFiles(true);
101+
disableRemovedfileEvent = false;
102+
103+
dropzoneEl.querySelector('.files').innerHTML = '';
104+
for (const el of dropzoneEl.querySelectorAll('.dz-preview')) el.remove();
105+
fileUuidDict = {};
106+
for (const attachment of respData) {
107+
const imgSrc = `${attachmentBaseLinkUrl}/${attachment.uuid}`;
108+
dzInst.emit('addedfile', attachment);
109+
dzInst.emit('thumbnail', attachment, imgSrc);
110+
dzInst.emit('complete', attachment);
111+
addCopyLink(attachment);
112+
fileUuidDict[attachment.uuid] = {submitted: true};
113+
const input = createElementFromAttrs('input', {name: 'files', type: 'hidden', id: `dropzone-file-${attachment.uuid}`, value: attachment.uuid});
114+
dropzoneEl.querySelector('.files').append(input);
115+
}
116+
if (!dropzoneEl.querySelector('.dz-preview')) {
117+
dropzoneEl.classList.remove('dz-started');
118+
}
119+
} catch (error) {
120+
// TODO: if listing the existing attachments failed, it should stop from operating the content or attachments,
121+
// otherwise the attachments might be lost.
122+
showErrorToast(`Failed to load attachments: ${error}`);
123+
console.error(error);
124+
}
125+
});
126+
127+
dzInst.on('error', (file, message) => {
128+
showErrorToast(`Dropzone upload error: ${message}`);
129+
dzInst.removeFile(file);
130+
});
131+
132+
if (listAttachmentsUrl) dzInst.emit('reload');
133+
return dzInst;
80134
}

web_src/js/features/repo-editor.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {hideElem, queryElems, showElem} from '../utils/dom.js';
55
import {initMarkupContent} from '../markup/content.js';
66
import {attachRefIssueContextPopup} from './contextpopup.js';
77
import {POST} from '../modules/fetch.js';
8+
import {initDropzone} from './dropzone.js';
89

910
function initEditPreviewTab($form) {
1011
const $tabMenu = $form.find('.repo-editor-menu');
@@ -41,8 +42,11 @@ function initEditPreviewTab($form) {
4142
}
4243

4344
export function initRepoEditor() {
44-
const $editArea = $('.repository.editor textarea#edit_area');
45-
if (!$editArea.length) return;
45+
const dropzoneUpload = document.querySelector('.page-content.repository.editor.upload .dropzone');
46+
if (dropzoneUpload) initDropzone(dropzoneUpload);
47+
48+
const editArea = document.querySelector('.page-content.repository.editor textarea#edit_area');
49+
if (!editArea) return;
4650

4751
for (const el of queryElems('.js-quick-pull-choice-option')) {
4852
el.addEventListener('input', () => {
@@ -108,7 +112,7 @@ export function initRepoEditor() {
108112
initEditPreviewTab($form);
109113

110114
(async () => {
111-
const editor = await createCodeEditor($editArea[0], filenameInput);
115+
const editor = await createCodeEditor(editArea, filenameInput);
112116

113117
// Using events from https://github.com/codedance/jquery.AreYouSure#advanced-usage
114118
// to enable or disable the commit button
@@ -142,7 +146,7 @@ export function initRepoEditor() {
142146

143147
commitButton?.addEventListener('click', (e) => {
144148
// A modal which asks if an empty file should be committed
145-
if (!$editArea.val()) {
149+
if (!editArea.value) {
146150
$('#edit-empty-content-modal').modal({
147151
onApprove() {
148152
$('.edit.form').trigger('submit');

0 commit comments

Comments
 (0)