TextAreaEditor.jsx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. import React from 'react';
  2. // import PropTypes from 'prop-types';
  3. import FormControl from 'react-bootstrap/es/FormControl';
  4. import InterceptorManager from '@commons/service/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 (Number.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. /* eslint-disable no-param-reassign, no-return-assign */
  71. const pos = lines
  72. .map((lineStr) => { return lineStr.length + 1 }) // correct length+1 of each lines
  73. .reduce((a, x) => { return a += x }, 0) // sum
  74. - 1; // -1
  75. /* eslint-enable no-param-reassign, no-return-assign */
  76. this.textarea.setSelectionRange(pos, pos);
  77. }
  78. /**
  79. * @inheritDoc
  80. */
  81. setScrollTopByLine(line) {
  82. // do nothing
  83. }
  84. /**
  85. * @inheritDoc
  86. */
  87. insertText(text) {
  88. const startPos = this.textarea.selectionStart;
  89. const endPos = this.textarea.selectionEnd;
  90. this.replaceValue(text, startPos, endPos);
  91. }
  92. /**
  93. * @inheritDoc
  94. */
  95. getStrFromBol() {
  96. const currentPos = this.textarea.selectionStart;
  97. return this.textarea.value.substring(this.getBolPos(), currentPos);
  98. }
  99. /**
  100. * @inheritDoc
  101. */
  102. getStrToEol() {
  103. const currentPos = this.textarea.selectionStart;
  104. return this.textarea.value.substring(currentPos, this.getEolPos());
  105. }
  106. /**
  107. * @inheritDoc
  108. */
  109. getStrFromBolToSelectedUpperPos() {
  110. const startPos = this.textarea.selectionStart;
  111. const endPos = this.textarea.selectionEnd;
  112. const upperPos = (startPos < endPos) ? startPos : endPos;
  113. return this.textarea.value.substring(this.getBolPos(), upperPos);
  114. }
  115. /**
  116. * @inheritDoc
  117. */
  118. replaceBolToCurrentPos(text) {
  119. const startPos = this.textarea.selectionStart;
  120. const endPos = this.textarea.selectionEnd;
  121. const lowerPos = (startPos < endPos) ? endPos : startPos;
  122. this.replaceValue(text, this.getBolPos(), lowerPos);
  123. }
  124. /**
  125. * @inheritDoc
  126. */
  127. replaceLine(text) {
  128. this.replaceValue(text, this.getBolPos(), this.getEolPos());
  129. }
  130. getBolPos() {
  131. const currentPos = this.textarea.selectionStart;
  132. return this.textarea.value.lastIndexOf('\n', currentPos - 1) + 1;
  133. }
  134. getEolPos() {
  135. const currentPos = this.textarea.selectionStart;
  136. const pos = this.textarea.value.indexOf('\n', currentPos);
  137. if (pos < 0) { // not found but EOF
  138. return this.textarea.value.length;
  139. }
  140. return pos;
  141. }
  142. replaceValue(text, startPos, endPos) {
  143. // create new value
  144. const value = this.textarea.value;
  145. const newValue = value.substring(0, startPos) + text + value.substring(endPos, value.length);
  146. // calculate new position
  147. const newPos = startPos + text.length;
  148. this.textarea.value = newValue;
  149. this.textarea.setSelectionRange(newPos, newPos);
  150. }
  151. /**
  152. * keypress event handler
  153. * @param {string} event
  154. */
  155. keyPressHandler(event) {
  156. const key = event.key.toLowerCase();
  157. if (key === 'enter') {
  158. if (event.ctrlKey || event.altKey || event.metaKey) {
  159. return;
  160. }
  161. this.handleEnterKey(event);
  162. }
  163. }
  164. /**
  165. * handle ENTER key
  166. * @param {string} event
  167. */
  168. handleEnterKey(event) {
  169. if (!this.state.isGfmMode) {
  170. return; // do nothing
  171. }
  172. const context = {
  173. handlers: [], // list of handlers which process enter key
  174. editor: this,
  175. };
  176. const interceptorManager = this.interceptorManager;
  177. interceptorManager.process('preHandleEnter', context)
  178. .then(() => {
  179. event.preventDefault();
  180. if (context.handlers.length === 0) {
  181. mlu.newlineAndIndentContinueMarkdownList(this);
  182. }
  183. });
  184. }
  185. /**
  186. * paste event handler
  187. * @param {any} event
  188. */
  189. pasteHandler(event) {
  190. const types = event.clipboardData.types;
  191. // files
  192. if (types.includes('Files')) {
  193. event.preventDefault();
  194. this.dispatchPasteFiles(event);
  195. }
  196. // text
  197. else if (types.includes('text/plain')) {
  198. pasteHelper.pasteText(this, event);
  199. }
  200. }
  201. dragEnterHandler(event) {
  202. this.dispatchDragEnter(event);
  203. }
  204. dispatchDragEnter(event) {
  205. if (this.props.onDragEnter != null) {
  206. this.props.onDragEnter(event);
  207. }
  208. }
  209. render() {
  210. return (
  211. <React.Fragment>
  212. <FormControl
  213. componentClass="textarea"
  214. className="textarea-editor"
  215. inputRef={(ref) => { this.textarea = ref }}
  216. defaultValue={this.state.value}
  217. onChange={(e) => {
  218. if (this.props.onChange != null) {
  219. this.props.onChange(e.target.value);
  220. }
  221. }}
  222. />
  223. </React.Fragment>
  224. );
  225. }
  226. }
  227. TextAreaEditor.propTypes = Object.assign({
  228. }, AbstractEditor.propTypes);