@@ -865,6 +865,73 @@ function initRepository() {
865865 issuesTribute . attach ( $textarea . get ( ) ) ;
866866 emojiTribute . attach ( $textarea . get ( ) ) ;
867867
868+ const $dropzone = $editContentZone . find ( '.dropzone' ) ;
869+ $dropzone . data ( "saved" , false ) ;
870+ const $files = $editContentZone . find ( '.comment-files' ) ;
871+ if ( $dropzone . length > 0 ) {
872+ const filenameDict = { } ;
873+ $dropzone . dropzone ( {
874+ url : $dropzone . data ( 'upload-url' ) ,
875+ headers : { "X-Csrf-Token" : csrf } ,
876+ maxFiles : $dropzone . data ( 'max-file' ) ,
877+ maxFilesize : $dropzone . data ( 'max-size' ) ,
878+ acceptedFiles : ( $dropzone . data ( 'accepts' ) === '*/*' ) ? null : $dropzone . data ( 'accepts' ) ,
879+ addRemoveLinks : true ,
880+ dictDefaultMessage : $dropzone . data ( 'default-message' ) ,
881+ dictInvalidFileType : $dropzone . data ( 'invalid-input-type' ) ,
882+ dictFileTooBig : $dropzone . data ( 'file-too-big' ) ,
883+ dictRemoveFile : $dropzone . data ( 'remove-file' ) ,
884+ init : function ( ) {
885+ this . on ( "success" , function ( file , data ) {
886+ filenameDict [ file . name ] = {
887+ "uuid" : data . uuid ,
888+ "submitted" : false
889+ }
890+ const input = $ ( '<input id="' + data . uuid + '" name="files" type="hidden">' ) . val ( data . uuid ) ;
891+ $files . append ( input ) ;
892+ } ) ;
893+ this . on ( "removedfile" , function ( file ) {
894+ if ( ! ( file . name in filenameDict ) ) {
895+ return ;
896+ }
897+ $ ( '#' + filenameDict [ file . name ] . uuid ) . remove ( ) ;
898+ if ( $dropzone . data ( 'remove-url' ) && $dropzone . data ( 'csrf' ) && ! filenameDict [ file . name ] . submitted ) {
899+ $ . post ( $dropzone . data ( 'remove-url' ) , {
900+ file : filenameDict [ file . name ] . uuid ,
901+ _csrf : $dropzone . data ( 'csrf' )
902+ } ) ;
903+ }
904+ } ) ;
905+ this . on ( "submit" , function ( ) {
906+ $ . each ( filenameDict , function ( name ) {
907+ filenameDict [ name ] . submitted = true ;
908+ } ) ;
909+ } ) ;
910+ this . on ( "reload" , function ( ) {
911+ $ . getJSON ( $editContentZone . data ( 'attachment-url' ) , function ( data ) {
912+ const drop = $dropzone . get ( 0 ) . dropzone ;
913+ drop . removeAllFiles ( true ) ;
914+ $files . empty ( ) ;
915+ $ . each ( data , function ( ) {
916+ const imgSrc = $dropzone . data ( 'upload-url' ) + "/" + this . uuid ;
917+ drop . emit ( "addedfile" , this ) ;
918+ drop . emit ( "thumbnail" , this , imgSrc ) ;
919+ drop . emit ( "complete" , this ) ;
920+ drop . files . push ( this ) ;
921+ filenameDict [ this . name ] = {
922+ "submitted" : true ,
923+ "uuid" : this . uuid
924+ }
925+ $dropzone . find ( "img[src='" + imgSrc + "']" ) . css ( "max-width" , "100%" ) ;
926+ const input = $ ( '<input id="' + this . uuid + '" name="files" type="hidden">' ) . val ( this . uuid ) ;
927+ $files . append ( input ) ;
928+ } ) ;
929+ } ) ;
930+ } ) ;
931+ }
932+ } ) ;
933+ $dropzone . get ( 0 ) . dropzone . emit ( "reload" ) ;
934+ }
868935 // Give new write/preview data-tab name to distinguish from others
869936 const $editContentForm = $editContentZone . find ( '.ui.comment.form' ) ;
870937 const $tabMenu = $editContentForm . find ( '.tabular.menu' ) ;
@@ -880,27 +947,49 @@ function initRepository() {
880947 $editContentZone . find ( '.cancel.button' ) . click ( function ( ) {
881948 $renderContent . show ( ) ;
882949 $editContentZone . hide ( ) ;
950+ $dropzone . get ( 0 ) . dropzone . emit ( "reload" ) ;
883951 } ) ;
884952 $editContentZone . find ( '.save.button' ) . click ( function ( ) {
885953 $renderContent . show ( ) ;
886954 $editContentZone . hide ( ) ;
887-
955+ const $attachments = $files . find ( "[name=files]" ) . map ( function ( ) {
956+ return $ ( this ) . val ( ) ;
957+ } ) . get ( ) ;
888958 $ . post ( $editContentZone . data ( 'update-url' ) , {
889- "_csrf" : csrf ,
890- "content" : $textarea . val ( ) ,
891- "context" : $editContentZone . data ( 'context' )
892- } ,
893- function ( data ) {
894- if ( data . length == 0 ) {
895- $renderContent . html ( $ ( '#no-content' ) . html ( ) ) ;
896- } else {
897- $renderContent . html ( data . content ) ;
898- emojify . run ( $renderContent [ 0 ] ) ;
899- $ ( 'pre code' , $renderContent [ 0 ] ) . each ( function ( ) {
900- hljs . highlightBlock ( this ) ;
901- } ) ;
959+ "_csrf" : csrf ,
960+ "content" : $textarea . val ( ) ,
961+ "context" : $editContentZone . data ( 'context' ) ,
962+ "files" : $attachments
963+ } ,
964+ function ( data ) {
965+ if ( data . length == 0 ) {
966+ $renderContent . html ( $ ( '#no-content' ) . html ( ) ) ;
967+ } else {
968+ $renderContent . html ( data . content ) ;
969+ emojify . run ( $renderContent [ 0 ] ) ;
970+ $ ( 'pre code' , $renderContent [ 0 ] ) . each ( function ( ) {
971+ hljs . highlightBlock ( this ) ;
972+ } ) ;
973+ }
974+ const $content = $segment . parent ( ) ;
975+ if ( ! $content . find ( ".ui.small.images" ) . length ) {
976+ if ( data . attachments != "" ) {
977+ $content . append (
978+ '<div class="ui bottom attached segment">' +
979+ ' <div class="ui small images">' +
980+ ' </div>' +
981+ '</div>'
982+ ) ;
983+ $content . find ( ".ui.small.images" ) . html ( data . attachments ) ;
902984 }
903- } ) ;
985+ } else if ( data . attachments == "" ) {
986+ $content . find ( ".ui.small.images" ) . parent ( ) . remove ( ) ;
987+ } else {
988+ $content . find ( ".ui.small.images" ) . html ( data . attachments ) ;
989+ }
990+ $dropzone . get ( 0 ) . dropzone . emit ( "submit" ) ;
991+ $dropzone . get ( 0 ) . dropzone . emit ( "reload" ) ;
992+ } ) ;
904993 } ) ;
905994 } else {
906995 $textarea = $segment . find ( 'textarea' ) ;
0 commit comments