TextAreaEditor.jsx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. getBolPos() {
  125. const currentPos = this.textarea.selectionStart;
  126. return this.textarea.value.lastIndexOf('\n', currentPos - 1) + 1;
  127. }
  128. getEolPos() {
  129. const currentPos = this.textarea.selectionStart;
  130. const pos = this.textarea.value.indexOf('\n', currentPos);
  131. if (pos < 0) { // not found but EOF
  132. return this.textarea.value.length;
  133. }
  134. return pos;
  135. }
  136. replaceValue(text, startPos, endPos) {
  137. // create new value
  138. const value = this.textarea.value;
  139. const newValue = value.substring(0, startPos) + text + value.substring(endPos, value.length);
  140. // calculate new position
  141. const newPos = startPos + text.length;
  142. this.textarea.value = newValue;
  143. this.textarea.setSelectionRange(newPos, newPos);
  144. }
  145. /**
  146. * keypress event handler
  147. * @param {string} event
  148. */
  149. keyPressHandler(event) {
  150. const key = event.key.toLowerCase();
  151. if (key === 'enter') {
  152. if (event.ctrlKey || event.altKey || event.metaKey) {
  153. return;
  154. }
  155. this.handleEnterKey(event);
  156. }
  157. }
  158. /**
  159. * handle ENTER key
  160. * @param {string} event
  161. */
  162. handleEnterKey(event) {
  163. if (!this.state.isGfmMode) {
  164. return; // do nothing
  165. }
  166. const context = {
  167. handlers: [], // list of handlers which process enter key
  168. editor: this,
  169. };
  170. const interceptorManager = this.interceptorManager;
  171. interceptorManager.process('preHandleEnter', context)
  172. .then(() => {
  173. event.preventDefault();
  174. if (context.handlers.length === 0) {
  175. mlu.newlineAndIndentContinueMarkdownList(this);
  176. }
  177. });
  178. }
  179. /**
  180. * paste event handler
  181. * @param {any} event
  182. */
  183. pasteHandler(event) {
  184. const types = event.clipboardData.types;
  185. // files
  186. if (types.includes('Files')) {
  187. event.preventDefault();
  188. this.dispatchPasteFiles(event);
  189. }
  190. // text
  191. else if (types.includes('text/plain')) {
  192. pasteHelper.pasteText(this, event);
  193. }
  194. }
  195. dragEnterHandler(event) {
  196. this.dispatchDragEnter(event);
  197. }
  198. dispatchDragEnter(event) {
  199. if (this.props.onDragEnter != null) {
  200. this.props.onDragEnter(event);
  201. }
  202. }
  203. render() {
  204. return (
  205. <React.Fragment>
  206. <FormControl
  207. componentClass="textarea"
  208. className="textarea-editor"
  209. inputRef={(ref) => { this.textarea = ref }}
  210. defaultValue={this.state.value}
  211. onChange={(e) => {
  212. if (this.props.onChange != null) {
  213. this.props.onChange(e.target.value);
  214. }
  215. }}
  216. />
  217. </React.Fragment>
  218. );
  219. }
  220. }
  221. TextAreaEditor.propTypes = Object.assign({
  222. }, AbstractEditor.propTypes);