TextAreaEditor.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. }
  42. /**
  43. * @inheritDoc
  44. */
  45. setCaretLine(line) {
  46. if (isNaN(line)) {
  47. return;
  48. }
  49. }
  50. /**
  51. * @inheritDoc
  52. */
  53. setScrollTopByLine(line) {
  54. if (isNaN(line)) {
  55. return;
  56. }
  57. }
  58. /**
  59. * @inheritDoc
  60. */
  61. insertText(text) {
  62. }
  63. /**
  64. * @inheritDoc
  65. */
  66. getStrFromBol() {
  67. }
  68. /**
  69. * @inheritDoc
  70. */
  71. getStrToEol() {
  72. }
  73. /**
  74. * @inheritDoc
  75. */
  76. replaceBolToCurrentPos(text) {
  77. }
  78. /**
  79. * handle ENTER key
  80. */
  81. handleEnterKey() {
  82. var context = {
  83. handlers: [], // list of handlers which process enter key
  84. editor: this,
  85. };
  86. const interceptorManager = this.interceptorManager;
  87. interceptorManager.process('preHandleEnter', context)
  88. .then(() => {
  89. if (context.handlers.length == 0) {
  90. // TODO impl
  91. // codemirror.commands.newlineAndIndentContinueMarkdownList(this.getCodeMirror());
  92. }
  93. });
  94. }
  95. /**
  96. * paste event handler
  97. * @param {any} event
  98. */
  99. pasteHandler(event) {
  100. const types = event.clipboardData.types;
  101. // text
  102. if (types.includes('text/plain')) {
  103. pasteHelper.pasteText(this, event);
  104. }
  105. // files
  106. else if (types.includes('Files')) {
  107. this.dispatchPasteFiles(event);
  108. }
  109. }
  110. dispatchDragEnter(event) {
  111. if (this.props.onDragEnter != null) {
  112. this.props.onDragEnter(event);
  113. }
  114. }
  115. render() {
  116. return <React.Fragment>
  117. <FormControl
  118. componentClass="textarea" className="textarea-editor"
  119. inputRef={ref => { this.textarea = ref }}
  120. defaultValue={this.state.value}
  121. onChange={(e) => {
  122. if (this.props.onChange != null) {
  123. this.props.onChange(e.target.value);
  124. }
  125. }} />
  126. </React.Fragment>;
  127. }
  128. }
  129. TextAreaEditor.propTypes = Object.assign({
  130. }, AbstractEditor.propTypes);