2022-05-16 19:18:35 +10:00
|
|
|
import PropTypes from 'prop-types';
|
2023-05-24 01:15:17 +10:00
|
|
|
import { PureComponent } from 'react';
|
|
|
|
|
2022-05-16 19:18:35 +10:00
|
|
|
import { injectIntl, defineMessages } from 'react-intl';
|
2023-05-24 01:15:17 +10:00
|
|
|
|
2022-05-16 19:18:35 +10:00
|
|
|
import classNames from 'classnames';
|
2023-05-24 01:15:17 +10:00
|
|
|
|
|
|
|
import { supportsPassiveEvents } from 'detect-passive-events';
|
|
|
|
import fuzzysort from 'fuzzysort';
|
|
|
|
import Overlay from 'react-overlays/Overlay';
|
|
|
|
|
2024-02-24 02:32:13 +11:00
|
|
|
import CancelIcon from '@/material-icons/400-24px/cancel-fill.svg?react';
|
|
|
|
import SearchIcon from '@/material-icons/400-24px/search.svg?react';
|
|
|
|
import TranslateIcon from '@/material-icons/400-24px/translate.svg?react';
|
2024-01-26 02:41:31 +11:00
|
|
|
import { Icon } from 'mastodon/components/icon';
|
2022-05-16 19:18:35 +10:00
|
|
|
import { languages as preloadedLanguages } from 'mastodon/initial_state';
|
|
|
|
|
|
|
|
const messages = defineMessages({
|
|
|
|
changeLanguage: { id: 'compose.language.change', defaultMessage: 'Change language' },
|
|
|
|
search: { id: 'compose.language.search', defaultMessage: 'Search languages...' },
|
|
|
|
clear: { id: 'emoji_button.clear', defaultMessage: 'Clear' },
|
|
|
|
});
|
|
|
|
|
2023-05-22 23:48:01 +10:00
|
|
|
const listenerOptions = supportsPassiveEvents ? { passive: true, capture: true } : true;
|
2022-05-16 19:18:35 +10:00
|
|
|
|
2023-05-23 18:52:27 +10:00
|
|
|
class LanguageDropdownMenu extends PureComponent {
|
2022-05-16 19:18:35 +10:00
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
value: PropTypes.string.isRequired,
|
|
|
|
frequentlyUsedLanguages: PropTypes.arrayOf(PropTypes.string).isRequired,
|
|
|
|
onClose: PropTypes.func.isRequired,
|
|
|
|
onChange: PropTypes.func.isRequired,
|
|
|
|
languages: PropTypes.arrayOf(PropTypes.arrayOf(PropTypes.string)),
|
|
|
|
intl: PropTypes.object,
|
|
|
|
};
|
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
languages: preloadedLanguages,
|
|
|
|
};
|
|
|
|
|
|
|
|
state = {
|
|
|
|
searchValue: '',
|
|
|
|
};
|
|
|
|
|
|
|
|
handleDocumentClick = e => {
|
|
|
|
if (this.node && !this.node.contains(e.target)) {
|
|
|
|
this.props.onClose();
|
2023-05-22 23:48:01 +10:00
|
|
|
e.stopPropagation();
|
2022-05-16 19:18:35 +10:00
|
|
|
}
|
2023-01-30 11:45:35 +11:00
|
|
|
};
|
2022-05-16 19:18:35 +10:00
|
|
|
|
|
|
|
componentDidMount () {
|
2023-05-22 23:48:01 +10:00
|
|
|
document.addEventListener('click', this.handleDocumentClick, { capture: true });
|
2022-05-16 19:18:35 +10:00
|
|
|
document.addEventListener('touchend', this.handleDocumentClick, listenerOptions);
|
2022-11-08 13:52:52 +11:00
|
|
|
|
|
|
|
// Because of https://github.com/react-bootstrap/react-bootstrap/issues/2614 we need
|
|
|
|
// to wait for a frame before focusing
|
|
|
|
requestAnimationFrame(() => {
|
|
|
|
if (this.node) {
|
|
|
|
const element = this.node.querySelector('input[type="search"]');
|
|
|
|
if (element) element.focus();
|
|
|
|
}
|
|
|
|
});
|
2022-05-16 19:18:35 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount () {
|
2023-05-22 23:48:01 +10:00
|
|
|
document.removeEventListener('click', this.handleDocumentClick, { capture: true });
|
2022-05-16 19:18:35 +10:00
|
|
|
document.removeEventListener('touchend', this.handleDocumentClick, listenerOptions);
|
|
|
|
}
|
|
|
|
|
|
|
|
setRef = c => {
|
|
|
|
this.node = c;
|
2023-01-30 11:45:35 +11:00
|
|
|
};
|
2022-05-16 19:18:35 +10:00
|
|
|
|
|
|
|
setListRef = c => {
|
|
|
|
this.listNode = c;
|
2023-01-30 11:45:35 +11:00
|
|
|
};
|
2022-05-16 19:18:35 +10:00
|
|
|
|
|
|
|
handleSearchChange = ({ target }) => {
|
|
|
|
this.setState({ searchValue: target.value });
|
2023-01-30 11:45:35 +11:00
|
|
|
};
|
2022-05-16 19:18:35 +10:00
|
|
|
|
|
|
|
search () {
|
|
|
|
const { languages, value, frequentlyUsedLanguages } = this.props;
|
|
|
|
const { searchValue } = this.state;
|
|
|
|
|
|
|
|
if (searchValue === '') {
|
|
|
|
return [...languages].sort((a, b) => {
|
|
|
|
// Push current selection to the top of the list
|
|
|
|
|
|
|
|
if (a[0] === value) {
|
|
|
|
return -1;
|
|
|
|
} else if (b[0] === value) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
// Sort according to frequently used languages
|
|
|
|
|
|
|
|
const indexOfA = frequentlyUsedLanguages.indexOf(a[0]);
|
|
|
|
const indexOfB = frequentlyUsedLanguages.indexOf(b[0]);
|
|
|
|
|
|
|
|
return ((indexOfA > -1 ? indexOfA : Infinity) - (indexOfB > -1 ? indexOfB : Infinity));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return fuzzysort.go(searchValue, languages, {
|
|
|
|
keys: ['0', '1', '2'],
|
|
|
|
limit: 5,
|
|
|
|
threshold: -10000,
|
|
|
|
}).map(result => result.obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
frequentlyUsed () {
|
|
|
|
const { languages, value } = this.props;
|
|
|
|
const current = languages.find(lang => lang[0] === value);
|
|
|
|
const results = [];
|
|
|
|
|
|
|
|
if (current) {
|
|
|
|
results.push(current);
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
handleClick = e => {
|
|
|
|
const value = e.currentTarget.getAttribute('data-index');
|
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
this.props.onClose();
|
|
|
|
this.props.onChange(value);
|
2023-01-30 11:45:35 +11:00
|
|
|
};
|
2022-05-16 19:18:35 +10:00
|
|
|
|
|
|
|
handleKeyDown = e => {
|
|
|
|
const { onClose } = this.props;
|
|
|
|
const index = Array.from(this.listNode.childNodes).findIndex(node => node === e.currentTarget);
|
|
|
|
|
|
|
|
let element = null;
|
|
|
|
|
|
|
|
switch(e.key) {
|
|
|
|
case 'Escape':
|
|
|
|
onClose();
|
|
|
|
break;
|
2024-02-27 22:41:19 +11:00
|
|
|
case ' ':
|
2022-05-16 19:18:35 +10:00
|
|
|
case 'Enter':
|
|
|
|
this.handleClick(e);
|
|
|
|
break;
|
|
|
|
case 'ArrowDown':
|
|
|
|
element = this.listNode.childNodes[index + 1] || this.listNode.firstChild;
|
|
|
|
break;
|
|
|
|
case 'ArrowUp':
|
|
|
|
element = this.listNode.childNodes[index - 1] || this.listNode.lastChild;
|
|
|
|
break;
|
|
|
|
case 'Tab':
|
|
|
|
if (e.shiftKey) {
|
|
|
|
element = this.listNode.childNodes[index - 1] || this.listNode.lastChild;
|
|
|
|
} else {
|
|
|
|
element = this.listNode.childNodes[index + 1] || this.listNode.firstChild;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'Home':
|
|
|
|
element = this.listNode.firstChild;
|
|
|
|
break;
|
|
|
|
case 'End':
|
|
|
|
element = this.listNode.lastChild;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (element) {
|
|
|
|
element.focus();
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
}
|
2023-01-30 11:45:35 +11:00
|
|
|
};
|
2022-05-16 19:18:35 +10:00
|
|
|
|
|
|
|
handleSearchKeyDown = e => {
|
|
|
|
const { onChange, onClose } = this.props;
|
|
|
|
const { searchValue } = this.state;
|
|
|
|
|
|
|
|
let element = null;
|
|
|
|
|
|
|
|
switch(e.key) {
|
|
|
|
case 'Tab':
|
|
|
|
case 'ArrowDown':
|
|
|
|
element = this.listNode.firstChild;
|
|
|
|
|
|
|
|
if (element) {
|
|
|
|
element.focus();
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
case 'Enter':
|
|
|
|
element = this.listNode.firstChild;
|
|
|
|
|
|
|
|
if (element) {
|
|
|
|
onChange(element.getAttribute('data-index'));
|
|
|
|
onClose();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'Escape':
|
|
|
|
if (searchValue !== '') {
|
|
|
|
e.preventDefault();
|
|
|
|
this.handleClear();
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2023-01-30 11:45:35 +11:00
|
|
|
};
|
2022-05-16 19:18:35 +10:00
|
|
|
|
|
|
|
handleClear = () => {
|
|
|
|
this.setState({ searchValue: '' });
|
2023-01-30 11:45:35 +11:00
|
|
|
};
|
2022-05-16 19:18:35 +10:00
|
|
|
|
|
|
|
renderItem = lang => {
|
|
|
|
const { value } = this.props;
|
|
|
|
|
|
|
|
return (
|
2023-04-05 00:33:44 +10:00
|
|
|
<div key={lang[0]} role='option' tabIndex={0} data-index={lang[0]} className={classNames('language-dropdown__dropdown__results__item', { active: lang[0] === value })} aria-selected={lang[0] === value} onClick={this.handleClick} onKeyDown={this.handleKeyDown}>
|
2023-02-21 19:23:39 +11:00
|
|
|
<span className='language-dropdown__dropdown__results__item__native-name' lang={lang[0]}>{lang[2]}</span> <span className='language-dropdown__dropdown__results__item__common-name'>({lang[1]})</span>
|
2022-05-16 19:18:35 +10:00
|
|
|
</div>
|
|
|
|
);
|
2023-01-30 11:45:35 +11:00
|
|
|
};
|
2022-05-16 19:18:35 +10:00
|
|
|
|
|
|
|
render () {
|
2023-01-12 07:58:46 +11:00
|
|
|
const { intl } = this.props;
|
|
|
|
const { searchValue } = this.state;
|
2022-05-16 19:18:35 +10:00
|
|
|
const isSearching = searchValue !== '';
|
|
|
|
const results = this.search();
|
|
|
|
|
|
|
|
return (
|
2023-01-12 07:58:46 +11:00
|
|
|
<div ref={this.setRef}>
|
|
|
|
<div className='emoji-mart-search'>
|
|
|
|
<input type='search' value={searchValue} onChange={this.handleSearchChange} onKeyDown={this.handleSearchKeyDown} placeholder={intl.formatMessage(messages.search)} />
|
2024-01-26 02:41:31 +11:00
|
|
|
<button type='button' className='emoji-mart-search-icon' disabled={!isSearching} aria-label={intl.formatMessage(messages.clear)} onClick={this.handleClear}><Icon icon={!isSearching ? SearchIcon : CancelIcon} /></button>
|
2023-01-12 07:58:46 +11:00
|
|
|
</div>
|
2022-05-16 19:18:35 +10:00
|
|
|
|
2023-01-12 07:58:46 +11:00
|
|
|
<div className='language-dropdown__dropdown__results emoji-mart-scroll' role='listbox' ref={this.setListRef}>
|
|
|
|
{results.map(this.renderItem)}
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-05-16 19:18:35 +10:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-05-23 18:52:27 +10:00
|
|
|
class LanguageDropdown extends PureComponent {
|
2022-05-16 19:18:35 +10:00
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
value: PropTypes.string,
|
|
|
|
frequentlyUsedLanguages: PropTypes.arrayOf(PropTypes.string),
|
|
|
|
intl: PropTypes.object.isRequired,
|
|
|
|
onChange: PropTypes.func,
|
|
|
|
onClose: PropTypes.func,
|
|
|
|
};
|
|
|
|
|
|
|
|
state = {
|
|
|
|
open: false,
|
|
|
|
placement: 'bottom',
|
|
|
|
};
|
|
|
|
|
2023-01-12 07:58:46 +11:00
|
|
|
handleToggle = () => {
|
2022-05-16 19:18:35 +10:00
|
|
|
if (this.state.open && this.activeElement) {
|
|
|
|
this.activeElement.focus({ preventScroll: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setState({ open: !this.state.open });
|
2023-01-30 11:45:35 +11:00
|
|
|
};
|
2022-05-16 19:18:35 +10:00
|
|
|
|
|
|
|
handleClose = () => {
|
|
|
|
const { value, onClose } = this.props;
|
|
|
|
|
|
|
|
if (this.state.open && this.activeElement) {
|
|
|
|
this.activeElement.focus({ preventScroll: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setState({ open: false });
|
|
|
|
onClose(value);
|
2023-01-30 11:45:35 +11:00
|
|
|
};
|
2022-05-16 19:18:35 +10:00
|
|
|
|
|
|
|
handleChange = value => {
|
|
|
|
const { onChange } = this.props;
|
|
|
|
onChange(value);
|
2023-01-30 11:45:35 +11:00
|
|
|
};
|
2022-05-16 19:18:35 +10:00
|
|
|
|
2023-01-12 07:58:46 +11:00
|
|
|
setTargetRef = c => {
|
|
|
|
this.target = c;
|
2023-01-30 11:45:35 +11:00
|
|
|
};
|
2023-01-12 07:58:46 +11:00
|
|
|
|
|
|
|
findTarget = () => {
|
|
|
|
return this.target;
|
2023-01-30 11:45:35 +11:00
|
|
|
};
|
2023-01-12 07:58:46 +11:00
|
|
|
|
|
|
|
handleOverlayEnter = (state) => {
|
|
|
|
this.setState({ placement: state.placement });
|
2023-01-30 11:45:35 +11:00
|
|
|
};
|
2023-01-12 07:58:46 +11:00
|
|
|
|
2022-05-16 19:18:35 +10:00
|
|
|
render () {
|
|
|
|
const { value, intl, frequentlyUsedLanguages } = this.props;
|
|
|
|
const { open, placement } = this.state;
|
2024-01-26 02:41:31 +11:00
|
|
|
const current = preloadedLanguages.find(lang => lang[0] === value) ?? [];
|
2022-05-16 19:18:35 +10:00
|
|
|
|
|
|
|
return (
|
2024-01-26 02:41:31 +11:00
|
|
|
<div ref={this.setTargetRef} onKeyDown={this.handleKeyDown}>
|
|
|
|
<button
|
|
|
|
type='button'
|
|
|
|
title={intl.formatMessage(messages.changeLanguage)}
|
|
|
|
aria-expanded={open}
|
|
|
|
onClick={this.handleToggle}
|
|
|
|
onMouseDown={this.handleMouseDown}
|
|
|
|
onKeyDown={this.handleButtonKeyDown}
|
|
|
|
className={classNames('dropdown-button', { active: open })}
|
|
|
|
>
|
|
|
|
<Icon icon={TranslateIcon} />
|
|
|
|
<span className='dropdown-button__label'>{current[2] ?? value}</span>
|
|
|
|
</button>
|
|
|
|
|
|
|
|
<Overlay show={open} offset={[5, 5]} placement={placement} flip target={this.findTarget} popperConfig={{ strategy: 'fixed', onFirstUpdate: this.handleOverlayEnter }}>
|
2023-01-12 07:58:46 +11:00
|
|
|
{({ props, placement }) => (
|
2023-01-13 02:43:02 +11:00
|
|
|
<div {...props}>
|
2023-01-12 07:58:46 +11:00
|
|
|
<div className={`dropdown-animation language-dropdown__dropdown ${placement}`} >
|
|
|
|
<LanguageDropdownMenu
|
|
|
|
value={value}
|
|
|
|
frequentlyUsedLanguages={frequentlyUsedLanguages}
|
|
|
|
onClose={this.handleClose}
|
|
|
|
onChange={this.handleChange}
|
|
|
|
intl={intl}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)}
|
2022-05-16 19:18:35 +10:00
|
|
|
</Overlay>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2023-03-24 13:17:53 +11:00
|
|
|
|
|
|
|
export default injectIntl(LanguageDropdown);
|