emoji.rb 1.66 KB
Newer Older
1 2
# frozen_string_literal: true

3 4 5
module Gitlab
  module Emoji
    extend self
Eric Eastwood's avatar
Eric Eastwood committed
6

7 8 9 10 11 12 13
    def emojis
      Gemojione.index.instance_variable_get(:@emoji_by_name)
    end

    def emojis_by_moji
      Gemojione.index.instance_variable_get(:@emoji_by_moji)
    end
14

henrik's avatar
henrik committed
15
    def emojis_unicodes
16
      emojis_by_moji.keys
henrik's avatar
henrik committed
17
    end
18

19
    def emojis_names
20
      emojis.keys
21 22
    end

Eric Eastwood's avatar
Eric Eastwood committed
23
    def emojis_aliases
Eric Eastwood's avatar
Eric Eastwood committed
24
      @emoji_aliases ||= JSON.parse(File.read(Rails.root.join('fixtures', 'emojis', 'aliases.json')))
Eric Eastwood's avatar
Eric Eastwood committed
25 26
    end

27 28 29
    def emoji_filename(name)
      emojis[name]["unicode"]
    end
30

henrik's avatar
henrik committed
31 32 33
    def emoji_unicode_filename(moji)
      emojis_by_moji[moji]["unicode"]
    end
Eric Eastwood's avatar
Eric Eastwood committed
34 35

    def emoji_unicode_version(name)
36
      emoji_unicode_versions_by_name[name]
Eric Eastwood's avatar
Eric Eastwood committed
37 38 39 40
    end

    def normalize_emoji_name(name)
      emojis_aliases[name] || name
Eric Eastwood's avatar
Eric Eastwood committed
41 42 43 44 45 46 47
    end

    def emoji_image_tag(name, src)
      "<img class='emoji' title=':#{name}:' alt=':#{name}:' src='#{src}' height='20' width='20' align='absmiddle' />"
    end

    # CSS sprite fallback takes precedence over image fallback
48
    def gl_emoji_tag(name, options = {})
Eric Eastwood's avatar
Eric Eastwood committed
49 50
      emoji_name = emojis_aliases[name] || name
      emoji_info = emojis[emoji_name]
51
      return unless emoji_info
Eric Eastwood's avatar
Eric Eastwood committed
52 53 54 55 56

      data = {
        name: emoji_name,
        unicode_version: emoji_unicode_version(emoji_name)
      }
57
      options = { title: emoji_info['description'], data: data }.merge(options)
58

59
      ActionController::Base.helpers.content_tag('gl-emoji', emoji_info['moji'], options)
Eric Eastwood's avatar
Eric Eastwood committed
60
    end
61 62 63 64 65 66 67

    private

    def emoji_unicode_versions_by_name
      @emoji_unicode_versions_by_name ||=
        JSON.parse(File.read(Rails.root.join('fixtures', 'emojis', 'emoji-unicode-version-map.json')))
    end
68 69
  end
end