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.

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