2018-01-30 19:48:26 +00:00
|
|
|
import React, { Component } from 'react';
|
2018-01-16 18:48:05 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2018-01-30 19:48:26 +00:00
|
|
|
import { ScrollView } from 'react-native';
|
2018-01-16 18:48:05 +00:00
|
|
|
import ScrollableTabView from 'react-native-scrollable-tab-view';
|
|
|
|
import _ from 'lodash';
|
2018-01-30 19:48:26 +00:00
|
|
|
import { emojify } from 'react-emojione';
|
2018-01-16 18:48:05 +00:00
|
|
|
import TabBar from './TabBar';
|
|
|
|
import EmojiCategory from './EmojiCategory';
|
|
|
|
import styles from './styles';
|
|
|
|
import categories from './categories';
|
2018-01-30 19:48:26 +00:00
|
|
|
import database from '../../lib/realm';
|
|
|
|
import { emojisByCategory } from '../../emojis';
|
2018-01-16 18:48:05 +00:00
|
|
|
|
2018-01-30 19:48:26 +00:00
|
|
|
const scrollProps = {
|
2018-02-08 14:08:50 +00:00
|
|
|
keyboardShouldPersistTaps: 'always',
|
|
|
|
keyboardDismissMode: 'none'
|
2018-01-30 19:48:26 +00:00
|
|
|
};
|
2018-01-16 18:48:05 +00:00
|
|
|
|
2018-02-08 14:08:50 +00:00
|
|
|
export default class EmojiPicker extends Component {
|
2018-01-16 18:48:05 +00:00
|
|
|
static propTypes = {
|
2018-01-30 19:48:26 +00:00
|
|
|
onEmojiSelected: PropTypes.func,
|
|
|
|
tabEmojiStyle: PropTypes.object,
|
|
|
|
emojisPerRow: PropTypes.number,
|
|
|
|
width: PropTypes.number
|
2018-01-16 18:48:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
frequentlyUsed: [],
|
|
|
|
customEmojis: []
|
|
|
|
};
|
|
|
|
this.frequentlyUsed = database.objects('frequentlyUsedEmoji').sorted('count', true);
|
|
|
|
this.customEmojis = database.objects('customEmojis');
|
|
|
|
this.updateFrequentlyUsed = this.updateFrequentlyUsed.bind(this);
|
|
|
|
this.updateCustomEmojis = this.updateCustomEmojis.bind(this);
|
|
|
|
}
|
2018-01-30 19:48:26 +00:00
|
|
|
//
|
|
|
|
// shouldComponentUpdate(nextProps) {
|
|
|
|
// return false;
|
|
|
|
// }
|
2018-01-16 18:48:05 +00:00
|
|
|
|
2018-02-08 14:08:50 +00:00
|
|
|
componentDidMount() {
|
|
|
|
requestAnimationFrame(() => this.setState({ show: true }));
|
2018-03-06 17:40:44 +00:00
|
|
|
this.frequentlyUsed.addListener(this.updateFrequentlyUsed);
|
|
|
|
this.customEmojis.addListener(this.updateCustomEmojis);
|
|
|
|
this.updateFrequentlyUsed();
|
|
|
|
this.updateCustomEmojis();
|
2018-02-08 14:08:50 +00:00
|
|
|
}
|
2018-01-16 18:48:05 +00:00
|
|
|
componentWillUnmount() {
|
2018-01-30 19:48:26 +00:00
|
|
|
this.frequentlyUsed.removeAllListeners();
|
|
|
|
this.customEmojis.removeAllListeners();
|
2018-01-16 18:48:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onEmojiSelected(emoji) {
|
|
|
|
if (emoji.isCustom) {
|
|
|
|
const count = this._getFrequentlyUsedCount(emoji.content);
|
|
|
|
this._addFrequentlyUsed({
|
|
|
|
content: emoji.content, extension: emoji.extension, count, isCustom: true
|
|
|
|
});
|
|
|
|
this.props.onEmojiSelected(`:${ emoji.content }:`);
|
|
|
|
} else {
|
2018-01-30 19:48:26 +00:00
|
|
|
const content = emoji;
|
2018-01-16 18:48:05 +00:00
|
|
|
const count = this._getFrequentlyUsedCount(content);
|
|
|
|
this._addFrequentlyUsed({ content, count, isCustom: false });
|
2018-01-30 19:48:26 +00:00
|
|
|
const shortname = `:${ emoji }:`;
|
|
|
|
this.props.onEmojiSelected(emojify(shortname, { output: 'unicode' }), shortname);
|
2018-01-16 18:48:05 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-02 21:31:44 +00:00
|
|
|
|
2018-01-16 18:48:05 +00:00
|
|
|
_addFrequentlyUsed = (emoji) => {
|
|
|
|
database.write(() => {
|
|
|
|
database.create('frequentlyUsedEmoji', emoji, true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
_getFrequentlyUsedCount = (content) => {
|
|
|
|
const emojiRow = this.frequentlyUsed.filtered('content == $0', content);
|
|
|
|
return emojiRow.length ? emojiRow[0].count + 1 : 1;
|
|
|
|
}
|
|
|
|
updateFrequentlyUsed() {
|
|
|
|
const frequentlyUsed = _.map(this.frequentlyUsed.slice(), (item) => {
|
|
|
|
if (item.isCustom) {
|
|
|
|
return item;
|
|
|
|
}
|
2018-01-30 19:48:26 +00:00
|
|
|
return emojify(`${ item.content }`, { output: 'unicode' });
|
2018-01-16 18:48:05 +00:00
|
|
|
});
|
|
|
|
this.setState({ frequentlyUsed });
|
|
|
|
}
|
|
|
|
|
|
|
|
updateCustomEmojis() {
|
2018-01-30 19:48:26 +00:00
|
|
|
const customEmojis = _.map(this.customEmojis.slice(), item =>
|
|
|
|
({ content: item.name, extension: item.extension, isCustom: true }));
|
2018-01-16 18:48:05 +00:00
|
|
|
this.setState({ customEmojis });
|
|
|
|
}
|
|
|
|
|
|
|
|
renderCategory(category, i) {
|
|
|
|
let emojis = [];
|
|
|
|
if (i === 0) {
|
|
|
|
emojis = this.state.frequentlyUsed;
|
|
|
|
} else if (i === 1) {
|
|
|
|
emojis = this.state.customEmojis;
|
|
|
|
} else {
|
|
|
|
emojis = emojisByCategory[category];
|
|
|
|
}
|
|
|
|
return (
|
2018-01-30 19:48:26 +00:00
|
|
|
<EmojiCategory
|
|
|
|
emojis={emojis}
|
|
|
|
onEmojiSelected={emoji => this.onEmojiSelected(emoji)}
|
|
|
|
style={styles.categoryContainer}
|
|
|
|
size={this.props.emojisPerRow}
|
|
|
|
width={this.props.width}
|
|
|
|
/>
|
2018-01-16 18:48:05 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2018-01-30 19:48:26 +00:00
|
|
|
if (!this.state.show) {
|
|
|
|
return null;
|
|
|
|
}
|
2018-01-16 18:48:05 +00:00
|
|
|
return (
|
2018-01-30 19:48:26 +00:00
|
|
|
// <View style={styles.container}>
|
|
|
|
<ScrollableTabView
|
|
|
|
renderTabBar={() => <TabBar tabEmojiStyle={this.props.tabEmojiStyle} />}
|
|
|
|
contentProps={scrollProps}
|
|
|
|
>
|
|
|
|
{
|
|
|
|
categories.tabs.map((tab, i) => (
|
|
|
|
<ScrollView
|
|
|
|
key={tab.category}
|
|
|
|
tabLabel={tab.tabLabel}
|
2018-02-08 14:08:50 +00:00
|
|
|
{...scrollProps}
|
2018-01-30 19:48:26 +00:00
|
|
|
>
|
|
|
|
{this.renderCategory(tab.category, i)}
|
|
|
|
</ScrollView>
|
|
|
|
))
|
|
|
|
}
|
|
|
|
</ScrollableTabView>
|
|
|
|
// </View>
|
2018-01-16 18:48:05 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|