TextAreaEditor.js 5.6 KB

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