TextAreaEditor.js 5.1 KB

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