UNPKG

@bigfishtv/cockpit

Version:

122 lines (92 loc) 4.27 kB
'use strict'; exports.__esModule = true; exports.default = undefined; var _class, _temp; // eslint-disable-next-line no-unused-vars var _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _redactor = require('../../lib/redactor/redactor'); var _redactor2 = _interopRequireDefault(_redactor); var _jquery = require('jquery'); var _jquery2 = _interopRequireDefault(_jquery); var _stringUtils = require('../utils/stringUtils'); var _redactorUtils = require('../utils/redactorUtils'); 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 defaultCreateSettings = function defaultCreateSettings() { return (0, _redactorUtils.createSettings)(); }; // @refactor this component to lazy load redactor and jquery /** * Rich text editor component */ var Redactor = (_temp = _class = function (_Component) { _inherits(Redactor, _Component); function Redactor(_ref) { var value = _ref.value; _classCallCheck(this, Redactor); var _this = _possibleConstructorReturn(this, _Component.call(this)); _this.state = { value: value }; return _this; } Redactor.prototype.componentDidMount = function componentDidMount() { var me = this; var settings = this.props.createSettings(); settings.callbacks.change = function () { // this version of redactor doesn't support disabling, so add hack, obviously! if (me.props.readOnly) { // do nothing } else { var value = (0, _stringUtils.simplifyNonBreakingSpaces)(this.code.get()); me.setState({ value: value }, function () { me.props.onChange(value); }); } }; (0, _jquery2.default)(this.root).redactor(settings); // this version of redactor doesn't support disabling, so add hack, obviously! var redactor = _jquery2.default.data(this.root, 'redactor'); redactor.core.editor().attr({ contenteditable: !this.props.readOnly }); }; Redactor.prototype.componentDidUpdate = function componentDidUpdate(prevProps, prevState) { if (this.props.value != prevState.value) { (0, _jquery2.default)(this.root).redactor('code.set', this.props.value); (0, _jquery2.default)(this.root).redactor('code.sync'); } // this version of redactor doesn't support disabling, so add hack, obviously! var redactor = _jquery2.default.data(this.root, 'redactor'); redactor.core.editor().attr({ contenteditable: !this.props.readOnly }); }; Redactor.prototype.shouldComponentUpdate = function shouldComponentUpdate(nextProps, nextState) { if (this.props.placeholder != nextProps.placeholder) { return true; } if (this.props.readOnly != nextProps.readOnly) { return true; } return nextProps.value != this.state.value; }; Redactor.prototype.render = function render() { var _this2 = this; return _react2.default.createElement('textarea', { ref: function ref(input) { return _this2.root = input; }, placeholder: this.props.placeholder, defaultValue: this.props.value, readOnly: this.props.readOnly }); }; return Redactor; }(_react.Component), _class.propTypes = { createSettings: _propTypes2.default.func, readOnly: _propTypes2.default.bool }, _class.defaultProps = { createSettings: defaultCreateSettings, readOnly: false }, _temp); exports.default = Redactor;