闭社主体 forked from https://github.com/tootsuite/mastodon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

212 lines
5.9 KiB

  1. import React from 'react';
  2. import AutosuggestAccountContainer from '../features/compose/containers/autosuggest_account_container';
  3. import ImmutablePropTypes from 'react-immutable-proptypes';
  4. import PropTypes from 'prop-types';
  5. import { isRtl } from '../rtl';
  6. import ImmutablePureComponent from 'react-immutable-pure-component';
  7. const textAtCursorMatchesToken = (str, caretPosition) => {
  8. let word;
  9. let left = str.slice(0, caretPosition).search(/\S+$/);
  10. let right = str.slice(caretPosition).search(/\s/);
  11. if (right < 0) {
  12. word = str.slice(left);
  13. } else {
  14. word = str.slice(left, right + caretPosition);
  15. }
  16. if (!word || word.trim().length < 2 || word[0] !== '@') {
  17. return [null, null];
  18. }
  19. word = word.trim().toLowerCase().slice(1);
  20. if (word.length > 0) {
  21. return [left + 1, word];
  22. } else {
  23. return [null, null];
  24. }
  25. };
  26. class AutosuggestTextarea extends ImmutablePureComponent {
  27. static propTypes = {
  28. value: PropTypes.string,
  29. suggestions: ImmutablePropTypes.list,
  30. disabled: PropTypes.bool,
  31. placeholder: PropTypes.string,
  32. onSuggestionSelected: PropTypes.func.isRequired,
  33. onSuggestionsClearRequested: PropTypes.func.isRequired,
  34. onSuggestionsFetchRequested: PropTypes.func.isRequired,
  35. onChange: PropTypes.func.isRequired,
  36. onKeyUp: PropTypes.func,
  37. onKeyDown: PropTypes.func,
  38. onPaste: PropTypes.func.isRequired,
  39. autoFocus: PropTypes.bool,
  40. };
  41. static defaultProps = {
  42. autoFocus: true,
  43. };
  44. state = {
  45. suggestionsHidden: false,
  46. selectedSuggestion: 0,
  47. lastToken: null,
  48. tokenStart: 0,
  49. };
  50. onChange = (e) => {
  51. const [ tokenStart, token ] = textAtCursorMatchesToken(e.target.value, e.target.selectionStart);
  52. if (token !== null && this.state.lastToken !== token) {
  53. this.setState({ lastToken: token, selectedSuggestion: 0, tokenStart });
  54. this.props.onSuggestionsFetchRequested(token);
  55. } else if (token === null) {
  56. this.setState({ lastToken: null });
  57. this.props.onSuggestionsClearRequested();
  58. }
  59. // auto-resize textarea
  60. e.target.style.height = 'auto';
  61. e.target.style.height = `${e.target.scrollHeight}px`;
  62. this.props.onChange(e);
  63. }
  64. onKeyDown = (e) => {
  65. const { suggestions, disabled } = this.props;
  66. const { selectedSuggestion, suggestionsHidden } = this.state;
  67. if (disabled) {
  68. e.preventDefault();
  69. return;
  70. }
  71. switch(e.key) {
  72. case 'Escape':
  73. if (!suggestionsHidden) {
  74. e.preventDefault();
  75. this.setState({ suggestionsHidden: true });
  76. }
  77. break;
  78. case 'ArrowDown':
  79. if (suggestions.size > 0 && !suggestionsHidden) {
  80. e.preventDefault();
  81. this.setState({ selectedSuggestion: Math.min(selectedSuggestion + 1, suggestions.size - 1) });
  82. }
  83. break;
  84. case 'ArrowUp':
  85. if (suggestions.size > 0 && !suggestionsHidden) {
  86. e.preventDefault();
  87. this.setState({ selectedSuggestion: Math.max(selectedSuggestion - 1, 0) });
  88. }
  89. break;
  90. case 'Enter':
  91. case 'Tab':
  92. // Select suggestion
  93. if (this.state.lastToken !== null && suggestions.size > 0 && !suggestionsHidden) {
  94. e.preventDefault();
  95. e.stopPropagation();
  96. this.props.onSuggestionSelected(this.state.tokenStart, this.state.lastToken, suggestions.get(selectedSuggestion));
  97. }
  98. break;
  99. }
  100. if (e.defaultPrevented || !this.props.onKeyDown) {
  101. return;
  102. }
  103. this.props.onKeyDown(e);
  104. }
  105. onBlur = () => {
  106. // If we hide the suggestions immediately, then this will prevent the
  107. // onClick for the suggestions themselves from firing.
  108. // Setting a short window for that to take place before hiding the
  109. // suggestions ensures that can't happen.
  110. setTimeout(() => {
  111. this.setState({ suggestionsHidden: true });
  112. }, 100);
  113. }
  114. onSuggestionClick = (e) => {
  115. const suggestion = Number(e.currentTarget.getAttribute('data-index'));
  116. e.preventDefault();
  117. this.props.onSuggestionSelected(this.state.tokenStart, this.state.lastToken, suggestion);
  118. this.textarea.focus();
  119. }
  120. componentWillReceiveProps (nextProps) {
  121. if (nextProps.suggestions !== this.props.suggestions && nextProps.suggestions.size > 0 && this.state.suggestionsHidden) {
  122. this.setState({ suggestionsHidden: false });
  123. }
  124. }
  125. setTextarea = (c) => {
  126. this.textarea = c;
  127. }
  128. onPaste = (e) => {
  129. if (e.clipboardData && e.clipboardData.files.length === 1) {
  130. this.props.onPaste(e.clipboardData.files);
  131. e.preventDefault();
  132. }
  133. }
  134. reset () {
  135. this.textarea.style.height = 'auto';
  136. }
  137. render () {
  138. const { value, suggestions, disabled, placeholder, onKeyUp, autoFocus } = this.props;
  139. const { suggestionsHidden, selectedSuggestion } = this.state;
  140. const style = { direction: 'ltr' };
  141. if (isRtl(value)) {
  142. style.direction = 'rtl';
  143. }
  144. return (
  145. <div className='autosuggest-textarea'>
  146. <textarea
  147. ref={this.setTextarea}
  148. className='autosuggest-textarea__textarea'
  149. disabled={disabled}
  150. placeholder={placeholder}
  151. autoFocus={autoFocus}
  152. value={value}
  153. onChange={this.onChange}
  154. onKeyDown={this.onKeyDown}
  155. onKeyUp={onKeyUp}
  156. onBlur={this.onBlur}
  157. onPaste={this.onPaste}
  158. style={style}
  159. />
  160. <div className={`autosuggest-textarea__suggestions ${suggestionsHidden || suggestions.isEmpty() ? '' : 'autosuggest-textarea__suggestions--visible'}`}>
  161. {suggestions.map((suggestion, i) => (
  162. <div
  163. role='button'
  164. tabIndex='0'
  165. key={suggestion}
  166. data-index={suggestion}
  167. className={`autosuggest-textarea__suggestions__item ${i === selectedSuggestion ? 'selected' : ''}`}
  168. onClick={this.onSuggestionClick}>
  169. <AutosuggestAccountContainer id={suggestion} />
  170. </div>
  171. ))}
  172. </div>
  173. </div>
  174. );
  175. }
  176. }
  177. export default AutosuggestTextarea;