.circleci
.dependabot
.github
app
chewy
controllers
helpers
javascript
fonts
images
mastodon
actions
components
__tests__
account.js
animated_number.js
attachment_list.js
autosuggest_emoji.js
autosuggest_hashtag.js
autosuggest_input.js
autosuggest_textarea.js
avatar.js
avatar_composite.js
avatar_overlay.js
button.js
column.js
column_back_button.js
column_back_button_slim.js
column_header.js
display_name.js
domain.js
dropdown_menu.js
error_boundary.js
gifv.js
hashtag.js
icon.js
icon_button.js
icon_with_badge.js
intersection_observer_article.js
load_gap.js
load_more.js
load_pending.js
loading_indicator.js
media_gallery.js
missing_indicator.js
modal_root.js
permalink.js
poll.js
radio_button.js
regeneration_indicator.js
relative_timestamp.js
scrollable_list.js
setting_text.js
status.js
status_action_bar.js
status_content.js
status_list.js
containers
features
locales
middleware
reducers
selectors
service_worker
storage
store
utils
api.js
base_polyfills.js
common.js
compare_id.js
extra_polyfills.js
initial_state.js
is_mobile.js
load_keyboard_extensions.js
load_polyfills.js
main.js
performance.js
ready.js
rtl.js
scroll.js
settings.js
stream.js
test_setup.js
uuid.js
packs
styles
lib
mailers
models
policies
presenters
serializers
services
validators
views
workers
bin
config
db
dist
lib
log
nanobox
public
spec
streaming
vendor
.buildpacks
.codeclimate.yml
.dockerignore
.editorconfig
.env.nanobox
.env.production.sample
.env.test
.env.vagrant
.eslintignore
.eslintrc.js
.foreman
.gitattributes
.gitignore
.haml-lint.yml
.nanoignore
.nvmrc
.profile
.rspec
.rubocop.yml
.ruby-version
.sass-lint.yml
.slugignore
.yarnclean
AUTHORS.md
Aptfile
CHANGELOG.md
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Capfile
Dockerfile
Gemfile
Gemfile.lock
LICENSE
Procfile
Procfile.dev
README.md
Rakefile
Vagrantfile
app.json
babel.config.js
boxfile.yml
config.ru
crowdin.yml
docker-compose.yml
package.json
postcss.config.js
priv-config
scalingo.json
yarn.lock
240 lines
7.0 KiB
JavaScript
240 lines
7.0 KiB
JavaScript
import React from 'react';
|
|
import AutosuggestAccountContainer from '../features/compose/containers/autosuggest_account_container';
|
|
import AutosuggestEmoji from './autosuggest_emoji';
|
|
import AutosuggestHashtag from './autosuggest_hashtag';
|
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
|
import PropTypes from 'prop-types';
|
|
import { isRtl } from '../rtl';
|
|
import ImmutablePureComponent from 'react-immutable-pure-component';
|
|
import Textarea from 'react-textarea-autosize';
|
|
import classNames from 'classnames';
|
|
|
|
const textAtCursorMatchesToken = (str, caretPosition) => {
|
|
let word;
|
|
|
|
let left = str.slice(0, caretPosition).search(/\S+$/);
|
|
let right = str.slice(caretPosition).search(/\s/);
|
|
|
|
if (right < 0) {
|
|
word = str.slice(left);
|
|
} else {
|
|
word = str.slice(left, right + caretPosition);
|
|
}
|
|
|
|
if (!word || word.trim().length < 3 || ['@', ':', '#'].indexOf(word[0]) === -1) {
|
|
return [null, null];
|
|
}
|
|
|
|
word = word.trim().toLowerCase();
|
|
|
|
if (word.length > 0) {
|
|
return [left + 1, word];
|
|
} else {
|
|
return [null, null];
|
|
}
|
|
};
|
|
|
|
export default class AutosuggestTextarea extends ImmutablePureComponent {
|
|
|
|
static propTypes = {
|
|
value: PropTypes.string,
|
|
suggestions: ImmutablePropTypes.list,
|
|
disabled: PropTypes.bool,
|
|
placeholder: PropTypes.string,
|
|
onSuggestionSelected: PropTypes.func.isRequired,
|
|
onSuggestionsClearRequested: PropTypes.func.isRequired,
|
|
onSuggestionsFetchRequested: PropTypes.func.isRequired,
|
|
onChange: PropTypes.func.isRequired,
|
|
onKeyUp: PropTypes.func,
|
|
onKeyDown: PropTypes.func,
|
|
onPaste: PropTypes.func.isRequired,
|
|
autoFocus: PropTypes.bool,
|
|
};
|
|
|
|
static defaultProps = {
|
|
autoFocus: true,
|
|
};
|
|
|
|
state = {
|
|
suggestionsHidden: true,
|
|
focused: false,
|
|
selectedSuggestion: 0,
|
|
lastToken: null,
|
|
tokenStart: 0,
|
|
};
|
|
|
|
onChange = (e) => {
|
|
const [ tokenStart, token ] = textAtCursorMatchesToken(e.target.value, e.target.selectionStart);
|
|
|
|
if (token !== null && this.state.lastToken !== token) {
|
|
this.setState({ lastToken: token, selectedSuggestion: 0, tokenStart });
|
|
this.props.onSuggestionsFetchRequested(token);
|
|
} else if (token === null) {
|
|
this.setState({ lastToken: null });
|
|
this.props.onSuggestionsClearRequested();
|
|
}
|
|
|
|
this.props.onChange(e);
|
|
}
|
|
|
|
onKeyDown = (e) => {
|
|
const { suggestions, disabled } = this.props;
|
|
const { selectedSuggestion, suggestionsHidden } = this.state;
|
|
|
|
if (disabled) {
|
|
e.preventDefault();
|
|
return;
|
|
}
|
|
|
|
if (e.which === 229 || e.isComposing) {
|
|
// Ignore key events during text composition
|
|
// e.key may be a name of the physical key even in this case (e.x. Safari / Chrome on Mac)
|
|
return;
|
|
}
|
|
|
|
switch(e.key) {
|
|
case 'Escape':
|
|
if (suggestions.size === 0 || suggestionsHidden) {
|
|
document.querySelector('.ui').parentElement.focus();
|
|
} else {
|
|
e.preventDefault();
|
|
this.setState({ suggestionsHidden: true });
|
|
}
|
|
|
|
break;
|
|
case 'ArrowDown':
|
|
if (suggestions.size > 0 && !suggestionsHidden) {
|
|
e.preventDefault();
|
|
this.setState({ selectedSuggestion: Math.min(selectedSuggestion + 1, suggestions.size - 1) });
|
|
}
|
|
|
|
break;
|
|
case 'ArrowUp':
|
|
if (suggestions.size > 0 && !suggestionsHidden) {
|
|
e.preventDefault();
|
|
this.setState({ selectedSuggestion: Math.max(selectedSuggestion - 1, 0) });
|
|
}
|
|
|
|
break;
|
|
case 'Enter':
|
|
case 'Tab':
|
|
// Select suggestion
|
|
if (this.state.lastToken !== null && suggestions.size > 0 && !suggestionsHidden) {
|
|
e.preventDefault();
|
|
e.stopPropagation();
|
|
this.props.onSuggestionSelected(this.state.tokenStart, this.state.lastToken, suggestions.get(selectedSuggestion));
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
if (e.defaultPrevented || !this.props.onKeyDown) {
|
|
return;
|
|
}
|
|
|
|
this.props.onKeyDown(e);
|
|
}
|
|
|
|
onBlur = () => {
|
|
this.setState({ suggestionsHidden: true, focused: false });
|
|
}
|
|
|
|
onFocus = (e) => {
|
|
this.setState({ focused: true });
|
|
if (this.props.onFocus) {
|
|
this.props.onFocus(e);
|
|
}
|
|
}
|
|
|
|
onSuggestionClick = (e) => {
|
|
const suggestion = this.props.suggestions.get(e.currentTarget.getAttribute('data-index'));
|
|
e.preventDefault();
|
|
this.props.onSuggestionSelected(this.state.tokenStart, this.state.lastToken, suggestion);
|
|
this.textarea.focus();
|
|
}
|
|
|
|
componentWillReceiveProps (nextProps) {
|
|
if (nextProps.suggestions !== this.props.suggestions && nextProps.suggestions.size > 0 && this.state.suggestionsHidden && this.state.focused) {
|
|
this.setState({ suggestionsHidden: false });
|
|
}
|
|
}
|
|
|
|
setTextarea = (c) => {
|
|
this.textarea = c;
|
|
}
|
|
|
|
onPaste = (e) => {
|
|
if (e.clipboardData && e.clipboardData.files.length === 1) {
|
|
this.props.onPaste(e.clipboardData.files);
|
|
e.preventDefault();
|
|
}
|
|
}
|
|
|
|
renderSuggestion = (suggestion, i) => {
|
|
const { selectedSuggestion } = this.state;
|
|
let inner, key;
|
|
|
|
if (suggestion.type === 'emoji') {
|
|
inner = <AutosuggestEmoji emoji={suggestion} />;
|
|
key = suggestion.id;
|
|
} else if (suggestion.type === 'hashtag') {
|
|
inner = <AutosuggestHashtag tag={suggestion} />;
|
|
key = suggestion.name;
|
|
} else if (suggestion.type === 'account') {
|
|
inner = <AutosuggestAccountContainer id={suggestion.id} />;
|
|
key = suggestion.id;
|
|
}
|
|
|
|
return (
|
|
<div role='button' tabIndex='0' key={key} data-index={i} className={classNames('autosuggest-textarea__suggestions__item', { selected: i === selectedSuggestion })} onMouseDown={this.onSuggestionClick}>
|
|
{inner}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
render () {
|
|
const { value, suggestions, disabled, placeholder, onKeyUp, autoFocus, children } = this.props;
|
|
const { suggestionsHidden } = this.state;
|
|
const style = { direction: 'ltr' };
|
|
|
|
if (isRtl(value)) {
|
|
style.direction = 'rtl';
|
|
}
|
|
|
|
return [
|
|
<div className='compose-form__autosuggest-wrapper' key='autosuggest-wrapper'>
|
|
<div className='autosuggest-textarea'>
|
|
<label>
|
|
<span style={{ display: 'none' }}>{placeholder}</span>
|
|
|
|
<Textarea
|
|
inputRef={this.setTextarea}
|
|
className='autosuggest-textarea__textarea'
|
|
disabled={disabled}
|
|
placeholder={placeholder}
|
|
autoFocus={autoFocus}
|
|
value={value}
|
|
onChange={this.onChange}
|
|
onKeyDown={this.onKeyDown}
|
|
onKeyUp={onKeyUp}
|
|
onFocus={this.onFocus}
|
|
onBlur={this.onBlur}
|
|
onPaste={this.onPaste}
|
|
style={style}
|
|
aria-autocomplete='list'
|
|
/>
|
|
</label>
|
|
</div>
|
|
{children}
|
|
</div>,
|
|
|
|
<div className='autosuggest-textarea__suggestions-wrapper' key='suggestions-wrapper'>
|
|
<div className={`autosuggest-textarea__suggestions ${suggestionsHidden || suggestions.isEmpty() ? '' : 'autosuggest-textarea__suggestions--visible'}`}>
|
|
{suggestions.map(this.renderSuggestion)}
|
|
</div>
|
|
</div>,
|
|
];
|
|
}
|
|
|
|
}
|