Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get emoji data from native #250

Merged
merged 21 commits into from
Mar 23, 2019
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
54f6893
Add .editorconfig file with indent_size = 2
johnsenpeder Nov 29, 2018
1c7a5d6
Add `getEmojiDataFromNative` util function to get emoji data from native
johnsenpeder Nov 29, 2018
f90defb
Add `Get emoji data from Native` story
johnsenpeder Nov 29, 2018
fd40560
Add section to readme for `getEmojiDataFromNative` util function
johnsenpeder Nov 29, 2018
fe17925
Fix story/readme skin prop typo
johnsenpeder Nov 29, 2018
bbbdcc5
Use same emoji in readme as in story
johnsenpeder Nov 29, 2018
f0b5e1e
Add example of `emojiData` object
johnsenpeder Nov 29, 2018
682d8d8
Remove whitespace
johnsenpeder Nov 29, 2018
22a5ca5
Merge branch 'emojimart' into get_emoji_data_from_native
johnsenpeder Mar 20, 2019
f760c55
Return skin emojis in search. Refactor getEmojiDataFromNative
johnsenpeder Mar 20, 2019
51d5934
Remove duplicate `set`
pederjohnsen Mar 20, 2019
9afc613
Split out .editorconfig addition into separate PR
pederjohnsen Mar 23, 2019
ed241d1
Revert skin tone emoji on search code
pederjohnsen Mar 23, 2019
3734067
Update story/readme
pederjohnsen Mar 23, 2019
a8c4912
Merge remote-tracking branch 'emojimart/master' into get_emoji_data_f…
pederjohnsen Mar 23, 2019
0ab34e2
Remove empty strings from skinTones and skinCodes.
pederjohnsen Mar 23, 2019
90b36d8
Get skinToneIndex from forEach function
pederjohnsen Mar 23, 2019
5c64cc6
Use for loop and refactor lookup code
pederjohnsen Mar 23, 2019
5570a7c
Run prettier
pederjohnsen Mar 23, 2019
d140dd8
typo
pederjohnsen Mar 23, 2019
ab2f600
Add tests
pederjohnsen Mar 23, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[*]
indent_size = 2
pederjohnsen marked this conversation as resolved.
Show resolved Hide resolved
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,36 @@ emojiIndex.search('christmas').map((o) => o.native)
// => [🎄, 🎅🏼, 🔔, 🎁, ⛄️, ❄️]
```

## Get emoji data from Native
You can get emoji data from native emoji unicode using the `getEmojiDataFromNative` util function.

```js
import { getEmojiDataFromNative, Emoji } from 'emoji-mart'
import data from 'emoji-mart/data/all.json'

let emojiData = getEmojiDataFromNative('🏊🏽‍♀️', 'apple', data)

<Emoji
skin={emojiData.skin || 0}
set={select('Emoji pack', SETS, SETS[0])}
emoji={emojiData}
size={48}
/>
```

#### Example of `emojiData` object:
```js
emojiData: {
"id": "woman-swimming",
"name": "Woman Swimming",
"colons": ":woman-swimming::skin-tone-4:",
"emoticons": [],
"unified": "1f3ca-1f3fd-200d-2640-fe0f",
"skin": 4,
"native": "🏊🏽‍♀️"
}
```

### With custom data
```js
import data from 'emoji-mart/datasets/messenger'
Expand Down
1 change: 1 addition & 0 deletions src/components/picker/nimble-picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,7 @@ export default class NimblePicker extends React.PureComponent {
ref={this.setSearchRef}
onSearch={this.handleSearch}
data={this.data}
set={set}
i18n={this.i18n}
emojisToShowFilter={emojisToShowFilter}
include={include}
Expand Down
2 changes: 1 addition & 1 deletion src/components/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default class Search extends React.PureComponent {
}

this.data = props.data
this.emojiIndex = new NimbleEmojiIndex(this.data)
this.emojiIndex = new NimbleEmojiIndex(this.data, props.set)
this.setRef = this.setRef.bind(this)
this.clear = this.clear.bind(this)
this.handleKeyUp = this.handleKeyUp.bind(this)
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export {
} from './utils/emoji-index/nimble-emoji-index'
export { default as store } from './utils/store'
export { default as frequently } from './utils/frequently'
export { getEmojiDataFromNative } from './utils'

export { default as Picker } from './components/picker/picker'
export { default as NimblePicker } from './components/picker/nimble-picker'
Expand Down
27 changes: 22 additions & 5 deletions src/utils/emoji-index/nimble-emoji-index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { getData, getSanitizedData, intersect } from '..'
import { uncompress } from '../data'
import store from '../store'

export default class NimbleEmojiIndex {
constructor(data) {
constructor(data, set) {
if (data.compressed) {
uncompress(data)
}

this.data = data || {}
this.set = set || null
this.originalPool = {}
this.index = {}
this.emojis = {}
Expand All @@ -20,7 +22,7 @@ export default class NimbleEmojiIndex {
buildIndex() {
for (let emoji in this.data.emojis) {
let emojiData = this.data.emojis[emoji],
{ short_names, emoticons } = emojiData,
{ short_names, emoticons, skin_variations } = emojiData,
id = short_names[0]

if (emoticons) {
Expand All @@ -33,7 +35,16 @@ export default class NimbleEmojiIndex {
})
}

this.emojis[id] = getSanitizedData(id, null, null, this.data)
// If skin variations include them
if (skin_variations) {
this.emojis[id] = {}
for (let skinTone = 1; skinTone <= 6; skinTone++) {
this.emojis[id][skinTone] = getSanitizedData({id: id, skin: skinTone}, skinTone, this.set, this.data)
}
} else {
this.emojis[id] = getSanitizedData(id, null, this.set, this.data)
}

this.originalPool[id] = emojiData
}
}
Expand Down Expand Up @@ -70,6 +81,8 @@ export default class NimbleEmojiIndex {
if (this.customEmojisList != custom)
this.addCustomToPool(custom, this.originalPool)

const skinTone = store.get('skin') || 1;

maxResults || (maxResults = 75)
include || (include = [])
exclude || (exclude = [])
Expand All @@ -79,7 +92,7 @@ export default class NimbleEmojiIndex {

if (value.length) {
if (value == '-' || value == '-1') {
return [this.emojis['-1']]
return [this.emojis['-1'][skinTone]]
}

var values = value.toLowerCase().split(/[\s|,|\-|_]+/),
Expand Down Expand Up @@ -148,7 +161,11 @@ export default class NimbleEmojiIndex {
let score = subIndex + 1
if (sub == id) score = 0

aIndex.results.push(this.emojis[id])
if (this.emojis[id] && this.emojis[id][skinTone]) {
aIndex.results.push(this.emojis[id][skinTone])
} else {
aIndex.results.push(this.emojis[id])
}
aIndex.pool[id] = emoji

scores[id] = score
Expand Down
45 changes: 45 additions & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { buildSearch } from './data'
import stringFromCodePoint from '../polyfills/stringFromCodePoint'
import { uncompress } from './data'

const _JSON = JSON

Expand Down Expand Up @@ -134,6 +135,49 @@ function getData(emoji, skin, set, data) {
return emojiData
}

function getEmojiDataFromNative(nativeString, set, data) {
if (data.compressed) {
uncompress(data);
}

const skinTones = ['', '🏻', '🏼', '🏽', '🏾', '🏿']
const skinCodes = ['', '1F3FB', '1F3FC', '1F3FD', '1F3FE', '1F3FF']

let skin
let skinCode
let baseNativeString = nativeString

skinTones.forEach((skinTone) => {
pederjohnsen marked this conversation as resolved.
Show resolved Hide resolved
if (nativeString.indexOf(skinTone) > 0) {
const skinToneIndex = skinTones.indexOf(skinTone)
pederjohnsen marked this conversation as resolved.
Show resolved Hide resolved
skin = skinToneIndex + 1
skinCode = skinCodes[skinToneIndex]
}
})

const emojiData = Object.values(data.emojis).find((emoji) => {
pederjohnsen marked this conversation as resolved.
Show resolved Hide resolved
emoji = JSON.parse(_JSON.stringify(emoji))
pederjohnsen marked this conversation as resolved.
Show resolved Hide resolved

if (emoji.variations && emoji.variations.length) {
emoji.unified = emoji.variations.shift()
}

if (skin && emoji.skin_variations && emoji.skin_variations[skinCode]) {
emoji.unified = emoji.skin_variations[skinCode].unified
}

return unifiedToNative(emoji.unified) === baseNativeString
pederjohnsen marked this conversation as resolved.
Show resolved Hide resolved
})

if (!emojiData) {
return null
}

emojiData.id = emojiData.short_names[0]

return getSanitizedData(emojiData, skin, set, data)
}

function uniq(arr) {
return arr.reduce((acc, item) => {
if (acc.indexOf(item) === -1) {
Expand Down Expand Up @@ -211,6 +255,7 @@ function throttleIdleTask(func) {

export {
getData,
getEmojiDataFromNative,
getSanitizedData,
uniq,
intersect,
Expand Down
35 changes: 34 additions & 1 deletion stories/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
color,
} from '@storybook/addon-knobs'

import { Picker, Emoji, emojiIndex } from '../dist'
import { Picker, Emoji, emojiIndex, getEmojiDataFromNative } from '../dist'
import data from '../data/all.json'
import '../css/emoji-mart.css'

const SETS = ['apple', 'google', 'twitter', 'emojione', 'messenger', 'facebook']
Expand Down Expand Up @@ -214,3 +215,35 @@ storiesOf('Headless Search', module)
</div>
)
})

storiesOf('Get emoji data from Native', module)
.addDecorator(withKnobs)
.add('Default', () => {
let emojiData = getEmojiDataFromNative(
text('Unicode', '🏊🏽‍♀️'),
select('Emoji pack', SETS, SETS[0]),
data
)
if (!emojiData) {
return (
<div>
Couldn`t find any emoji data...
</div>
)
}

return (
<div>
<Emoji
skin={emojiData.skin || 0}
set={select('Emoji pack', SETS, SETS[0])}
emoji={emojiData}
size={48}
/>

<pre>
emojiData: {JSON.stringify(emojiData, null, 2)}
</pre>
</div>
)
})