logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

autosuggest_textarea.js (6407B)


  1. import React from 'react';
  2. import AutosuggestAccountContainer from '../features/compose/containers/autosuggest_account_container';
  3. import AutosuggestEmoji from './autosuggest_emoji';
  4. import ImmutablePropTypes from 'react-immutable-proptypes';
  5. import PropTypes from 'prop-types';
  6. import { isRtl } from '../rtl';
  7. import ImmutablePureComponent from 'react-immutable-pure-component';
  8. import Textarea from 'react-textarea-autosize';
  9. import classNames from 'classnames';
  10. const textAtCursorMatchesToken = (str, caretPosition) => {
  11. let word;
  12. let left = str.slice(0, caretPosition).search(/\S+$/);
  13. let right = str.slice(caretPosition).search(/\s/);
  14. if (right < 0) {
  15. word = str.slice(left);
  16. } else {
  17. word = str.slice(left, right + caretPosition);
  18. }
  19. if (!word || word.trim().length < 3 || ['@', ':', '#'].indexOf(word[0]) === -1) {
  20. return [null, null];
  21. }
  22. word = word.trim().toLowerCase();
  23. if (word.length > 0) {
  24. return [left + 1, word];
  25. } else {
  26. return [null, null];
  27. }
  28. };
  29. export default class AutosuggestTextarea extends ImmutablePureComponent {
  30. static propTypes = {
  31. value: PropTypes.string,
  32. suggestions: ImmutablePropTypes.list,
  33. disabled: PropTypes.bool,
  34. placeholder: PropTypes.string,
  35. onSuggestionSelected: PropTypes.func.isRequired,
  36. onSuggestionsClearRequested: PropTypes.func.isRequired,
  37. onSuggestionsFetchRequested: PropTypes.func.isRequired,
  38. onChange: PropTypes.func.isRequired,
  39. onKeyUp: PropTypes.func,
  40. onKeyDown: PropTypes.func,
  41. onPaste: PropTypes.func.isRequired,
  42. autoFocus: PropTypes.bool,
  43. };
  44. static defaultProps = {
  45. autoFocus: true,
  46. };
  47. state = {
  48. suggestionsHidden: false,
  49. selectedSuggestion: 0,
  50. lastToken: null,
  51. tokenStart: 0,
  52. };
  53. onChange = (e) => {
  54. const [ tokenStart, token ] = textAtCursorMatchesToken(e.target.value, e.target.selectionStart);
  55. if (token !== null && this.state.lastToken !== token) {
  56. this.setState({ lastToken: token, selectedSuggestion: 0, tokenStart });
  57. this.props.onSuggestionsFetchRequested(token);
  58. } else if (token === null) {
  59. this.setState({ lastToken: null });
  60. this.props.onSuggestionsClearRequested();
  61. }
  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. onKeyUp = e => {
  106. if (e.key === 'Escape' && this.state.suggestionsHidden) {
  107. document.querySelector('.ui').parentElement.focus();
  108. }
  109. if (this.props.onKeyUp) {
  110. this.props.onKeyUp(e);
  111. }
  112. }
  113. onBlur = () => {
  114. this.setState({ suggestionsHidden: true });
  115. }
  116. onSuggestionClick = (e) => {
  117. const suggestion = this.props.suggestions.get(e.currentTarget.getAttribute('data-index'));
  118. e.preventDefault();
  119. this.props.onSuggestionSelected(this.state.tokenStart, this.state.lastToken, suggestion);
  120. this.textarea.focus();
  121. }
  122. componentWillReceiveProps (nextProps) {
  123. if (nextProps.suggestions !== this.props.suggestions && nextProps.suggestions.size > 0 && this.state.suggestionsHidden) {
  124. this.setState({ suggestionsHidden: false });
  125. }
  126. }
  127. setTextarea = (c) => {
  128. this.textarea = c;
  129. }
  130. onPaste = (e) => {
  131. if (e.clipboardData && e.clipboardData.files.length === 1) {
  132. this.props.onPaste(e.clipboardData.files);
  133. e.preventDefault();
  134. }
  135. }
  136. renderSuggestion = (suggestion, i) => {
  137. const { selectedSuggestion } = this.state;
  138. let inner, key;
  139. if (typeof suggestion === 'object') {
  140. inner = <AutosuggestEmoji emoji={suggestion} />;
  141. key = suggestion.id;
  142. } else if (suggestion[0] === '#') {
  143. inner = suggestion;
  144. key = suggestion;
  145. } else {
  146. inner = <AutosuggestAccountContainer id={suggestion} />;
  147. key = suggestion;
  148. }
  149. return (
  150. <div role='button' tabIndex='0' key={key} data-index={i} className={classNames('autosuggest-textarea__suggestions__item', { selected: i === selectedSuggestion })} onMouseDown={this.onSuggestionClick}>
  151. {inner}
  152. </div>
  153. );
  154. }
  155. render () {
  156. const { value, suggestions, disabled, placeholder, autoFocus } = this.props;
  157. const { suggestionsHidden } = this.state;
  158. const style = { direction: 'ltr' };
  159. if (isRtl(value)) {
  160. style.direction = 'rtl';
  161. }
  162. return (
  163. <div className='autosuggest-textarea'>
  164. <label>
  165. <span style={{ display: 'none' }}>{placeholder}</span>
  166. <Textarea
  167. inputRef={this.setTextarea}
  168. className='autosuggest-textarea__textarea'
  169. disabled={disabled}
  170. placeholder={placeholder}
  171. autoFocus={autoFocus}
  172. value={value}
  173. onChange={this.onChange}
  174. onKeyDown={this.onKeyDown}
  175. onKeyUp={this.onKeyUp}
  176. onBlur={this.onBlur}
  177. onPaste={this.onPaste}
  178. style={style}
  179. aria-autocomplete='list'
  180. />
  181. </label>
  182. <div className={`autosuggest-textarea__suggestions ${suggestionsHidden || suggestions.isEmpty() ? '' : 'autosuggest-textarea__suggestions--visible'}`}>
  183. {suggestions.map(this.renderSuggestion)}
  184. </div>
  185. </div>
  186. );
  187. }
  188. }