@@ -3,6 +3,7 @@ import _ from 'lodash'
33import CodeMirror from 'codemirror'
44import path from 'path'
55import copyImage from 'browser/main/lib/dataApi/copyImage'
6+ import { findStorage } from 'browser/lib/findStorage'
67
78CodeMirror . modeURL = '../node_modules/codemirror/mode/%N/%N.js'
89
@@ -39,6 +40,7 @@ export default class CodeEditor extends React.Component {
3940 }
4041 this . props . onBlur != null && this . props . onBlur ( e )
4142 }
43+ this . pasteHandler = ( editor , e ) => this . handlePaste ( editor , e )
4244 this . loadStyleHandler = ( e ) => {
4345 this . editor . refresh ( )
4446 }
@@ -98,6 +100,7 @@ export default class CodeEditor extends React.Component {
98100
99101 this . editor . on ( 'blur' , this . blurHandler )
100102 this . editor . on ( 'change' , this . changeHandler )
103+ this . editor . on ( 'paste' , this . pasteHandler )
101104
102105 let editorTheme = document . getElementById ( 'editorTheme' )
103106 editorTheme . addEventListener ( 'load' , this . loadStyleHandler )
@@ -106,6 +109,7 @@ export default class CodeEditor extends React.Component {
106109 componentWillUnmount ( ) {
107110 this . editor . off ( 'blur' , this . blurHandler )
108111 this . editor . off ( 'change' , this . changeHandler )
112+ this . editor . off ( 'paste' , this . pasteHandler )
109113 let editorTheme = document . getElementById ( 'editorTheme' )
110114 editorTheme . removeEventListener ( 'load' , this . loadStyleHandler )
111115 }
@@ -204,6 +208,29 @@ export default class CodeEditor extends React.Component {
204208 cm . replaceSelection ( `${ textarea . value . substr ( 0 , textarea . selectionStart ) } ${ imageMd } ${ textarea . value . substr ( textarea . selectionEnd ) } ` )
205209 }
206210
211+ handlePaste ( editor , e ) {
212+ const dataTransferItem = e . clipboardData . items [ 0 ]
213+ if ( ! dataTransferItem . type . match ( 'image' ) ) return
214+
215+ const blob = dataTransferItem . getAsFile ( )
216+ let reader = new FileReader ( )
217+ let base64data
218+
219+ reader . readAsDataURL ( blob )
220+ reader . onloadend = ( ) => {
221+ base64data = reader . result . replace ( / ^ d a t a : i m a g e \/ p n g ; b a s e 6 4 , / , '' )
222+ base64data += base64data . replace ( '+' , ' ' )
223+ const binaryData = new Buffer ( base64data , 'base64' ) . toString ( 'binary' )
224+ const imageName = Math . random ( ) . toString ( 36 ) . slice ( - 16 )
225+ const storagePath = findStorage ( this . props . storageKey ) . path
226+ const imagePath = path . join ( `${ storagePath } ` , 'images' , `${ imageName } .png` )
227+
228+ require ( 'fs' ) . writeFile ( imagePath , binaryData , 'binary' )
229+ const imageMd = ` } )`
230+ this . insertImageMd ( imageMd )
231+ }
232+ }
233+
207234 render ( ) {
208235 let { className, fontFamily, fontSize } = this . props
209236 fontFamily = _ . isString ( fontFamily ) && fontFamily . length > 0
0 commit comments