TextAreaEditor.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 InterceptorManager from '../../../../lib/util/interceptor-manager';
  7. import MarkdownListInterceptor from './MarkdownListInterceptor';
  8. export default class TextAreaEditor extends AbstractEditor {
  9. constructor(props) {
  10. super(props);
  11. this.logger = require('@alias/logger')('growi:PageEditor:TextAreaEditor');
  12. this.state = {
  13. value: this.props.value,
  14. };
  15. this.init();
  16. this.handleEnterKey = this.handleEnterKey.bind(this);
  17. this.pasteHandler = this.pasteHandler.bind(this);
  18. }
  19. init() {
  20. this.interceptorManager = new InterceptorManager();
  21. this.interceptorManager.addInterceptors([
  22. new MarkdownListInterceptor(),
  23. ]);
  24. }
  25. componentDidMount() {
  26. // initialize caret line
  27. this.setCaretLine(0);
  28. this.textarea.addEventListener('paste', (e) => {
  29. this.logger.info('paste');
  30. this.pasteHandler(e);
  31. });
  32. this.textarea.addEventListener('dragenter', (e) => {
  33. this.logger.info('dragenter');
  34. this.dispatchDragEnter(e);
  35. });
  36. }
  37. /**
  38. * @inheritDoc
  39. */
  40. forceToFocus() {
  41. setTimeout(() => {
  42. this.textarea.focus();
  43. }, 150);
  44. }
  45. /**
  46. * @inheritDoc
  47. */
  48. setCaretLine(line) {
  49. if (isNaN(line)) {
  50. return;
  51. }
  52. const lines = this.textarea.value.split('\n').slice(0, line+1);
  53. const pos = lines
  54. .map(lineStr => lineStr.length + 1) // correct length+1 of each lines
  55. .reduce((a, x) => a += x, 0) // sum
  56. - 1; // -1
  57. this.textarea.setSelectionRange(pos, pos);
  58. }
  59. /**
  60. * @inheritDoc
  61. */
  62. setScrollTopByLine(line) {
  63. if (isNaN(line)) {
  64. return;
  65. }
  66. }
  67. /**
  68. * @inheritDoc
  69. */
  70. insertText(text) {
  71. }
  72. /**
  73. * @inheritDoc
  74. */
  75. getStrFromBol() {
  76. }
  77. /**
  78. * @inheritDoc
  79. */
  80. getStrToEol() {
  81. }
  82. /**
  83. * @inheritDoc
  84. */
  85. replaceBolToCurrentPos(text) {
  86. }
  87. /**
  88. * handle ENTER key
  89. */
  90. handleEnterKey() {
  91. var context = {
  92. handlers: [], // list of handlers which process enter key
  93. editor: this,
  94. };
  95. const interceptorManager = this.interceptorManager;
  96. interceptorManager.process('preHandleEnter', context)
  97. .then(() => {
  98. if (context.handlers.length == 0) {
  99. // TODO impl
  100. // codemirror.commands.newlineAndIndentContinueMarkdownList(this.getCodeMirror());
  101. }
  102. });
  103. }
  104. /**
  105. * paste event handler
  106. * @param {any} event
  107. */
  108. pasteHandler(event) {
  109. const types = event.clipboardData.types;
  110. // text
  111. if (types.includes('text/plain')) {
  112. pasteHelper.pasteText(this, event);
  113. }
  114. // files
  115. else if (types.includes('Files')) {
  116. this.dispatchPasteFiles(event);
  117. }
  118. }
  119. dispatchDragEnter(event) {
  120. if (this.props.onDragEnter != null) {
  121. this.props.onDragEnter(event);
  122. }
  123. }
  124. render() {
  125. return <React.Fragment>
  126. <FormControl
  127. componentClass="textarea" className="textarea-editor"
  128. inputRef={ref => { this.textarea = ref }}
  129. defaultValue={this.state.value}
  130. onChange={(e) => {
  131. if (this.props.onChange != null) {
  132. this.props.onChange(e.target.value);
  133. }
  134. }} />
  135. </React.Fragment>;
  136. }
  137. }
  138. TextAreaEditor.propTypes = Object.assign({
  139. }, AbstractEditor.propTypes);