diff --git a/browser/components/CodeEditor.js b/browser/components/CodeEditor.js index ecf02c15..259f6bd6 100644 --- a/browser/components/CodeEditor.js +++ b/browser/components/CodeEditor.js @@ -2,6 +2,7 @@ import React, { PropTypes } from 'react' import _ from 'lodash' import CodeMirror from 'codemirror' import path from 'path' +import copyImage from 'browser/main/lib/dataApi/copyImage' CodeMirror.modeURL = '../node_modules/codemirror/mode/%N/%N.js' @@ -184,13 +185,16 @@ export default class CodeEditor extends React.Component { e.preventDefault() const imagePath = e.dataTransfer.files[0].path const filename = path.basename(imagePath) - const imageMd = `![${encodeURI(filename)}](${encodeURI(imagePath)})` - this.insertImage(imageMd) + + copyImage(imagePath, this.props.storageKey).then((imagePathInTheStorage) => { + const 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 () { diff --git a/browser/components/MarkdownEditor.js b/browser/components/MarkdownEditor.js index f81233ba..78b0f2f0 100644 --- a/browser/components/MarkdownEditor.js +++ b/browser/components/MarkdownEditor.js @@ -203,7 +203,7 @@ class MarkdownEditor extends React.Component { } render () { - let { className, value, config } = this.props + let { className, value, config, storageKey } = this.props let editorFontSize = parseInt(config.editor.fontSize, 10) if (!(editorFontSize > 0 && editorFontSize < 101)) editorFontSize = 14 @@ -236,6 +236,7 @@ class MarkdownEditor extends React.Component { fontSize={editorFontSize} indentType={config.editor.indentType} indentSize={editorIndentSize} + storageKey={storageKey} onChange={(e) => this.handleChange(e)} onBlur={(e) => this.handleBlur(e)} /> diff --git a/browser/main/Detail/MarkdownNoteDetail.js b/browser/main/Detail/MarkdownNoteDetail.js index 8c63f952..a8a0d4bd 100644 --- a/browser/main/Detail/MarkdownNoteDetail.js +++ b/browser/main/Detail/MarkdownNoteDetail.js @@ -330,6 +330,7 @@ class MarkdownNoteDetail extends React.Component { styleName='body-noteEditor' config={config} value={this.state.note.content} + storageKey={this.state.note.storage} onChange={(e) => this.handleChange(e)} ignorePreviewPointerEvents={this.props.ignorePreviewPointerEvents} /> diff --git a/browser/main/lib/dataApi/copyImage.js b/browser/main/lib/dataApi/copyImage.js new file mode 100644 index 00000000..8c6f1787 --- /dev/null +++ b/browser/main/lib/dataApi/copyImage.js @@ -0,0 +1,32 @@ +const fs = require('fs') +const path = require('path') +const _ = require('lodash') +const sander = require('sander') + +/** + * @description To copy an image and return the path. + * @param {String} filePath + * @param {String} storageKey + * @return {String} an image path + */ +function copyImage (filePath, storageKey) { + return new Promise((resolve, reject) => { + try { + const cachedStorageList = JSON.parse(localStorage.getItem('storages')) + if (!_.isArray(cachedStorageList)) throw new Error('Target storage doesn\'t exist.') + const storage = _.find(cachedStorageList, {key: storageKey}) + if (storage === undefined) throw new Error('Target storage doesn\'t exist.') + const targetStorage = storage + + const inputImage = fs.createReadStream(filePath) + const imageName = path.basename(filePath) + 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