UNPKG

ep-react-ui

Version:
177 lines (136 loc) 7.47 kB
'use strict'; 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 _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); var _draftJs = require('draft-js'); var _ControlItem = require('../../ControlItem'); var _ControlItem2 = _interopRequireDefault(_ControlItem); var _LinkForm = require('./LinkForm'); var _LinkForm2 = _interopRequireDefault(_LinkForm); 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; } /** * Date picker input */ var LinkControl = function (_PureComponent) { _inherits(LinkControl, _PureComponent); function LinkControl(props) { _classCallCheck(this, LinkControl); // actions var _this = _possibleConstructorReturn(this, (LinkControl.__proto__ || Object.getPrototypeOf(LinkControl)).call(this, props)); _this.state = { isShow: false, value: '' }; _this._onToggle = _this._onToggle.bind(_this); _this._onConfirm = _this._onConfirm.bind(_this); return _this; } _createClass(LinkControl, [{ key: '_onToggle', value: function _onToggle() { // show popup if (!this.state.isShow) { this.onShow(); } // hide popup else { this.onHide(); } } }, { key: '_onConfirm', value: function _onConfirm(url) { // apply if (url) { this.onApply(url); } // remove else { this.onRemove(); } } }, { key: 'render', value: function render() { var _state = this.state, isShow = _state.isShow, value = _state.value; return _react2.default.createElement( 'div', { className: 'LinkControl', style: { display: 'inline', position: 'relative' } }, _react2.default.createElement(_ControlItem2.default, { label: 'Link', onClick: this._onToggle }), isShow && _react2.default.createElement(_LinkForm2.default, { value: value, onChange: this._onChange, onCancel: this._onToggle, onConfirm: this._onConfirm }) ); } // internal }, { key: 'onShow', value: function onShow() { var editorState = this.props.editorState; var selection = editorState.getSelection(); // have selection if (!selection.isCollapsed()) { var contentState = editorState.getCurrentContent(); var startKey = editorState.getSelection().getStartKey(); var startOffset = editorState.getSelection().getStartOffset(); var blockWithLinkAtBeginning = contentState.getBlockForKey(startKey); var linkKey = blockWithLinkAtBeginning.getEntityAt(startOffset); var url = ''; if (linkKey) { var linkInstance = contentState.getEntity(linkKey); url = linkInstance.getData().url; } this.setState({ isShow: true, value: url }); } } }, { key: 'onHide', value: function onHide() { this.setState({ isShow: false }); } }, { key: 'onApply', value: function onApply(url) { var editorState = this.props.editorState; var contentState = editorState.getCurrentContent(); var contentStateWithEntity = contentState.createEntity('LINK', 'MUTABLE', { url: url }); var entityKey = contentStateWithEntity.getLastCreatedEntityKey(); var newEditorState = _draftJs.EditorState.set(editorState, { currentContent: contentStateWithEntity }); var newEditorStateWithLink = _draftJs.RichUtils.toggleLink(newEditorState, newEditorState.getSelection(), entityKey); this.props.onToggle(newEditorStateWithLink); this.setState({ isShow: false }); } }, { key: 'onRemove', value: function onRemove() { var editorState = this.props.editorState; var newEditorStateWithLink = _draftJs.RichUtils.toggleLink(editorState, editorState.getSelection(), null); this.props.onToggle(newEditorStateWithLink); this.setState({ isShow: false }); } }]); return LinkControl; }(_react.PureComponent); LinkControl.propTypes = { editorState: _propTypes2.default.object, onToggle: _propTypes2.default.func }; exports.default = LinkControl;