闭社主体 forked from https://github.com/tootsuite/mastodon
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.

401 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/lib/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 onClick={this.handleClick} data-index={1}><Emoji emoji='fist' set='twitter' size={22} sheetSize={32} skin={1} backgroundImageFn={backgroundImageFn} /></button>
  83. <button onClick={this.handleClick} data-index={2}><Emoji emoji='fist' set='twitter' size={22} sheetSize={32} skin={2} backgroundImageFn={backgroundImageFn} /></button>
  84. <button onClick={this.handleClick} data-index={3}><Emoji emoji='fist' set='twitter' size={22} sheetSize={32} skin={3} backgroundImageFn={backgroundImageFn} /></button>
  85. <button onClick={this.handleClick} data-index={4}><Emoji emoji='fist' set='twitter' size={22} sheetSize={32} skin={4} backgroundImageFn={backgroundImageFn} /></button>
  86. <button onClick={this.handleClick} data-index={5}><Emoji emoji='fist' set='twitter' size={22} sheetSize={32} skin={5} backgroundImageFn={backgroundImageFn} /></button>
  87. <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. @injectIntl
  122. class EmojiPickerMenu extends React.PureComponent {
  123. static propTypes = {
  124. custom_emojis: ImmutablePropTypes.list,
  125. frequentlyUsedEmojis: PropTypes.arrayOf(PropTypes.string),
  126. loading: PropTypes.bool,
  127. onClose: PropTypes.func.isRequired,
  128. onPick: PropTypes.func.isRequired,
  129. style: PropTypes.object,
  130. placement: PropTypes.string,
  131. arrowOffsetLeft: PropTypes.string,
  132. arrowOffsetTop: PropTypes.string,
  133. intl: PropTypes.object.isRequired,
  134. skinTone: PropTypes.number.isRequired,
  135. onSkinTone: PropTypes.func.isRequired,
  136. };
  137. static defaultProps = {
  138. style: {},
  139. loading: true,
  140. frequentlyUsedEmojis: [],
  141. };
  142. state = {
  143. modifierOpen: false,
  144. placement: null,
  145. };
  146. handleDocumentClick = e => {
  147. if (this.node && !this.node.contains(e.target)) {
  148. this.props.onClose();
  149. }
  150. }
  151. componentDidMount () {
  152. document.addEventListener('click', this.handleDocumentClick, false);
  153. document.addEventListener('touchend', this.handleDocumentClick, listenerOptions);
  154. }
  155. componentWillUnmount () {
  156. document.removeEventListener('click', this.handleDocumentClick, false);
  157. document.removeEventListener('touchend', this.handleDocumentClick, listenerOptions);
  158. }
  159. setRef = c => {
  160. this.node = c;
  161. }
  162. getI18n = () => {
  163. const { intl } = this.props;
  164. return {
  165. search: intl.formatMessage(messages.emoji_search),
  166. categories: {
  167. search: intl.formatMessage(messages.search_results),
  168. recent: intl.formatMessage(messages.recent),
  169. people: intl.formatMessage(messages.people),
  170. nature: intl.formatMessage(messages.nature),
  171. foods: intl.formatMessage(messages.food),
  172. activity: intl.formatMessage(messages.activity),
  173. places: intl.formatMessage(messages.travel),
  174. objects: intl.formatMessage(messages.objects),
  175. symbols: intl.formatMessage(messages.symbols),
  176. flags: intl.formatMessage(messages.flags),
  177. custom: intl.formatMessage(messages.custom),
  178. },
  179. };
  180. }
  181. handleClick = (emoji, event) => {
  182. if (!emoji.native) {
  183. emoji.native = emoji.colons;
  184. }
  185. if (!(event.ctrlKey || event.metaKey)) {
  186. this.props.onClose();
  187. }
  188. this.props.onPick(emoji);
  189. }
  190. handleModifierOpen = () => {
  191. this.setState({ modifierOpen: true });
  192. }
  193. handleModifierClose = () => {
  194. this.setState({ modifierOpen: false });
  195. }
  196. handleModifierChange = modifier => {
  197. this.props.onSkinTone(modifier);
  198. }
  199. render () {
  200. const { loading, style, intl, custom_emojis, skinTone, frequentlyUsedEmojis } = this.props;
  201. if (loading) {
  202. return <div style={{ width: 299 }} />;
  203. }
  204. const title = intl.formatMessage(messages.emoji);
  205. const { modifierOpen } = this.state;
  206. const categoriesSort = [
  207. 'recent',
  208. 'people',
  209. 'nature',
  210. 'foods',
  211. 'activity',
  212. 'places',
  213. 'objects',
  214. 'symbols',
  215. 'flags',
  216. ];
  217. categoriesSort.splice(1, 0, ...Array.from(categoriesFromEmojis(custom_emojis)).sort());
  218. return (
  219. <div className={classNames('emoji-picker-dropdown__menu', { selecting: modifierOpen })} style={style} ref={this.setRef}>
  220. <EmojiPicker
  221. perLine={8}
  222. emojiSize={22}
  223. sheetSize={32}
  224. custom={buildCustomEmojis(custom_emojis)}
  225. color=''
  226. emoji=''
  227. set='twitter'
  228. title={title}
  229. i18n={this.getI18n()}
  230. onClick={this.handleClick}
  231. include={categoriesSort}
  232. recent={frequentlyUsedEmojis}
  233. skin={skinTone}
  234. showPreview={false}
  235. showSkinTones={false}
  236. backgroundImageFn={backgroundImageFn}
  237. notFound={notFoundFn}
  238. autoFocus
  239. emojiTooltip
  240. />
  241. <ModifierPicker
  242. active={modifierOpen}
  243. modifier={skinTone}
  244. onOpen={this.handleModifierOpen}
  245. onClose={this.handleModifierClose}
  246. onChange={this.handleModifierChange}
  247. />
  248. </div>
  249. );
  250. }
  251. }
  252. export default @injectIntl
  253. class EmojiPickerDropdown extends React.PureComponent {
  254. static propTypes = {
  255. custom_emojis: ImmutablePropTypes.list,
  256. frequentlyUsedEmojis: PropTypes.arrayOf(PropTypes.string),
  257. intl: PropTypes.object.isRequired,
  258. onPickEmoji: PropTypes.func.isRequired,
  259. onSkinTone: PropTypes.func.isRequired,
  260. skinTone: PropTypes.number.isRequired,
  261. button: PropTypes.node,
  262. };
  263. state = {
  264. active: false,
  265. loading: false,
  266. };
  267. setRef = (c) => {
  268. this.dropdown = c;
  269. }
  270. onShowDropdown = ({ target }) => {
  271. this.setState({ active: true });
  272. if (!EmojiPicker) {
  273. this.setState({ loading: true });
  274. EmojiPickerAsync().then(EmojiMart => {
  275. EmojiPicker = EmojiMart.Picker;
  276. Emoji = EmojiMart.Emoji;
  277. this.setState({ loading: false });
  278. }).catch(() => {
  279. this.setState({ loading: false, active: false });
  280. });
  281. }
  282. const { top } = target.getBoundingClientRect();
  283. this.setState({ placement: top * 2 < innerHeight ? 'bottom' : 'top' });
  284. }
  285. onHideDropdown = () => {
  286. this.setState({ active: false });
  287. }
  288. onToggle = (e) => {
  289. if (!this.state.loading && (!e.key || e.key === 'Enter')) {
  290. if (this.state.active) {
  291. this.onHideDropdown();
  292. } else {
  293. this.onShowDropdown(e);
  294. }
  295. }
  296. }
  297. handleKeyDown = e => {
  298. if (e.key === 'Escape') {
  299. this.onHideDropdown();
  300. }
  301. }
  302. setTargetRef = c => {
  303. this.target = c;
  304. }
  305. findTarget = () => {
  306. return this.target;
  307. }
  308. render () {
  309. const { intl, onPickEmoji, onSkinTone, skinTone, frequentlyUsedEmojis, button } = this.props;
  310. const title = intl.formatMessage(messages.emoji);
  311. const { active, loading, placement } = this.state;
  312. return (
  313. <div className='emoji-picker-dropdown' onKeyDown={this.handleKeyDown}>
  314. <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}>
  315. {button || <img
  316. className={classNames('emojione', { 'pulse-loading': active && loading })}
  317. alt='🙂'
  318. src={`${assetHost}/emoji/1f602.svg`}
  319. />}
  320. </div>
  321. <Overlay show={active} placement={placement} target={this.findTarget}>
  322. <EmojiPickerMenu
  323. custom_emojis={this.props.custom_emojis}
  324. loading={loading}
  325. onClose={this.onHideDropdown}
  326. onPick={onPickEmoji}
  327. onSkinTone={onSkinTone}
  328. skinTone={skinTone}
  329. frequentlyUsedEmojis={frequentlyUsedEmojis}
  330. />
  331. </Overlay>
  332. </div>
  333. );
  334. }
  335. }