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.

202 lines
5.9 KiB

  1. import AutosuggestAccountContainer from '../features/compose/containers/autosuggest_account_container';
  2. import ImmutablePropTypes from 'react-immutable-proptypes';
  3. const textAtCursorMatchesToken = (str, caretPosition) => {
  4. let word;
  5. let left = str.slice(0, caretPosition).search(/\S+$/);
  6. let right = str.slice(caretPosition).search(/\s/);
  7. if (right < 0) {
  8. word = str.slice(left);
  9. } else {
  10. word = str.slice(left, right + caretPosition);
  11. }
  12. if (!word || word.trim().length < 2 || word[0] !== '@') {
  13. return [null, null];
  14. }
  15. word = word.trim().toLowerCase().slice(1);
  16. if (word.length > 0) {
  17. return [left + 1, word];
  18. } else {
  19. return [null, null];
  20. }
  21. };
  22. const AutosuggestTextarea = React.createClass({
  23. propTypes: {
  24. value: React.PropTypes.string,
  25. suggestions: ImmutablePropTypes.list,
  26. disabled: React.PropTypes.bool,
  27. fileDropDate: React.PropTypes.instanceOf(Date),
  28. placeholder: React.PropTypes.string,
  29. onSuggestionSelected: React.PropTypes.func.isRequired,
  30. onSuggestionsClearRequested: React.PropTypes.func.isRequired,
  31. onSuggestionsFetchRequested: React.PropTypes.func.isRequired,
  32. onChange: React.PropTypes.func.isRequired,
  33. onKeyUp: React.PropTypes.func,
  34. onKeyDown: React.PropTypes.func
  35. },
  36. getInitialState () {
  37. return {
  38. isFileDragging: false,
  39. fileDraggingDate: undefined,
  40. suggestionsHidden: false,
  41. selectedSuggestion: 0,
  42. lastToken: null,
  43. tokenStart: 0
  44. };
  45. },
  46. onChange (e) {
  47. const [ tokenStart, token ] = textAtCursorMatchesToken(e.target.value, e.target.selectionStart);
  48. if (token != null && this.state.lastToken !== token) {
  49. this.setState({ lastToken: token, selectedSuggestion: 0, tokenStart });
  50. this.props.onSuggestionsFetchRequested(token);
  51. } else if (token === null) {
  52. this.setState({ lastToken: null });
  53. this.props.onSuggestionsClearRequested();
  54. }
  55. this.props.onChange(e);
  56. },
  57. onKeyDown (e) {
  58. const { suggestions, disabled } = this.props;
  59. const { selectedSuggestion, suggestionsHidden } = this.state;
  60. if (disabled) {
  61. e.preventDefault();
  62. return;
  63. }
  64. switch(e.key) {
  65. case 'Escape':
  66. if (!suggestionsHidden) {
  67. e.preventDefault();
  68. this.setState({ suggestionsHidden: true });
  69. }
  70. break;
  71. case 'ArrowDown':
  72. if (suggestions.size > 0 && !suggestionsHidden) {
  73. e.preventDefault();
  74. this.setState({ selectedSuggestion: Math.min(selectedSuggestion + 1, suggestions.size - 1) });
  75. }
  76. break;
  77. case 'ArrowUp':
  78. if (suggestions.size > 0 && !suggestionsHidden) {
  79. e.preventDefault();
  80. this.setState({ selectedSuggestion: Math.max(selectedSuggestion - 1, 0) });
  81. }
  82. break;
  83. case 'Enter':
  84. case 'Tab':
  85. // Select suggestion
  86. if (this.state.lastToken != null && suggestions.size > 0 && !suggestionsHidden) {
  87. e.preventDefault();
  88. e.stopPropagation();
  89. this.props.onSuggestionSelected(this.state.tokenStart, this.state.lastToken, suggestions.get(selectedSuggestion));
  90. }
  91. break;
  92. }
  93. if (e.defaultPrevented || !this.props.onKeyDown) {
  94. return;
  95. }
  96. this.props.onKeyDown(e);
  97. },
  98. onBlur () {
  99. this.setState({ suggestionsHidden: true });
  100. },
  101. onSuggestionClick (suggestion, e) {
  102. e.preventDefault();
  103. this.props.onSuggestionSelected(this.state.tokenStart, this.state.lastToken, suggestion);
  104. },
  105. componentWillReceiveProps (nextProps) {
  106. if (nextProps.suggestions !== this.props.suggestions && nextProps.suggestions.size > 0 && this.state.suggestionsHidden) {
  107. this.setState({ suggestionsHidden: false });
  108. }
  109. const fileDropDate = nextProps.fileDropDate;
  110. const { isFileDragging, fileDraggingDate } = this.state;
  111. /*
  112. * We can't detect drop events, because they might not be on the textarea (the app allows dropping anywhere in the
  113. * window). Instead, on-drop, we notify this textarea to stop its hover effect by passing in a prop with the
  114. * drop-date.
  115. */
  116. if (isFileDragging && fileDraggingDate && fileDropDate // if dragging when props updated, and dates aren't undefined
  117. && fileDropDate > fileDraggingDate) { // and if the drop date is now greater than when we started dragging
  118. // then we should stop dragging
  119. this.setState({
  120. isFileDragging: false
  121. });
  122. }
  123. },
  124. setTextarea (c) {
  125. this.textarea = c;
  126. },
  127. onDragEnter () {
  128. this.setState({
  129. isFileDragging: true,
  130. fileDraggingDate: new Date()
  131. })
  132. },
  133. onDragExit () {
  134. this.setState({
  135. isFileDragging: false
  136. })
  137. },
  138. render () {
  139. const { value, suggestions, fileDropDate, disabled, placeholder, onKeyUp } = this.props;
  140. const { isFileDragging, suggestionsHidden, selectedSuggestion } = this.state;
  141. const className = isFileDragging ? 'autosuggest-textarea__textarea file-drop' : 'autosuggest-textarea__textarea';
  142. return (
  143. <div className='autosuggest-textarea'>
  144. <textarea
  145. ref={this.setTextarea}
  146. className={className}
  147. disabled={disabled}
  148. placeholder={placeholder}
  149. value={value}
  150. onChange={this.onChange}
  151. onKeyDown={this.onKeyDown}
  152. onKeyUp={onKeyUp}
  153. onBlur={this.onBlur}
  154. onDragEnter={this.onDragEnter}
  155. onDragExit={this.onDragExit}
  156. />
  157. <div style={{ display: (suggestions.size > 0 && !suggestionsHidden) ? 'block' : 'none' }} className='autosuggest-textarea__suggestions'>
  158. {suggestions.map((suggestion, i) => (
  159. <div key={suggestion} className={`autosuggest-textarea__suggestions__item ${i === selectedSuggestion ? 'selected' : ''}`} onClick={this.onSuggestionClick.bind(this, suggestion)}>
  160. <AutosuggestAccountContainer id={suggestion} />
  161. </div>
  162. ))}
  163. </div>
  164. </div>
  165. );
  166. }
  167. });
  168. export default AutosuggestTextarea;