暫無描述

index.js 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import { a as _inherits, b as _createClass, c as _classCallCheck, d as _possibleConstructorReturn, e as _getPrototypeOf, i as _assertThisInitialized } from '../_rollupPluginBabelHelpers-22a1b8cb.js';
  2. import React from 'react';
  3. import 'react-dom';
  4. import { s as styleInject } from '../style-inject.es-1f59c1d0.js';
  5. import '../_commonjsHelpers-97e6d7b1.js';
  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-eeda2ee2.js';
  7. import '../_react-dom_commonjs-external-d3012e04.js';
  8. var css = ".RichTextEditor_wrapper__A033L .RichTextEditor_btnListWrapper__3iF9d {\n display: flex;\n}\n.RichTextEditor_wrapper__A033L .RichTextEditor_editorWrapper__PzQ_1 {\n border: 1px solid #ccc;\n}\n";
  9. var styles = {
  10. "wrapper": "RichTextEditor_wrapper__A033L",
  11. "btnListWrapper": "RichTextEditor_btnListWrapper__3iF9d",
  12. "editorWrapper": "RichTextEditor_editorWrapper__PzQ_1"
  13. };
  14. styleInject(css);
  15. var Link = function Link(props) {
  16. var _props$contentState$g = props.contentState.getEntity(props.entityKey).getData(),
  17. url = _props$contentState$g.url;
  18. return React.createElement("a", {
  19. href: url
  20. }, props.children);
  21. };
  22. var decorator = new Draft_4([{
  23. strategy: function strategy(contentBlock, callback, contentState) {
  24. contentBlock.findEntityRanges(function (character) {
  25. var entityKey = character.getEntity();
  26. return entityKey !== null && contentState.getEntity(entityKey).getType() === 'LINK';
  27. }, function () {
  28. callback.apply(void 0, arguments);
  29. });
  30. },
  31. component: Link
  32. }, {
  33. strategy: function strategy(contentBlock, callback, contentState) {
  34. contentBlock.findEntityRanges(function (character) {
  35. var entityKey = character.getEntity();
  36. return entityKey !== null && contentState.getEntity(entityKey).getType() === 'EMOJI';
  37. }, function () {
  38. callback.apply(void 0, arguments);
  39. });
  40. },
  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. _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. console.log(selectionState.getAnchorKey());
  93. }
  94. break;
  95. }
  96. return "not-handled";
  97. }
  98. }, {
  99. key: "_onBoldClick",
  100. value: function _onBoldClick() {
  101. this.onChange(Draft_15.toggleInlineStyle(this.state.editorState, 'BOLD'));
  102. }
  103. }, {
  104. key: "_onLinkClick",
  105. value: function _onLinkClick() {
  106. var editorState = this.state.editorState;
  107. var contentState = editorState.getCurrentContent();
  108. var selectionState = editorState.getSelection();
  109. var contentStateWithEntity = contentState.createEntity('LINK', 'MUTABLE', {
  110. url: 'http://www.zombo.com'
  111. });
  112. var entityKey = contentStateWithEntity.getLastCreatedEntityKey();
  113. var contentStateWithLink = Draft_14.applyEntity(contentStateWithEntity, selectionState, entityKey);
  114. var newEditorState = Draft_3.push(editorState, contentStateWithLink, 'apply-entity');
  115. this.onChange(newEditorState);
  116. }
  117. }, {
  118. key: "_onEmojiClick",
  119. value: function _onEmojiClick(e, emojiCode) {
  120. var editorState = this.state.editorState;
  121. var contentState = editorState.getCurrentContent();
  122. var selectionState = editorState.getSelection();
  123. var EMOJIEntity = contentState.createEntity('EMOJI', 'IMMUTABLE', {
  124. emojiCode: emojiCode
  125. });
  126. var entityKey = EMOJIEntity.getLastCreatedEntityKey();
  127. var ncsWithEntity = Draft_14.insertText(contentState, selectionState, 'e', undefined, entityKey);
  128. var newEditorState = Draft_3.push(editorState, ncsWithEntity, 'insert-characters');
  129. this.onChange(newEditorState);
  130. }
  131. }, {
  132. key: "_onCheckRange",
  133. value: function _onCheckRange() {
  134. console.log(this.state.editorState.getCurrentContent());
  135. console.log(Draft_20(this.state.editorState.getCurrentContent()));
  136. }
  137. }, {
  138. key: "defaultBlockStyleFn",
  139. value: function defaultBlockStyleFn(contentBlock) {
  140. var type = contentBlock.getType();
  141. return "";
  142. }
  143. }, {
  144. key: "defaultBlockRenderFn",
  145. value: function defaultBlockRenderFn(contentBlock) {
  146. var type = contentBlock.getType();
  147. return contentBlock;
  148. }
  149. }, {
  150. key: "render",
  151. value: function render() {
  152. var _this2 = this;
  153. var editorState = this.state.editorState;
  154. return React.createElement("div", {
  155. className: styles.wrapper
  156. }, React.createElement("div", {
  157. className: styles.btnListWrapper
  158. }, React.createElement("button", {
  159. onClick: this._onBoldClick.bind(this)
  160. }, "Bold"), React.createElement("button", {
  161. onClick: this._onCheckRange.bind(this)
  162. }, "Check"), React.createElement("button", {
  163. onClick: this._onLinkClick.bind(this)
  164. }, "Link"), React.createElement("button", {
  165. onClick: function onClick(e) {
  166. return _this2._onEmojiClick(e, '0011');
  167. }
  168. }, "Emoji")), React.createElement("div", {
  169. className: styles.editorWrapper
  170. }, React.createElement(Draft_1, {
  171. editorState: editorState,
  172. blockStyleFn: this.defaultBlockStyleFn,
  173. blockRendererFn: this.defaultBlockRenderFn,
  174. handleKeyCommand: this.handleKeyCommand,
  175. onChange: this.onChange
  176. })));
  177. }
  178. }]);
  179. return RichTextEditor;
  180. }(React.Component);
  181. export default RichTextEditor;
  182. //# sourceMappingURL=index.js.map