UNPKG

uxcore-mention

Version:

Mention anywhere with this component

131 lines (105 loc) 4.43 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); var _keycode = require('../utils/keycode'); require('../utils/rangy-position'); var _mentionMixin = require('../components/mentionMixin'); var _mentionMixin2 = _interopRequireDefault(_mentionMixin); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _defaults(obj, defaults) { var keys = Object.getOwnPropertyNames(defaults); for (var i = 0; i < keys.length; i++) { var key = keys[i]; var value = Object.getOwnPropertyDescriptor(defaults, key); if (value && value.configurable && obj[key] === undefined) { Object.defineProperty(obj, key, value); } } return 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) : _defaults(subClass, superClass); } var BaseEditor = function (_BaseMention) { _inherits(BaseEditor, _BaseMention); function BaseEditor() { _classCallCheck(this, BaseEditor); return _possibleConstructorReturn(this, _BaseMention.apply(this, arguments)); } BaseEditor.prototype.onFocus = function onFocus() { this.props.onFocus(this); }; BaseEditor.prototype.onKeydown = function onKeydown(e) { var panelVisible = this.props.panelVisible; switch (e.keyCode) { case _keycode.KEYCODE.UP: case _keycode.KEYCODE.DOWN: if (panelVisible) { e.preventDefault(); } break; case _keycode.KEYCODE.ENTER: if (panelVisible) { e.preventDefault(); } else if (this.handleEnterPress) { this.handleEnterPress(e); } break; default: break; } }; BaseEditor.prototype.onKeyup = function onKeyup(e) { var panelVisible = this.props.panelVisible; switch (e.keyCode) { case _keycode.KEYCODE.UP: case _keycode.KEYCODE.DOWN: if (panelVisible) { e.preventDefault(); } break; case _keycode.KEYCODE.ENTER: break; default: if (this.handleDefaultKeyup) { this.handleDefaultKeyup(); } break; } }; BaseEditor.prototype.insertMentionData = function insertMentionData(mentionData) { var _props = this.props, mentionFormatter = _props.mentionFormatter, onAdd = _props.onAdd; var insertContent = mentionFormatter(mentionData); this.insert(insertContent); onAdd(insertContent, mentionData); var added = this._added = this._added || []; var mentionDataJson = JSON.stringify(mentionData); if (!added.filter(function (n) { return n.dataJson === mentionDataJson; }).length) { added.push({ content: insertContent, data: mentionData, dataJson: mentionDataJson }); } }; BaseEditor.prototype.getValue = function getValue() { return this.state.value; }; BaseEditor.prototype.getMentions = function getMentions() { var content = this.getValue() || ''; var added = this._added || []; return added.filter(function (n) { return !!~content.indexOf(n.content); }).map(function (n) { return n.data; }); }; return BaseEditor; }(_mentionMixin2["default"]); BaseEditor.displayName = 'BaseEditor'; BaseEditor.propTypes = { panelVisible: _propTypes2["default"]["boolean"], onFocus: _propTypes2["default"].func, mentionFormatter: _propTypes2["default"].func, onAdd: _propTypes2["default"].func }; BaseEditor.defaultProps = {}; exports["default"] = BaseEditor; module.exports = exports['default'];