@@ -2,7 +2,7 @@ import $ from 'jquery';
2
2
3
3
const { csrfToken} = window . config ;
4
4
5
- async function uploadFile ( file , uploadUrl ) {
5
+ async function uploadFile ( file , uploadUrl , dropzone ) {
6
6
const formData = new FormData ( ) ;
7
7
formData . append ( 'file' , file , file . name ) ;
8
8
@@ -11,7 +11,27 @@ async function uploadFile(file, uploadUrl) {
11
11
headers : { 'X-Csrf-Token' : csrfToken } ,
12
12
body : formData ,
13
13
} ) ;
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
+ }
15
35
}
16
36
17
37
function clipboardPastedImages ( e ) {
@@ -89,6 +109,8 @@ class CodeMirrorEditor {
89
109
90
110
91
111
export function initEasyMDEImagePaste ( easyMDE , $dropzone ) {
112
+ if ( $dropzone . length !== 1 ) throw new Error ( 'invalid dropzone attach' ) ;
113
+
92
114
const uploadUrl = $dropzone . attr ( 'data-upload-url' ) ;
93
115
const $files = $dropzone . find ( '.files' ) ;
94
116
@@ -107,7 +129,7 @@ export function initEasyMDEImagePaste(easyMDE, $dropzone) {
107
129
108
130
const placeholder = `` ;
109
131
editor . insertPlaceholder ( placeholder ) ;
110
- const data = await uploadFile ( img , uploadUrl ) ;
132
+ const data = await uploadFile ( img , uploadUrl , $dropzone [ 0 ] . dropzone ) ;
111
133
editor . replacePlaceholder ( placeholder , `` ) ;
112
134
113
135
const $input = $ ( `<input name="files" type="hidden">` ) . attr ( 'id' , data . uuid ) . val ( data . uuid ) ;
0 commit comments