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.

409 lines
11 KiB

  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { defineMessages, injectIntl } from 'react-intl';
  4. import { EmojiPicker as EmojiPickerAsync } from '../../ui/util/async-components';
  5. import Overlay from 'react-overlays/lib/Overlay';
  6. import classNames from 'classnames';
  7. import ImmutablePropTypes from 'react-immutable-proptypes';
  8. import detectPassiveEvents from 'detect-passive-events';
  9. import { buildCustomEmojis } from '../../emoji/emoji';
  10. const messages = defineMessages({
  11. emoji: { id: 'emoji_button.label', defaultMessage: 'Insert emoji' },
  12. emoji_search: { id: 'emoji_button.search', defaultMessage: 'Search...' },
  13. emoji_not_found: { id: 'emoji_button.not_found', defaultMessage: 'No emojos!! (╯°□°)╯︵ ┻━┻' },
  14. custom: { id: 'emoji_button.custom', defaultMessage: 'Custom' },
  15. recent: { id: 'emoji_button.recent', defaultMessage: 'Frequently used' },
  16. search_results: { id: 'emoji_button.search_results', defaultMessage: 'Search results' },
  17. people: { id: 'emoji_button.people', defaultMessage: 'People' },
  18. nature: { id: 'emoji_button.nature', defaultMessage: 'Nature' },
  19. food: { id: 'emoji_button.food', defaultMessage: 'Food & Drink' },
  20. activity: { id: 'emoji_button.activity', defaultMessage: 'Activity' },
  21. travel: { id: 'emoji_button.travel', defaultMessage: 'Travel & Places' },
  22. objects: { id: 'emoji_button.objects', defaultMessage: 'Objects' },
  23. symbols: { id: 'emoji_button.symbols', defaultMessage: 'Symbols' },
  24. flags: { id: 'emoji_button.flags', defaultMessage: 'Flags' },
  25. });
  26. const assetHost = process.env.CDN_HOST || '';
  27. let EmojiPicker, Emoji; // load asynchronously
  28. const backgroundImageFn = () => `${assetHost}/emoji/sheet_10.png`;
  29. const listenerOptions = detectPassiveEvents.hasSupport ? { passive: true } : false;
  30. const categoriesSort = [
  31. 'recent',
  32. 'custom',
  33. 'people',
  34. 'nature',
  35. 'foods',
  36. 'activity',
  37. 'places',
  38. 'objects',
  39. 'symbols',
  40. 'flags',
  41. ];
  42. class ModifierPickerMenu extends React.PureComponent {
  43. static propTypes = {
  44. active: PropTypes.bool,
  45. onSelect: PropTypes.func.isRequired,
  46. onClose: PropTypes.func.isRequired,
  47. modifier: PropTypes.number,
  48. };
  49. handleClick = e => {
  50. this.props.onSelect(e.currentTarget.getAttribute('data-index') * 1);
  51. }
  52. componentWillReceiveProps (nextProps) {
  53. if (nextProps.active) {
  54. this.attachListeners();
  55. } else {
  56. this.removeListeners();
  57. }
  58. }
  59. componentWillUnmount () {
  60. this.removeListeners();
  61. }
  62. handleDocumentClick = e => {
  63. if (this.node && !this.node.contains(e.target)) {
  64. this.props.onClose();
  65. }
  66. }
  67. attachListeners () {
  68. document.addEventListener('click', this.handleDocumentClick, false);
  69. document.addEventListener('touchend', this.handleDocumentClick, listenerOptions);
  70. }
  71. removeListeners () {
  72. document.removeEventListener('click', this.handleDocumentClick, false);
  73. document.removeEventListener('touchend', this.handleDocumentClick, listenerOptions);
  74. }
  75. setRef = c => {
  76. this.node = c;
  77. if (this.node) {
  78. this.node.querySelector('li:first-child button').focus(); // focus the first element when opened
  79. }
  80. }
  81. render () {
  82. const { active, modifier } = this.props;
  83. return (
  84. <ul
  85. className='emoji-picker-dropdown__modifiers__menu'
  86. style={{ display: active ? 'block' : 'none' }}
  87. role='menuitem'
  88. ref={this.setRef}
  89. >
  90. {[1, 2, 3, 4, 5, 6].map(i => (
  91. <li
  92. onClick={this.handleClick}
  93. role='menuitemradio'
  94. aria-checked={i === (modifier || 1)}
  95. data-index={i}
  96. key={i}
  97. >
  98. <Emoji
  99. emoji='fist' set='twitter' size={22} sheetSize={32} skin={i}
  100. backgroundImageFn={backgroundImageFn}
  101. />
  102. </li>
  103. ))}
  104. </ul>
  105. );
  106. }
  107. }
  108. class ModifierPicker extends React.PureComponent {
  109. static propTypes = {
  110. active: PropTypes.bool,
  111. modifier: PropTypes.number,
  112. onChange: PropTypes.func,
  113. onClose: PropTypes.func,
  114. onOpen: PropTypes.func,
  115. };
  116. handleClick = () => {
  117. if (this.props.active) {
  118. this.props.onClose();
  119. } else {
  120. this.props.onOpen();
  121. }
  122. }
  123. handleSelect = modifier => {
  124. this.props.onChange(modifier);
  125. this.props.onClose();
  126. }
  127. render () {
  128. const { active, modifier } = this.props;
  129. function setRef(ref) {
  130. if (!ref) {
  131. return;
  132. }
  133. // TODO: It would be nice if we could pass props directly to emoji-mart's buttons.
  134. const button = ref.querySelector('button');
  135. button.setAttribute('aria-haspopup', 'true');
  136. button.setAttribute('aria-expanded', active);
  137. }
  138. return (
  139. <div className='emoji-picker-dropdown__modifiers'>
  140. <div ref={setRef}>
  141. <Emoji emoji='fist' set='twitter' size={22} sheetSize={32} skin={modifier} onClick={this.handleClick} backgroundImageFn={backgroundImageFn} />
  142. </div>
  143. <ModifierPickerMenu active={active} modifier={modifier} onSelect={this.handleSelect} onClose={this.props.onClose} />
  144. </div>
  145. );
  146. }
  147. }
  148. @injectIntl
  149. class EmojiPickerMenu extends React.PureComponent {
  150. static propTypes = {
  151. custom_emojis: ImmutablePropTypes.list,
  152. frequentlyUsedEmojis: PropTypes.arrayOf(PropTypes.string),
  153. loading: PropTypes.bool,
  154. onClose: PropTypes.func.isRequired,
  155. onPick: PropTypes.func.isRequired,
  156. style: PropTypes.object,
  157. placement: PropTypes.string,
  158. arrowOffsetLeft: PropTypes.string,
  159. arrowOffsetTop: PropTypes.string,
  160. intl: PropTypes.object.isRequired,
  161. skinTone: PropTypes.number.isRequired,
  162. onSkinTone: PropTypes.func.isRequired,
  163. };
  164. static defaultProps = {
  165. style: {},
  166. loading: true,
  167. frequentlyUsedEmojis: [],
  168. };
  169. state = {
  170. modifierOpen: false,
  171. placement: null,
  172. };
  173. handleDocumentClick = e => {
  174. if (this.node && !this.node.contains(e.target)) {
  175. this.props.onClose();
  176. }
  177. }
  178. componentDidMount () {
  179. document.addEventListener('click', this.handleDocumentClick, false);
  180. document.addEventListener('touchend', this.handleDocumentClick, listenerOptions);
  181. }
  182. componentWillUnmount () {
  183. document.removeEventListener('click', this.handleDocumentClick, false);
  184. document.removeEventListener('touchend', this.handleDocumentClick, listenerOptions);
  185. }
  186. setRef = c => {
  187. this.node = c;
  188. }
  189. getI18n = () => {
  190. const { intl } = this.props;
  191. return {
  192. search: intl.formatMessage(messages.emoji_search),
  193. notfound: intl.formatMessage(messages.emoji_not_found),
  194. categories: {
  195. search: intl.formatMessage(messages.search_results),
  196. recent: intl.formatMessage(messages.recent),
  197. people: intl.formatMessage(messages.people),
  198. nature: intl.formatMessage(messages.nature),
  199. foods: intl.formatMessage(messages.food),
  200. activity: intl.formatMessage(messages.activity),
  201. places: intl.formatMessage(messages.travel),
  202. objects: intl.formatMessage(messages.objects),
  203. symbols: intl.formatMessage(messages.symbols),
  204. flags: intl.formatMessage(messages.flags),
  205. custom: intl.formatMessage(messages.custom),
  206. },
  207. };
  208. }
  209. handleClick = emoji => {
  210. if (!emoji.native) {
  211. emoji.native = emoji.colons;
  212. }
  213. this.props.onClose();
  214. this.props.onPick(emoji);
  215. }
  216. handleModifierOpen = () => {
  217. this.setState({ modifierOpen: true });
  218. }
  219. handleModifierClose = () => {
  220. this.setState({ modifierOpen: false });
  221. }
  222. handleModifierChange = modifier => {
  223. this.props.onSkinTone(modifier);
  224. }
  225. render () {
  226. const { loading, style, intl, custom_emojis, skinTone, frequentlyUsedEmojis } = this.props;
  227. if (loading) {
  228. return <div style={{ width: 299 }} />;
  229. }
  230. const title = intl.formatMessage(messages.emoji);
  231. const { modifierOpen } = this.state;
  232. return (
  233. <div className={classNames('emoji-picker-dropdown__menu', { selecting: modifierOpen })} style={style} ref={this.setRef}>
  234. <EmojiPicker
  235. perLine={8}
  236. emojiSize={22}
  237. sheetSize={32}
  238. custom={buildCustomEmojis(custom_emojis)}
  239. color=''
  240. emoji=''
  241. set='twitter'
  242. title={title}
  243. i18n={this.getI18n()}
  244. onClick={this.handleClick}
  245. include={categoriesSort}
  246. recent={frequentlyUsedEmojis}
  247. skin={skinTone}
  248. showPreview={false}
  249. backgroundImageFn={backgroundImageFn}
  250. autoFocus
  251. emojiTooltip
  252. />
  253. <ModifierPicker
  254. active={modifierOpen}
  255. modifier={skinTone}
  256. onOpen={this.handleModifierOpen}
  257. onClose={this.handleModifierClose}
  258. onChange={this.handleModifierChange}
  259. />
  260. </div>
  261. );
  262. }
  263. }
  264. export default @injectIntl
  265. class EmojiPickerDropdown extends React.PureComponent {
  266. static propTypes = {
  267. custom_emojis: ImmutablePropTypes.list,
  268. frequentlyUsedEmojis: PropTypes.arrayOf(PropTypes.string),
  269. intl: PropTypes.object.isRequired,
  270. onPickEmoji: PropTypes.func.isRequired,
  271. onSkinTone: PropTypes.func.isRequired,
  272. skinTone: PropTypes.number.isRequired,
  273. };
  274. state = {
  275. active: false,
  276. loading: false,
  277. };
  278. setRef = (c) => {
  279. this.dropdown = c;
  280. }
  281. onShowDropdown = ({ target }) => {
  282. this.setState({ active: true });
  283. if (!EmojiPicker) {
  284. this.setState({ loading: true });
  285. EmojiPickerAsync().then(EmojiMart => {
  286. EmojiPicker = EmojiMart.Picker;
  287. Emoji = EmojiMart.Emoji;
  288. this.setState({ loading: false });
  289. }).catch(() => {
  290. this.setState({ loading: false });
  291. });
  292. }
  293. const { top } = target.getBoundingClientRect();
  294. this.setState({ placement: top * 2 < innerHeight ? 'bottom' : 'top' });
  295. }
  296. onHideDropdown = () => {
  297. this.setState({ active: false });
  298. }
  299. onToggle = (e) => {
  300. if (!this.state.loading && (!e.key || e.key === 'Enter')) {
  301. if (this.state.active) {
  302. this.onHideDropdown();
  303. } else {
  304. this.onShowDropdown(e);
  305. }
  306. }
  307. }
  308. handleKeyDown = e => {
  309. if (e.key === 'Escape') {
  310. this.onHideDropdown();
  311. }
  312. }
  313. setTargetRef = c => {
  314. this.target = c;
  315. }
  316. findTarget = () => {
  317. return this.target;
  318. }
  319. render () {
  320. const { intl, onPickEmoji, onSkinTone, skinTone, frequentlyUsedEmojis } = this.props;
  321. const title = intl.formatMessage(messages.emoji);
  322. const { active, loading, placement } = this.state;
  323. return (
  324. <div className='emoji-picker-dropdown' onKeyDown={this.handleKeyDown}>
  325. <div ref={this.setTargetRef} className='emoji-button' title={title} aria-label={title} aria-expanded={active} role='button' onClick={this.onToggle} onKeyDown={this.onToggle} tabIndex={0}>
  326. <img
  327. className={classNames('emojione', { 'pulse-loading': active && loading })}
  328. alt='🙂'
  329. src={`${assetHost}/emoji/1f602.svg`}
  330. />
  331. </div>
  332. <Overlay show={active} placement={placement} target={this.findTarget}>
  333. <EmojiPickerMenu
  334. custom_emojis={this.props.custom_emojis}
  335. loading={loading}
  336. onClose={this.onHideDropdown}
  337. onPick={onPickEmoji}
  338. onSkinTone={onSkinTone}
  339. skinTone={skinTone}
  340. frequentlyUsedEmojis={frequentlyUsedEmojis}
  341. />
  342. </Overlay>
  343. </div>
  344. );
  345. }
  346. }