2017-12-24 07:16:45 +01:00
|
|
|
// Package imports.
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import React from 'react';
|
|
|
|
|
|
|
|
// Components.
|
|
|
|
import AccountContainer from 'flavours/glitch/containers/account_container';
|
|
|
|
|
|
|
|
// Utils.
|
2018-01-05 06:17:30 +01:00
|
|
|
import { unicodeMapping } from 'flavours/glitch/util/emoji';
|
2017-12-24 07:16:45 +01:00
|
|
|
import { assignHandlers } from 'flavours/glitch/util/react_helpers';
|
|
|
|
|
|
|
|
// Gets our asset host from the environment, if available.
|
|
|
|
const assetHost = ((process || {}).env || {}).CDN_HOST || '';
|
|
|
|
|
|
|
|
// Handlers.
|
|
|
|
const handlers = {
|
|
|
|
|
|
|
|
// Handles a click on a suggestion.
|
2018-01-03 21:36:21 +01:00
|
|
|
handleClick (e) {
|
2017-12-24 07:16:45 +01:00
|
|
|
const {
|
|
|
|
index,
|
|
|
|
onClick,
|
|
|
|
} = this.props;
|
|
|
|
if (onClick) {
|
|
|
|
e.preventDefault();
|
2018-01-06 05:04:13 +01:00
|
|
|
e.stopPropagation(); // Prevents following account links
|
2017-12-24 07:16:45 +01:00
|
|
|
onClick(index);
|
|
|
|
}
|
|
|
|
},
|
2018-01-06 05:04:13 +01:00
|
|
|
|
|
|
|
// This prevents the focus from changing, which would mess with
|
|
|
|
// our suggestion code.
|
|
|
|
handleMouseDown (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
},
|
2017-12-24 07:16:45 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// The component.
|
|
|
|
export default class ComposerTextareaSuggestionsItem extends React.Component {
|
|
|
|
|
|
|
|
// Constructor.
|
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
|
|
|
assignHandlers(this, handlers);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rendering.
|
|
|
|
render () {
|
2018-01-06 05:04:13 +01:00
|
|
|
const {
|
|
|
|
handleMouseDown,
|
|
|
|
handleClick,
|
|
|
|
} = this.handlers;
|
2017-12-24 07:16:45 +01:00
|
|
|
const {
|
|
|
|
selected,
|
|
|
|
suggestion,
|
|
|
|
} = this.props;
|
|
|
|
const computedClass = classNames('composer--textarea--suggestions--item', { selected });
|
|
|
|
|
|
|
|
// The result.
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className={computedClass}
|
2018-01-06 05:04:13 +01:00
|
|
|
onMouseDown={handleMouseDown}
|
|
|
|
onClickCapture={handleClick} // Jumps in front of contents
|
2017-12-29 23:55:06 +01:00
|
|
|
role='button'
|
|
|
|
tabIndex='0'
|
2017-12-24 07:16:45 +01:00
|
|
|
>
|
|
|
|
{ // If the suggestion is an object, then we render an emoji.
|
|
|
|
// Otherwise, we render an account.
|
|
|
|
typeof suggestion === 'object' ? function () {
|
|
|
|
const url = function () {
|
|
|
|
if (suggestion.custom) {
|
|
|
|
return suggestion.imageUrl;
|
|
|
|
} else {
|
|
|
|
const mapping = unicodeMapping[suggestion.native] || unicodeMapping[suggestion.native.replace(/\uFE0F$/, '')];
|
|
|
|
if (!mapping) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return `${assetHost}/emoji/${mapping.filename}.svg`;
|
|
|
|
}
|
|
|
|
}();
|
|
|
|
return url ? (
|
|
|
|
<div className='emoji'>
|
|
|
|
<img
|
|
|
|
alt={suggestion.native || suggestion.colons}
|
|
|
|
className='emojione'
|
|
|
|
src={url}
|
|
|
|
/>
|
|
|
|
{suggestion.colons}
|
|
|
|
</div>
|
|
|
|
) : null;
|
|
|
|
}() : (
|
|
|
|
<AccountContainer
|
|
|
|
id={suggestion}
|
|
|
|
small
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Props.
|
|
|
|
ComposerTextareaSuggestionsItem.propTypes = {
|
|
|
|
index: PropTypes.number,
|
|
|
|
onClick: PropTypes.func,
|
|
|
|
selected: PropTypes.bool,
|
|
|
|
suggestion: PropTypes.oneOfType([PropTypes.object, PropTypes.string]),
|
|
|
|
};
|