@@ -333,20 +333,19 @@ async function onEditContent(event) {
333333 let dz ;
334334 const $dropzone = $editContentZone . find ( '.dropzone' ) ;
335335 if ( $dropzone . length === 1 ) {
336- $dropzone . data ( 'saved' , false ) ;
337-
338- const fileUuidDict = { } ;
339- dz = await createDropzone ( $dropzone [ 0 ] , {
340- url : $dropzone . data ( 'upload-url' ) ,
336+ let disableRemovedfileEvent = false ; // when resetting the dropzone (removeAllFiles), disable the "removedfile" event
337+ 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
338+ const dz = await createDropzone ( $dropzone [ 0 ] , {
339+ url : $dropzone . attr ( 'data-upload-url' ) ,
341340 headers : { 'X-Csrf-Token' : csrfToken } ,
342- maxFiles : $dropzone . data ( ' max-file') ,
343- maxFilesize : $dropzone . data ( ' max-size') ,
344- acceptedFiles : ( [ '*/*' , '' ] . includes ( $dropzone . data ( ' accepts') ) ) ? null : $dropzone . data ( ' accepts') ,
341+ maxFiles : $dropzone . attr ( 'data- max-file') ,
342+ maxFilesize : $dropzone . attr ( 'data- max-size') ,
343+ acceptedFiles : ( [ '*/*' , '' ] . includes ( $dropzone . attr ( 'data- accepts') ) ) ? null : $dropzone . attr ( 'data- accepts') ,
345344 addRemoveLinks : true ,
346- dictDefaultMessage : $dropzone . data ( ' default-message') ,
347- dictInvalidFileType : $dropzone . data ( ' invalid-input-type') ,
348- dictFileTooBig : $dropzone . data ( ' file-too-big') ,
349- dictRemoveFile : $dropzone . data ( ' remove-file') ,
345+ dictDefaultMessage : $dropzone . attr ( 'data- default-message') ,
346+ dictInvalidFileType : $dropzone . attr ( 'data- invalid-input-type') ,
347+ dictFileTooBig : $dropzone . attr ( 'data- file-too-big') ,
348+ dictRemoveFile : $dropzone . attr ( 'data- remove-file') ,
350349 timeout : 0 ,
351350 thumbnailMethod : 'contain' ,
352351 thumbnailWidth : 480 ,
@@ -359,9 +358,10 @@ async function onEditContent(event) {
359358 $dropzone . find ( '.files' ) . append ( input ) ;
360359 } ) ;
361360 this . on ( 'removedfile' , ( file ) => {
361+ if ( disableRemovedfileEvent ) return ;
362362 $ ( `#${ file . uuid } ` ) . remove ( ) ;
363- if ( $dropzone . data ( ' remove-url') && ! fileUuidDict [ file . uuid ] . submitted ) {
364- $ . post ( $dropzone . data ( ' remove-url') , {
363+ if ( $dropzone . attr ( 'data- remove-url') && ! fileUuidDict [ file . uuid ] . submitted ) {
364+ $ . post ( $dropzone . attr ( 'data- remove-url') , {
365365 file : file . uuid ,
366366 _csrf : csrfToken ,
367367 } ) ;
@@ -373,20 +373,25 @@ async function onEditContent(event) {
373373 } ) ;
374374 } ) ;
375375 this . on ( 'reload' , ( ) => {
376- $ . getJSON ( $editContentZone . data ( 'attachment-url' ) , ( data ) => {
376+ $ . getJSON ( $editContentZone . attr ( 'data-attachment-url' ) , ( data ) => {
377+ // do not trigger the "removedfile" event, otherwise the attachments would be deleted from server
378+ disableRemovedfileEvent = true ;
377379 dz . removeAllFiles ( true ) ;
378380 $dropzone . find ( '.files' ) . empty ( ) ;
379- $ . each ( data , function ( ) {
380- const imgSrc = `${ $dropzone . data ( 'link-url' ) } /${ this . uuid } ` ;
381- dz . emit ( 'addedfile' , this ) ;
382- dz . emit ( 'thumbnail' , this , imgSrc ) ;
383- dz . emit ( 'complete' , this ) ;
384- dz . files . push ( this ) ;
385- fileUuidDict [ this . uuid ] = { submitted : true } ;
381+ fileUuidDict = { } ;
382+ disableRemovedfileEvent = false ;
383+
384+ for ( const attachment of data ) {
385+ const imgSrc = `${ $dropzone . attr ( 'data-link-url' ) } /${ attachment . uuid } ` ;
386+ dz . emit ( 'addedfile' , attachment ) ;
387+ dz . emit ( 'thumbnail' , attachment , imgSrc ) ;
388+ dz . emit ( 'complete' , attachment ) ;
389+ dz . files . push ( attachment ) ;
390+ fileUuidDict [ attachment . uuid ] = { submitted : true } ;
386391 $dropzone . find ( `img[src='${ imgSrc } ']` ) . css ( 'max-width' , '100%' ) ;
387- const input = $ ( `<input id="${ this . uuid } " name="files" type="hidden">` ) . val ( this . uuid ) ;
392+ const input = $ ( `<input id="${ attachment . uuid } " name="files" type="hidden">` ) . val ( attachment . uuid ) ;
388393 $dropzone . find ( '.files' ) . append ( input ) ;
389- } ) ;
394+ }
390395 } ) ;
391396 } ) ;
392397 } ,
0 commit comments