@@ -327,11 +327,11 @@ function getPastedImages(e) {
327
327
return files ;
328
328
}
329
329
330
- async function uploadFile ( file ) {
330
+ async function uploadFile ( file , uploadUrl ) {
331
331
const formData = new FormData ( ) ;
332
332
formData . append ( 'file' , file , file . name ) ;
333
333
334
- const res = await fetch ( $ ( '#dropzone' ) . data ( 'upload-url' ) , {
334
+ const res = await fetch ( uploadUrl , {
335
335
method : 'POST' ,
336
336
headers : { 'X-Csrf-Token' : csrf } ,
337
337
body : formData ,
@@ -345,24 +345,33 @@ function reload() {
345
345
346
346
function initImagePaste ( target ) {
347
347
target . each ( function ( ) {
348
- this . addEventListener ( 'paste' , async ( e ) => {
349
- for ( const img of getPastedImages ( e ) ) {
350
- const name = img . name . substr ( 0 , img . name . lastIndexOf ( '.' ) ) ;
351
- insertAtCursor ( this , `![${ name } ]()` ) ;
352
- const data = await uploadFile ( img ) ;
353
- replaceAndKeepCursor ( this , `![${ name } ]()` , `` ) ;
354
- const input = $ ( `<input id="${ data . uuid } " name="files" type="hidden">` ) . val ( data . uuid ) ;
355
- $ ( '.files' ) . append ( input ) ;
356
- }
357
- } , false ) ;
348
+ const dropzone = this . querySelector ( '.dropzone' ) ;
349
+ if ( ! dropzone ) {
350
+ return ;
351
+ }
352
+ const uploadUrl = dropzone . dataset . uploadUrl ;
353
+ const dropzoneFiles = dropzone . querySelector ( '.files' ) ;
354
+ for ( const textarea of this . querySelectorAll ( 'textarea' ) ) {
355
+ textarea . addEventListener ( 'paste' , async ( e ) => {
356
+ for ( const img of getPastedImages ( e ) ) {
357
+ const name = img . name . substr ( 0 , img . name . lastIndexOf ( '.' ) ) ;
358
+ insertAtCursor ( textarea , `![${ name } ]()` ) ;
359
+ const data = await uploadFile ( img , uploadUrl ) ;
360
+ replaceAndKeepCursor ( textarea , `![${ name } ]()` , `` ) ;
361
+ const input = $ ( `<input id="${ data . uuid } " name="files" type="hidden">` ) . val ( data . uuid ) ;
362
+ dropzoneFiles . appendChild ( input [ 0 ] ) ;
363
+ }
364
+ } , false ) ;
365
+ }
358
366
} ) ;
359
367
}
360
368
361
- function initSimpleMDEImagePaste ( simplemde , files ) {
369
+ function initSimpleMDEImagePaste ( simplemde , dropzone , files ) {
370
+ const uploadUrl = dropzone . dataset . uploadUrl ;
362
371
simplemde . codemirror . on ( 'paste' , async ( _ , e ) => {
363
372
for ( const img of getPastedImages ( e ) ) {
364
373
const name = img . name . substr ( 0 , img . name . lastIndexOf ( '.' ) ) ;
365
- const data = await uploadFile ( img ) ;
374
+ const data = await uploadFile ( img , uploadUrl ) ;
366
375
const pos = simplemde . codemirror . getCursor ( ) ;
367
376
simplemde . codemirror . replaceRange ( `` , pos ) ;
368
377
const input = $ ( `<input id="${ data . uuid } " name="files" type="hidden">` ) . val ( data . uuid ) ;
@@ -381,7 +390,7 @@ function initCommentForm() {
381
390
autoSimpleMDE = setCommentSimpleMDE ( $ ( '.comment.form textarea:not(.review-textarea)' ) ) ;
382
391
initBranchSelector ( ) ;
383
392
initCommentPreviewTab ( $ ( '.comment.form' ) ) ;
384
- initImagePaste ( $ ( '.comment.form textarea ' ) ) ;
393
+ initImagePaste ( $ ( '.comment.form' ) ) ;
385
394
386
395
// Listsubmit
387
396
function initListSubmits ( selector , outerSelector ) {
@@ -993,8 +1002,7 @@ async function initRepository() {
993
1002
994
1003
let dz ;
995
1004
const $dropzone = $editContentZone . find ( '.dropzone' ) ;
996
- const $files = $editContentZone . find ( '.comment-files' ) ;
997
- if ( $dropzone . length > 0 ) {
1005
+ if ( $dropzone . length === 1 ) {
998
1006
$dropzone . data ( 'saved' , false ) ;
999
1007
1000
1008
const filenameDict = { } ;
@@ -1020,7 +1028,7 @@ async function initRepository() {
1020
1028
submitted : false
1021
1029
} ;
1022
1030
const input = $ ( `<input id="${ data . uuid } " name="files" type="hidden">` ) . val ( data . uuid ) ;
1023
- $files . append ( input ) ;
1031
+ $dropzone . find ( '. files' ) . append ( input ) ;
1024
1032
} ) ;
1025
1033
this . on ( 'removedfile' , ( file ) => {
1026
1034
if ( ! ( file . name in filenameDict ) ) {
@@ -1042,7 +1050,7 @@ async function initRepository() {
1042
1050
this . on ( 'reload' , ( ) => {
1043
1051
$ . getJSON ( $editContentZone . data ( 'attachment-url' ) , ( data ) => {
1044
1052
dz . removeAllFiles ( true ) ;
1045
- $files . empty ( ) ;
1053
+ $dropzone . find ( '. files' ) . empty ( ) ;
1046
1054
$ . each ( data , function ( ) {
1047
1055
const imgSrc = `${ $dropzone . data ( 'link-url' ) } /${ this . uuid } ` ;
1048
1056
dz . emit ( 'addedfile' , this ) ;
@@ -1055,7 +1063,7 @@ async function initRepository() {
1055
1063
} ;
1056
1064
$dropzone . find ( `img[src='${ imgSrc } ']` ) . css ( 'max-width' , '100%' ) ;
1057
1065
const input = $ ( `<input id="${ this . uuid } " name="files" type="hidden">` ) . val ( this . uuid ) ;
1058
- $files . append ( input ) ;
1066
+ $dropzone . find ( '. files' ) . append ( input ) ;
1059
1067
} ) ;
1060
1068
} ) ;
1061
1069
} ) ;
@@ -1075,7 +1083,9 @@ async function initRepository() {
1075
1083
$simplemde = setCommentSimpleMDE ( $textarea ) ;
1076
1084
commentMDEditors [ $editContentZone . data ( 'write' ) ] = $simplemde ;
1077
1085
initCommentPreviewTab ( $editContentForm ) ;
1078
- initSimpleMDEImagePaste ( $simplemde , $files ) ;
1086
+ if ( $dropzone . length === 1 ) {
1087
+ initSimpleMDEImagePaste ( $simplemde , $dropzone [ 0 ] , $dropzone . find ( '.files' ) ) ;
1088
+ }
1079
1089
1080
1090
$editContentZone . find ( '.cancel.button' ) . on ( 'click' , ( ) => {
1081
1091
$renderContent . show ( ) ;
@@ -1087,7 +1097,7 @@ async function initRepository() {
1087
1097
$editContentZone . find ( '.save.button' ) . on ( 'click' , ( ) => {
1088
1098
$renderContent . show ( ) ;
1089
1099
$editContentZone . hide ( ) ;
1090
- const $attachments = $files . find ( '[name=files]' ) . map ( function ( ) {
1100
+ const $attachments = $dropzone . find ( '. files' ) . find ( '[name=files]' ) . map ( function ( ) {
1091
1101
return $ ( this ) . val ( ) ;
1092
1102
} ) . get ( ) ;
1093
1103
$ . post ( $editContentZone . data ( 'update-url' ) , {
@@ -1369,6 +1379,13 @@ function initPullRequestReview() {
1369
1379
$simplemde . codemirror . focus ( ) ;
1370
1380
assingMenuAttributes ( form . find ( '.menu' ) ) ;
1371
1381
} ) ;
1382
+
1383
+ const $reviewBox = $ ( '.review-box' ) ;
1384
+ if ( $reviewBox . length === 1 ) {
1385
+ setCommentSimpleMDE ( $reviewBox . find ( 'textarea' ) ) ;
1386
+ initImagePaste ( $reviewBox ) ;
1387
+ }
1388
+
1372
1389
// The following part is only for diff views
1373
1390
if ( $ ( '.repository.pull.diff' ) . length === 0 ) {
1374
1391
return ;
@@ -1656,6 +1673,10 @@ $.fn.getCursorPosition = function () {
1656
1673
} ;
1657
1674
1658
1675
function setCommentSimpleMDE ( $editArea ) {
1676
+ if ( $editArea . length === 0 ) {
1677
+ return null ;
1678
+ }
1679
+
1659
1680
const simplemde = new SimpleMDE ( {
1660
1681
autoDownloadFontAwesome : false ,
1661
1682
element : $editArea [ 0 ] ,
@@ -1827,7 +1848,8 @@ function initReleaseEditor() {
1827
1848
const $files = $editor . parent ( ) . find ( '.files' ) ;
1828
1849
const $simplemde = setCommentSimpleMDE ( $textarea ) ;
1829
1850
initCommentPreviewTab ( $editor ) ;
1830
- initSimpleMDEImagePaste ( $simplemde , $files ) ;
1851
+ const dropzone = $editor . parent ( ) . find ( '.dropzone' ) [ 0 ] ;
1852
+ initSimpleMDEImagePaste ( $simplemde , dropzone , $files ) ;
1831
1853
}
1832
1854
1833
1855
function initOrganization ( ) {
@@ -2610,11 +2632,10 @@ $(document).ready(async () => {
2610
2632
initLinkAccountView ( ) ;
2611
2633
2612
2634
// Dropzone
2613
- const $dropzone = $ ( '#dropzone' ) ;
2614
- if ( $dropzone . length > 0 ) {
2635
+ for ( const el of document . querySelectorAll ( '.dropzone' ) ) {
2615
2636
const filenameDict = { } ;
2616
-
2617
- await createDropzone ( '#dropzone' , {
2637
+ const $dropzone = $ ( el ) ;
2638
+ await createDropzone ( el , {
2618
2639
url : $dropzone . data ( 'upload-url' ) ,
2619
2640
headers : { 'X-Csrf-Token' : csrf } ,
2620
2641
maxFiles : $dropzone . data ( 'max-file' ) ,
@@ -2633,7 +2654,7 @@ $(document).ready(async () => {
2633
2654
this . on ( 'success' , ( file , data ) => {
2634
2655
filenameDict [ file . name ] = data . uuid ;
2635
2656
const input = $ ( `<input id="${ data . uuid } " name="files" type="hidden">` ) . val ( data . uuid ) ;
2636
- $ ( '.files' ) . append ( input ) ;
2657
+ $dropzone . find ( '.files' ) . append ( input ) ;
2637
2658
} ) ;
2638
2659
this . on ( 'removedfile' , ( file ) => {
2639
2660
if ( file . name in filenameDict ) {
0 commit comments