2018-10-22 03:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-25 17:48:12 -04:00
|
|
|
module Gitlab
|
|
|
|
module Emoji
|
|
|
|
extend self
|
2017-02-27 23:44:34 -05:00
|
|
|
|
2016-06-25 17:48:12 -04:00
|
|
|
def emojis
|
|
|
|
Gemojione.index.instance_variable_get(:@emoji_by_name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def emojis_by_moji
|
|
|
|
Gemojione.index.instance_variable_get(:@emoji_by_moji)
|
|
|
|
end
|
2016-10-11 10:27:09 -04:00
|
|
|
|
2016-10-11 09:41:10 -04:00
|
|
|
def emojis_unicodes
|
2016-10-11 10:27:09 -04:00
|
|
|
emojis_by_moji.keys
|
2016-10-11 09:41:10 -04:00
|
|
|
end
|
2016-10-11 10:27:09 -04:00
|
|
|
|
2016-06-25 17:48:12 -04:00
|
|
|
def emojis_names
|
2016-10-11 10:27:09 -04:00
|
|
|
emojis.keys
|
2016-06-25 17:48:12 -04:00
|
|
|
end
|
|
|
|
|
2017-02-27 23:44:34 -05:00
|
|
|
def emojis_aliases
|
2020-04-30 20:09:59 -04:00
|
|
|
@emoji_aliases ||= Gitlab::Json.parse(File.read(Rails.root.join('fixtures', 'emojis', 'aliases.json')))
|
2017-02-27 23:44:34 -05:00
|
|
|
end
|
|
|
|
|
2016-06-25 17:48:12 -04:00
|
|
|
def emoji_filename(name)
|
|
|
|
emojis[name]["unicode"]
|
|
|
|
end
|
2016-10-11 10:27:09 -04:00
|
|
|
|
2016-10-11 09:41:10 -04:00
|
|
|
def emoji_unicode_filename(moji)
|
|
|
|
emojis_by_moji[moji]["unicode"]
|
|
|
|
end
|
2017-02-27 23:44:34 -05:00
|
|
|
|
|
|
|
def emoji_unicode_version(name)
|
2017-09-18 13:25:23 -04:00
|
|
|
emoji_unicode_versions_by_name[name]
|
2017-03-06 12:59:26 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def normalize_emoji_name(name)
|
|
|
|
emojis_aliases[name] || name
|
2017-02-27 23:44:34 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def emoji_image_tag(name, src)
|
2021-06-07 08:10:00 -04:00
|
|
|
image_options = {
|
|
|
|
class: 'emoji',
|
|
|
|
src: src,
|
|
|
|
title: ":#{name}:",
|
|
|
|
alt: ":#{name}:",
|
|
|
|
height: 20,
|
|
|
|
width: 20,
|
|
|
|
align: 'absmiddle'
|
|
|
|
}
|
|
|
|
|
|
|
|
ActionController::Base.helpers.tag(:img, image_options)
|
2017-02-27 23:44:34 -05:00
|
|
|
end
|
|
|
|
|
2020-06-30 08:08:57 -04:00
|
|
|
def emoji_exists?(name)
|
|
|
|
emojis.has_key?(name)
|
|
|
|
end
|
|
|
|
|
2017-02-27 23:44:34 -05:00
|
|
|
# CSS sprite fallback takes precedence over image fallback
|
2020-03-30 23:07:51 -04:00
|
|
|
def gl_emoji_tag(name, options = {})
|
2017-02-27 23:44:34 -05:00
|
|
|
emoji_name = emojis_aliases[name] || name
|
|
|
|
emoji_info = emojis[emoji_name]
|
2017-03-16 06:03:35 -04:00
|
|
|
return unless emoji_info
|
2017-03-06 12:59:26 -05:00
|
|
|
|
|
|
|
data = {
|
|
|
|
name: emoji_name,
|
|
|
|
unicode_version: emoji_unicode_version(emoji_name)
|
|
|
|
}
|
2020-03-30 23:07:51 -04:00
|
|
|
options = { title: emoji_info['description'], data: data }.merge(options)
|
2017-03-16 06:03:35 -04:00
|
|
|
|
2020-03-30 23:07:51 -04:00
|
|
|
ActionController::Base.helpers.content_tag('gl-emoji', emoji_info['moji'], options)
|
2017-02-27 23:44:34 -05:00
|
|
|
end
|
2017-09-18 13:25:23 -04:00
|
|
|
|
2021-02-05 10:09:28 -05:00
|
|
|
def custom_emoji_tag(name, image_source)
|
|
|
|
data = {
|
|
|
|
name: name
|
|
|
|
}
|
|
|
|
|
|
|
|
ActionController::Base.helpers.content_tag('gl-emoji', title: name, data: data) do
|
|
|
|
emoji_image_tag(name, image_source).html_safe
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-18 13:25:23 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def emoji_unicode_versions_by_name
|
|
|
|
@emoji_unicode_versions_by_name ||=
|
2020-04-30 20:09:59 -04:00
|
|
|
Gitlab::Json.parse(File.read(Rails.root.join('fixtures', 'emojis', 'emoji-unicode-version-map.json')))
|
2017-09-18 13:25:23 -04:00
|
|
|
end
|
2016-06-25 17:48:12 -04:00
|
|
|
end
|
|
|
|
end
|