tao-react-components
Version:
react components crafted with love by Tao
124 lines (101 loc) • 5.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _mediumDraft = require('medium-draft');
var _draftJs = require('draft-js');
var _exporter = require('medium-draft/lib/exporter');
var _exporter2 = _interopRequireDefault(_exporter);
require('./draft-style.css');
require('./style.css');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var ContentEditable = function (_Component) {
_inherits(ContentEditable, _Component);
function ContentEditable(props) {
_classCallCheck(this, ContentEditable);
var _this = _possibleConstructorReturn(this, (ContentEditable.__proto__ || Object.getPrototypeOf(ContentEditable)).call(this, props));
var SavedEditorState = null;
if (_this.props.SavedEditorState) {
SavedEditorState = _this.props.SavedEditorState;
}
_this.state = {
editorState: SavedEditorState && SavedEditorState.blocks ? (0, _mediumDraft.createEditorState)(SavedEditorState) : (0, _mediumDraft.createEditorState)() };
_this.onChange = function (editorState) {
_this.setState({ editorState: editorState });
var HTMLContent = (0, _exporter2.default)(_this.state.editorState.getCurrentContent());
var TextContent = editorState.getCurrentContent().getPlainText();
var EditorState = (0, _draftJs.convertToRaw)(editorState.getCurrentContent());
_this.props.onChange && _this.props.onChange(HTMLContent, TextContent, EditorState);
};
_this.sideButtons = [];
_this.blockButtons = [];
_this.inlineButtons = [{
label: 'B',
style: 'BOLD',
icon: 'bold',
description: 'Bold'
}, {
label: 'I',
style: 'ITALIC',
icon: 'italic',
description: 'Italic'
}, {
label: 'U',
style: 'UNDERLINE',
icon: 'underline',
description: 'Underline'
}, {
label: 'Hi',
style: 'HIGHLIGHT',
icon: 'highlight',
description: 'Hightlight text'
}, {
label: '#',
style: 'hyperlink',
icon: 'link',
description: 'Add a link'
}];
return _this;
}
_createClass(ContentEditable, [{
key: 'componentDidMount',
value: function componentDidMount() {
this.refs.editor.focus();
document.getElementsByClassName('md-RichEditor-root')[0] && document.getElementsByClassName('md-RichEditor-root')[0].classList.add(this.props.EditorStyleClassName);
document.getElementsByClassName('DraftEditor-editorContainer')[0] && document.getElementsByClassName('DraftEditor-editorContainer')[0].classList.add(this.props.TextStyleClassName);
}
}, {
key: 'componentWillReceiveProps',
value: function componentWillReceiveProps(nextProps) {
nextProps.curentEditingQuestion != this.props.curentEditingQuestion && this.setState({
editorState: nextProps.SavedEditorState && nextProps.SavedEditorState.blocks ? (0, _mediumDraft.createEditorState)(nextProps.SavedEditorState) : (0, _mediumDraft.createEditorState)()
});
}
}, {
key: 'render',
value: function render() {
var editorState = this.state.editorState;
return _react2.default.createElement(
'div',
{ className: "Tao-ContentEditable " + this.props.className },
_react2.default.createElement(_mediumDraft.Editor, {
sideButtons: this.sideButtons,
placeholder: this.props.placeholder ? this.props.placeholder : 'Write Your Text Here..',
ref: 'editor',
editorState: editorState,
onChange: this.onChange,
inlineButtons: this.props.inlineButtons ? this.props.inlineButtons : this.inlineButtons,
blockButtons: this.props.blockButtons ? this.props.blockButtons : this.blockButtons
})
);
}
}]);
return ContentEditable;
}(_react.Component);
exports.default = ContentEditable;