Skip to content
This repository has been archived by the owner on Apr 12, 2023. It is now read-only.

Commit

Permalink
Add support for skin tone in emoji
Browse files Browse the repository at this point in the history
  • Loading branch information
zcbenz committed Apr 8, 2020
1 parent d76c71b commit 1e73679
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 20 deletions.
2 changes: 1 addition & 1 deletion lib/service/slack/emoji.json

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions lib/service/slack/message-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@ function parseEmoji(account, size, p1) {
const data = emoji[p1]
if (!data)
return null
if (p1.startsWith('skin-tone-'))
return ''
const x = data.x * size
const y = data.y * size
return `<span style="background-position: -${x}px -${y}px" class="apple size-${size} emoji"></span>`
}

const rules = [
{
regex: /:([a-zA-Z0-9_\-\+]+):/g,
regex: /:([a-zA-Z0-9_\-\+]+(::skin-tone-\d)?):/g,
replacer(account, match, p1) {
const r = parseEmoji(account, 22, p1)
return r !== null ? r : match[0]
Expand Down
9 changes: 0 additions & 9 deletions lib/service/slack/slack-reaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,8 @@ const Reaction = require('../../model/reaction')

const {parseEmoji} = require('./message-parser')

function normalizeReactionName(name) {
const skin = name.indexOf('::')
if (skin !== -1)
return name.substring(0, skin)
else
return name
}

class SlackReaction extends Reaction {
constructor(account, name, count, userIds) {
name = normalizeReactionName(name)
super(name, count, parseEmoji(account, 16, name))
if (userIds.includes(account.currentUserId))
this.reacted = true
Expand Down
39 changes: 32 additions & 7 deletions scripts/generate_emoji_json.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,50 @@ const path = require('path')
const fs = require('fs-extra')
const axios = require('axios')

const emojiJsonUrl = 'https://github.com/iamcal/emoji-data/raw/master/emoji.json'
const emojiJsonUrl = 'https://github.com/iamcal/emoji-data/raw/v4.1.0/emoji.json'
const target = path.resolve(__dirname, '..', 'lib', 'service', 'slack', 'emoji.json')

main()

async function main() {
const {data} = await axios(emojiJsonUrl)
const outputJson = {}
for (const d of data) {
const codes = d.unified.split('-').map((u) => parseInt(u, 16))
const native = String.fromCodePoint.apply(null, codes)
for (const name of d.short_names) {
outputJson[name] = {
x: d.sheet_x,
y: d.sheet_y,
native,
outputJson[d.short_name] = {
x: d.sheet_x,
y: d.sheet_y,
}
if (d.skin_variations) {
for (const k in d.skin_variations) {
const tone = skinToneToId(k)
if (tone < 0)
continue
const s = d.skin_variations[k]
outputJson[d.short_name + '::skin-tone-' + tone] = {
x: s.sheet_x,
y: s.sheet_y,
}
}
}
}

await fs.writeJson(target, outputJson)
}

main()
function skinToneToId(skin) {
switch (skin) {
case '1F3FB': return 2
case '1F3FC': return 3
case '1F3FD': return 4
case '1F3FE': return 5
case '1F3FF': return 6
default: return -1
}
}

function codeToNative(unified) {
const codes = unified.split('-').map((u) => parseInt(u, 16))
return String.fromCodePoint.apply(null, codes)
}

0 comments on commit 1e73679

Please sign in to comment.