CodeMirrorEditor.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import Modal from 'react-bootstrap/es/Modal';
  4. import AbstractEditor from './AbstractEditor';
  5. import urljoin from 'url-join';
  6. const loadScript = require('simple-load-script');
  7. const loadCssSync = require('load-css-file');
  8. import * as codemirror from 'codemirror';
  9. // set save handler
  10. codemirror.commands.save = (instance) => {
  11. if (instance.codeMirrorEditor != null) {
  12. instance.codeMirrorEditor.dispatchSave();
  13. }
  14. };
  15. // set CodeMirror instance as 'CodeMirror' so that CDN addons can reference
  16. window.CodeMirror = require('codemirror');
  17. import { UnControlled as ReactCodeMirror } from 'react-codemirror2';
  18. require('codemirror/addon/display/placeholder');
  19. require('codemirror/addon/edit/matchbrackets');
  20. require('codemirror/addon/edit/matchtags');
  21. require('codemirror/addon/edit/closetag');
  22. require('codemirror/addon/edit/continuelist');
  23. require('codemirror/addon/hint/show-hint');
  24. require('codemirror/addon/hint/show-hint.css');
  25. require('codemirror/addon/search/searchcursor');
  26. require('codemirror/addon/search/match-highlighter');
  27. require('codemirror/addon/selection/active-line');
  28. require('codemirror/addon/scroll/annotatescrollbar');
  29. require('codemirror/addon/fold/foldcode');
  30. require('codemirror/addon/fold/foldgutter');
  31. require('codemirror/addon/fold/foldgutter.css');
  32. require('codemirror/addon/fold/markdown-fold');
  33. require('codemirror/addon/fold/brace-fold');
  34. require('codemirror/addon/display/placeholder');
  35. require('codemirror/mode/gfm/gfm');
  36. require('../../util/codemirror/autorefresh.ext');
  37. import pasteHelper from './PasteHelper';
  38. import EmojiAutoCompleteHelper from './EmojiAutoCompleteHelper';
  39. import InterceptorManager from '../../../../lib/util/interceptor-manager';
  40. import PreventMarkdownListInterceptor from './PreventMarkdownListInterceptor';
  41. import MarkdownTableInterceptor from './MarkdownTableInterceptor';
  42. import mtu from './MarkdownTableUtil';
  43. export default class CodeMirrorEditor extends AbstractEditor {
  44. constructor(props) {
  45. super(props);
  46. this.logger = require('@alias/logger')('growi:PageEditor:CodeMirrorEditor');
  47. this.state = {
  48. value: this.props.value,
  49. isGfmMode: this.props.isGfmMode,
  50. isEnabledEmojiAutoComplete: false,
  51. isLoadingKeymap: false,
  52. isSimpleCheatsheetShown: this.props.isGfmMode && this.props.value.length === 0,
  53. isCheatsheetModalShown: false,
  54. additionalClassSet: new Set(),
  55. };
  56. this.init();
  57. this.getCodeMirror = this.getCodeMirror.bind(this);
  58. this.getBol = this.getBol.bind(this);
  59. this.getEol = this.getEol.bind(this);
  60. this.loadTheme = this.loadTheme.bind(this);
  61. this.loadKeymapMode = this.loadKeymapMode.bind(this);
  62. this.setKeymapMode = this.setKeymapMode.bind(this);
  63. this.handleEnterKey = this.handleEnterKey.bind(this);
  64. this.handleCtrlEnterKey = this.handleCtrlEnterKey.bind(this);
  65. this.scrollCursorIntoViewHandler = this.scrollCursorIntoViewHandler.bind(this);
  66. this.pasteHandler = this.pasteHandler.bind(this);
  67. this.cursorHandler = this.cursorHandler.bind(this);
  68. this.changeHandler = this.changeHandler.bind(this);
  69. this.renderLoadingKeymapOverlay = this.renderLoadingKeymapOverlay.bind(this);
  70. this.renderCheatsheetModalButton = this.renderCheatsheetModalButton.bind(this);
  71. }
  72. init() {
  73. this.cmCdnRoot = 'https://cdn.jsdelivr.net/npm/codemirror@5.37.0';
  74. this.interceptorManager = new InterceptorManager();
  75. this.interceptorManager.addInterceptors([
  76. new PreventMarkdownListInterceptor(),
  77. new MarkdownTableInterceptor(),
  78. ]);
  79. this.loadedThemeSet = new Set(['eclipse', 'elegant']); // themes imported in _vendor.scss
  80. this.loadedKeymapSet = new Set();
  81. }
  82. componentWillMount() {
  83. if (this.props.emojiStrategy != null) {
  84. this.emojiAutoCompleteHelper = new EmojiAutoCompleteHelper(this.props.emojiStrategy);
  85. this.setState({isEnabledEmojiAutoComplete: true});
  86. }
  87. }
  88. componentDidMount() {
  89. // ensure to be able to resolve 'this' to use 'codemirror.commands.save'
  90. this.getCodeMirror().codeMirrorEditor = this;
  91. // initialize caret line
  92. this.setCaretLine(0);
  93. }
  94. componentWillReceiveProps(nextProps) {
  95. // load theme
  96. const theme = nextProps.editorOptions.theme;
  97. this.loadTheme(theme);
  98. // set keymap
  99. const keymapMode = nextProps.editorOptions.keymapMode;
  100. this.setKeymapMode(keymapMode);
  101. }
  102. getCodeMirror() {
  103. return this.refs.cm.editor;
  104. }
  105. /**
  106. * @inheritDoc
  107. */
  108. forceToFocus() {
  109. const editor = this.getCodeMirror();
  110. // use setInterval with reluctance -- 2018.01.11 Yuki Takei
  111. const intervalId = setInterval(() => {
  112. this.getCodeMirror().focus();
  113. if (editor.hasFocus()) {
  114. clearInterval(intervalId);
  115. // refresh
  116. editor.refresh();
  117. }
  118. }, 100);
  119. }
  120. /**
  121. * @inheritDoc
  122. */
  123. setValue(newValue) {
  124. this.setState({ value: newValue });
  125. this.getCodeMirror().getDoc().setValue(newValue);
  126. }
  127. /**
  128. * @inheritDoc
  129. */
  130. setGfmMode(bool) {
  131. // update state
  132. this.setState({
  133. isGfmMode: bool,
  134. isEnabledEmojiAutoComplete: bool,
  135. });
  136. // update CodeMirror option
  137. const mode = bool ? 'gfm' : undefined;
  138. this.getCodeMirror().setOption('mode', mode);
  139. }
  140. /**
  141. * @inheritDoc
  142. */
  143. setCaretLine(line) {
  144. if (isNaN(line)) {
  145. return;
  146. }
  147. const editor = this.getCodeMirror();
  148. const linePosition = Math.max(0, line);
  149. editor.setCursor({line: linePosition}); // leave 'ch' field as null/undefined to indicate the end of line
  150. this.setScrollTopByLine(linePosition);
  151. }
  152. /**
  153. * @inheritDoc
  154. */
  155. setScrollTopByLine(line) {
  156. if (isNaN(line)) {
  157. return;
  158. }
  159. const editor = this.getCodeMirror();
  160. // get top position of the line
  161. const top = editor.charCoords({line, ch: 0}, 'local').top;
  162. editor.scrollTo(null, top);
  163. }
  164. /**
  165. * @inheritDoc
  166. */
  167. getStrFromBol() {
  168. const editor = this.getCodeMirror();
  169. const curPos = editor.getCursor();
  170. return editor.getDoc().getRange(this.getBol(), curPos);
  171. }
  172. /**
  173. * @inheritDoc
  174. */
  175. getStrToEol() {
  176. const editor = this.getCodeMirror();
  177. const curPos = editor.getCursor();
  178. return editor.getDoc().getRange(curPos, this.getEol());
  179. }
  180. /**
  181. * @inheritDoc
  182. */
  183. getStrFromBolToSelectedUpperPos() {
  184. const editor = this.getCodeMirror();
  185. const pos = this.selectUpperPos(editor.getCursor('from'), editor.getCursor('to'));
  186. return editor.getDoc().getRange(this.getBol(), pos);
  187. }
  188. /**
  189. * @inheritDoc
  190. */
  191. replaceBolToCurrentPos(text) {
  192. const editor = this.getCodeMirror();
  193. const pos = this.selectLowerPos(editor.getCursor('from'), editor.getCursor('to'));
  194. editor.getDoc().replaceRange(text, this.getBol(), pos);
  195. }
  196. /**
  197. * @inheritDoc
  198. */
  199. insertText(text) {
  200. const editor = this.getCodeMirror();
  201. editor.getDoc().replaceSelection(text);
  202. }
  203. /**
  204. * return the postion of the BOL(beginning of line)
  205. */
  206. getBol() {
  207. const editor = this.getCodeMirror();
  208. const curPos = editor.getCursor();
  209. return { line: curPos.line, ch: 0 };
  210. }
  211. /**
  212. * return the postion of the EOL(end of line)
  213. */
  214. getEol() {
  215. const editor = this.getCodeMirror();
  216. const curPos = editor.getCursor();
  217. const lineLength = editor.getDoc().getLine(curPos.line).length;
  218. return { line: curPos.line, ch: lineLength };
  219. }
  220. /**
  221. * select the upper position of pos1 and pos2
  222. * @param {{line: number, ch: number}} pos1
  223. * @param {{line: number, ch: number}} pos2
  224. */
  225. selectUpperPos(pos1, pos2) {
  226. // if both is in same line
  227. if (pos1.line === pos2.line) {
  228. return (pos1.ch < pos2.ch) ? pos1 : pos2;
  229. }
  230. return (pos1.line < pos2.line) ? pos1 : pos2;
  231. }
  232. /**
  233. * select the lower position of pos1 and pos2
  234. * @param {{line: number, ch: number}} pos1
  235. * @param {{line: number, ch: number}} pos2
  236. */
  237. selectLowerPos(pos1, pos2) {
  238. // if both is in same line
  239. if (pos1.line === pos2.line) {
  240. return (pos1.ch < pos2.ch) ? pos2 : pos1;
  241. }
  242. return (pos1.line < pos2.line) ? pos2 : pos1;
  243. }
  244. loadCss(source) {
  245. return new Promise((resolve) => {
  246. loadCssSync(source);
  247. resolve();
  248. });
  249. }
  250. /**
  251. * load Theme
  252. * @see https://codemirror.net/doc/manual.html#config
  253. *
  254. * @param {string} theme
  255. */
  256. loadTheme(theme) {
  257. if (!this.loadedThemeSet.has(theme)) {
  258. this.loadCss(urljoin(this.cmCdnRoot, `theme/${theme}.min.css`));
  259. // update Set
  260. this.loadedThemeSet.add(theme);
  261. }
  262. }
  263. /**
  264. * load assets for Key Maps
  265. * @param {*} keymapMode 'default' or 'vim' or 'emacs' or 'sublime'
  266. */
  267. loadKeymapMode(keymapMode) {
  268. const loadCss = this.loadCss;
  269. let scriptList = [];
  270. let cssList = [];
  271. // add dependencies
  272. if (this.loadedKeymapSet.size == 0) {
  273. scriptList.push(loadScript(urljoin(this.cmCdnRoot, 'addon/dialog/dialog.min.js')));
  274. cssList.push(loadCss(urljoin(this.cmCdnRoot, 'addon/dialog/dialog.min.css')));
  275. }
  276. // load keymap
  277. if (!this.loadedKeymapSet.has(keymapMode)) {
  278. scriptList.push(loadScript(urljoin(this.cmCdnRoot, `keymap/${keymapMode}.min.js`)));
  279. // update Set
  280. this.loadedKeymapSet.add(keymapMode);
  281. }
  282. // set loading state
  283. this.setState({ isLoadingKeymap: true });
  284. return Promise.all(scriptList.concat(cssList))
  285. .then(() => {
  286. this.setState({ isLoadingKeymap: false });
  287. });
  288. }
  289. /**
  290. * set Key Maps
  291. * @see https://codemirror.net/doc/manual.html#keymaps
  292. *
  293. * @param {string} keymapMode 'default' or 'vim' or 'emacs' or 'sublime'
  294. */
  295. setKeymapMode(keymapMode) {
  296. if (!keymapMode.match(/^(vim|emacs|sublime)$/)) {
  297. // reset
  298. this.getCodeMirror().setOption('keyMap', 'default');
  299. return;
  300. }
  301. this.loadKeymapMode(keymapMode)
  302. .then(() => {
  303. this.getCodeMirror().setOption('keyMap', keymapMode);
  304. });
  305. }
  306. /**
  307. * handle ENTER key
  308. */
  309. handleEnterKey() {
  310. if (!this.state.isGfmMode) {
  311. codemirror.commands.newlineAndIndent(this.getCodeMirror());
  312. return;
  313. }
  314. const context = {
  315. handlers: [], // list of handlers which process enter key
  316. editor: this,
  317. };
  318. const interceptorManager = this.interceptorManager;
  319. interceptorManager.process('preHandleEnter', context)
  320. .then(() => {
  321. if (context.handlers.length == 0) {
  322. codemirror.commands.newlineAndIndentContinueMarkdownList(this.getCodeMirror());
  323. }
  324. });
  325. }
  326. /**
  327. * handle Ctrl+ENTER key
  328. */
  329. handleCtrlEnterKey() {
  330. if (this.props.onCtrlEnter != null) {
  331. this.props.onCtrlEnter();
  332. }
  333. }
  334. scrollCursorIntoViewHandler(editor, event) {
  335. if (this.props.onScrollCursorIntoView != null) {
  336. const line = editor.getCursor().line;
  337. this.props.onScrollCursorIntoView(line);
  338. }
  339. }
  340. cursorHandler(editor, event) {
  341. const strFromBol = this.getStrFromBol();
  342. const autoformatTableClass = 'autoformat-markdown-table-activated';
  343. const additionalClassSet = this.state.additionalClassSet;
  344. const hasCustomClass = additionalClassSet.has(autoformatTableClass);
  345. if (mtu.isEndOfLine(editor) && mtu.linePartOfTableRE.test(strFromBol)) {
  346. if (!hasCustomClass) {
  347. additionalClassSet.add(autoformatTableClass);
  348. this.setState({additionalClassSet});
  349. }
  350. }
  351. else {
  352. if (hasCustomClass) {
  353. additionalClassSet.delete(autoformatTableClass);
  354. this.setState({additionalClassSet});
  355. }
  356. }
  357. }
  358. changeHandler(editor, data, value) {
  359. if (this.props.onChange != null) {
  360. this.props.onChange(value);
  361. }
  362. // update isSimpleCheatsheetShown
  363. const isSimpleCheatsheetShown = this.state.isGfmMode && value.length === 0;
  364. this.setState({isSimpleCheatsheetShown});
  365. // Emoji AutoComplete
  366. if (this.state.isEnabledEmojiAutoComplete) {
  367. this.emojiAutoCompleteHelper.showHint(editor);
  368. }
  369. }
  370. /**
  371. * CodeMirror paste event handler
  372. * see: https://codemirror.net/doc/manual.html#events
  373. * @param {any} editor An editor instance of CodeMirror
  374. * @param {any} event
  375. */
  376. pasteHandler(editor, event) {
  377. const types = event.clipboardData.types;
  378. // text
  379. if (types.includes('text/plain')) {
  380. pasteHelper.pasteText(this, event);
  381. }
  382. // files
  383. else if (types.includes('Files')) {
  384. this.dispatchPasteFiles(event);
  385. }
  386. }
  387. renderLoadingKeymapOverlay() {
  388. // centering
  389. const style = {
  390. top: 0,
  391. right: 0,
  392. bottom: 0,
  393. left: 0,
  394. };
  395. return this.state.isLoadingKeymap
  396. ? <div className="overlay overlay-loading-keymap">
  397. <span style={style} className="overlay-content">
  398. <div className="speeding-wheel d-inline-block"></div> Loading Keymap ...
  399. </span>
  400. </div>
  401. : '';
  402. }
  403. renderSimpleCheatsheet() {
  404. return (
  405. <div className="panel panel-default gfm-cheatsheet mb-0">
  406. <div className="panel-heading"><i className="icon-fw icon-question"/>Markdown Help</div>
  407. <div className="panel-body small p-b-0">
  408. <div className="row">
  409. <div className="col-xs-6">
  410. <p>
  411. # 見出し1<br />
  412. ## 見出し2
  413. </p>
  414. <p><i>*斜体*</i>&nbsp;&nbsp;<b>**強調**</b></p>
  415. <p>
  416. [リンク](http://..)<br />
  417. [/ページ名/子ページ名]
  418. </p>
  419. <p>
  420. ```javascript:index.js<br />
  421. writeCode();<br />
  422. ```
  423. </p>
  424. </div>
  425. <div className="col-xs-6">
  426. <p>
  427. - リスト 1<br />
  428. &nbsp;&nbsp;&nbsp;&nbsp;- リスト 1_1<br />
  429. - リスト 2<br />
  430. 1. 番号付きリスト 1
  431. 1. 番号付きリスト 2
  432. </p>
  433. <hr />
  434. <p>行末にスペース2つ[ ][ ]<br />で改行</p>
  435. </div>
  436. </div>
  437. </div>
  438. </div>
  439. );
  440. }
  441. renderCheatsheetModalBody() {
  442. return (
  443. <div className="row small">
  444. <div className="col-sm-6">
  445. <h4>Header</h4>
  446. <ul className="hljs">
  447. <li><code># </code>見出し1</li>
  448. <li><code>## </code>見出し2</li>
  449. <li><code>### </code>見出し3</li>
  450. </ul>
  451. <h4>Block</h4>
  452. <p className="mb-1"><code>[空白行]</code>を挟むことで段落になります</p>
  453. <ul className="hljs">
  454. <li>text</li>
  455. <li></li>
  456. <li>text</li>
  457. </ul>
  458. <h4>Line breaks</h4>
  459. <p className="mb-1">段落中、<code>[space][space]</code>(スペース2つ) で改行されます</p>
  460. <ul className="hljs">
  461. <li>text<code> </code><code> </code></li>
  462. <li>text</li>
  463. </ul>
  464. <h4>Typography</h4>
  465. <ul className="hljs">
  466. <li><i>*イタリック*</i></li>
  467. <li><b>**ボールド**</b></li>
  468. <li><i><b>***イタリックボールド***</b></i></li>
  469. <li>~~取り消し線~~ => <s>striked text</s></li>
  470. </ul>
  471. <h4>Link</h4>
  472. <ul className="hljs">
  473. <li>[Google](https://www.google.co.jp/)</li>
  474. <li>[/Page1/ChildPage1]</li>
  475. </ul>
  476. <h4>コードハイライト</h4>
  477. <ul className="hljs">
  478. <li>```javascript:index.js</li>
  479. <li>writeCode();</li>
  480. <li>```</li>
  481. </ul>
  482. </div>
  483. <div className="col-sm-6">
  484. <h4>リスト</h4>
  485. <ul className="hljs">
  486. <li>- リスト 1</li>
  487. <li>&nbsp;&nbsp;- リスト 1_1</li>
  488. <li>- リスト 2</li>
  489. </ul>
  490. <ul className="hljs">
  491. <li>1. 番号付きリスト 1</li>
  492. <li>1. 番号付きリスト 2</li>
  493. </ul>
  494. <ul className="hljs">
  495. <li>- [ ] タスク(チェックなし)</li>
  496. <li>- [x] タスク(チェック付き)</li>
  497. </ul>
  498. <h4>引用</h4>
  499. <ul className="hljs">
  500. <li>> 複数行の引用文を</li>
  501. <li>> 書くことができます</li>
  502. </ul>
  503. <ul className="hljs">
  504. <li>>> 多重引用</li>
  505. <li>>>> 多重引用</li>
  506. <li>>>>> 多重引用</li>
  507. </ul>
  508. <h4>Table</h4>
  509. <ul className="hljs text-center">
  510. <li>|&nbsp;&nbsp;&nbsp;左寄せ&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;中央寄せ&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;右寄せ&nbsp;&nbsp;&nbsp;|</li>
  511. <li>|:-----------|:----------:|-----------:|</li>
  512. <li>|column 1&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;column 2&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;column 3|</li>
  513. <li>|column 1&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;column 2&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;column 3|</li>
  514. </ul>
  515. <h4>Images</h4>
  516. <p className="mb-1"><code> ![Alt文字列](URL)</code> で<span className="text-info">&lt;img&gt;</span>タグを挿入できます</p>
  517. <ul className="hljs">
  518. <li>![ex](https://example.com/images/a.png)</li>
  519. </ul>
  520. <hr />
  521. <a href="/Sandbox" className="btn btn-info btn-block" target="_blank">
  522. <i className="icon-share-alt"/> Sandbox を開く
  523. </a>
  524. </div>
  525. </div>
  526. );
  527. }
  528. renderCheatsheetModalButton() {
  529. const showCheatsheetModal = () => {
  530. this.setState({isCheatsheetModalShown: true});
  531. };
  532. const hideCheatsheetModal = () => {
  533. this.setState({isCheatsheetModalShown: false});
  534. };
  535. return (
  536. <React.Fragment>
  537. <Modal className="modal-gfm-cheatsheet" show={this.state.isCheatsheetModalShown} onHide={() => { hideCheatsheetModal() }}>
  538. <Modal.Header closeButton>
  539. <Modal.Title><i className="icon-fw icon-question"/>Markdown Help</Modal.Title>
  540. </Modal.Header>
  541. <Modal.Body className="pt-1">
  542. { this.renderCheatsheetModalBody() }
  543. </Modal.Body>
  544. </Modal>
  545. <a className="gfm-cheatsheet-modal-link text-muted small" onClick={() => { showCheatsheetModal() }}>
  546. <i className="icon-question" /> Markdown
  547. </a>
  548. </React.Fragment>
  549. );
  550. }
  551. render() {
  552. const mode = this.state.isGfmMode ? 'gfm' : undefined;
  553. const defaultEditorOptions = {
  554. theme: 'elegant',
  555. lineNumbers: true,
  556. };
  557. const additionalClasses = Array.from(this.state.additionalClassSet).join(' ');
  558. const editorOptions = Object.assign(defaultEditorOptions, this.props.editorOptions || {});
  559. const placeholder = this.state.isGfmMode ? 'Input with Markdown..' : 'Input with Plane Text..';
  560. return <React.Fragment>
  561. <ReactCodeMirror
  562. ref="cm"
  563. className={additionalClasses}
  564. placeholder="search"
  565. editorDidMount={(editor) => {
  566. // add event handlers
  567. editor.on('paste', this.pasteHandler);
  568. editor.on('scrollCursorIntoView', this.scrollCursorIntoViewHandler);
  569. }}
  570. value={this.state.value}
  571. options={{
  572. mode: mode,
  573. theme: editorOptions.theme,
  574. styleActiveLine: editorOptions.styleActiveLine,
  575. lineNumbers: this.props.lineNumbers,
  576. tabSize: 4,
  577. indentUnit: 4,
  578. lineWrapping: true,
  579. autoRefresh: {force: true}, // force option is enabled by autorefresh.ext.js -- Yuki Takei
  580. autoCloseTags: true,
  581. placeholder: placeholder,
  582. matchBrackets: true,
  583. matchTags: {bothTags: true},
  584. // folding
  585. foldGutter: this.props.lineNumbers,
  586. gutters: this.props.lineNumbers ? ['CodeMirror-linenumbers', 'CodeMirror-foldgutter'] : [],
  587. // match-highlighter, matchesonscrollbar, annotatescrollbar options
  588. highlightSelectionMatches: {annotateScrollbar: true},
  589. // markdown mode options
  590. highlightFormatting: true,
  591. // continuelist, indentlist
  592. extraKeys: {
  593. 'Enter': this.handleEnterKey,
  594. 'Ctrl-Enter': this.handleCtrlEnterKey,
  595. 'Cmd-Enter': this.handleCtrlEnterKey,
  596. 'Tab': 'indentMore',
  597. 'Shift-Tab': 'indentLess',
  598. 'Ctrl-Q': (cm) => { cm.foldCode(cm.getCursor()) },
  599. }
  600. }}
  601. onCursor={this.cursorHandler}
  602. onScroll={(editor, data) => {
  603. if (this.props.onScroll != null) {
  604. // add line data
  605. const line = editor.lineAtHeight(data.top, 'local');
  606. data.line = line;
  607. this.props.onScroll(data);
  608. }
  609. }}
  610. onChange={this.changeHandler}
  611. onDragEnter={(editor, event) => {
  612. if (this.props.onDragEnter != null) {
  613. this.props.onDragEnter(event);
  614. }
  615. }}
  616. />
  617. { this.renderLoadingKeymapOverlay() }
  618. <div className="overlay overlay-gfm-cheatsheet mt-1 p-3 pt-3">
  619. { this.state.isSimpleCheatsheetShown && this.renderSimpleCheatsheet() }
  620. { !this.state.isSimpleCheatsheetShown && this.renderCheatsheetModalButton() }
  621. </div>
  622. </React.Fragment>;
  623. }
  624. }
  625. CodeMirrorEditor.propTypes = Object.assign({
  626. emojiStrategy: PropTypes.object,
  627. lineNumbers: PropTypes.bool,
  628. }, AbstractEditor.propTypes);
  629. CodeMirrorEditor.defaultProps = {
  630. lineNumbers: true,
  631. };