The code powering m.abunchtell.com https://m.abunchtell.com
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
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, categoriesFromEmojis } 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. 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 => {
  170. if (!emoji.native) {
  171. emoji.native = emoji.colons;
  172. }
  173. this.props.onClose();
  174. this.props.onPick(emoji);
  175. }
  176. handleModifierOpen = () => {
  177. this.setState({ modifierOpen: true });
  178. }
  179. handleModifierClose = () => {
  180. this.setState({ modifierOpen: false });
  181. }
  182. handleModifierChange = modifier => {
  183. this.props.onSkinTone(modifier);
  184. }
  185. render () {
  186. const { loading, style, intl, custom_emojis, skinTone, frequentlyUsedEmojis } = this.props;
  187. if (loading) {
  188. return <div style={{ width: 299 }} />;
  189. }
  190. const title = intl.formatMessage(messages.emoji);
  191. const { modifierOpen } = this.state;
  192. const categoriesSort = [
  193. 'recent',
  194. 'people',
  195. 'nature',
  196. 'foods',
  197. 'activity',
  198. 'places',
  199. 'objects',
  200. 'symbols',
  201. 'flags',
  202. ];
  203. categoriesSort.splice(1, 0, ...Array.from(categoriesFromEmojis(custom_emojis)).sort());
  204. return (
  205. <div className={classNames('emoji-picker-dropdown__menu', { selecting: modifierOpen })} style={style} ref={this.setRef}>
  206. <EmojiPicker
  207. perLine={8}
  208. emojiSize={22}
  209. sheetSize={32}
  210. custom={buildCustomEmojis(custom_emojis)}
  211. color=''
  212. emoji=''
  213. set='twitter'
  214. title={title}
  215. i18n={this.getI18n()}
  216. onClick={this.handleClick}
  217. include={categoriesSort}
  218. recent={frequentlyUsedEmojis}
  219. skin={skinTone}
  220. showPreview={false}
  221. backgroundImageFn={backgroundImageFn}
  222. autoFocus
  223. emojiTooltip
  224. />
  225. <ModifierPicker
  226. active={modifierOpen}
  227. modifier={skinTone}
  228. onOpen={this.handleModifierOpen}
  229. onClose={this.handleModifierClose}
  230. onChange={this.handleModifierChange}
  231. />
  232. </div>
  233. );
  234. }
  235. }
  236. export default @injectIntl
  237. class EmojiPickerDropdown extends React.PureComponent {
  238. static propTypes = {
  239. custom_emojis: ImmutablePropTypes.list,
  240. frequentlyUsedEmojis: PropTypes.arrayOf(PropTypes.string),
  241. intl: PropTypes.object.isRequired,
  242. onPickEmoji: PropTypes.func.isRequired,
  243. onSkinTone: PropTypes.func.isRequired,
  244. skinTone: PropTypes.number.isRequired,
  245. button: PropTypes.node,
  246. };
  247. state = {
  248. active: false,
  249. loading: false,
  250. };
  251. setRef = (c) => {
  252. this.dropdown = c;
  253. }
  254. onShowDropdown = ({ target }) => {
  255. this.setState({ active: true });
  256. if (!EmojiPicker) {
  257. this.setState({ loading: true });
  258. EmojiPickerAsync().then(EmojiMart => {
  259. EmojiPicker = EmojiMart.Picker;
  260. Emoji = EmojiMart.Emoji;
  261. this.setState({ loading: false });
  262. }).catch(() => {
  263. this.setState({ loading: false });
  264. });
  265. }
  266. const { top } = target.getBoundingClientRect();
  267. this.setState({ placement: top * 2 < innerHeight ? 'bottom' : 'top' });
  268. }
  269. onHideDropdown = () => {
  270. this.setState({ active: false });
  271. }
  272. onToggle = (e) => {
  273. if (!this.state.loading && (!e.key || e.key === 'Enter')) {
  274. if (this.state.active) {
  275. this.onHideDropdown();
  276. } else {
  277. this.onShowDropdown(e);
  278. }
  279. }
  280. }
  281. handleKeyDown = e => {
  282. if (e.key === 'Escape') {
  283. this.onHideDropdown();
  284. }
  285. }
  286. setTargetRef = c => {
  287. this.target = c;
  288. }
  289. findTarget = () => {
  290. return this.target;
  291. }
  292. render () {
  293. const { intl, onPickEmoji, onSkinTone, skinTone, frequentlyUsedEmojis, button } = this.props;
  294. const title = intl.formatMessage(messages.emoji);
  295. const { active, loading, placement } = this.state;
  296. return (
  297. <div className='emoji-picker-dropdown' onKeyDown={this.handleKeyDown}>
  298. <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}>
  299. {button || <img
  300. className={classNames('emojione', { 'pulse-loading': active && loading })}
  301. alt='🙂'
  302. src={`${assetHost}/emoji/1f602.svg`}
  303. />}
  304. </div>
  305. <Overlay show={active} placement={placement} target={this.findTarget}>
  306. <EmojiPickerMenu
  307. custom_emojis={this.props.custom_emojis}
  308. loading={loading}
  309. onClose={this.onHideDropdown}
  310. onPick={onPickEmoji}
  311. onSkinTone={onSkinTone}
  312. skinTone={skinTone}
  313. frequentlyUsedEmojis={frequentlyUsedEmojis}
  314. />
  315. </Overlay>
  316. </div>
  317. );
  318. }
  319. }