Skip to content

Instantly share code, notes, and snippets.

@KOBAYASHI-Toshinobu
Last active August 29, 2015 14: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 KOBAYASHI-Toshinobu/1333c9b1e05d170e1234 to your computer and use it in GitHub Desktop.
Save KOBAYASHI-Toshinobu/1333c9b1e05d170e1234 to your computer and use it in GitHub Desktop.
React + UIkit Markdown Editor component
(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