sc-react-ions
Version:
An open source set of React components that implement Ambassador's Design and UX patterns.
195 lines (154 loc) • 6.01 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
var _bind = require('classnames/bind');
var _bind2 = _interopRequireDefault(_bind);
var _quill = require('quill');
var _quill2 = _interopRequireDefault(_quill);
var _Toolbar = require('./Toolbar');
var _Toolbar2 = _interopRequireDefault(_Toolbar);
var _style = require('./style.scss');
var _style2 = _interopRequireDefault(_style);
require('../../styles/global/quill.scss');
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; }
/**
* The TextEditor component.
*/
var TextEditor = function (_PureComponent) {
_inherits(TextEditor, _PureComponent);
function TextEditor(props) {
_classCallCheck(this, TextEditor);
var _this = _possibleConstructorReturn(this, (TextEditor.__proto__ || Object.getPrototypeOf(TextEditor)).call(this, props));
_this.state = {
value: _this.props.value
};
_this.registerEventHandlers = function () {
// On text change
_this.state.textEditor.on('text-change', function (delta, oldDelta, source) {
var value = _this.getHTML();
if (value !== _this.state.value) {
_this.handleChange(value);
}
});
};
_this.handleChange = function (value) {
var event = {
target: {
name: _this.props.name,
value: value
}
// Set state before triggering the callback
};_this.setState({ value: value }, function () {
if (_this.props.changeCallback) {
_this.props.changeCallback(event);
}
});
};
_this.setContent = function (value) {
_this._editor.firstChild.innerHTML = value;
};
_this.componentDidMount = function () {
// Define editor options
var options = {
modules: {
toolbar: _this._toolbar
},
placeholder: _this.props.placeholder || '',
theme: 'snow'
// Use style attribute for align and font tools
};var AlignAttribute = _quill2.default.import('attributors/style/align');
var FontAttribute = _quill2.default.import('attributors/style/font');
_quill2.default.register(AlignAttribute, true);
_quill2.default.register(FontAttribute, true);
// Initialize the editor
_this.setState({ textEditor: new _quill2.default(_this._editor, options) }, function () {
// Set the content
if (_this.props.value) {
_this.setContent(_this.props.value);
}
// Disable the editor
if (_this.props.disabled) {
_this.state.textEditor.disable();
}
// Register event handlers
_this.registerEventHandlers();
});
};
_this.componentWillReceiveProps = function (nextProps) {
// Enable/disable the editor
_this.state.textEditor.enable(!nextProps.disabled);
// If the value changed set the editor content and state
if (nextProps.value !== _this.state.value) {
_this.setContent(nextProps.value);
_this.setState({ value: nextProps.value });
}
};
_this.setToolbarRef = function (toolbar) {
_this._toolbar = toolbar;
};
_this.render = function () {
var cx = _bind2.default.bind(_style2.default);
var disabledClass = _this.props.disabled ? _style2.default['editor-disabled'] : '';
var editorClass = cx(_style2.default['editor-component'], _this.props.optClass, disabledClass);
return _react2.default.createElement(
'div',
{ className: editorClass },
_react2.default.createElement(_Toolbar2.default, { textEditor: _this.state.textEditor, mergeTags: _this.props.mergeTags, onMount: _this.setToolbarRef }),
_react2.default.createElement('div', { ref: function ref(c) {
return _this._editor = c;
} }),
_react2.default.createElement('div', { className: _style2.default.overlay })
);
};
_this.getHTML = function () {
if (_this._editor) {
return _this._editor.firstChild.innerHTML;
}
};
return _this;
}
return TextEditor;
}(_react.PureComponent);
TextEditor.defaultProps = {
disabled: false,
value: ''
};
TextEditor.propTypes = {
/**
* Whether the text editor is disabled.
*/
disabled: _propTypes2.default.bool,
/**
* Value of the text editor (HTML).
*/
value: _propTypes2.default.string,
/**
* Optional placeholder text.
*/
placeholder: _propTypes2.default.string,
/**
* Name of the text editor.
*/
name: _propTypes2.default.string,
/**
* Optional styles to add to the text editor.
*/
optClass: _propTypes2.default.string,
/**
* A callback function to be called when the text editor changes.
*/
changeCallback: _propTypes2.default.func,
/**
* Merge tags to display in the toolbar.
*/
mergeTags: _propTypes2.default.array
};
exports.default = TextEditor;