Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.67 KB | None | 0 0
  1. class RichEditor extends React.Component {
  2. constructor(props) {
  3. super(props);
  4. this.state = {editorState: EditorState.createEmpty()};
  5.  
  6. this.focus = () => this.refs.editor.focus();
  7. this.onChange = (editorState) => {
  8. this.setState({editorState});
  9. }
  10.  
  11. this.handleKeyCommand = (command) => this._handleKeyCommand(command);
  12. this.onTab = (e) => this._onTab(e);
  13. this.toggleBlockType = (type) => this._toggleBlockType(type);
  14. this.toggleInlineStyle = (style) => this._toggleInlineStyle(style);
  15. }
  16.  
  17. _handleKeyCommand(command) {
  18. const {editorState} = this.state;
  19. const newState = RichUtils.handleKeyCommand(editorState, command);
  20.  
  21. if (newState) {
  22. this.onChange(newState);
  23. return true;
  24. }
  25. return false;
  26. }
  27.  
  28. _onTab(e) {
  29. const maxDepth = 4;
  30. this.onChange(RichUtils.onTab(e, this.state.editorState, maxDepth));
  31. }
  32.  
  33. _toggleBlockType(blockType) {
  34. this.onChange(
  35. RichUtils.toggleBlockType(
  36. this.state.editorState,
  37. blockType
  38. )
  39. );
  40. }
  41.  
  42. _toggleInlineStyle(inlineStyle) {
  43. this.onChange(
  44. RichUtils.toggleInlineStyle(
  45. this.state.editorState,
  46. inlineStyle
  47. )
  48. );
  49. }
  50.  
  51. render() {
  52. const { editorState } = this.state;
  53.  
  54. let className = 'RichEditor-editor';
  55. var contentState = editorState.getCurrentContent();
  56.  
  57. if (!contentState.hasText()) {
  58. if (contentState.getBlockMap().first().getType() !== 'unstyled') {
  59. className += ' RichEditor-hidePlaceholder';
  60. }
  61. }
  62.  
  63. return (
  64. <div className="RichEditor-root">
  65. <BlockStyleControls
  66. editorState={editorState}
  67. onToggle={this.toggleBlockType}
  68. />
  69. <InlineStyleControls
  70. editorState={editorState}
  71. onToggle={this.toggleInlineStyle}
  72. />
  73. <div className={className} onClick={this.focus}>
  74. <Editor
  75. blockStyleFn={getBlockStyle}
  76. customStyleMap={styleMap}
  77. editorState={editorState}
  78. handleKeyCommand={this.handleKeyCommand}
  79. onChange={this.onChange}
  80. onTab={this.onTab}
  81. placeholder=""
  82. ref="editor"
  83. spellCheck={true}
  84. />
  85. </div>
  86. </div>
  87. );
  88. }
  89. }
  90.  
  91. module.exports = RichEditor
  92.  
  93. class WrapperComponent extends React.Component {
  94.  
  95. constructor(props) {
  96. super(props);
  97. this.onChange = (editorState2) => {
  98. this.setState({editorState2});
  99. console.log("onChange");
  100. console.log(editorState2);
  101. }
  102. this.state = {
  103. editorState1: EditorState.createEmpty(),
  104. editorState2: EditorState.createEmpty(),
  105. html: null
  106. }
  107. }
  108.  
  109. update() {
  110. console.log("update");
  111. console.log(this.state.editorState2);
  112. console.log(convertToRaw(this.state.editorState2.getCurrentContent()));
  113. //this.setState({ draftEditor2: e.value });
  114. this.setState({ html: stateToHTML(this.state.editorState2.getCurrentContent()) });
  115. }
  116.  
  117. render () {
  118.  
  119. const Editable = () => (
  120. <div className="editor">
  121. <div className="editor-inner">
  122. <h3>Redigerar: Anbudsbrev</h3>
  123. <h4>Rubrik</h4>
  124. <input type="text" name="title" />
  125. <h4>Text 1</h4>
  126. <RichEditor editorState={this.state.editorState1} updateStateToParent={this.onChange} name="text01" ref="editor" />
  127. <h4>Citat</h4>
  128. <input type="text" name="quote01" />
  129. <h4>Text 2</h4>
  130. <RichEditor updateStateToParent={this.onChange} name="text02" ref="editor" />
  131. <EditorFooter {...this.props} submitForm={this.submitForm} />
  132. <button onClick={this.update.bind(this)}>Update</button>
  133. <div>{this.state.html}</div>
  134. </div>
  135. </div>
  136. );
  137.  
  138. const Readable = () => (
  139. <div>
  140. <h1 className="header66">{this.props.title}</h1>
  141. <div className="text66">{this.props.text01}</div>
  142. <div className="quote100">{this.props.quote01}</div>
  143. <div className="text66">{this.props.text02}</div>
  144. </div>
  145. );
  146.  
  147. return (
  148. <div>
  149. { this.props.isInEditMode ? <Editable /> : <Readable /> }
  150. </div>
  151. );
  152. }
  153. };
  154.  
  155. WrapperComponent.defaultProps = {
  156. height: 100,
  157. title: "Lorem ipsum dolor sit amet",
  158. text01: "Mauris sollicitudin purus accumsan libero fringilla, vitae vulputate lorem aliquam. Nunc ipsum nisl, consectetur ac ultrices ac, pretium vitae lectus. Cras vestibulum, arcu non condimentum hendrerit, dolor ante molestie ante, eget dapibus felis quam a ante. Nunc fringilla risus eget nunc tincidunt sodales.",
  159. quote01: "Aliquam erat volutpat.",
  160. text02: "Praesent non erat quis sem mollis sodales. Integer convallis metus in ligula vehicula, quis vulputate lectus accumsan. Aliquam luctus posuere mollis. Aliquam luctus dignissim quam, ut aliquet ligula pellentesque ac. Nulla sodales lacus sem, eu pharetra arcu aliquet ac. Sed in venenatis libero."
  161. };
  162.  
  163. WrapperComponent.propTypes = {
  164. content: PropTypes.object,
  165. itemId: PropTypes.string
  166. }
  167.  
  168. module.exports = WrapperComponent
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement