TextAreaEditor.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. const pos = this.textarea.value.indexOf('\n', currentPos);
  102. if (pos < 0) { // not found but EOF
  103. return this.textarea.value.length;
  104. }
  105. return pos;
  106. }
  107. replaceValue(text, startPos, endPos) {
  108. // create new value
  109. const value = this.textarea.value;
  110. const newValue = value.substring(0, startPos) + text + value.substring(endPos, value.length);
  111. // calculate new position
  112. const newPos = startPos + text.length;
  113. this.textarea.value = newValue;
  114. this.textarea.setSelectionRange(newPos, newPos);
  115. }
  116. /**
  117. * keypress event handler
  118. * @param {string} event
  119. */
  120. keyPressHandler(event) {
  121. const key = event.key.toLowerCase();
  122. if (key === 'enter') {
  123. if (event.ctrlKey || event.altKey || event.metaKey) {
  124. return;
  125. }
  126. event.preventDefault();
  127. this.handleEnterKey();
  128. }
  129. }
  130. /**
  131. * handle ENTER key
  132. */
  133. handleEnterKey() {
  134. var context = {
  135. handlers: [], // list of handlers which process enter key
  136. editor: this,
  137. };
  138. const interceptorManager = this.interceptorManager;
  139. interceptorManager.process('preHandleEnter', context)
  140. .then(() => {
  141. if (context.handlers.length == 0) {
  142. mlu.newlineAndIndentContinueMarkdownList(this);
  143. }
  144. });
  145. }
  146. /**
  147. * paste event handler
  148. * @param {any} event
  149. */
  150. pasteHandler(event) {
  151. const types = event.clipboardData.types;
  152. // text
  153. if (types.includes('text/plain')) {
  154. pasteHelper.pasteText(this, event);
  155. }
  156. // files
  157. else if (types.includes('Files')) {
  158. this.dispatchPasteFiles(event);
  159. }
  160. }
  161. dragEnterHandler(event) {
  162. this.dispatchDragEnter(event);
  163. }
  164. dispatchDragEnter(event) {
  165. if (this.props.onDragEnter != null) {
  166. this.props.onDragEnter(event);
  167. }
  168. }
  169. render() {
  170. return <React.Fragment>
  171. <FormControl
  172. componentClass="textarea" className="textarea-editor"
  173. inputRef={ref => { this.textarea = ref }}
  174. defaultValue={this.state.value}
  175. onChange={(e) => {
  176. if (this.props.onChange != null) {
  177. this.props.onChange(e.target.value);
  178. }
  179. }} />
  180. </React.Fragment>;
  181. }
  182. }
  183. TextAreaEditor.propTypes = Object.assign({
  184. }, AbstractEditor.propTypes);