Skip to content

Instantly share code, notes, and snippets.

@charliewilco
Created December 11, 2017 17:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save charliewilco/05a8e1ad07d31fcfc7b0ce6d7bb7cbf3 to your computer and use it in GitHub Desktop.
Save charliewilco/05a8e1ad07d31fcfc7b0ce6d7bb7cbf3 to your computer and use it in GitHub Desktop.
import React from 'react'
import Dropzone from 'react-dropzone'
import fm from 'front-matter'
import { Editor, EditorState, convertFromRaw } from 'draft-js'
import { markdownToDraft } from 'markdown-draft-js'
import './App.css'
const styles = {
fontFamily: 'Operator Mono, sans-serif'
}
let reader = new FileReader()
class MarkdownReader extends React.Component {
state = { files: [], content: '', title: '', editorState: EditorState.createEmpty() }
setContent = files => {
reader.onload = e => {
let md = fm(reader.result)
console.log(md)
return this.setState({
files,
title: md.attributes.title || '',
editorState: EditorState.createWithContent(
convertFromRaw(markdownToDraft(md.body))
)
})
}
reader.readAsText(files[0])
}
onChange = editorState => this.setState({ editorState })
onDrop = files => this.setState(this.setContent(files))
render() {
const { editorState, title } = this.state
let ContentState = editorState.getCurrentContent()
return (
<div>
<Dropzone
onDrop={this.onDrop}
multiple={false}
disableClick
disabled={ContentState.hasText()}
style={{ border: `1px solid red`,margin: 'auto', maxWidth: 640 }}>
<input style={{ fontSize: 32, display: 'block', width: '100%', }} value={title} onChange={({ target }) => this.setState({ title: target.value })} />
<Editor
editorState={editorState}
onChange={this.onChange}
/>
</Dropzone>
</div>
)
}
}
export default () => (
<div style={styles}>
<h2>Start editing to see some magic happen {'\u2728'}</h2>
<MarkdownReader />
</div>
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment