|
| 1 | +import CodeMirror from 'codemirror'; |
| 2 | +import 'codemirror/mode/css/css'; |
| 3 | +import 'codemirror/mode/clike/clike'; |
| 4 | +import 'codemirror/addon/selection/active-line'; |
| 5 | +import 'codemirror/addon/lint/lint'; |
| 6 | +import 'codemirror/addon/lint/javascript-lint'; |
| 7 | +import 'codemirror/addon/lint/css-lint'; |
| 8 | +import 'codemirror/addon/lint/html-lint'; |
| 9 | +import 'codemirror/addon/fold/brace-fold'; |
| 10 | +import 'codemirror/addon/fold/comment-fold'; |
| 11 | +import 'codemirror/addon/fold/foldcode'; |
| 12 | +import 'codemirror/addon/fold/foldgutter'; |
| 13 | +import 'codemirror/addon/fold/indent-fold'; |
| 14 | +import 'codemirror/addon/fold/xml-fold'; |
| 15 | +import 'codemirror/addon/comment/comment'; |
| 16 | +import 'codemirror/keymap/sublime'; |
| 17 | +import 'codemirror/addon/search/searchcursor'; |
| 18 | +import 'codemirror/addon/search/matchesonscrollbar'; |
| 19 | +import 'codemirror/addon/search/match-highlighter'; |
| 20 | +import 'codemirror/addon/search/jump-to-line'; |
| 21 | +import 'codemirror/addon/edit/matchbrackets'; |
| 22 | +import 'codemirror/addon/edit/closebrackets'; |
| 23 | +import 'codemirror/addon/selection/mark-selection'; |
| 24 | +import 'codemirror-colorpicker'; |
| 25 | + |
| 26 | +import { debounce } from 'lodash'; |
| 27 | +import emmet from '@emmetio/codemirror-plugin'; |
| 28 | + |
| 29 | +import { metaKey } from '../../../../utils/metaKey'; |
| 30 | +import { showHint } from './hinter'; |
| 31 | +import tidyCode from './tidier'; |
| 32 | + |
| 33 | +const INDENTATION_AMOUNT = 2; |
| 34 | + |
| 35 | +emmet(CodeMirror); |
| 36 | + |
| 37 | +function setupCodeMirrorHooks( |
| 38 | + cmInstance, |
| 39 | + { |
| 40 | + setUnsavedChanges, |
| 41 | + hideRuntimeErrorWarning, |
| 42 | + updateFileContent, |
| 43 | + file, |
| 44 | + autorefresh, |
| 45 | + isPlaying, |
| 46 | + clearConsole, |
| 47 | + startSketch, |
| 48 | + autocompleteHinter, |
| 49 | + fontSize |
| 50 | + }, |
| 51 | + updateLineNumber |
| 52 | +) { |
| 53 | + cmInstance.on( |
| 54 | + 'change', |
| 55 | + debounce(() => { |
| 56 | + setUnsavedChanges(true); |
| 57 | + hideRuntimeErrorWarning(); |
| 58 | + updateFileContent(file.id, cmInstance.getValue()); |
| 59 | + if (autorefresh && isPlaying) { |
| 60 | + clearConsole(); |
| 61 | + startSketch(); |
| 62 | + } |
| 63 | + }, 1000) |
| 64 | + ); |
| 65 | + |
| 66 | + cmInstance.on('keyup', () => { |
| 67 | + const lineNumber = parseInt(cmInstance.getCursor().line + 1, 10); |
| 68 | + updateLineNumber(lineNumber); |
| 69 | + }); |
| 70 | + |
| 71 | + cmInstance.on('keydown', (_cm, e) => { |
| 72 | + // Show hint |
| 73 | + const mode = cmInstance.getOption('mode'); |
| 74 | + if (/^[a-z]$/i.test(e.key) && (mode === 'css' || mode === 'javascript')) { |
| 75 | + showHint(_cm, autocompleteHinter, fontSize); |
| 76 | + } |
| 77 | + if (e.key === 'Escape') { |
| 78 | + e.preventDefault(); |
| 79 | + const selections = cmInstance.listSelections(); |
| 80 | + |
| 81 | + if (selections.length > 1) { |
| 82 | + const firstPos = selections[0].head || selections[0].anchor; |
| 83 | + cmInstance.setSelection(firstPos); |
| 84 | + cmInstance.scrollIntoView(firstPos); |
| 85 | + } else { |
| 86 | + cmInstance.getInputField().blur(); |
| 87 | + } |
| 88 | + } |
| 89 | + }); |
| 90 | + |
| 91 | + cmInstance.getWrapperElement().style['font-size'] = `${fontSize}px`; |
| 92 | +} |
| 93 | + |
| 94 | +export default function setupCodeMirror( |
| 95 | + container, |
| 96 | + props, |
| 97 | + onUpdateLinting, |
| 98 | + docs, |
| 99 | + updateLineNumber |
| 100 | +) { |
| 101 | + const { theme, lineNumbers, linewrap, autocloseBracketsQuotes } = props; |
| 102 | + const cm = CodeMirror(container, { |
| 103 | + theme: `p5-${theme}`, |
| 104 | + lineNumbers, |
| 105 | + styleActiveLine: true, |
| 106 | + inputStyle: 'contenteditable', |
| 107 | + lineWrapping: linewrap, |
| 108 | + fixedGutter: false, |
| 109 | + foldGutter: true, |
| 110 | + foldOptions: { widget: '\u2026' }, |
| 111 | + gutters: ['CodeMirror-foldgutter', 'CodeMirror-lint-markers'], |
| 112 | + keyMap: 'sublime', |
| 113 | + highlightSelectionMatches: true, // highlight current search match |
| 114 | + matchBrackets: true, |
| 115 | + emmet: { |
| 116 | + preview: ['html'], |
| 117 | + markTagPairs: true, |
| 118 | + autoRenameTags: true |
| 119 | + }, |
| 120 | + autoCloseBrackets: autocloseBracketsQuotes, |
| 121 | + styleSelectedText: true, |
| 122 | + lint: { |
| 123 | + onUpdateLinting, |
| 124 | + options: { |
| 125 | + asi: true, |
| 126 | + eqeqeq: false, |
| 127 | + '-W041': false, |
| 128 | + esversion: 11 |
| 129 | + } |
| 130 | + }, |
| 131 | + colorpicker: { |
| 132 | + type: 'sketch', |
| 133 | + mode: 'edit' |
| 134 | + } |
| 135 | + }); |
| 136 | + |
| 137 | + delete cm.options.lint.options.errors; |
| 138 | + |
| 139 | + const replaceCommand = |
| 140 | + metaKey === 'Ctrl' ? `${metaKey}-H` : `${metaKey}-Option-F`; |
| 141 | + cm.setOption('extraKeys', { |
| 142 | + Tab: (tabCm) => { |
| 143 | + if (!tabCm.execCommand('emmetExpandAbbreviation')) return; |
| 144 | + // might need to specify and indent more? |
| 145 | + const selection = tabCm.doc.getSelection(); |
| 146 | + if (selection.length > 0) { |
| 147 | + tabCm.execCommand('indentMore'); |
| 148 | + } else { |
| 149 | + tabCm.replaceSelection(' '.repeat(INDENTATION_AMOUNT)); |
| 150 | + } |
| 151 | + }, |
| 152 | + Enter: 'emmetInsertLineBreak', |
| 153 | + Esc: 'emmetResetAbbreviation', |
| 154 | + [`Shift-Tab`]: false, |
| 155 | + [`${metaKey}-Enter`]: () => null, |
| 156 | + [`Shift-${metaKey}-Enter`]: () => null, |
| 157 | + [`${metaKey}-F`]: 'findPersistent', |
| 158 | + [`Shift-${metaKey}-F`]: () => tidyCode(cm), |
| 159 | + [`${metaKey}-G`]: 'findPersistentNext', |
| 160 | + [`Shift-${metaKey}-G`]: 'findPersistentPrev', |
| 161 | + [replaceCommand]: 'replace', |
| 162 | + // Cassie Tarakajian: If you don't set a default color, then when you |
| 163 | + // choose a color, it deletes characters inline. This is a |
| 164 | + // hack to prevent that. |
| 165 | + [`${metaKey}-K`]: (metaCm, event) => |
| 166 | + metaCm.state.colorpicker.popup_color_picker({ length: 0 }), |
| 167 | + [`${metaKey}-.`]: 'toggleComment' // Note: most adblockers use the shortcut ctrl+. |
| 168 | + }); |
| 169 | + |
| 170 | + setupCodeMirrorHooks(cm, props, updateLineNumber); |
| 171 | + |
| 172 | + cm.swapDoc(docs[props.file.id]); |
| 173 | + |
| 174 | + return cm; |
| 175 | +} |
0 commit comments