UNPKG

@bigfishtv/cockpit

Version:

101 lines (84 loc) 3.73 kB
var _class, _temp; 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; } import PropTypes from 'prop-types'; import React, { Component } from 'react'; // eslint-disable-next-line no-unused-vars import redactor from '../../lib/redactor/redactor'; import $ from 'jquery'; import { simplifyNonBreakingSpaces } from '../utils/stringUtils'; import { createSettings } from '../utils/redactorUtils'; var defaultCreateSettings = function defaultCreateSettings() { return 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 = simplifyNonBreakingSpaces(this.code.get()); me.setState({ value: value }, function () { me.props.onChange(value); }); } }; $(this.root).redactor(settings); // this version of redactor doesn't support disabling, so add hack, obviously! var redactor = $.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) { $(this.root).redactor('code.set', this.props.value); $(this.root).redactor('code.sync'); } // this version of redactor doesn't support disabling, so add hack, obviously! var redactor = $.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 React.createElement('textarea', { ref: function ref(input) { return _this2.root = input; }, placeholder: this.props.placeholder, defaultValue: this.props.value, readOnly: this.props.readOnly }); }; return Redactor; }(Component), _class.propTypes = { createSettings: PropTypes.func, readOnly: PropTypes.bool }, _class.defaultProps = { createSettings: defaultCreateSettings, readOnly: false }, _temp); export { Redactor as default };