TextAreaEditor.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. setValue(newValue) {
  48. this.setState({ value: newValue });
  49. this.textarea.value = newValue;
  50. }
  51. /**
  52. * @inheritDoc
  53. */
  54. setCaretLine(line) {
  55. if (isNaN(line)) {
  56. return;
  57. }
  58. // scroll to bottom
  59. this.textarea.scrollTop = this.textarea.scrollHeight;
  60. const lines = this.textarea.value.split('\n').slice(0, line+1);
  61. const pos = lines
  62. .map(lineStr => lineStr.length + 1) // correct length+1 of each lines
  63. .reduce((a, x) => a += x, 0) // sum
  64. - 1; // -1
  65. this.textarea.setSelectionRange(pos, pos);
  66. }
  67. /**
  68. * @inheritDoc
  69. */
  70. setScrollTopByLine(line) {
  71. // do nothing
  72. }
  73. /**
  74. * @inheritDoc
  75. */
  76. insertText(text) {
  77. const startPos = this.textarea.selectionStart;
  78. const endPos = this.textarea.selectionEnd;
  79. this.replaceValue(text, startPos, endPos);
  80. }
  81. /**
  82. * @inheritDoc
  83. */
  84. getStrFromBol() {
  85. const currentPos = this.textarea.selectionStart;
  86. return this.textarea.value.substring(this.getBolPos(), currentPos);
  87. }
  88. /**
  89. * @inheritDoc
  90. */
  91. getStrToEol() {
  92. const currentPos = this.textarea.selectionStart;
  93. return this.textarea.value.substring(currentPos, this.getEolPos());
  94. }
  95. /**
  96. * @inheritDoc
  97. */
  98. getStrFromBolToSelectedUpperPos() {
  99. const startPos = this.textarea.selectionStart;
  100. const endPos = this.textarea.selectionEnd;
  101. const upperPos = (startPos < endPos) ? startPos : endPos;
  102. return this.textarea.value.substring(this.getBolPos(), upperPos);
  103. }
  104. /**
  105. * @inheritDoc
  106. */
  107. replaceBolToCurrentPos(text) {
  108. const startPos = this.textarea.selectionStart;
  109. const endPos = this.textarea.selectionEnd;
  110. const lowerPos = (startPos < endPos) ? endPos : startPos;
  111. this.replaceValue(text, this.getBolPos(), lowerPos);
  112. }
  113. getBolPos() {
  114. const currentPos = this.textarea.selectionStart;
  115. return this.textarea.value.lastIndexOf('\n', currentPos-1) + 1;
  116. }
  117. getEolPos() {
  118. const currentPos = this.textarea.selectionStart;
  119. const pos = this.textarea.value.indexOf('\n', currentPos);
  120. if (pos < 0) { // not found but EOF
  121. return this.textarea.value.length;
  122. }
  123. return pos;
  124. }
  125. replaceValue(text, startPos, endPos) {
  126. // create new value
  127. const value = this.textarea.value;
  128. const newValue = value.substring(0, startPos) + text + value.substring(endPos, value.length);
  129. // calculate new position
  130. const newPos = startPos + text.length;
  131. this.textarea.value = newValue;
  132. this.textarea.setSelectionRange(newPos, newPos);
  133. }
  134. /**
  135. * keypress event handler
  136. * @param {string} event
  137. */
  138. keyPressHandler(event) {
  139. const key = event.key.toLowerCase();
  140. if (key === 'enter') {
  141. if (event.ctrlKey || event.altKey || event.metaKey) {
  142. return;
  143. }
  144. event.preventDefault();
  145. this.handleEnterKey();
  146. }
  147. }
  148. /**
  149. * handle ENTER key
  150. */
  151. handleEnterKey() {
  152. var context = {
  153. handlers: [], // list of handlers which process enter key
  154. editor: this,
  155. };
  156. const interceptorManager = this.interceptorManager;
  157. interceptorManager.process('preHandleEnter', context)
  158. .then(() => {
  159. if (context.handlers.length == 0) {
  160. mlu.newlineAndIndentContinueMarkdownList(this);
  161. }
  162. });
  163. }
  164. /**
  165. * paste event handler
  166. * @param {any} event
  167. */
  168. pasteHandler(event) {
  169. const types = event.clipboardData.types;
  170. // text
  171. if (types.includes('text/plain')) {
  172. pasteHelper.pasteText(this, event);
  173. }
  174. // files
  175. else if (types.includes('Files')) {
  176. this.dispatchPasteFiles(event);
  177. }
  178. }
  179. dragEnterHandler(event) {
  180. this.dispatchDragEnter(event);
  181. }
  182. dispatchDragEnter(event) {
  183. if (this.props.onDragEnter != null) {
  184. this.props.onDragEnter(event);
  185. }
  186. }
  187. render() {
  188. return <React.Fragment>
  189. <FormControl
  190. componentClass="textarea" className="textarea-editor"
  191. inputRef={ref => { this.textarea = ref }}
  192. defaultValue={this.state.value}
  193. onChange={(e) => {
  194. if (this.props.onChange != null) {
  195. this.props.onChange(e.target.value);
  196. }
  197. }} />
  198. </React.Fragment>;
  199. }
  200. }
  201. TextAreaEditor.propTypes = Object.assign({
  202. }, AbstractEditor.propTypes);