Skip to content

Commit c6c05b2

Browse files
committed
fix merge, fix the bug for the removal of saved comment's attachments
1 parent 63f9cc2 commit c6c05b2

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

web_src/js/features/comp/ImagePaste.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import $ from 'jquery';
22

33
const {csrfToken} = window.config;
44

5-
async function uploadFile(file, uploadUrl) {
5+
async function uploadFile(file, uploadUrl, dropzone) {
66
const formData = new FormData();
77
formData.append('file', file, file.name);
88

@@ -11,7 +11,27 @@ async function uploadFile(file, uploadUrl) {
1111
headers: {'X-Csrf-Token': csrfToken},
1212
body: formData,
1313
});
14-
return await res.json();
14+
const data = await res.json();
15+
const upfile = {name: file.name, size: file.size, uuid: data.uuid};
16+
dropzone.emit('addedfile', upfile);
17+
dropzone.emit('thumbnail', upfile, `/attachments/${data.uuid}`);
18+
dropzone.emit('complete', upfile);
19+
dropzone.files.push(upfile);
20+
return data;
21+
}
22+
23+
/**
24+
* @param editor{EasyMDE}
25+
* @param fileUuid
26+
*/
27+
export function removeUploadedFileFromEditor(editor, fileUuid) {
28+
// the raw regexp is: /!\[[^\]]*]\(\/attachments\/{uuid}\)/
29+
const re = new RegExp(`!\\[[^\\]]*]\\(/attachments/${fileUuid}\\)`);
30+
editor.value(editor.value().replace(re, '')); // at the moment, we assume the editor is an EasyMDE
31+
if (editor.element) {
32+
// when using "simple textarea" mode, the value of the textarea should be replaced too.
33+
editor.element.value = editor.element.value.replace(re, '');
34+
}
1535
}
1636

1737
function clipboardPastedImages(e) {
@@ -89,6 +109,8 @@ class CodeMirrorEditor {
89109

90110

91111
export function initEasyMDEImagePaste(easyMDE, $dropzone) {
112+
if ($dropzone.length !== 1) throw new Error('invalid dropzone attach');
113+
92114
const uploadUrl = $dropzone.attr('data-upload-url');
93115
const $files = $dropzone.find('.files');
94116

@@ -107,7 +129,7 @@ export function initEasyMDEImagePaste(easyMDE, $dropzone) {
107129

108130
const placeholder = `![${name}](uploading ...)`;
109131
editor.insertPlaceholder(placeholder);
110-
const data = await uploadFile(img, uploadUrl);
132+
const data = await uploadFile(img, uploadUrl, $dropzone[0].dropzone);
111133
editor.replacePlaceholder(placeholder, `![${name}](/attachments/${data.uuid})`);
112134

113135
const $input = $(`<input name="files" type="hidden">`).attr('id', data.uuid).val(data.uuid);

web_src/js/features/repo-legacy.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import $ from 'jquery';
22
import {createCommentEasyMDE, getAttachedEasyMDE} from './comp/EasyMDE.js';
33
import {initCompMarkupContentPreviewTab} from './comp/MarkupContentPreview.js';
4-
import {initCompImagePaste, initEasyMDEImagePaste, removeUploadedFileFromEditor} from './comp/ImagePaste.js';
4+
import {initEasyMDEImagePaste, removeUploadedFileFromEditor} from './comp/ImagePaste.js';
55
import {
66
initRepoIssueBranchSelect, initRepoIssueCodeCommentCancel,
77
initRepoIssueCommentDelete,
@@ -33,7 +33,7 @@ import initRepoPullRequestMergeForm from './repo-issue-pr-form.js';
3333
const {csrfToken} = window.config;
3434

3535
export function initRepoCommentForm() {
36-
const $commentForm = $('.comment.form');
36+
const $commentForm = $('#comment-form');
3737
if ($commentForm.length === 0) {
3838
return;
3939
}
@@ -284,7 +284,7 @@ async function onEditContent(event) {
284284
if ($dropzone.length === 1) {
285285
$dropzone.data('saved', false);
286286

287-
const fileUuidDict = {}; // suspicious logic, need to be confirmed in the future, fix or comment.
287+
const fileUuidDict = {}; // if a comment has been saved, then the uploaded files won't be deleted when clicking the Remove in the dropzone
288288
dz = await createDropzone($dropzone[0], {
289289
url: $dropzone.data('upload-url'),
290290
headers: {'X-Csrf-Token': csrfToken},
@@ -316,6 +316,9 @@ async function onEditContent(event) {
316316
}).then(() => {
317317
removeUploadedFileFromEditor(easyMDE, file.uuid);
318318
});
319+
} else {
320+
// for saved comment's attachment's removal, only remove the link in the editor
321+
removeUploadedFileFromEditor(easyMDE, file.uuid);
319322
}
320323
});
321324
this.on('submit', () => {

0 commit comments

Comments
 (0)