Use the same emoji data on the frontend and backend (#4284)
* Use the same emoji data on the frontend and backend * Move emoji.json to repository, add tests This way you don't need to install node dependencies if you only want to run Ruby code
This commit is contained in:
1 parent
c1bc5e14eb
commit
a390abdefb
5 files changed
+66
-6
No files matched your search
@@ -28,7 +28,6 @@ gem 'devise', '~> 4.2'
|
||||
gem 'devise-two-factor', '~> 3.0'
|
||||
gem 'doorkeeper', '~> 4.2'
|
||||
gem 'fast_blank', '~> 1.0'
|
||||
gem 'gemoji', '~> 3.0'
|
||||
gem 'goldfinger', '~> 1.2'
|
||||
gem 'hiredis', '~> 0.6'
|
||||
gem 'redis-namespace', '~> 1.5'
|
||||
|
||||
@@ -1,19 +1,24 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module EmojiHelper
|
||||
EMOJI_PATTERN = /(?<=[^[:alnum:]:]|\n|^):([\w+-]+):(?=[^[:alnum:]:]|$)/x
|
||||
|
||||
def emojify(text)
|
||||
return text if text.blank?
|
||||
|
||||
text.gsub(EMOJI_PATTERN) do |match|
|
||||
emoji = Emoji.find_by_alias($1) # rubocop:disable Rails/DynamicFindBy,Style/PerlBackrefs
|
||||
text.gsub(emoji_pattern) do |match|
|
||||
emoji = Emoji.instance.unicode($1) # rubocop:disable Style/PerlBackrefs
|
||||
|
||||
if emoji
|
||||
emoji.raw
|
||||
emoji
|
||||
else
|
||||
match
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def emoji_pattern
|
||||
@emoji_pattern ||=
|
||||
/(?<=[^[:alnum:]:]|\n|^)
|
||||
(#{Emoji.instance.names.map { |name| Regexp.escape(name) }.join('|')})
|
||||
(?=[^[:alnum:]:]|$)/x
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,40 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'singleton'
|
||||
|
||||
class Emoji
|
||||
include Singleton
|
||||
|
||||
def initialize
|
||||
data = Oj.load(File.open(File.join(Rails.root, 'lib', 'assets', 'emoji.json')))
|
||||
|
||||
@map = {}
|
||||
|
||||
data.each do |_, emoji|
|
||||
keys = [emoji['shortname']] + emoji['aliases']
|
||||
unicode = codepoint_to_unicode(emoji['unicode'])
|
||||
|
||||
keys.each do |key|
|
||||
@map[key] = unicode
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def unicode(shortcode)
|
||||
@map[shortcode]
|
||||
end
|
||||
|
||||
def names
|
||||
@map.keys
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def codepoint_to_unicode(codepoint)
|
||||
if codepoint.include?('-')
|
||||
codepoint.split('-').map(&:hex).pack('U')
|
||||
else
|
||||
[codepoint.hex].pack('U')
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1 @@
|
||||
{"grinning":{"unicode":"1f600","unicode_alt":"","code_decimal":"😀","name":"grinning face","shortname":":grinning:","category":"people","emoji_order":"1","aliases":[],"aliases_ascii":[],"keywords":["happy","smiley","emotion"]},"grin":{"unicode":"1f601","unicode_alt":"","code_decimal":"😁","name":"grinning face with smiling eyes","shortname":":grin:","category":"people","emoji_order":"2","aliases":[],"aliases_ascii":[],"keywords":["happy","silly","smiley","emotion","good","selfie"]},"joy":{"unicode":"1f602","unicode_alt":"","code_decimal":"😂","name":"face with tears of joy","shortname":":joy:","category":"people","emoji_order":"3","aliases":[],"aliases_ascii":[":')",":'-)"],"keywords":["happy","silly","smiley","cry","laugh","emotion","sarcastic"]},"rofl":{"unicode":"1f923","unicode_alt":"","code_decimal":"🤣","name":"rolling on the floor laughing","shortname":":rofl:","category":"people","emoji_order":"4","aliases":[":rolling_on_the_floor_laughing:"],"aliases_ascii":[],"keywords":[]},"smiley":{"unicode":"1f603","unicode_alt":"","code_decimal":"😃","name":"smiling face with open mouth","shortname":":smiley:","category":"people","emoji_order":"5","aliases":[],"aliases_ascii":[":D",":-D","=D"],"keywords":["happy","smiley","emotion","good"]},"smile":{"unicode":"1f604","unicode_alt":"","code_decimal":"😄","name":"smiling face with open mouth and smiling eyes","shortname":":smile:","category":"people","emoji_order":"6","aliases":[],"aliases_ascii":[],"keywords":["happy","smiley","emotion"]},"sweat_smile":{"unicode":"1f605","unicode_alt":"","code_decimal":"😅","name":"smiling face with open mouth and cold sweat","shortname":":sweat_smile:","category":"people","emoji_order":"7","aliases":[],"aliases_ascii":["':)","':-)","'=)","':D","':-D","'=D"],"keywords":["smiley","workout","sweat","emotion"]},"laughing":{"unicode":"1f606","unicode_alt":"","code_decimal":"😆","name":"smiling face with open mouth and tightly-closed eyes","shortname":":laughing:","category":"people","emoji_order":"8","aliases":[":satisfied:"],"aliases_ascii":[">:)",">;)",">:-)",">=)"],"keywords":["happy","smiley","laugh","emotion"]},"wink":{"unicode":"1f609","unicode_alt":"","code_decimal":"😉","name":"winking face","shortname":":wink:","category":"people","emoji_order":"9","aliases":[],"aliases_ascii":[";)",";-)","*-)","*)",";-]",";]",";D",";^)"],"keywords":["silly","smiley","emotion"]},"blush":{"unicode":"1f60a","unicode_alt":"","code_decimal":"😊","name":"smiling face with smiling eyes","shortname":":blush:","category":"people","emoji_order":"10","aliases":[],"aliases_ascii":[],"keywords":["happy","smiley","emotion","good","beautiful"]},"yum":{"unicode":"1f60b","unicode_alt":"","code_decimal":"😋","name":"face savouring delicious food","shortname":":yum:","category":"people","emoji_order":"11","aliases":[],"aliases_ascii":[],"keywords":["happy","silly","smiley","emotion","sarcastic","good"]},"sunglasses":{"unicode":"1f60e","unicode_alt":"","code_decimal":"😎","name":"smiling face with sunglasses","shortname":":sunglasses:","category":"people","emoji_order":"12","aliases":[],"aliases_ascii":["B-)","B)","8)","8-)","B-D","8-D"],"keywords":["silly","smiley","emojione","glasses","boys night"]},"heart_eyes":{"unicode":"1f60d","unicode_alt":"","code_decimal":"😍","name":"smiling face with heart-shaped eyes","shortname":":heart_eyes:","category":"people","emoji_order":"13","aliases":[],"aliases_ascii":[],"keywords":["happy","smiley","love","sex","heart eyes","emotion","beautiful"]},"kissing_heart":{"unicode":"1f618","unicode_alt":"","code_decimal":"😘","name":"face throwing a kiss","shortname":":kissing_heart:","category":"people","emoji_order":"14","aliases":[],"aliases_ascii":[":*",":-*","=*",":^*"],"keywords":["smiley","love","sexy"]},"kissing":{"unicode":"1f617","unicode_alt":"","code_decimal":"😗","name":"kissing face","shortname":":kissing:","category":"people","emoji_order":"15","aliases":[],"aliases_ascii":[],"keywords":["smiley","sexy"]},"kissing_smiling_eyes":{"unicode":"1f619","unicode_alt":"","code_decimal":"😙","name":"kissing face with smiling eyes","shortname":":kissing_smiling_eyes:","category":"people","emoji_order":"16","aliases":[],"aliases_ascii":[],"keywords":["smiley","sexy"]},"kissing_closed_eyes":{"unicode":"1f61a","unicode_alt":"","code_decimal":"😚","name":"kissing face with closed eyes","shortname":":kissing_closed_eyes:","category":"people","emoji_order":"17","aliases":[],"aliases_ascii":[],"keywords":["smiley","sexy"]},"relaxed":{"unicode":"263a","unicode_alt":"263a-fe0f","code_decimal":"☺","name":"white smiling face","shortname":":relaxed:","category":"people","emoji_order":"18","aliases":[],"aliases_ascii":[],"keywords":["happy","smiley"]},"slight_smile":{"unicode":"1f642","unicode_alt":"","code_decimal":"🙂","name":"slightly smiling face","shortname":":slight_smile:","category":"people","emoLine truncated
|
||||
@@ -0,0 +1,15 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Emoji do
|
||||
describe '#unicode' do
|
||||
it 'returns a unicode for a shortcode' do
|
||||
expect(Emoji.instance.unicode(':joy:')).to eq '😂'
|
||||
end
|
||||
end
|
||||
|
||||
describe '#names' do
|
||||
it 'returns an array' do
|
||||
expect(Emoji.instance.names).to be_an Array
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user