Skip to content

Commit 40f1ae5

Browse files
committed
fix
1 parent 24f4ebb commit 40f1ae5

File tree

7 files changed

+158
-183
lines changed

7 files changed

+158
-183
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: 105 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,130 @@
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, createElementFromObject} 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+
if (!dropzoneEl) return null;
45+
46+
const listAttachmentsUrl = dropzoneEl.closest('[data-attachment-url]')?.getAttribute('data-attachment-url');
47+
const removeAttachmentUrl = dropzoneEl.getAttribute('data-remove-url');
48+
const attachmentBaseLinkUrl = dropzoneEl.getAttribute('data-link-url');
49+
50+
let disableRemovedfileEvent = false; // when resetting the dropzone (removeAllFiles), disable the "removedfile" event
51+
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
52+
const dzInst = await createDropzone(dropzoneEl, {
53+
url: dropzoneEl.getAttribute('data-upload-url'),
2954
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'),
55+
maxFiles: dropzoneEl.getAttribute('data-max-file'),
56+
maxFilesize: dropzoneEl.getAttribute('data-max-size'),
57+
acceptedFiles: ['*/*', ''].includes(dropzoneEl.getAttribute('data-accepts')) ? null : dropzoneEl.getAttribute('data-accepts'),
3358
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'),
59+
dictDefaultMessage: dropzoneEl.getAttribute('data-default-message'),
60+
dictInvalidFileType: dropzoneEl.getAttribute('data-invalid-input-type'),
61+
dictFileTooBig: dropzoneEl.getAttribute('data-file-too-big'),
62+
dictRemoveFile: dropzoneEl.getAttribute('data-remove-file'),
3863
timeout: 0,
3964
thumbnailMethod: 'contain',
4065
thumbnailWidth: 480,
4166
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-
},
7967
});
68+
69+
dzInst.on('success', (file, data) => {
70+
file.uuid = data.uuid;
71+
fileUuidDict[file.uuid] = {submitted: false};
72+
const input = createElementFromObject('input', {name: 'files', type: 'hidden', id: `dropzone-file-${data.uuid}`, value: data.uuid});
73+
dropzoneEl.querySelector('.files').append(input);
74+
addCopyLink(file);
75+
});
76+
77+
dzInst.on('removedfile', async (file) => {
78+
if (disableRemovedfileEvent) return;
79+
document.querySelector(`#dropzone-file-${file.uuid}`)?.remove();
80+
if (removeAttachmentUrl && !fileUuidDict[file.uuid].submitted) {
81+
await POST(removeAttachmentUrl, {data: new URLSearchParams({file: file.uuid})});
82+
}
83+
});
84+
85+
dzInst.on('submit', () => {
86+
for (const fileUuid of Object.keys(fileUuidDict)) {
87+
fileUuidDict[fileUuid].submitted = true;
88+
}
89+
});
90+
91+
dzInst.on('reload', async () => {
92+
try {
93+
const resp = await GET(listAttachmentsUrl);
94+
const respData = await resp.json();
95+
// do not trigger the "removedfile" event, otherwise the attachments would be deleted from server
96+
disableRemovedfileEvent = true;
97+
dzInst.removeAllFiles(true);
98+
disableRemovedfileEvent = false;
99+
100+
dropzoneEl.querySelector('.files').innerHTML = '';
101+
for (const el of dropzoneEl.querySelectorAll('.dz-preview')) el.remove();
102+
fileUuidDict = {};
103+
for (const attachment of respData) {
104+
const imgSrc = `${attachmentBaseLinkUrl}/${attachment.uuid}`;
105+
dzInst.emit('addedfile', attachment);
106+
dzInst.emit('thumbnail', attachment, imgSrc);
107+
dzInst.emit('complete', attachment);
108+
addCopyLink(attachment);
109+
fileUuidDict[attachment.uuid] = {submitted: true};
110+
const input = createElementFromObject('input', {name: 'files', type: 'hidden', id: `dropzone-file-${attachment.uuid}`, value: attachment.uuid});
111+
dropzoneEl.querySelector('.files').append(input);
112+
}
113+
if (!dropzoneEl.querySelector('.dz-preview')) {
114+
dropzoneEl.classList.remove('dz-started');
115+
}
116+
} catch (error) {
117+
// TODO: if listing the existing attachments failed, it should stop from operating the content or attachments,
118+
// otherwise the attachments might be lost.
119+
console.error(error);
120+
}
121+
});
122+
123+
dzInst.on('error', function (file, message) {
124+
showErrorToast(message);
125+
this.removeFile(file);
126+
});
127+
128+
if (listAttachmentsUrl) dzInst.emit('reload');
129+
return dzInst;
80130
}

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)