No Description

index.js 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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-0494a5d0.js');
  5. var React = require('react');
  6. var React__default = _interopDefault(React);
  7. require('react-dom');
  8. var styleInject_es = require('../style-inject.es-dcee06b6.js');
  9. require('../_commonjsHelpers-72d386ba.js');
  10. var Draft = require('../Draft-4ccef031.js');
  11. require('../_react-dom_commonjs-external-55867475.js');
  12. 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";
  13. var styles = {
  14. "wrapper": "RichTextEditor_wrapper__A033L",
  15. "btnListWrapper": "RichTextEditor_btnListWrapper__3iF9d",
  16. "editorWrapper": "RichTextEditor_editorWrapper__PzQ_1"
  17. };
  18. styleInject_es.styleInject(css);
  19. var Link = function Link(props) {
  20. var _props$contentState$g = props.contentState.getEntity(props.entityKey).getData(),
  21. url = _props$contentState$g.url;
  22. return React__default.createElement("a", {
  23. href: url
  24. }, props.children);
  25. };
  26. var decorator = new Draft.Draft_4([{
  27. strategy: function strategy(contentBlock, callback, contentState) {
  28. contentBlock.findEntityRanges(function (character) {
  29. var entityKey = character.getEntity();
  30. return entityKey !== null && contentState.getEntity(entityKey).getType() === 'LINK';
  31. }, function () {
  32. callback.apply(void 0, arguments);
  33. });
  34. },
  35. component: Link
  36. }, {
  37. strategy: function strategy(contentBlock, callback, contentState) {
  38. contentBlock.findEntityRanges(function (character) {
  39. var entityKey = character.getEntity();
  40. return entityKey !== null && contentState.getEntity(entityKey).getType() === 'EMOJI';
  41. }, function () {
  42. callback.apply(void 0, arguments);
  43. });
  44. },
  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. _this.state = {
  67. editorState: Draft.Draft_3.createEmpty(decorator)
  68. };
  69. _this.onChange = function (editorState) {
  70. return _this.setState({
  71. editorState: editorState
  72. });
  73. };
  74. _this.handleKeyCommand = _this.handleKeyCommand.bind(_rollupPluginBabelHelpers._assertThisInitialized(_this));
  75. _this.defaultBlockStyleFn = _this.defaultBlockStyleFn.bind(_rollupPluginBabelHelpers._assertThisInitialized(_this));
  76. return _this;
  77. }
  78. _rollupPluginBabelHelpers._createClass(RichTextEditor, [{
  79. key: "handleKeyCommand",
  80. value: function handleKeyCommand(command, editorState) {
  81. var newState = Draft.Draft_15.handleKeyCommand(editorState, command);
  82. console.log('command: ', command);
  83. console.log('newState: ', newState);
  84. if (newState) {
  85. this.onChange(newState);
  86. return "handled";
  87. }
  88. switch (command) {
  89. case 'backspace':
  90. var contentState = editorState.getCurrentContent();
  91. var selectionState = editorState.getSelection();
  92. var _ref = [selectionState.getStartOffset(), selectionState.getEndOffset()],
  93. startOffset = _ref[0],
  94. endOffset = _ref[1];
  95. if (startOffset === endOffset) {
  96. console.log(selectionState.getAnchorKey());
  97. }
  98. break;
  99. }
  100. return "not-handled";
  101. }
  102. }, {
  103. key: "_onBoldClick",
  104. value: function _onBoldClick() {
  105. this.onChange(Draft.Draft_15.toggleInlineStyle(this.state.editorState, 'BOLD'));
  106. }
  107. }, {
  108. key: "_onLinkClick",
  109. value: function _onLinkClick() {
  110. var editorState = this.state.editorState;
  111. var contentState = editorState.getCurrentContent();
  112. var selectionState = editorState.getSelection();
  113. var contentStateWithEntity = contentState.createEntity('LINK', 'MUTABLE', {
  114. url: 'http://www.zombo.com'
  115. });
  116. var entityKey = contentStateWithEntity.getLastCreatedEntityKey();
  117. var contentStateWithLink = Draft.Draft_14.applyEntity(contentStateWithEntity, selectionState, entityKey);
  118. var newEditorState = Draft.Draft_3.push(editorState, contentStateWithLink, 'apply-entity');
  119. this.onChange(newEditorState);
  120. }
  121. }, {
  122. key: "_onEmojiClick",
  123. value: function _onEmojiClick(e, emojiCode) {
  124. var editorState = this.state.editorState;
  125. var contentState = editorState.getCurrentContent();
  126. var selectionState = editorState.getSelection();
  127. var EMOJIEntity = contentState.createEntity('EMOJI', 'IMMUTABLE', {
  128. emojiCode: emojiCode
  129. });
  130. var entityKey = EMOJIEntity.getLastCreatedEntityKey();
  131. var ncsWithEntity = Draft.Draft_14.insertText(contentState, selectionState, 'e', undefined, entityKey);
  132. var newEditorState = Draft.Draft_3.push(editorState, ncsWithEntity, 'insert-characters');
  133. this.onChange(newEditorState);
  134. }
  135. }, {
  136. key: "_onCheckRange",
  137. value: function _onCheckRange() {
  138. console.log(this.state.editorState.getCurrentContent());
  139. console.log(Draft.Draft_20(this.state.editorState.getCurrentContent()));
  140. }
  141. }, {
  142. key: "defaultBlockStyleFn",
  143. value: function defaultBlockStyleFn(contentBlock) {
  144. var type = contentBlock.getType();
  145. return "";
  146. }
  147. }, {
  148. key: "defaultBlockRenderFn",
  149. value: function defaultBlockRenderFn(contentBlock) {
  150. var type = contentBlock.getType();
  151. return contentBlock;
  152. }
  153. }, {
  154. key: "render",
  155. value: function render() {
  156. var _this2 = this;
  157. var editorState = this.state.editorState;
  158. return React__default.createElement("div", {
  159. className: styles.wrapper
  160. }, React__default.createElement("div", {
  161. className: styles.btnListWrapper
  162. }, React__default.createElement("button", {
  163. onClick: this._onBoldClick.bind(this)
  164. }, "Bold"), React__default.createElement("button", {
  165. onClick: this._onCheckRange.bind(this)
  166. }, "Check"), React__default.createElement("button", {
  167. onClick: this._onLinkClick.bind(this)
  168. }, "Link"), React__default.createElement("button", {
  169. onClick: function onClick(e) {
  170. return _this2._onEmojiClick(e, '0011');
  171. }
  172. }, "Emoji")), React__default.createElement("div", {
  173. className: styles.editorWrapper
  174. }, React__default.createElement(Draft.Draft_1, {
  175. editorState: editorState,
  176. blockStyleFn: this.defaultBlockStyleFn,
  177. blockRendererFn: this.defaultBlockRenderFn,
  178. handleKeyCommand: this.handleKeyCommand,
  179. onChange: this.onChange
  180. })));
  181. }
  182. }]);
  183. return RichTextEditor;
  184. }(React__default.Component);
  185. exports.default = RichTextEditor;
  186. //# sourceMappingURL=index.js.map