Skip to content

Commit fb55033

Browse files
committed
First step for multiple dropzones per page.
1 parent c1ff59c commit fb55033

File tree

6 files changed

+41
-34
lines changed

6 files changed

+41
-34
lines changed

templates/repo/editor/upload.tmpl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
</div>
2727
</div>
2828
<div class="field">
29-
<div class="files"></div>
3029
{{template "repo/upload" .}}
3130
</div>
3231
{{template "repo/editor/commit_form" .}}

templates/repo/issue/comment_tab.tmpl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
</div>
1515
{{if .IsAttachmentEnabled}}
1616
<div class="field">
17-
<div class="files"></div>
1817
{{template "repo/upload" .}}
1918
</div>
2019
{{end}}

templates/repo/issue/view_content.tmpl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,6 @@
197197
</div>
198198
{{if .IsAttachmentEnabled}}
199199
<div class="field">
200-
<div class="comment-files"></div>
201200
{{template "repo/upload" .}}
202201
</div>
203202
{{end}}

templates/repo/release/new.tmpl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@
7676
{{end}}
7777
{{if .IsAttachmentEnabled}}
7878
<div class="field">
79-
<div class="files"></div>
8079
{{template "repo/upload" .}}
8180
</div>
8281
{{end}}

templates/repo/upload.tmpl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<div
22
class="ui dropzone"
3-
id="dropzone"
43
data-link-url="{{.UploadLinkUrl}}"
54
data-upload-url="{{.UploadUrl}}"
65
data-remove-url="{{.UploadRemoveUrl}}"
@@ -11,4 +10,6 @@
1110
data-invalid-input-type="{{.i18n.Tr "dropzone.invalid_input_type"}}"
1211
data-file-too-big="{{.i18n.Tr "dropzone.file_too_big"}}"
1312
data-remove-file="{{.i18n.Tr "dropzone.remove_file"}}"
14-
></div>
13+
>
14+
<div class="files"></div>
15+
</div>

web_src/js/index.js

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -327,11 +327,11 @@ function getPastedImages(e) {
327327
return files;
328328
}
329329

330-
async function uploadFile(file) {
330+
async function uploadFile(file, uploadUrl) {
331331
const formData = new FormData();
332332
formData.append('file', file, file.name);
333333

334-
const res = await fetch($('#dropzone').data('upload-url'), {
334+
const res = await fetch(uploadUrl, {
335335
method: 'POST',
336336
headers: {'X-Csrf-Token': csrf},
337337
body: formData,
@@ -345,24 +345,33 @@ function reload() {
345345

346346
function initImagePaste(target) {
347347
target.each(function () {
348-
this.addEventListener('paste', async (e) => {
349-
for (const img of getPastedImages(e)) {
350-
const name = img.name.substr(0, img.name.lastIndexOf('.'));
351-
insertAtCursor(this, `![${name}]()`);
352-
const data = await uploadFile(img);
353-
replaceAndKeepCursor(this, `![${name}]()`, `![${name}](${AppSubUrl}/attachments/${data.uuid})`);
354-
const input = $(`<input id="${data.uuid}" name="files" type="hidden">`).val(data.uuid);
355-
$('.files').append(input);
356-
}
357-
}, false);
348+
const dropzone = this.querySelector('.dropzone');
349+
if (!dropzone) {
350+
return;
351+
}
352+
const uploadUrl = dropzone.dataset.uploadUrl;
353+
const dropzoneFiles = dropzone.querySelector('.files');
354+
for (const textarea of this.querySelectorAll('textarea')) {
355+
textarea.addEventListener('paste', async (e) => {
356+
for (const img of getPastedImages(e)) {
357+
const name = img.name.substr(0, img.name.lastIndexOf('.'));
358+
insertAtCursor(textarea, `![${name}]()`);
359+
const data = await uploadFile(img, uploadUrl);
360+
replaceAndKeepCursor(textarea, `![${name}]()`, `![${name}](${AppSubUrl}/attachments/${data.uuid})`);
361+
const input = $(`<input id="${data.uuid}" name="files" type="hidden">`).val(data.uuid);
362+
dropzoneFiles.appendChild(input[0]);
363+
}
364+
}, false);
365+
}
358366
});
359367
}
360368

361-
function initSimpleMDEImagePaste(simplemde, files) {
369+
function initSimpleMDEImagePaste(simplemde, dropzone, files) {
370+
const uploadUrl = dropzone.dataset.uploadUrl;
362371
simplemde.codemirror.on('paste', async (_, e) => {
363372
for (const img of getPastedImages(e)) {
364373
const name = img.name.substr(0, img.name.lastIndexOf('.'));
365-
const data = await uploadFile(img);
374+
const data = await uploadFile(img, uploadUrl);
366375
const pos = simplemde.codemirror.getCursor();
367376
simplemde.codemirror.replaceRange(`![${name}](${AppSubUrl}/attachments/${data.uuid})`, pos);
368377
const input = $(`<input id="${data.uuid}" name="files" type="hidden">`).val(data.uuid);
@@ -381,7 +390,7 @@ function initCommentForm() {
381390
autoSimpleMDE = setCommentSimpleMDE($('.comment.form textarea:not(.review-textarea)'));
382391
initBranchSelector();
383392
initCommentPreviewTab($('.comment.form'));
384-
initImagePaste($('.comment.form textarea'));
393+
initImagePaste($('.comment.form'));
385394

386395
// Listsubmit
387396
function initListSubmits(selector, outerSelector) {
@@ -993,8 +1002,7 @@ async function initRepository() {
9931002

9941003
let dz;
9951004
const $dropzone = $editContentZone.find('.dropzone');
996-
const $files = $editContentZone.find('.comment-files');
997-
if ($dropzone.length > 0) {
1005+
if ($dropzone.length === 1) {
9981006
$dropzone.data('saved', false);
9991007

10001008
const filenameDict = {};
@@ -1020,7 +1028,7 @@ async function initRepository() {
10201028
submitted: false
10211029
};
10221030
const input = $(`<input id="${data.uuid}" name="files" type="hidden">`).val(data.uuid);
1023-
$files.append(input);
1031+
$dropzone.find('.files').append(input);
10241032
});
10251033
this.on('removedfile', (file) => {
10261034
if (!(file.name in filenameDict)) {
@@ -1042,7 +1050,7 @@ async function initRepository() {
10421050
this.on('reload', () => {
10431051
$.getJSON($editContentZone.data('attachment-url'), (data) => {
10441052
dz.removeAllFiles(true);
1045-
$files.empty();
1053+
$dropzone.find('.files').empty();
10461054
$.each(data, function () {
10471055
const imgSrc = `${$dropzone.data('link-url')}/${this.uuid}`;
10481056
dz.emit('addedfile', this);
@@ -1055,7 +1063,7 @@ async function initRepository() {
10551063
};
10561064
$dropzone.find(`img[src='${imgSrc}']`).css('max-width', '100%');
10571065
const input = $(`<input id="${this.uuid}" name="files" type="hidden">`).val(this.uuid);
1058-
$files.append(input);
1066+
$dropzone.find('.files').append(input);
10591067
});
10601068
});
10611069
});
@@ -1075,7 +1083,9 @@ async function initRepository() {
10751083
$simplemde = setCommentSimpleMDE($textarea);
10761084
commentMDEditors[$editContentZone.data('write')] = $simplemde;
10771085
initCommentPreviewTab($editContentForm);
1078-
initSimpleMDEImagePaste($simplemde, $files);
1086+
if ($dropzone.length === 1) {
1087+
initSimpleMDEImagePaste($simplemde, $dropzone[0], $dropzone.find('.files'));
1088+
}
10791089

10801090
$editContentZone.find('.cancel.button').on('click', () => {
10811091
$renderContent.show();
@@ -1087,7 +1097,7 @@ async function initRepository() {
10871097
$editContentZone.find('.save.button').on('click', () => {
10881098
$renderContent.show();
10891099
$editContentZone.hide();
1090-
const $attachments = $files.find('[name=files]').map(function () {
1100+
const $attachments = $dropzone.find('.files').find('[name=files]').map(function () {
10911101
return $(this).val();
10921102
}).get();
10931103
$.post($editContentZone.data('update-url'), {
@@ -1827,7 +1837,8 @@ function initReleaseEditor() {
18271837
const $files = $editor.parent().find('.files');
18281838
const $simplemde = setCommentSimpleMDE($textarea);
18291839
initCommentPreviewTab($editor);
1830-
initSimpleMDEImagePaste($simplemde, $files);
1840+
const dropzone = $editor.parent().find('.dropzone')[0];
1841+
initSimpleMDEImagePaste($simplemde, dropzone, $files);
18311842
}
18321843

18331844
function initOrganization() {
@@ -2610,11 +2621,10 @@ $(document).ready(async () => {
26102621
initLinkAccountView();
26112622

26122623
// Dropzone
2613-
const $dropzone = $('#dropzone');
2614-
if ($dropzone.length > 0) {
2624+
for (const el of document.querySelectorAll('.dropzone')) {
26152625
const filenameDict = {};
2616-
2617-
await createDropzone('#dropzone', {
2626+
const $dropzone = $(el);
2627+
await createDropzone(el, {
26182628
url: $dropzone.data('upload-url'),
26192629
headers: {'X-Csrf-Token': csrf},
26202630
maxFiles: $dropzone.data('max-file'),
@@ -2633,7 +2643,7 @@ $(document).ready(async () => {
26332643
this.on('success', (file, data) => {
26342644
filenameDict[file.name] = data.uuid;
26352645
const input = $(`<input id="${data.uuid}" name="files" type="hidden">`).val(data.uuid);
2636-
$('.files').append(input);
2646+
$dropzone.find('.files').append(input);
26372647
});
26382648
this.on('removedfile', (file) => {
26392649
if (file.name in filenameDict) {

0 commit comments

Comments
 (0)