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.

218 lines
6.1 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. autoFucus: true
  43. };
  44. constructor (props, context) {
  45. super(props, context);
  46. this.state = {
  47. suggestionsHidden: false,
  48. selectedSuggestion: 0,
  49. lastToken: null,
  50. tokenStart: 0
  51. };
  52. this.onChange = this.onChange.bind(this);
  53. this.onKeyDown = this.onKeyDown.bind(this);
  54. this.onBlur = this.onBlur.bind(this);
  55. this.onSuggestionClick = this.onSuggestionClick.bind(this);
  56. this.setTextarea = this.setTextarea.bind(this);
  57. this.onPaste = this.onPaste.bind(this);
  58. }
  59. onChange (e) {
  60. const [ tokenStart, token ] = textAtCursorMatchesToken(e.target.value, e.target.selectionStart);
  61. if (token !== null && this.state.lastToken !== token) {
  62. this.setState({ lastToken: token, selectedSuggestion: 0, tokenStart });
  63. this.props.onSuggestionsFetchRequested(token);
  64. } else if (token === null) {
  65. this.setState({ lastToken: null });
  66. this.props.onSuggestionsClearRequested();
  67. }
  68. // auto-resize textarea
  69. e.target.style.height = `${e.target.scrollHeight}px`;
  70. this.props.onChange(e);
  71. }
  72. onKeyDown (e) {
  73. const { suggestions, disabled } = this.props;
  74. const { selectedSuggestion, suggestionsHidden } = this.state;
  75. if (disabled) {
  76. e.preventDefault();
  77. return;
  78. }
  79. switch(e.key) {
  80. case 'Escape':
  81. if (!suggestionsHidden) {
  82. e.preventDefault();
  83. this.setState({ suggestionsHidden: true });
  84. }
  85. break;
  86. case 'ArrowDown':
  87. if (suggestions.size > 0 && !suggestionsHidden) {
  88. e.preventDefault();
  89. this.setState({ selectedSuggestion: Math.min(selectedSuggestion + 1, suggestions.size - 1) });
  90. }
  91. break;
  92. case 'ArrowUp':
  93. if (suggestions.size > 0 && !suggestionsHidden) {
  94. e.preventDefault();
  95. this.setState({ selectedSuggestion: Math.max(selectedSuggestion - 1, 0) });
  96. }
  97. break;
  98. case 'Enter':
  99. case 'Tab':
  100. // Select suggestion
  101. if (this.state.lastToken !== null && suggestions.size > 0 && !suggestionsHidden) {
  102. e.preventDefault();
  103. e.stopPropagation();
  104. this.props.onSuggestionSelected(this.state.tokenStart, this.state.lastToken, suggestions.get(selectedSuggestion));
  105. }
  106. break;
  107. }
  108. if (e.defaultPrevented || !this.props.onKeyDown) {
  109. return;
  110. }
  111. this.props.onKeyDown(e);
  112. }
  113. onBlur () {
  114. // If we hide the suggestions immediately, then this will prevent the
  115. // onClick for the suggestions themselves from firing.
  116. // Setting a short window for that to take place before hiding the
  117. // suggestions ensures that can't happen.
  118. setTimeout(() => {
  119. this.setState({ suggestionsHidden: true });
  120. }, 100);
  121. }
  122. onSuggestionClick (suggestion, e) {
  123. e.preventDefault();
  124. this.props.onSuggestionSelected(this.state.tokenStart, this.state.lastToken, suggestion);
  125. this.textarea.focus();
  126. }
  127. componentWillReceiveProps (nextProps) {
  128. if (nextProps.suggestions !== this.props.suggestions && nextProps.suggestions.size > 0 && this.state.suggestionsHidden) {
  129. this.setState({ suggestionsHidden: false });
  130. }
  131. }
  132. setTextarea (c) {
  133. this.textarea = c;
  134. }
  135. onPaste (e) {
  136. if (e.clipboardData && e.clipboardData.files.length === 1) {
  137. this.props.onPaste(e.clipboardData.files)
  138. e.preventDefault();
  139. }
  140. }
  141. reset () {
  142. this.textarea.style.height = 'auto';
  143. }
  144. render () {
  145. const { value, suggestions, disabled, placeholder, onKeyUp, autoFocus } = this.props;
  146. const { suggestionsHidden, selectedSuggestion } = this.state;
  147. const style = { direction: 'ltr' };
  148. if (isRtl(value)) {
  149. style.direction = 'rtl';
  150. }
  151. return (
  152. <div className='autosuggest-textarea'>
  153. <textarea
  154. ref={this.setTextarea}
  155. className='autosuggest-textarea__textarea'
  156. disabled={disabled}
  157. placeholder={placeholder}
  158. autoFocus={autoFocus}
  159. value={value}
  160. onChange={this.onChange}
  161. onKeyDown={this.onKeyDown}
  162. onKeyUp={onKeyUp}
  163. onBlur={this.onBlur}
  164. onPaste={this.onPaste}
  165. style={style}
  166. />
  167. <div style={{ display: (suggestions.size > 0 && !suggestionsHidden) ? 'block' : 'none' }} className='autosuggest-textarea__suggestions'>
  168. {suggestions.map((suggestion, i) => (
  169. <div
  170. role='button'
  171. tabIndex='0'
  172. key={suggestion}
  173. className={`autosuggest-textarea__suggestions__item ${i === selectedSuggestion ? 'selected' : ''}`}
  174. onClick={this.onSuggestionClick.bind(this, suggestion)}>
  175. <AutosuggestAccountContainer id={suggestion} />
  176. </div>
  177. ))}
  178. </div>
  179. </div>
  180. );
  181. }
  182. }
  183. export default AutosuggestTextarea;