1
0
mirror of https://github.com/BoostIo/Boostnote synced 2025-12-17 19:51:42 +00:00

handle all dropped images

This commit is contained in:
Baptiste Augrain
2018-11-28 15:58:58 +01:00
parent c2e4bae9dd
commit 900f20f164

View File

@@ -261,29 +261,28 @@ function generateAttachmentMarkdown (fileName, path, showPreview) {
* @param {Event} dropEvent DropEvent * @param {Event} dropEvent DropEvent
*/ */
function handleAttachmentDrop (codeEditor, storageKey, noteKey, dropEvent) { function handleAttachmentDrop (codeEditor, storageKey, noteKey, dropEvent) {
let promise
if (dropEvent.dataTransfer.files.length > 0) { if (dropEvent.dataTransfer.files.length > 0) {
const file = dropEvent.dataTransfer.files[0] promise = Promise.all(Array.from(dropEvent.dataTransfer.files).map(file => {
const filePath = file.path if (file['type'].startsWith('image')) {
const originalFileName = path.basename(filePath) return fixRotate(file)
const fileType = file['type'] .then(data => copyAttachment({type: 'base64', data: data, sourceFilePath: file.path}, storageKey, noteKey)
const isImage = fileType.startsWith('image') .then(fileName => ({
let promise fileName,
if (isImage) { title: path.basename(file.path),
console.log(file) isImage: true
promise = fixRotate(file).then(base64data => { }))
return copyAttachment({type: 'base64', data: base64data, sourceFilePath: filePath}, storageKey, noteKey) )
}) } else {
} else { return copyAttachment(file.path, storageKey, noteKey).then(fileName => ({
promise = copyAttachment(filePath, storageKey, noteKey) fileName,
} title: path.basename(file.path),
promise.then((fileName) => { isImage: false
const imageMd = generateAttachmentMarkdown(originalFileName, path.join(STORAGE_FOLDER_PLACEHOLDER, noteKey, fileName), isImage) }))
codeEditor.insertAttachmentMd(imageMd) }
}) }))
} else { } else {
for (let i = 0; i < dropEvent.dataTransfer.items.length; i++) { promise = Promise.all(Array.from(dropEvent.dataTransfer.items).map(item => {
const item = dropEvent.dataTransfer.items[i]
if (item.type === 'text/html') { if (item.type === 'text/html') {
const html = dropEvent.dataTransfer.getData('text/html') const html = dropEvent.dataTransfer.getData('text/html')
@@ -291,7 +290,7 @@ function handleAttachmentDrop (codeEditor, storageKey, noteKey, dropEvent) {
if (match) { if (match) {
const imageURL = match[1] const imageURL = match[1]
getImage(imageURL) return getImage(imageURL)
.then(image => { .then(image => {
const canvas = document.createElement('canvas') const canvas = document.createElement('canvas')
const context = canvas.getContext('2d') const context = canvas.getContext('2d')
@@ -301,17 +300,21 @@ function handleAttachmentDrop (codeEditor, storageKey, noteKey, dropEvent) {
return copyAttachment({type: 'base64', data: canvas.toDataURL(), sourceFilePath: imageURL}, storageKey, noteKey) return copyAttachment({type: 'base64', data: canvas.toDataURL(), sourceFilePath: imageURL}, storageKey, noteKey)
}) })
.then(fileName => { .then(fileName => ({
const imageMd = generateAttachmentMarkdown(imageURL, path.join(STORAGE_FOLDER_PLACEHOLDER, noteKey, fileName), true) fileName,
title: imageURL,
codeEditor.insertAttachmentMd(imageMd) isImage: true
}) }))
break
} }
} }
} }))
} }
promise.then(files => {
const attachments = files.filter(file => !!file).map(file => generateAttachmentMarkdown(file.title, path.join(STORAGE_FOLDER_PLACEHOLDER, noteKey, file.fileName), file.isImage))
codeEditor.insertAttachmentMd(attachments.join('\n'))
})
} }
/** /**