|
| 1 | +import React, {Component} from 'react'; |
| 2 | +import {connect} from 'react-redux'; |
| 3 | +import {EditorState, convertToRaw, convertFromRaw} from 'draft-js'; |
| 4 | +import { Editor } from 'draft-js'; |
| 5 | +import {getDefaultKeyBinding, KeyBindingUtil} from 'draft-js'; |
| 6 | +import createAutoListPlugin from 'draft-js-autolist-plugin' |
| 7 | +import createMarkdownPlugin from 'draft-js-markdown-plugin'; |
| 8 | +import {markdownToDraft, draftToMarkdown} from 'markdown-draft-js'; |
| 9 | +import ReactMarkdown from 'react-markdown'; |
| 10 | +import './Wysiwyg.css'; |
| 11 | + |
| 12 | +const {hasCommandModifier} = KeyBindingUtil; |
| 13 | + |
| 14 | +function myKeyBindingFn(e) { |
| 15 | + if (e.keyCode === 13 /* `enter` key */ && hasCommandModifier(e)) { |
| 16 | + return 'myeditor-save'; |
| 17 | + } |
| 18 | + return getDefaultKeyBinding(e); |
| 19 | +} |
| 20 | + |
| 21 | +const autoListPlugin = createAutoListPlugin(); |
| 22 | + |
| 23 | +const plugins = [ |
| 24 | + autoListPlugin, |
| 25 | + createMarkdownPlugin() |
| 26 | +]; |
| 27 | + |
| 28 | +class Wysiwyg extends Component { |
| 29 | + constructor(props) { |
| 30 | + super(props); |
| 31 | + const content = this.calculateContent(); |
| 32 | + |
| 33 | + this.state = { |
| 34 | + editMode: false, |
| 35 | + editorState: EditorState.createWithContent(convertFromRaw(content)) |
| 36 | + }; |
| 37 | + } |
| 38 | + |
| 39 | + calculateContent = () => { |
| 40 | + const {tasks: { taskMap }, taskGid, attributeName} = this.props, |
| 41 | + attributeValue = taskMap[taskGid][attributeName], |
| 42 | + content = attributeValue ? markdownToDraft(attributeValue, { |
| 43 | + remarkableOptions: { |
| 44 | + html: false, |
| 45 | + preserveNewlines: true |
| 46 | + } |
| 47 | + }) : markdownToDraft(''); |
| 48 | + |
| 49 | + return content; |
| 50 | + } |
| 51 | + |
| 52 | + editModeEnable = () => { |
| 53 | + const content = this.calculateContent(); |
| 54 | + |
| 55 | + this.setState({ |
| 56 | + editMode: true, |
| 57 | + editorState: EditorState.moveFocusToEnd( |
| 58 | + EditorState.createWithContent(convertFromRaw(content)) |
| 59 | + ) |
| 60 | + }); |
| 61 | + }; |
| 62 | + |
| 63 | + onChange = (editorState) => { |
| 64 | + this.setState({ |
| 65 | + editorState |
| 66 | + }); |
| 67 | + }; |
| 68 | + |
| 69 | + save = () => { |
| 70 | + const {patchTask} = this.props, |
| 71 | + markdown = draftToMarkdown(convertToRaw(this.state.editorState.getCurrentContent())), |
| 72 | + payload = {gid: this.props.taskGid}; |
| 73 | + |
| 74 | + payload[this.props.attributeName] = markdown; |
| 75 | + |
| 76 | + patchTask(payload); |
| 77 | + |
| 78 | + this.setState({ |
| 79 | + editMode: false |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + editModeFalse = () => { |
| 84 | + this.setState({ |
| 85 | + editMode: false |
| 86 | + }); |
| 87 | + } |
| 88 | + |
| 89 | + handleKeyCommand = (command) => { |
| 90 | + if (command === 'myeditor-save') { |
| 91 | + this.save(); |
| 92 | + return 'handled'; |
| 93 | + } |
| 94 | + return 'not-handled'; |
| 95 | + } |
| 96 | + |
| 97 | + render() { |
| 98 | + const {tasks: {taskMap}} = this.props, |
| 99 | + attributeValue = taskMap[this.props.taskGid][this.props.attributeName]; |
| 100 | + |
| 101 | + return ( |
| 102 | + <div> |
| 103 | + <h4> |
| 104 | + <strong>Description / Deliverables</strong> |
| 105 | + {this.state.editMode && <button className='wysiwyg-save' onClick={this.save}>Save</button>} |
| 106 | + {!this.state.editMode && <button className='wysiwyg-edit' onClick={this.editModeEnable}>Edit</button>} |
| 107 | + </h4> |
| 108 | + { |
| 109 | + this.state.editMode && ( |
| 110 | + <div className='wysiwyg'> |
| 111 | + <Editor |
| 112 | + editorState={this.state.editorState} |
| 113 | + onChange={this.onChange} |
| 114 | + onEscape={this.editModeFalse} |
| 115 | + plugins={plugins} |
| 116 | + handleKeyCommand={this.handleKeyCommand} |
| 117 | + keyBindingFn={myKeyBindingFn} |
| 118 | + /> |
| 119 | + </div> |
| 120 | + ) |
| 121 | + } |
| 122 | + { |
| 123 | + !this.state.editMode && ( |
| 124 | + <div> |
| 125 | + <ReactMarkdown |
| 126 | + source={attributeValue} |
| 127 | + render={{Link: props => { |
| 128 | + if (props.href.startsWith('/')) { |
| 129 | + return <a href={props.href}>{props.children}</a>; |
| 130 | + } |
| 131 | + // If link to external site, open in new tab |
| 132 | + return <a href={props.href} target="_blank">{props.children}</a>; |
| 133 | + }}} /> |
| 134 | + </div> |
| 135 | + ) |
| 136 | + } |
| 137 | + </div> |
| 138 | + ); |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +export default connect(({tasks}) => ({tasks}), null)(Wysiwyg); |
0 commit comments