No Description

index.js 7.7KB

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