Aucune description

index.js 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
  4. var _rollupPluginBabelHelpers = require('../_rollupPluginBabelHelpers-3469b70d.js');
  5. var React = require('react');
  6. var React__default = _interopDefault(React);
  7. require('../_commonjsHelpers-72d386ba.js');
  8. var styleInject_es = require('../style-inject.es-dcee06b6.js');
  9. require('react-dom');
  10. var Draft = require('../Draft-a5afe13a.js');
  11. require('../_react-dom_commonjs-external-55867475.js');
  12. var css = ".RichTextEditor_wrapper__3X3Yv .RichTextEditor_btnListWrapper__vUaki {\n display: flex;\n}\n.RichTextEditor_wrapper__3X3Yv .RichTextEditor_editorWrapper__3UFgF {\n border: 1px solid #ccc;\n}\n";
  13. var styles = {"wrapper":"RichTextEditor_wrapper__3X3Yv","btnListWrapper":"RichTextEditor_btnListWrapper__vUaki","editorWrapper":"RichTextEditor_editorWrapper__3UFgF"};
  14. styleInject_es.styleInject(css);
  15. // 自定义组件,用于超链接
  16. var Link = function Link(props) {
  17. // 这里通过contentState来获取entity�,之后通过getData获取entity中包含的数据
  18. var _props$contentState$g = props.contentState.getEntity(props.entityKey).getData(),
  19. url = _props$contentState$g.url;
  20. return React__default.createElement("a", {
  21. href: url
  22. }, props.children);
  23. }; // decorator,用于超链接
  24. var decorator = new Draft.Draft_4([{
  25. strategy: function strategy(contentBlock, callback, contentState) {
  26. // 这个方法接收2个函数作为参数,如果第一个参数的函数执行时�返回true,就会执行第二个参数函数,同时会�将匹配的�字符的起始位置和结束位置传递给第二个参数。
  27. contentBlock.findEntityRanges(function (character) {
  28. var entityKey = character.getEntity();
  29. return entityKey !== null && contentState.getEntity(entityKey).getType() === 'LINK';
  30. }, function () {
  31. callback.apply(void 0, arguments);
  32. });
  33. },
  34. component: Link
  35. }, {
  36. strategy: function strategy(contentBlock, callback, contentState) {
  37. contentBlock.findEntityRanges(function (character) {
  38. var entityKey = character.getEntity();
  39. return entityKey !== null && contentState.getEntity(entityKey).getType() === 'EMOJI';
  40. }, function () {
  41. callback.apply(void 0, arguments);
  42. });
  43. },
  44. // component: (props: any) => (<span style={{ color: 'red' }}>[Emoji]</span>)
  45. component: function component(props) {
  46. return React__default.createElement("i", {
  47. style: {
  48. display: 'inline-block',
  49. height: '18px',
  50. width: '18px',
  51. backgroundImage: "url(https://i.pinimg.com/originals/03/7e/79/037e79b2fb52127537be79110891ae3f.png)",
  52. backgroundSize: '100% 100%',
  53. color: 'transparent'
  54. }
  55. }, "e");
  56. }
  57. }]);
  58. var RichTextEditor =
  59. /*#__PURE__*/
  60. function (_React$Component) {
  61. _rollupPluginBabelHelpers._inherits(RichTextEditor, _React$Component);
  62. function RichTextEditor(props) {
  63. var _this;
  64. _rollupPluginBabelHelpers._classCallCheck(this, RichTextEditor);
  65. _this = _rollupPluginBabelHelpers._possibleConstructorReturn(this, _rollupPluginBabelHelpers._getPrototypeOf(RichTextEditor).call(this, props));
  66. _rollupPluginBabelHelpers._defineProperty(_rollupPluginBabelHelpers._assertThisInitialized(_this), "onChange", void 0);
  67. _this.state = {
  68. editorState: Draft.Draft_3.createEmpty(decorator)
  69. };
  70. _this.onChange = function (editorState) {
  71. return _this.setState({
  72. editorState: editorState
  73. });
  74. };
  75. _this.handleKeyCommand = _this.handleKeyCommand.bind(_rollupPluginBabelHelpers._assertThisInitialized(_this));
  76. _this.defaultBlockStyleFn = _this.defaultBlockStyleFn.bind(_rollupPluginBabelHelpers._assertThisInitialized(_this));
  77. return _this;
  78. }
  79. _rollupPluginBabelHelpers._createClass(RichTextEditor, [{
  80. key: "handleKeyCommand",
  81. value: function handleKeyCommand(command, editorState) {
  82. var newState = Draft.Draft_15.handleKeyCommand(editorState, command);
  83. console.log('command: ', command);
  84. console.log('newState: ', newState);
  85. if (newState) {
  86. this.onChange(newState);
  87. return "handled";
  88. }
  89. switch (command) {
  90. case 'backspace':
  91. var contentState = editorState.getCurrentContent();
  92. var selectionState = editorState.getSelection();
  93. var _ref = [selectionState.getStartOffset(), selectionState.getEndOffset()],
  94. startOffset = _ref[0],
  95. endOffset = _ref[1];
  96. if (startOffset === endOffset) {
  97. // 未选中状态
  98. console.log(selectionState.getAnchorKey());
  99. } // 选中状态
  100. break;
  101. }
  102. return "not-handled";
  103. }
  104. }, {
  105. key: "_onBoldClick",
  106. value: function _onBoldClick() {
  107. this.onChange(Draft.Draft_15.toggleInlineStyle(this.state.editorState, 'BOLD'));
  108. }
  109. }, {
  110. key: "_onLinkClick",
  111. value: function _onLinkClick() {
  112. var editorState = this.state.editorState;
  113. var contentState = editorState.getCurrentContent();
  114. var selectionState = editorState.getSelection();
  115. var contentStateWithEntity = contentState.createEntity('LINK', 'MUTABLE', {
  116. url: 'http://www.zombo.com'
  117. });
  118. var entityKey = contentStateWithEntity.getLastCreatedEntityKey();
  119. var contentStateWithLink = Draft.Draft_14.applyEntity(contentStateWithEntity, selectionState, entityKey);
  120. var newEditorState = Draft.Draft_3.push(editorState, contentStateWithLink, 'apply-entity');
  121. this.onChange(newEditorState);
  122. }
  123. }, {
  124. key: "_onEmojiClick",
  125. value: function _onEmojiClick(e, emojiCode) {
  126. var editorState = this.state.editorState;
  127. var contentState = editorState.getCurrentContent();
  128. var selectionState = editorState.getSelection();
  129. var EMOJIEntity = contentState.createEntity('EMOJI', 'IMMUTABLE', {
  130. emojiCode: emojiCode
  131. });
  132. var entityKey = EMOJIEntity.getLastCreatedEntityKey();
  133. var ncsWithEntity = Draft.Draft_14.insertText(contentState, selectionState, 'e', undefined, entityKey);
  134. var newEditorState = Draft.Draft_3.push(editorState, ncsWithEntity, 'insert-characters'); // const newEditorState = AtomicBlockUtils.insertAtomicBlock(
  135. // editorState,
  136. // entityKey,
  137. // ' '
  138. // );
  139. this.onChange(newEditorState);
  140. }
  141. }, {
  142. key: "_onCheckRange",
  143. value: function _onCheckRange() {
  144. console.log(this.state.editorState.getCurrentContent());
  145. console.log(Draft.Draft_20(this.state.editorState.getCurrentContent()));
  146. }
  147. }, {
  148. key: "defaultBlockStyleFn",
  149. value: function defaultBlockStyleFn(contentBlock) {
  150. var type = contentBlock.getType();
  151. return "";
  152. }
  153. }, {
  154. key: "defaultBlockRenderFn",
  155. value: function defaultBlockRenderFn(contentBlock) {
  156. var type = contentBlock.getType();
  157. return contentBlock;
  158. }
  159. }, {
  160. key: "render",
  161. value: function render() {
  162. var _this2 = this;
  163. var editorState = this.state.editorState;
  164. return React__default.createElement("div", {
  165. className: styles.wrapper
  166. }, React__default.createElement("div", {
  167. className: styles.btnListWrapper
  168. }, React__default.createElement("button", {
  169. onClick: this._onBoldClick.bind(this)
  170. }, "Bold"), React__default.createElement("button", {
  171. onClick: this._onCheckRange.bind(this)
  172. }, "Check"), React__default.createElement("button", {
  173. onClick: this._onLinkClick.bind(this)
  174. }, "Link"), React__default.createElement("button", {
  175. onClick: function onClick(e) {
  176. return _this2._onEmojiClick(e, '0011');
  177. }
  178. }, "Emoji")), React__default.createElement("div", {
  179. className: styles.editorWrapper
  180. }, React__default.createElement(Draft.Draft_1, {
  181. editorState: editorState,
  182. blockStyleFn: this.defaultBlockStyleFn,
  183. blockRendererFn: this.defaultBlockRenderFn,
  184. handleKeyCommand: this.handleKeyCommand,
  185. onChange: this.onChange
  186. })));
  187. }
  188. }]);
  189. return RichTextEditor;
  190. }(React__default.Component);
  191. exports.default = RichTextEditor;
  192. //# sourceMappingURL=index.js.map