mirror of
https://github.com/BoostIo/Boostnote
synced 2025-12-13 17:56:25 +00:00
Improve the Promise of copyImage
This commit is contained in:
@@ -169,17 +169,16 @@ export default class CodeEditor extends React.Component {
|
||||
e.preventDefault()
|
||||
let imagePath = e.dataTransfer.files[0].path
|
||||
let filename = path.basename(imagePath)
|
||||
const cachedStorageList = JSON.parse(localStorage.getItem('storages'))
|
||||
const storagePath = _.find(cachedStorageList, {key: this.props.storageKey})
|
||||
|
||||
copyImage(imagePath, this.props.storageKey)
|
||||
const imageMd = `})`
|
||||
this.insertImage(imageMd)
|
||||
copyImage(imagePath, this.props.storageKey).then((imagePathInTheStorage) => {
|
||||
let imageMd = ``
|
||||
this.insertImageMd(imageMd)
|
||||
})
|
||||
}
|
||||
|
||||
insertImage (imageMd) {
|
||||
const textarea = this.editor.getInputField()
|
||||
textarea.value = textarea.value.substr(0, textarea.selectionStart) + imageMd + textarea.value.substr(textarea.selectionEnd)
|
||||
insertImageMd (imageMd) {
|
||||
const cm = this.editor
|
||||
cm.setValue(cm.getValue() + imageMd)
|
||||
}
|
||||
|
||||
render () {
|
||||
|
||||
@@ -15,13 +15,19 @@ function copyImage (filePath, storageKey) {
|
||||
return Promise.reject(e)
|
||||
}
|
||||
|
||||
//return resolveStorageData(targetStorage)
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
try {
|
||||
const inputImage = fs.createReadStream(filePath)
|
||||
const imageName = path.basename(filePath)
|
||||
sander.mkdirSync(`${targetStorage.path}/images`)
|
||||
const outputImage = fs.createWriteStream(path.join(targetStorage.path, 'images', imageName))
|
||||
inputImage.pipe(outputImage)
|
||||
resolve(`${encodeURI(targetStorage.path)}/images/${encodeURI(imageName)}`)
|
||||
}
|
||||
catch(e) {
|
||||
return reject(e)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = copyImage
|
||||
|
||||
Reference in New Issue
Block a user