UNPKG

choerodon-ui

Version:

An enterprise-class UI design language and React-based implementation

102 lines (94 loc) 3.28 kB
import _extends from "@babel/runtime/helpers/extends"; import _objectSpread from "@babel/runtime/helpers/objectSpread2"; import React, { createElement } from 'react'; import noop from 'lodash/noop'; import _Suggestions from '../component/Suggestions.react'; import SuggestionPortal from '../component/SuggestionPortal.react'; import MentionContent from '../component/MentionContent.react'; import mentionStore from '../model/mentionStore'; import exportContent from './exportContent'; import getRegExp from '../utils/getRegExp'; function findWithRegex(regex, contentBlock, callback) { // Get the text from the contentBlock var text = contentBlock.getText(); var matchArr; var start; // eslint-disable-line // Go through all matches in the text and return the indizes to the callback while ((matchArr = regex.exec(text)) !== null) { // eslint-disable-line start = matchArr.index; callback(start, start + matchArr[0].length); } } function mentionContentStrategy(contentBlock, callback, contentState) { contentBlock.findEntityRanges(function (character) { var entityKey = character.getEntity(); return entityKey && contentState.getEntity(entityKey).getType() === 'mention'; }, callback); } var MentionContentComponent = function MentionContentComponent(props) { var entityKey = props.entityKey, tag = props.tag, callbacks = props.callbacks; var contentState = callbacks.getEditorState().getCurrentContent(); var data = contentState.getEntity(entityKey).getData(); return createElement(tag, _objectSpread({}, props, { data: data })); }; export default function createMention() { var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var callbacks = { onChange: noop, onUpArrow: noop, onDownArrow: noop, getEditorState: noop, setEditorState: noop, handleReturn: noop, onBlur: noop }; var componentProps = { callbacks: callbacks, mentionStore: mentionStore }; var suggestionRegex = getRegExp(config.prefix); var tag = config.tag || MentionContent; var decorators = [{ strategy: function strategy(contentBlock, callback) { findWithRegex(suggestionRegex, contentBlock, callback); }, component: function component(props) { return React.createElement(SuggestionPortal, _extends({}, props, componentProps, { style: config.mentionStyle, suggestionRegex: getRegExp(config.prefix) })); } }]; if (config.mode !== 'immutable') { decorators.unshift({ strategy: mentionContentStrategy, component: function component(props) { return React.createElement(MentionContentComponent, _extends({ tag: tag }, props, { callbacks: callbacks })); } }); } return { name: 'mention', Suggestions: function Suggestions(props) { return React.createElement(_Suggestions, _extends({}, props, componentProps, { store: mentionStore })); }, decorators: decorators, onChange: function onChange(editorState) { return callbacks.onChange ? callbacks.onChange(editorState) : editorState; }, callbacks: callbacks, "export": exportContent }; } //# sourceMappingURL=createMention.js.map