No Description

index.js 7.6KB

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