Skip to content

Commit 900f20f

Browse files
committed
handle all dropped images
1 parent c2e4bae commit 900f20f

File tree

1 file changed

+33
-30
lines changed

1 file changed

+33
-30
lines changed

browser/main/lib/dataApi/attachmentManagement.js

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -261,37 +261,36 @@ function generateAttachmentMarkdown (fileName, path, showPreview) {
261261
* @param {Event} dropEvent DropEvent
262262
*/
263263
function handleAttachmentDrop (codeEditor, storageKey, noteKey, dropEvent) {
264+
let promise
264265
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+
}))
283284
} 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 => {
287286
if (item.type === 'text/html') {
288287
const html = dropEvent.dataTransfer.getData('text/html')
289288

290289
const match = /<img[^>]*[\s"']src="([^"]+)"/.exec(html)
291290
if (match) {
292291
const imageURL = match[1]
293292

294-
getImage(imageURL)
293+
return getImage(imageURL)
295294
.then(image => {
296295
const canvas = document.createElement('canvas')
297296
const context = canvas.getContext('2d')
@@ -301,17 +300,21 @@ function handleAttachmentDrop (codeEditor, storageKey, noteKey, dropEvent) {
301300

302301
return copyAttachment({type: 'base64', data: canvas.toDataURL(), sourceFilePath: imageURL}, storageKey, noteKey)
303302
})
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+
}))
311308
}
312309
}
313-
}
310+
}))
314311
}
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+
})
315318
}
316319

317320
/**

0 commit comments

Comments
 (0)