|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import AceEditor from 'react-ace'; |
| 3 | + |
| 4 | +import 'brace/mode/html'; |
| 5 | +import 'brace/mode/markdown'; |
| 6 | +import 'brace/theme/github'; |
| 7 | +import 'brace/theme/chrome'; |
| 8 | + |
| 9 | +const styles = require('./CustomInput.scss'); |
| 10 | + |
| 11 | +// editorType is what sort of editor. All are ACE Editor |
| 12 | +// modes except 0, which is text input |
| 13 | + |
| 14 | +// ACE editor mode names |
| 15 | +const EDITORTYPES = [ |
| 16 | + 'normal', // must be first |
| 17 | + 'text', |
| 18 | + 'markdown', |
| 19 | + 'html', |
| 20 | +]; |
| 21 | + |
| 22 | +// human readable editor names |
| 23 | +const EDITORTYPENAMES = [ |
| 24 | + 'single line input', |
| 25 | + 'multi-line text input', |
| 26 | + 'markdown', |
| 27 | + 'html', |
| 28 | +]; |
| 29 | + |
| 30 | +// short human readable editor names |
| 31 | +// for the visible label |
| 32 | +const SHORTEDITORTYPENAMES = ['', 'multi-line', 'markdown', 'html']; |
| 33 | + |
| 34 | +const NORMALKEY = 0; |
| 35 | + |
| 36 | +const createInitialState = data => { |
| 37 | + const initialState = { |
| 38 | + editorType: NORMALKEY, |
| 39 | + data: data, |
| 40 | + }; |
| 41 | + return initialState; |
| 42 | +}; |
| 43 | + |
| 44 | +const TextInput = props => { |
| 45 | + const { standardProps, placeholderProp } = props; |
| 46 | + const { defaultValue, onChange } = standardProps; |
| 47 | + const allProps = { ...standardProps }; |
| 48 | + delete allProps.defaultValue; |
| 49 | + const [state, updateState] = useState(createInitialState(defaultValue)); |
| 50 | + const { editorType, data } = state; |
| 51 | + |
| 52 | + const updateData = (newData, currentState) => { |
| 53 | + return { |
| 54 | + ...currentState, |
| 55 | + data: newData, |
| 56 | + }; |
| 57 | + }; |
| 58 | + |
| 59 | + const cycleEditorType = currentState => { |
| 60 | + const nextEditorType = (currentState.editorType + 1) % EDITORTYPES.length; |
| 61 | + |
| 62 | + return { |
| 63 | + ...currentState, |
| 64 | + editorType: nextEditorType, |
| 65 | + }; |
| 66 | + }; |
| 67 | + |
| 68 | + const handleKeyUpEvent = e => { |
| 69 | + if ((e.ctrlKey || event.metaKey) && e.which === 32) { |
| 70 | + updateState(cycleEditorType); |
| 71 | + } |
| 72 | + }; |
| 73 | + |
| 74 | + const handleEditorExec = () => { |
| 75 | + updateState(cycleEditorType); |
| 76 | + }; |
| 77 | + |
| 78 | + const handleInputChangeAndPropagate = e => { |
| 79 | + const val = e.target.value; |
| 80 | + updateState(currentState => updateData(val, currentState)); |
| 81 | + if (onChange) { |
| 82 | + onChange(e); |
| 83 | + } |
| 84 | + }; |
| 85 | + |
| 86 | + const handleTextAreaChangeAndPropagate = (value, e) => { |
| 87 | + const val = value; |
| 88 | + updateState(currentState => updateData(val, currentState)); |
| 89 | + if (onChange) { |
| 90 | + onChange(e, value); |
| 91 | + } |
| 92 | + }; |
| 93 | + |
| 94 | + const getAceEditor = curmode => { |
| 95 | + return ( |
| 96 | + <AceEditor |
| 97 | + key="ace_text_editor" |
| 98 | + {...allProps} |
| 99 | + mode={curmode} |
| 100 | + theme="chrome" |
| 101 | + name="texttoggler" |
| 102 | + minLines={10} |
| 103 | + maxLines={100} |
| 104 | + width="100%" |
| 105 | + value={data} |
| 106 | + showPrintMargin={false} |
| 107 | + onChange={handleTextAreaChangeAndPropagate} |
| 108 | + showGutter={false} |
| 109 | + focus |
| 110 | + commands={[ |
| 111 | + { |
| 112 | + name: 'toggleEditor', |
| 113 | + bindKey: { win: 'Ctrl-Space', mac: 'Command-Space' }, |
| 114 | + exec: handleEditorExec, |
| 115 | + }, |
| 116 | + ]} |
| 117 | + /> |
| 118 | + ); |
| 119 | + }; |
| 120 | + |
| 121 | + const getNormalEditor = () => { |
| 122 | + return ( |
| 123 | + <input |
| 124 | + key="input_text_editor" |
| 125 | + {...allProps} |
| 126 | + placeholder={placeholderProp} |
| 127 | + value={data} |
| 128 | + onChange={handleInputChangeAndPropagate} |
| 129 | + onKeyUp={handleKeyUpEvent} |
| 130 | + className={allProps.className + ' ' + styles.normalInput} |
| 131 | + /> |
| 132 | + ); |
| 133 | + }; |
| 134 | + |
| 135 | + const editor = |
| 136 | + editorType === NORMALKEY |
| 137 | + ? getNormalEditor() |
| 138 | + : getAceEditor(EDITORTYPES[editorType]); |
| 139 | + |
| 140 | + return ( |
| 141 | + <span className="text_input_editor"> |
| 142 | + <label>{editor}</label> |
| 143 | + <span |
| 144 | + onClick={() => updateState(cycleEditorType)} |
| 145 | + title={ |
| 146 | + 'Change to ' + |
| 147 | + EDITORTYPENAMES[(editorType + 1) % EDITORTYPES.length] + |
| 148 | + ' (Ctrl + Space)' |
| 149 | + } |
| 150 | + > |
| 151 | + <span className={styles.modeType}> |
| 152 | + {SHORTEDITORTYPENAMES[editorType]} |
| 153 | + </span> |
| 154 | + <i |
| 155 | + key="icon_text_editor" |
| 156 | + className={ |
| 157 | + 'fa ' + |
| 158 | + styles.modeToggleButton + |
| 159 | + (editorType === NORMALKEY ? ' fa-expand' : ' fa-chevron-right') |
| 160 | + } |
| 161 | + /> |
| 162 | + </span> |
| 163 | + </span> |
| 164 | + ); |
| 165 | +}; |
| 166 | +export default TextInput; |
0 commit comments