1
0
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:
asmsuechan
2017-03-10 20:33:11 -08:00
parent b6eddf0821
commit aae2bddd32
2 changed files with 20 additions and 15 deletions

View File

@@ -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 = `![${encodeURI(filename)}](${storagePath.path}/images/${encodeURI(filename)})`
this.insertImage(imageMd)
copyImage(imagePath, this.props.storageKey).then((imagePathInTheStorage) => {
let imageMd = `![${encodeURI(filename)}](${imagePathInTheStorage})`
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 () {

View File

@@ -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