Editor.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import * as codemirror from 'codemirror';
  4. import { UnControlled as ReactCodeMirror } from 'react-codemirror2';
  5. require('codemirror/lib/codemirror.css');
  6. require('codemirror/addon/display/autorefresh');
  7. require('codemirror/addon/edit/matchbrackets');
  8. require('codemirror/addon/edit/matchtags');
  9. require('codemirror/addon/edit/closetag');
  10. require('codemirror/addon/edit/continuelist');
  11. require('codemirror/addon/hint/show-hint');
  12. require('codemirror/addon/hint/show-hint.css');
  13. require('codemirror/addon/search/match-highlighter');
  14. require('codemirror/addon/scroll/annotatescrollbar');
  15. require('codemirror/mode/gfm/gfm');
  16. require('codemirror/theme/eclipse.css');
  17. export default class Editor extends React.Component {
  18. constructor(props) {
  19. super(props);
  20. this.state = {
  21. value: this.props.value,
  22. };
  23. this.getCodeMirror = this.getCodeMirror.bind(this);
  24. this.setCaretLine = this.setCaretLine.bind(this);
  25. this.forceToFocus = this.forceToFocus.bind(this);
  26. this.dispatchSave = this.dispatchSave.bind(this);
  27. this.emojiComplete = this.emojiComplete.bind(this);
  28. }
  29. componentDidMount() {
  30. // initialize caret line
  31. this.setCaretLine(0);
  32. // set save handler
  33. codemirror.commands.save = this.dispatchSave;
  34. }
  35. getCodeMirror() {
  36. return this.refs.cm.editor;
  37. }
  38. forceToFocus() {
  39. const editor = this.getCodeMirror();
  40. // use setInterval with reluctance -- 2018.01.11 Yuki Takei
  41. const intervalId = setInterval(() => {
  42. this.getCodeMirror().focus();
  43. if (editor.hasFocus()) {
  44. clearInterval(intervalId);
  45. }
  46. }, 100);
  47. }
  48. /**
  49. * set caret position of codemirror
  50. * @param {string} number
  51. */
  52. setCaretLine(line) {
  53. const editor = this.getCodeMirror();
  54. editor.setCursor({line: line-1}); // leave 'ch' field as null/undefined to indicate the end of line
  55. }
  56. // sample data
  57. getEmojiList() {
  58. return ['apple:', 'abc:', 'axz:', 'bee:', 'beam:', 'bleach:']
  59. }
  60. emojiComplete() {
  61. const cm = this.getCodeMirror();
  62. cm.showHint({
  63. completeSingle: false,
  64. hint: () => {
  65. const emojiList = this.getEmojiList();
  66. let cur = cm.getCursor(), token = cm.getTokenAt(cur);
  67. let start = token.start, end = cur.ch, word = token.string.slice(0, end - start);
  68. let ch = cur.ch, line = cur.line;
  69. let currentWord = token.string;
  70. while (ch-- > -1) {
  71. let t = cm.getTokenAt({ch, line}).string;
  72. if (t === ':') {
  73. let filteredList = emojiList.filter((item) => {
  74. return item.indexOf(currentWord) == 0 ? true : false
  75. });
  76. if (filteredList.length >= 1) {
  77. return {
  78. list: filteredList,
  79. from: codemirror.Pos(line, ch),
  80. to: codemirror.Pos(line, end)
  81. };
  82. }
  83. }
  84. currentWord = t + currentWord;
  85. }
  86. },
  87. });
  88. }
  89. /**
  90. * dispatch onSave event
  91. */
  92. dispatchSave() {
  93. if (this.props.onSave != null) {
  94. this.props.onSave();
  95. }
  96. }
  97. render() {
  98. return (
  99. <ReactCodeMirror
  100. ref="cm"
  101. value={this.state.value}
  102. options={{
  103. mode: 'gfm',
  104. theme: 'eclipse',
  105. lineNumbers: true,
  106. tabSize: 4,
  107. indentUnit: 4,
  108. lineWrapping: true,
  109. autoRefresh: true,
  110. autoCloseTags: true,
  111. matchBrackets: true,
  112. matchTags: {bothTags: true},
  113. // match-highlighter, matchesonscrollbar, annotatescrollbar options
  114. highlightSelectionMatches: {annotateScrollbar: true},
  115. // markdown mode options
  116. highlightFormatting: true,
  117. // continuelist, indentlist
  118. extraKeys: {
  119. "Enter": "newlineAndIndentContinueMarkdownList",
  120. "Tab": "indentMore",
  121. "Shift-Tab": "indentLess",
  122. }
  123. }}
  124. onScroll={(editor, data) => {
  125. if (this.props.onScroll != null) {
  126. this.props.onScroll(data);
  127. }
  128. }}
  129. onChange={(editor, data, value) => {
  130. this.emojiComplete(editor);
  131. if (this.props.onChange != null) {
  132. this.props.onChange(value);
  133. }
  134. }}
  135. />
  136. )
  137. }
  138. }
  139. Editor.propTypes = {
  140. value: PropTypes.string,
  141. onChange: PropTypes.func,
  142. onScroll: PropTypes.func,
  143. onSave: PropTypes.func,
  144. };