Last active
August 29, 2015 14:21
-
-
Save KOBAYASHI-Toshinobu/1333c9b1e05d170e1234 to your computer and use it in GitHub Desktop.
React + UIkit Markdown Editor component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
$(function() { | |
"use strict"; | |
var MarkdownEditor = React.createClass({ | |
propTypes: { | |
onChange: React.PropTypes.func, | |
name: React.PropTypes.string, | |
textValue: React.PropTypes.string | |
}, | |
getDefaultProps: function() { | |
return { | |
textValue: '' | |
}; | |
}, | |
getInitialState: function() { | |
return { | |
textValue: this.props.textValue | |
}; | |
}, | |
componentWillMount: function() { | |
var el = document.createElement('textarea'); | |
el.setAttribute("name", this.props.name); | |
el.value = this.props.textValue; | |
var htmleditor = this.htmleditor = UIkit.htmleditor(el, { mode:'split', maxsplitsize:600, markdown:true }); | |
this.editor = htmleditor.htmleditor[0]; | |
}, | |
componentDidMount: function() { | |
React.findDOMNode(this).appendChild(this.editor); | |
this.htmleditor.redraw(); | |
$(this.htmleditor.element).on('input', this.handleChange); | |
}, | |
handleChange: function(event) { | |
if (this.props.onChange) { | |
this.props.onChange(event); | |
} | |
this.setState({textValue: event.target.text}); | |
}, | |
componentWillReceiveProps: function() { | |
this.setState({textValue: this.props.textValue}); | |
}, | |
componentDidUpdate: function() { | |
if (this.htmleditor.element[0].value === this.props.textValue) return; | |
this.componentWillUnMount(); | |
this.componentWillMount(); | |
this.componentDidMount(); | |
}, | |
componentWillUnMount: function () { | |
$(this.htmleditor.element).off(); | |
React.findDOMNode(this).removeChild(this.editor); | |
this.htmleditor = null; | |
}, | |
render: function() { | |
return ( | |
<div /> | |
); | |
} | |
}); | |
window.MarkdownEditor = MarkdownEditor; | |
}); | |
}).call(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment