debian-mirror-gitlab/lib/gitlab/emoji.rb

84 lines
2 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2016-08-24 12:49:21 +05:30
module Gitlab
module Emoji
extend self
2017-08-17 22:00:37 +05:30
2016-08-24 12:49:21 +05:30
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-11-03 12:29:30 +05:30
def emojis_unicodes
emojis_by_moji.keys
end
2016-08-24 12:49:21 +05:30
def emojis_names
2016-11-03 12:29:30 +05:30
emojis.keys
2016-08-24 12:49:21 +05:30
end
2017-08-17 22:00:37 +05:30
def emojis_aliases
2020-05-24 23:13:21 +05:30
@emoji_aliases ||= Gitlab::Json.parse(File.read(Rails.root.join('fixtures', 'emojis', 'aliases.json')))
2017-08-17 22:00:37 +05:30
end
2016-08-24 12:49:21 +05:30
def emoji_filename(name)
emojis[name]["unicode"]
end
2016-11-03 12:29:30 +05:30
def emoji_unicode_filename(moji)
emojis_by_moji[moji]["unicode"]
end
2017-08-17 22:00:37 +05:30
def emoji_unicode_version(name)
2018-03-17 18:26:18 +05:30
emoji_unicode_versions_by_name[name]
2017-08-17 22:00:37 +05:30
end
def normalize_emoji_name(name)
emojis_aliases[name] || name
end
def emoji_image_tag(name, src)
"<img class='emoji' title=':#{name}:' alt=':#{name}:' src='#{src}' height='20' width='20' align='absmiddle' />"
end
2020-07-28 23:09:34 +05:30
def emoji_exists?(name)
emojis.has_key?(name)
end
2017-08-17 22:00:37 +05:30
# CSS sprite fallback takes precedence over image fallback
2020-04-22 19:07:51 +05:30
def gl_emoji_tag(name, options = {})
2017-08-17 22:00:37 +05:30
emoji_name = emojis_aliases[name] || name
emoji_info = emojis[emoji_name]
return unless emoji_info
data = {
name: emoji_name,
unicode_version: emoji_unicode_version(emoji_name)
}
2020-04-22 19:07:51 +05:30
options = { title: emoji_info['description'], data: data }.merge(options)
2017-08-17 22:00:37 +05:30
2020-04-22 19:07:51 +05:30
ActionController::Base.helpers.content_tag('gl-emoji', emoji_info['moji'], options)
2017-08-17 22:00:37 +05:30
end
2018-03-17 18:26:18 +05:30
2021-03-11 19:13:27 +05:30
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
2018-03-17 18:26:18 +05:30
private
def emoji_unicode_versions_by_name
@emoji_unicode_versions_by_name ||=
2020-05-24 23:13:21 +05:30
Gitlab::Json.parse(File.read(Rails.root.join('fixtures', 'emojis', 'emoji-unicode-version-map.json')))
2018-03-17 18:26:18 +05:30
end
2016-08-24 12:49:21 +05:30
end
end