1
0
mirror of https://github.com/BoostIo/Boostnote synced 2025-12-13 17:56:25 +00:00

Merge pull request #250 from asmsuechan/add-drop-image

Dropping images into CodeEditor
This commit is contained in:
Sota Sugiura
2017-01-21 16:28:56 +09:00
committed by GitHub

View File

@@ -1,6 +1,7 @@
import React, { PropTypes } from 'react' import React, { PropTypes } from 'react'
import _ from 'lodash' import _ from 'lodash'
import CodeMirror from 'codemirror' import CodeMirror from 'codemirror'
import path from 'path'
CodeMirror.modeURL = '../node_modules/codemirror/mode/%N/%N.js' CodeMirror.modeURL = '../node_modules/codemirror/mode/%N/%N.js'
@@ -163,6 +164,19 @@ export default class CodeEditor extends React.Component {
this.editor.setCursor(cursor) this.editor.setCursor(cursor)
} }
handleDropImage (e) {
e.preventDefault()
let imagePath = e.dataTransfer.files[0].path
let filename = path.basename(imagePath)
let imageMd = `![${filename}](${imagePath})`
this.insertImage(imageMd)
}
insertImage (imageMd) {
const textarea = this.editor.getInputField()
textarea.value = textarea.value.substr(0, textarea.selectionStart) + imageMd + textarea.value.substr(textarea.selectionEnd)
}
render () { render () {
let { className, fontFamily, fontSize } = this.props let { className, fontFamily, fontSize } = this.props
fontFamily = _.isString(fontFamily) && fontFamily.length > 0 fontFamily = _.isString(fontFamily) && fontFamily.length > 0
@@ -180,6 +194,7 @@ export default class CodeEditor extends React.Component {
fontFamily: fontFamily.join(', '), fontFamily: fontFamily.join(', '),
fontSize: fontSize fontSize: fontSize
}} }}
onDrop={(e) => this.handleDropImage(e)}
/> />
) )
} }