Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions browser/components/CodeEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -168,13 +169,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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good Rename!

const cm = this.editor
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What the mean of cm? If it is not popular wording, I want you to explain more detail.

Copy link
Contributor Author

@asmsuechan asmsuechan Mar 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's CodeMirror and used this name overall of Boostnote.

cm.setValue(cm.getValue() + imageMd)
}

render () {
Expand Down
3 changes: 2 additions & 1 deletion browser/components/MarkdownEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,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
Expand Down Expand Up @@ -210,6 +210,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)}
/>
Expand Down
1 change: 1 addition & 0 deletions browser/main/Detail/MarkdownNoteDetail.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,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}
/>
Expand Down
32 changes: 32 additions & 0 deletions browser/main/lib/dataApi/copyImage.js
Original file line number Diff line number Diff line change
@@ -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