|
| 1 | +import React, { PureComponent } from "react"; |
| 2 | +import { Map } from "immutable"; |
| 3 | +import { EditorState, EditorBlock, Modifier } from "draft-js"; |
| 4 | +import enhanceWithClickOutside from "react-click-outside"; |
| 5 | + |
| 6 | +const alias = { |
| 7 | + javascript: "js", |
| 8 | + jsx: "js", |
| 9 | +}; |
| 10 | + |
| 11 | +const CodeSwitchContainer = enhanceWithClickOutside( |
| 12 | + class SwitchContainer extends PureComponent { |
| 13 | + handleClickOutside() { |
| 14 | + this.props.onClickOutside(); |
| 15 | + } |
| 16 | + |
| 17 | + render() { |
| 18 | + return ( |
| 19 | + <div contentEditable={false} onClick={this.props.onClick}> |
| 20 | + {this.props.children} |
| 21 | + </div> |
| 22 | + ); |
| 23 | + } |
| 24 | + } |
| 25 | +); |
| 26 | + |
| 27 | +class CodeBlock extends PureComponent { |
| 28 | + state = { |
| 29 | + isOpen: false, |
| 30 | + }; |
| 31 | + |
| 32 | + onChange = ev => { |
| 33 | + ev.preventDefault(); |
| 34 | + ev.stopPropagation(); |
| 35 | + const blockKey = this.props.block.getKey(); |
| 36 | + const { |
| 37 | + getEditorState, |
| 38 | + setReadOnly, |
| 39 | + setEditorState, |
| 40 | + } = this.props.blockProps; |
| 41 | + |
| 42 | + const editorState = getEditorState(); |
| 43 | + const selection = editorState.getSelection(); |
| 44 | + const language = ev.currentTarget.value; |
| 45 | + const blockSelection = selection.merge({ |
| 46 | + anchorKey: blockKey, |
| 47 | + focusKey: blockKey, |
| 48 | + }); |
| 49 | + |
| 50 | + let content = editorState.getCurrentContent(); |
| 51 | + content = Modifier.mergeBlockData( |
| 52 | + content, |
| 53 | + blockSelection, |
| 54 | + Map({ language }) |
| 55 | + ); |
| 56 | + setReadOnly(false); |
| 57 | + |
| 58 | + const newEditorState = EditorState.push( |
| 59 | + editorState, |
| 60 | + content, |
| 61 | + "change-block-data" |
| 62 | + ); |
| 63 | + |
| 64 | + setEditorState(EditorState.forceSelection(newEditorState, selection)); |
| 65 | + }; |
| 66 | + |
| 67 | + cancelClicks = event => event.preventDefault(); |
| 68 | + |
| 69 | + onSelectClick = event => { |
| 70 | + const { setReadOnly } = this.props.blockProps; |
| 71 | + event.stopPropagation(); |
| 72 | + setReadOnly(true); |
| 73 | + }; |
| 74 | + |
| 75 | + onClickOutside = () => { |
| 76 | + const { |
| 77 | + getEditorState, |
| 78 | + setReadOnly, |
| 79 | + setEditorState, |
| 80 | + } = this.props.blockProps; |
| 81 | + |
| 82 | + setReadOnly(false); |
| 83 | + |
| 84 | + const editorState = getEditorState(); |
| 85 | + const selection = editorState.getSelection(); |
| 86 | + |
| 87 | + setEditorState(EditorState.forceSelection(editorState, selection)); |
| 88 | + }; |
| 89 | + |
| 90 | + render() { |
| 91 | + const { |
| 92 | + languages, |
| 93 | + renderLanguageSelect, |
| 94 | + language: _language, |
| 95 | + } = this.props.blockProps; |
| 96 | + |
| 97 | + const language = alias[_language] || _language; |
| 98 | + const selectedLabel = languages[language]; |
| 99 | + const selectedValue = language; |
| 100 | + |
| 101 | + const options = Object.keys(languages).reduce( |
| 102 | + (acc, val) => [ |
| 103 | + ...acc, |
| 104 | + { |
| 105 | + label: languages[val], |
| 106 | + value: val, |
| 107 | + }, |
| 108 | + ], |
| 109 | + [] |
| 110 | + ); |
| 111 | + |
| 112 | + return ( |
| 113 | + <div> |
| 114 | + <EditorBlock {...this.props} /> |
| 115 | + <CodeSwitchContainer |
| 116 | + onClickOutside={this.onClickOutside} |
| 117 | + onClick={this.onSelectClick} |
| 118 | + > |
| 119 | + {renderLanguageSelect({ |
| 120 | + selectedLabel, |
| 121 | + selectedValue, |
| 122 | + onChange: this.onChange, |
| 123 | + options, |
| 124 | + })} |
| 125 | + </CodeSwitchContainer> |
| 126 | + </div> |
| 127 | + ); |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +export default CodeBlock; |
0 commit comments