TextAreaEditor.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. import React from 'react';
  2. // import PropTypes from 'prop-types';
  3. import FormControl from 'react-bootstrap/es/FormControl';
  4. import InterceptorManager from '@commons/util/interceptor-manager';
  5. import AbstractEditor from './AbstractEditor';
  6. import pasteHelper from './PasteHelper';
  7. import mlu from './MarkdownListUtil';
  8. import PreventMarkdownListInterceptor from './PreventMarkdownListInterceptor';
  9. export default class TextAreaEditor extends AbstractEditor {
  10. constructor(props) {
  11. super(props);
  12. this.logger = require('@alias/logger')('growi:PageEditor:TextAreaEditor');
  13. this.state = {
  14. value: this.props.value,
  15. isGfmMode: this.props.isGfmMode,
  16. };
  17. this.init();
  18. this.handleEnterKey = this.handleEnterKey.bind(this);
  19. this.keyPressHandler = this.keyPressHandler.bind(this);
  20. this.pasteHandler = this.pasteHandler.bind(this);
  21. this.dragEnterHandler = this.dragEnterHandler.bind(this);
  22. }
  23. init() {
  24. this.interceptorManager = new InterceptorManager();
  25. this.interceptorManager.addInterceptors([
  26. new PreventMarkdownListInterceptor(),
  27. ]);
  28. }
  29. componentDidMount() {
  30. // initialize caret line
  31. this.setCaretLine(0);
  32. // set event handlers
  33. this.textarea.addEventListener('keypress', this.keyPressHandler);
  34. this.textarea.addEventListener('paste', this.pasteHandler);
  35. this.textarea.addEventListener('dragenter', this.dragEnterHandler);
  36. }
  37. /**
  38. * @inheritDoc
  39. */
  40. forceToFocus() {
  41. setTimeout(() => {
  42. this.textarea.focus();
  43. }, 150);
  44. }
  45. /**
  46. * @inheritDoc
  47. */
  48. setValue(newValue) {
  49. this.setState({ value: newValue });
  50. this.textarea.value = newValue;
  51. }
  52. /**
  53. * @inheritDoc
  54. */
  55. setGfmMode(bool) {
  56. this.setState({
  57. isGfmMode: bool,
  58. });
  59. }
  60. /**
  61. * @inheritDoc
  62. */
  63. setCaretLine(line) {
  64. if (isNaN(line)) {
  65. return;
  66. }
  67. // scroll to bottom
  68. this.textarea.scrollTop = this.textarea.scrollHeight;
  69. const lines = this.textarea.value.split('\n').slice(0, line+1);
  70. const pos = lines
  71. .map(lineStr => lineStr.length + 1) // correct length+1 of each lines
  72. .reduce((a, x) => a += x, 0) // sum
  73. - 1; // -1
  74. this.textarea.setSelectionRange(pos, pos);
  75. }
  76. /**
  77. * @inheritDoc
  78. */
  79. setScrollTopByLine(line) {
  80. // do nothing
  81. }
  82. /**
  83. * @inheritDoc
  84. */
  85. insertText(text) {
  86. const startPos = this.textarea.selectionStart;
  87. const endPos = this.textarea.selectionEnd;
  88. this.replaceValue(text, startPos, endPos);
  89. }
  90. /**
  91. * @inheritDoc
  92. */
  93. getStrFromBol() {
  94. const currentPos = this.textarea.selectionStart;
  95. return this.textarea.value.substring(this.getBolPos(), currentPos);
  96. }
  97. /**
  98. * @inheritDoc
  99. */
  100. getStrToEol() {
  101. const currentPos = this.textarea.selectionStart;
  102. return this.textarea.value.substring(currentPos, this.getEolPos());
  103. }
  104. /**
  105. * @inheritDoc
  106. */
  107. getStrFromBolToSelectedUpperPos() {
  108. const startPos = this.textarea.selectionStart;
  109. const endPos = this.textarea.selectionEnd;
  110. const upperPos = (startPos < endPos) ? startPos : endPos;
  111. return this.textarea.value.substring(this.getBolPos(), upperPos);
  112. }
  113. /**
  114. * @inheritDoc
  115. */
  116. replaceBolToCurrentPos(text) {
  117. const startPos = this.textarea.selectionStart;
  118. const endPos = this.textarea.selectionEnd;
  119. const lowerPos = (startPos < endPos) ? endPos : startPos;
  120. this.replaceValue(text, this.getBolPos(), lowerPos);
  121. }
  122. getBolPos() {
  123. const currentPos = this.textarea.selectionStart;
  124. return this.textarea.value.lastIndexOf('\n', currentPos-1) + 1;
  125. }
  126. getEolPos() {
  127. const currentPos = this.textarea.selectionStart;
  128. const pos = this.textarea.value.indexOf('\n', currentPos);
  129. if (pos < 0) { // not found but EOF
  130. return this.textarea.value.length;
  131. }
  132. return pos;
  133. }
  134. replaceValue(text, startPos, endPos) {
  135. // create new value
  136. const value = this.textarea.value;
  137. const newValue = value.substring(0, startPos) + text + value.substring(endPos, value.length);
  138. // calculate new position
  139. const newPos = startPos + text.length;
  140. this.textarea.value = newValue;
  141. this.textarea.setSelectionRange(newPos, newPos);
  142. }
  143. /**
  144. * keypress event handler
  145. * @param {string} event
  146. */
  147. keyPressHandler(event) {
  148. const key = event.key.toLowerCase();
  149. if (key === 'enter') {
  150. if (event.ctrlKey || event.altKey || event.metaKey) {
  151. return;
  152. }
  153. this.handleEnterKey(event);
  154. }
  155. }
  156. /**
  157. * handle ENTER key
  158. * @param {string} event
  159. */
  160. handleEnterKey(event) {
  161. if (!this.state.isGfmMode) {
  162. return; // do nothing
  163. }
  164. const context = {
  165. handlers: [], // list of handlers which process enter key
  166. editor: this,
  167. };
  168. const interceptorManager = this.interceptorManager;
  169. interceptorManager.process('preHandleEnter', context)
  170. .then(() => {
  171. event.preventDefault();
  172. if (context.handlers.length == 0) {
  173. mlu.newlineAndIndentContinueMarkdownList(this);
  174. }
  175. });
  176. }
  177. /**
  178. * paste event handler
  179. * @param {any} event
  180. */
  181. pasteHandler(event) {
  182. const types = event.clipboardData.types;
  183. // text
  184. if (types.includes('text/plain')) {
  185. pasteHelper.pasteText(this, event);
  186. }
  187. // files
  188. else if (types.includes('Files')) {
  189. this.dispatchPasteFiles(event);
  190. }
  191. }
  192. dragEnterHandler(event) {
  193. this.dispatchDragEnter(event);
  194. }
  195. dispatchDragEnter(event) {
  196. if (this.props.onDragEnter != null) {
  197. this.props.onDragEnter(event);
  198. }
  199. }
  200. render() {
  201. return <React.Fragment>
  202. <FormControl
  203. componentClass="textarea" className="textarea-editor"
  204. inputRef={ref => { this.textarea = ref }}
  205. defaultValue={this.state.value}
  206. onChange={(e) => {
  207. if (this.props.onChange != null) {
  208. this.props.onChange(e.target.value);
  209. }
  210. }} />
  211. </React.Fragment>;
  212. }
  213. }
  214. TextAreaEditor.propTypes = Object.assign({
  215. }, AbstractEditor.propTypes);