Skip to content

Commit 2908884

Browse files
committed
drag and drop image from browser
1 parent a6eddb5 commit 2908884

File tree

1 file changed

+65
-23
lines changed

1 file changed

+65
-23
lines changed

browser/main/lib/dataApi/attachmentManagement.js

Lines changed: 65 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,23 @@ const PATH_SEPARATORS = escapeStringRegexp(path.posix.sep) + escapeStringRegexp(
1818
* @returns {Promise<Image>} Image element created
1919
*/
2020
function getImage (file) {
21-
return new Promise((resolve) => {
22-
const reader = new FileReader()
23-
const img = new Image()
24-
img.onload = () => resolve(img)
25-
reader.onload = e => {
26-
img.src = e.target.result
27-
}
28-
reader.readAsDataURL(file)
29-
})
21+
if (_.isString(file)) {
22+
return new Promise(resolve => {
23+
const img = new Image()
24+
img.onload = () => resolve(img)
25+
img.src = file
26+
})
27+
} else {
28+
return new Promise(resolve => {
29+
const reader = new FileReader()
30+
const img = new Image()
31+
img.onload = () => resolve(img)
32+
reader.onload = e => {
33+
img.src = e.target.result
34+
}
35+
reader.readAsDataURL(file)
36+
})
37+
}
3038
}
3139

3240
/**
@@ -253,23 +261,57 @@ function generateAttachmentMarkdown (fileName, path, showPreview) {
253261
* @param {Event} dropEvent DropEvent
254262
*/
255263
function handleAttachmentDrop (codeEditor, storageKey, noteKey, dropEvent) {
256-
const file = dropEvent.dataTransfer.files[0]
257-
const filePath = file.path
258-
const originalFileName = path.basename(filePath)
259-
const fileType = file['type']
260-
const isImage = fileType.startsWith('image')
261-
let promise
262-
if (isImage) {
263-
promise = fixRotate(file).then(base64data => {
264-
return copyAttachment({type: 'base64', data: base64data, sourceFilePath: filePath}, storageKey, noteKey)
264+
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)
265282
})
266283
} else {
267-
promise = copyAttachment(filePath, storageKey, noteKey)
284+
for (let i = 0; i < dropEvent.dataTransfer.items.length; i++) {
285+
const item = dropEvent.dataTransfer.items[i]
286+
287+
if (item.type === 'text/html') {
288+
const html = dropEvent.dataTransfer.getData('text/html')
289+
290+
const match = /<img[^>]*src="([^"]+)"/.exec(html)
291+
if (match) {
292+
const imageURL = match[1]
293+
294+
getImage(imageURL)
295+
.then(image => {
296+
const canvas = document.createElement('canvas')
297+
const context = canvas.getContext('2d')
298+
canvas.width = image.width
299+
canvas.height = image.height
300+
context.drawImage(image, 0, 0)
301+
302+
return copyAttachment({type: 'base64', data: canvas.toDataURL(), sourceFilePath: imageURL}, storageKey, noteKey)
303+
})
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
311+
}
312+
}
313+
}
268314
}
269-
promise.then((fileName) => {
270-
const imageMd = generateAttachmentMarkdown(originalFileName, path.join(STORAGE_FOLDER_PLACEHOLDER, noteKey, fileName), isImage)
271-
codeEditor.insertAttachmentMd(imageMd)
272-
})
273315
}
274316

275317
/**

0 commit comments

Comments
 (0)