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.

195 lines
5.8 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. },
  35. getInitialState () {
  36. return {
  37. isFileDragging: false,
  38. fileDraggingDate: undefined,
  39. suggestionsHidden: false,
  40. selectedSuggestion: 0,
  41. lastToken: null,
  42. tokenStart: 0
  43. };
  44. },
  45. onChange (e) {
  46. const [ tokenStart, token ] = textAtCursorMatchesToken(e.target.value, e.target.selectionStart);
  47. if (token != null && this.state.lastToken !== token) {
  48. this.setState({ lastToken: token, selectedSuggestion: 0, tokenStart });
  49. this.props.onSuggestionsFetchRequested(token);
  50. } else if (token === null) {
  51. this.setState({ lastToken: null });
  52. this.props.onSuggestionsClearRequested();
  53. }
  54. this.props.onChange(e);
  55. },
  56. onKeyDown (e) {
  57. const { suggestions, disabled } = this.props;
  58. const { selectedSuggestion, suggestionsHidden } = this.state;
  59. if (disabled) {
  60. e.preventDefault();
  61. return;
  62. }
  63. switch(e.key) {
  64. case 'Escape':
  65. if (!suggestionsHidden) {
  66. e.preventDefault();
  67. this.setState({ suggestionsHidden: true });
  68. }
  69. break;
  70. case 'ArrowDown':
  71. if (suggestions.size > 0 && !suggestionsHidden) {
  72. e.preventDefault();
  73. this.setState({ selectedSuggestion: Math.min(selectedSuggestion + 1, suggestions.size - 1) });
  74. }
  75. break;
  76. case 'ArrowUp':
  77. if (suggestions.size > 0 && !suggestionsHidden) {
  78. e.preventDefault();
  79. this.setState({ selectedSuggestion: Math.max(selectedSuggestion - 1, 0) });
  80. }
  81. break;
  82. case 'Enter':
  83. case 'Tab':
  84. // Select suggestion
  85. if (this.state.lastToken != null && suggestions.size > 0 && !suggestionsHidden) {
  86. e.preventDefault();
  87. e.stopPropagation();
  88. this.props.onSuggestionSelected(this.state.tokenStart, this.state.lastToken, suggestions.get(selectedSuggestion));
  89. }
  90. break;
  91. }
  92. },
  93. onBlur () {
  94. this.setState({ suggestionsHidden: true });
  95. },
  96. onSuggestionClick (suggestion, e) {
  97. e.preventDefault();
  98. this.props.onSuggestionSelected(this.state.tokenStart, this.state.lastToken, suggestion);
  99. },
  100. componentWillReceiveProps (nextProps) {
  101. if (nextProps.suggestions !== this.props.suggestions && nextProps.suggestions.size > 0 && this.state.suggestionsHidden) {
  102. this.setState({ suggestionsHidden: false });
  103. }
  104. const fileDropDate = nextProps.fileDropDate;
  105. const { isFileDragging, fileDraggingDate } = this.state;
  106. /*
  107. * We can't detect drop events, because they might not be on the textarea (the app allows dropping anywhere in the
  108. * window). Instead, on-drop, we notify this textarea to stop its hover effect by passing in a prop with the
  109. * drop-date.
  110. */
  111. if (isFileDragging && fileDraggingDate && fileDropDate // if dragging when props updated, and dates aren't undefined
  112. && fileDropDate > fileDraggingDate) { // and if the drop date is now greater than when we started dragging
  113. // then we should stop dragging
  114. this.setState({
  115. isFileDragging: false
  116. });
  117. }
  118. },
  119. setTextarea (c) {
  120. this.textarea = c;
  121. },
  122. onDragEnter () {
  123. this.setState({
  124. isFileDragging: true,
  125. fileDraggingDate: new Date()
  126. })
  127. },
  128. onDragExit () {
  129. this.setState({
  130. isFileDragging: false
  131. })
  132. },
  133. render () {
  134. const { value, suggestions, fileDropDate, disabled, placeholder, onKeyUp } = this.props;
  135. const { isFileDragging, suggestionsHidden, selectedSuggestion } = this.state;
  136. const className = isFileDragging ? 'autosuggest-textarea__textarea file-drop' : 'autosuggest-textarea__textarea';
  137. return (
  138. <div className='autosuggest-textarea'>
  139. <textarea
  140. ref={this.setTextarea}
  141. className={className}
  142. disabled={disabled}
  143. placeholder={placeholder}
  144. value={value}
  145. onChange={this.onChange}
  146. onKeyDown={this.onKeyDown}
  147. onKeyUp={onKeyUp}
  148. onBlur={this.onBlur}
  149. onDragEnter={this.onDragEnter}
  150. onDragExit={this.onDragExit}
  151. />
  152. <div style={{ display: (suggestions.size > 0 && !suggestionsHidden) ? 'block' : 'none' }} className='autosuggest-textarea__suggestions'>
  153. {suggestions.map((suggestion, i) => (
  154. <div key={suggestion} className={`autosuggest-textarea__suggestions__item ${i === selectedSuggestion ? 'selected' : ''}`} onClick={this.onSuggestionClick.bind(this, suggestion)}>
  155. <AutosuggestAccountContainer id={suggestion} />
  156. </div>
  157. ))}
  158. </div>
  159. </div>
  160. );
  161. }
  162. });
  163. export default AutosuggestTextarea;