@@ -261,37 +261,36 @@ function generateAttachmentMarkdown (fileName, path, showPreview) {
261
261
* @param {Event } dropEvent DropEvent
262
262
*/
263
263
function handleAttachmentDrop ( codeEditor , storageKey , noteKey , dropEvent ) {
264
+ let promise
264
265
if ( dropEvent . dataTransfer . files . length > 0 ) {
265
- const file = dropEvent . dataTransfer . files [ 0 ]
266
- const filePath = file . path
267
- const originalFileName = path . basename ( filePath )
268
- const fileType = file [ ' type' ]
269
- const isImage = fileType . startsWith ( 'image' )
270
- let promise
271
- if ( isImage ) {
272
- console . log ( file )
273
- promise = fixRotate ( file ) . then ( base64data => {
274
- return copyAttachment ( { type : 'base64' , data : base64data , sourceFilePath : filePath } , storageKey , noteKey )
275
- } )
276
- } else {
277
- promise = copyAttachment ( filePath , storageKey , noteKey )
278
- }
279
- promise . then ( ( fileName ) => {
280
- const imageMd = generateAttachmentMarkdown ( originalFileName , path . join ( STORAGE_FOLDER_PLACEHOLDER , noteKey , fileName ) , isImage )
281
- codeEditor . insertAttachmentMd ( imageMd )
282
- } )
266
+ promise = Promise . all ( Array . from ( dropEvent . dataTransfer . files ) . map ( file => {
267
+ if ( file [ 'type' ] . startsWith ( 'image' ) ) {
268
+ return fixRotate ( file )
269
+ . then ( data => copyAttachment ( { type : 'base64' , data : data , sourceFilePath : file . path } , storageKey , noteKey )
270
+ . then ( fileName => ( {
271
+ fileName ,
272
+ title : path . basename ( file . path ) ,
273
+ isImage : true
274
+ } ) )
275
+ )
276
+ } else {
277
+ return copyAttachment ( file . path , storageKey , noteKey ) . then ( fileName => ( {
278
+ fileName ,
279
+ title : path . basename ( file . path ) ,
280
+ isImage : false
281
+ } ) )
282
+ }
283
+ } ) )
283
284
} else {
284
- for ( let i = 0 ; i < dropEvent . dataTransfer . items . length ; i ++ ) {
285
- const item = dropEvent . dataTransfer . items [ i ]
286
-
285
+ promise = Promise . all ( Array . from ( dropEvent . dataTransfer . items ) . map ( item => {
287
286
if ( item . type === 'text/html' ) {
288
287
const html = dropEvent . dataTransfer . getData ( 'text/html' )
289
288
290
289
const match = / < i m g [ ^ > ] * [ \s " ' ] s r c = " ( [ ^ " ] + ) " / . exec ( html )
291
290
if ( match ) {
292
291
const imageURL = match [ 1 ]
293
292
294
- getImage ( imageURL )
293
+ return getImage ( imageURL )
295
294
. then ( image => {
296
295
const canvas = document . createElement ( 'canvas' )
297
296
const context = canvas . getContext ( '2d' )
@@ -301,17 +300,21 @@ function handleAttachmentDrop (codeEditor, storageKey, noteKey, dropEvent) {
301
300
302
301
return copyAttachment ( { type : 'base64' , data : canvas . toDataURL ( ) , sourceFilePath : imageURL } , storageKey , noteKey )
303
302
} )
304
- . then ( fileName => {
305
- const imageMd = generateAttachmentMarkdown ( imageURL , path . join ( STORAGE_FOLDER_PLACEHOLDER , noteKey , fileName ) , true )
306
-
307
- codeEditor . insertAttachmentMd ( imageMd )
308
- } )
309
-
310
- break
303
+ . then ( fileName => ( {
304
+ fileName,
305
+ title : imageURL ,
306
+ isImage : true
307
+ } ) )
311
308
}
312
309
}
313
- }
310
+ } ) )
314
311
}
312
+
313
+ promise . then ( files => {
314
+ const attachments = files . filter ( file => ! ! file ) . map ( file => generateAttachmentMarkdown ( file . title , path . join ( STORAGE_FOLDER_PLACEHOLDER , noteKey , file . fileName ) , file . isImage ) )
315
+
316
+ codeEditor . insertAttachmentMd ( attachments . join ( '\n' ) )
317
+ } )
315
318
}
316
319
317
320
/**
0 commit comments