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.

413 lines
12 KiB

  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
  4. import { EmojiPicker as EmojiPickerAsync } from '../../ui/util/async-components';
  5. import Overlay from 'react-overlays/Overlay';
  6. import classNames from 'classnames';
  7. import ImmutablePropTypes from 'react-immutable-proptypes';
  8. import { supportsPassiveEvents } from 'detect-passive-events';
  9. import { buildCustomEmojis, categoriesFromEmojis } from '../../emoji/emoji';
  10. import { assetHost } from 'mastodon/utils/config';
  11. const messages = defineMessages({
  12. emoji: { id: 'emoji_button.label', defaultMessage: 'Insert emoji' },
  13. emoji_search: { id: 'emoji_button.search', defaultMessage: 'Search...' },
  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. let EmojiPicker, Emoji; // load asynchronously
  27. const listenerOptions = supportsPassiveEvents ? { passive: true } : false;
  28. const backgroundImageFn = () => `${assetHost}/emoji/sheet_13.png`;
  29. const notFoundFn = () => (
  30. <div className='emoji-mart-no-results'>
  31. <Emoji
  32. emoji='sleuth_or_spy'
  33. set='twitter'
  34. size={32}
  35. sheetSize={32}
  36. backgroundImageFn={backgroundImageFn}
  37. />
  38. <div className='emoji-mart-no-results-label'>
  39. <FormattedMessage id='emoji_button.not_found' defaultMessage='No matching emojis found' />
  40. </div>
  41. </div>
  42. );
  43. class ModifierPickerMenu extends React.PureComponent {
  44. static propTypes = {
  45. active: PropTypes.bool,
  46. onSelect: PropTypes.func.isRequired,
  47. onClose: PropTypes.func.isRequired,
  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. };
  78. render () {
  79. const { active } = this.props;
  80. return (
  81. <div className='emoji-picker-dropdown__modifiers__menu' style={{ display: active ? 'block' : 'none' }} ref={this.setRef}>
  82. <button type='button' onClick={this.handleClick} data-index={1}><Emoji emoji='fist' set='twitter' size={22} sheetSize={32} skin={1} backgroundImageFn={backgroundImageFn} /></button>
  83. <button type='button' onClick={this.handleClick} data-index={2}><Emoji emoji='fist' set='twitter' size={22} sheetSize={32} skin={2} backgroundImageFn={backgroundImageFn} /></button>
  84. <button type='button' onClick={this.handleClick} data-index={3}><Emoji emoji='fist' set='twitter' size={22} sheetSize={32} skin={3} backgroundImageFn={backgroundImageFn} /></button>
  85. <button type='button' onClick={this.handleClick} data-index={4}><Emoji emoji='fist' set='twitter' size={22} sheetSize={32} skin={4} backgroundImageFn={backgroundImageFn} /></button>
  86. <button type='button' onClick={this.handleClick} data-index={5}><Emoji emoji='fist' set='twitter' size={22} sheetSize={32} skin={5} backgroundImageFn={backgroundImageFn} /></button>
  87. <button type='button' onClick={this.handleClick} data-index={6}><Emoji emoji='fist' set='twitter' size={22} sheetSize={32} skin={6} backgroundImageFn={backgroundImageFn} /></button>
  88. </div>
  89. );
  90. }
  91. }
  92. class ModifierPicker extends React.PureComponent {
  93. static propTypes = {
  94. active: PropTypes.bool,
  95. modifier: PropTypes.number,
  96. onChange: PropTypes.func,
  97. onClose: PropTypes.func,
  98. onOpen: PropTypes.func,
  99. };
  100. handleClick = () => {
  101. if (this.props.active) {
  102. this.props.onClose();
  103. } else {
  104. this.props.onOpen();
  105. }
  106. };
  107. handleSelect = modifier => {
  108. this.props.onChange(modifier);
  109. this.props.onClose();
  110. };
  111. render () {
  112. const { active, modifier } = this.props;
  113. return (
  114. <div className='emoji-picker-dropdown__modifiers'>
  115. <Emoji emoji='fist' set='twitter' size={22} sheetSize={32} skin={modifier} onClick={this.handleClick} backgroundImageFn={backgroundImageFn} />
  116. <ModifierPickerMenu active={active} onSelect={this.handleSelect} onClose={this.props.onClose} />
  117. </div>
  118. );
  119. }
  120. }
  121. class EmojiPickerMenuImpl extends React.PureComponent {
  122. static propTypes = {
  123. custom_emojis: ImmutablePropTypes.list,
  124. frequentlyUsedEmojis: PropTypes.arrayOf(PropTypes.string),
  125. loading: PropTypes.bool,
  126. onClose: PropTypes.func.isRequired,
  127. onPick: PropTypes.func.isRequired,
  128. style: PropTypes.object,
  129. intl: PropTypes.object.isRequired,
  130. skinTone: PropTypes.number.isRequired,
  131. onSkinTone: PropTypes.func.isRequired,
  132. };
  133. static defaultProps = {
  134. style: {},
  135. loading: true,
  136. frequentlyUsedEmojis: [],
  137. };
  138. state = {
  139. modifierOpen: false,
  140. readyToFocus: false,
  141. };
  142. handleDocumentClick = e => {
  143. if (this.node && !this.node.contains(e.target)) {
  144. this.props.onClose();
  145. }
  146. };
  147. componentDidMount () {
  148. document.addEventListener('click', this.handleDocumentClick, false);
  149. document.addEventListener('touchend', this.handleDocumentClick, listenerOptions);
  150. // Because of https://github.com/react-bootstrap/react-bootstrap/issues/2614 we need
  151. // to wait for a frame before focusing
  152. requestAnimationFrame(() => {
  153. this.setState({ readyToFocus: true });
  154. if (this.node) {
  155. const element = this.node.querySelector('input[type="search"]');
  156. if (element) element.focus();
  157. }
  158. });
  159. }
  160. componentWillUnmount () {
  161. document.removeEventListener('click', this.handleDocumentClick, false);
  162. document.removeEventListener('touchend', this.handleDocumentClick, listenerOptions);
  163. }
  164. setRef = c => {
  165. this.node = c;
  166. };
  167. getI18n = () => {
  168. const { intl } = this.props;
  169. return {
  170. search: intl.formatMessage(messages.emoji_search),
  171. categories: {
  172. search: intl.formatMessage(messages.search_results),
  173. recent: intl.formatMessage(messages.recent),
  174. people: intl.formatMessage(messages.people),
  175. nature: intl.formatMessage(messages.nature),
  176. foods: intl.formatMessage(messages.food),
  177. activity: intl.formatMessage(messages.activity),
  178. places: intl.formatMessage(messages.travel),
  179. objects: intl.formatMessage(messages.objects),
  180. symbols: intl.formatMessage(messages.symbols),
  181. flags: intl.formatMessage(messages.flags),
  182. custom: intl.formatMessage(messages.custom),
  183. },
  184. };
  185. };
  186. handleClick = (emoji, event) => {
  187. if (!emoji.native) {
  188. emoji.native = emoji.colons;
  189. }
  190. if (!(event.ctrlKey || event.metaKey)) {
  191. this.props.onClose();
  192. }
  193. this.props.onPick(emoji);
  194. };
  195. handleModifierOpen = () => {
  196. this.setState({ modifierOpen: true });
  197. };
  198. handleModifierClose = () => {
  199. this.setState({ modifierOpen: false });
  200. };
  201. handleModifierChange = modifier => {
  202. this.props.onSkinTone(modifier);
  203. };
  204. render () {
  205. const { loading, style, intl, custom_emojis, skinTone, frequentlyUsedEmojis } = this.props;
  206. if (loading) {
  207. return <div style={{ width: 299 }} />;
  208. }
  209. const title = intl.formatMessage(messages.emoji);
  210. const { modifierOpen } = this.state;
  211. const categoriesSort = [
  212. 'recent',
  213. 'people',
  214. 'nature',
  215. 'foods',
  216. 'activity',
  217. 'places',
  218. 'objects',
  219. 'symbols',
  220. 'flags',
  221. ];
  222. categoriesSort.splice(1, 0, ...Array.from(categoriesFromEmojis(custom_emojis)).sort());
  223. return (
  224. <div className={classNames('emoji-picker-dropdown__menu', { selecting: modifierOpen })} style={style} ref={this.setRef}>
  225. <EmojiPicker
  226. perLine={8}
  227. emojiSize={22}
  228. sheetSize={32}
  229. custom={buildCustomEmojis(custom_emojis)}
  230. color=''
  231. emoji=''
  232. set='twitter'
  233. title={title}
  234. i18n={this.getI18n()}
  235. onClick={this.handleClick}
  236. include={categoriesSort}
  237. recent={frequentlyUsedEmojis}
  238. skin={skinTone}
  239. showPreview={false}
  240. showSkinTones={false}
  241. backgroundImageFn={backgroundImageFn}
  242. notFound={notFoundFn}
  243. autoFocus={this.state.readyToFocus}
  244. emojiTooltip
  245. />
  246. <ModifierPicker
  247. active={modifierOpen}
  248. modifier={skinTone}
  249. onOpen={this.handleModifierOpen}
  250. onClose={this.handleModifierClose}
  251. onChange={this.handleModifierChange}
  252. />
  253. </div>
  254. );
  255. }
  256. }
  257. const EmojiPickerMenu = injectIntl(EmojiPickerMenuImpl);
  258. class EmojiPickerDropdown extends React.PureComponent {
  259. static propTypes = {
  260. custom_emojis: ImmutablePropTypes.list,
  261. frequentlyUsedEmojis: PropTypes.arrayOf(PropTypes.string),
  262. intl: PropTypes.object.isRequired,
  263. onPickEmoji: PropTypes.func.isRequired,
  264. onSkinTone: PropTypes.func.isRequired,
  265. skinTone: PropTypes.number.isRequired,
  266. button: PropTypes.node,
  267. };
  268. state = {
  269. active: false,
  270. loading: false,
  271. };
  272. setRef = (c) => {
  273. this.dropdown = c;
  274. };
  275. onShowDropdown = () => {
  276. this.setState({ active: true });
  277. if (!EmojiPicker) {
  278. this.setState({ loading: true });
  279. EmojiPickerAsync().then(EmojiMart => {
  280. EmojiPicker = EmojiMart.Picker;
  281. Emoji = EmojiMart.Emoji;
  282. this.setState({ loading: false });
  283. }).catch(() => {
  284. this.setState({ loading: false, active: false });
  285. });
  286. }
  287. };
  288. onHideDropdown = () => {
  289. this.setState({ active: false });
  290. };
  291. onToggle = (e) => {
  292. if (!this.state.loading && (!e.key || e.key === 'Enter')) {
  293. if (this.state.active) {
  294. this.onHideDropdown();
  295. } else {
  296. this.onShowDropdown(e);
  297. }
  298. }
  299. };
  300. handleKeyDown = e => {
  301. if (e.key === 'Escape') {
  302. this.onHideDropdown();
  303. }
  304. };
  305. setTargetRef = c => {
  306. this.target = c;
  307. };
  308. findTarget = () => {
  309. return this.target;
  310. };
  311. render () {
  312. const { intl, onPickEmoji, onSkinTone, skinTone, frequentlyUsedEmojis, button } = this.props;
  313. const title = intl.formatMessage(messages.emoji);
  314. const { active, loading } = this.state;
  315. return (
  316. <div className='emoji-picker-dropdown' onKeyDown={this.handleKeyDown}>
  317. <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}>
  318. {button || <img
  319. className={classNames('emojione', { 'pulse-loading': active && loading })}
  320. alt='🙂'
  321. src={`${assetHost}/emoji/1f602.svg`}
  322. />}
  323. </div>
  324. <Overlay show={active} placement={'bottom'} target={this.findTarget} popperConfig={{ strategy: 'fixed' }}>
  325. {({ props, placement })=> (
  326. <div {...props} style={{ ...props.style, width: 299 }}>
  327. <div className={`dropdown-animation ${placement}`}>
  328. <EmojiPickerMenu
  329. custom_emojis={this.props.custom_emojis}
  330. loading={loading}
  331. onClose={this.onHideDropdown}
  332. onPick={onPickEmoji}
  333. onSkinTone={onSkinTone}
  334. skinTone={skinTone}
  335. frequentlyUsedEmojis={frequentlyUsedEmojis}
  336. />
  337. </div>
  338. </div>
  339. )}
  340. </Overlay>
  341. </div>
  342. );
  343. }
  344. }
  345. export default injectIntl(EmojiPickerDropdown);