Module: Gitlab::ColorSchemes

Defined in:
lib/gitlab/color_schemes.rb

Overview

Module containing GitLab’s syntax color scheme definitions and helper methods for accessing them.

Defined Under Namespace

Classes: Scheme

Class Method Summary collapse

Class Method Details

.available_schemesObject



10
11
12
13
14
15
16
17
18
19
# File 'lib/gitlab/color_schemes.rb', line 10

def self.available_schemes
  [
    Scheme.new(1, s_('SynthaxHighlightingTheme|Light'),           'white'),
    Scheme.new(2, s_('SynthaxHighlightingTheme|Dark'),            'dark'),
    Scheme.new(3, s_('SynthaxHighlightingTheme|Solarized Light'), 'solarized-light'),
    Scheme.new(4, s_('SynthaxHighlightingTheme|Solarized Dark'),  'solarized-dark'),
    Scheme.new(5, s_('SynthaxHighlightingTheme|Monokai'),         'monokai'),
    Scheme.new(6, s_('SynthaxHighlightingTheme|None'),            'none')
  ]
end

.body_classesObject

Convenience method to get a space-separated String of all the color scheme classes that might be applied to a code block.

Returns a String



25
26
27
# File 'lib/gitlab/color_schemes.rb', line 25

def self.body_classes
  available_schemes.collect(&:css_class).uniq.join(' ')
end

.by_id(id) ⇒ Object

Get a Scheme by its ID

If the ID is invalid, returns the default Scheme.

id - Integer ID

Returns a Scheme



36
37
38
# File 'lib/gitlab/color_schemes.rb', line 36

def self.by_id(id)
  available_schemes.detect { |s| s.id == id } || default
end

.countObject

Returns the number of defined Schemes



41
42
43
# File 'lib/gitlab/color_schemes.rb', line 41

def self.count
  available_schemes.size
end

.dark_for_user(user) ⇒ Object

Get the dark Scheme for the specified user, or the default

user - User record

Returns a Scheme



103
104
105
106
107
108
109
# File 'lib/gitlab/color_schemes.rb', line 103

def self.dark_for_user(user)
  if user && !user.dark_color_scheme_id.nil?
    by_id(user.dark_color_scheme_id)
  else
    default_dark
  end
end

.defaultObject

Get the default Scheme

Returns a Scheme



48
49
50
# File 'lib/gitlab/color_schemes.rb', line 48

def self.default
  by_id(Gitlab::CurrentSettings.default_syntax_highlighting_theme)
end

.default_darkObject

Get the default dark Scheme

Returns a Scheme



55
56
57
# File 'lib/gitlab/color_schemes.rb', line 55

def self.default_dark
  by_id(Gitlab::CurrentSettings.default_dark_syntax_highlighting_theme)
end

.each(&block) ⇒ Object

Iterate through each Scheme

Yields the Scheme object



62
63
64
# File 'lib/gitlab/color_schemes.rb', line 62

def self.each(&block)
  available_schemes.each(&block)
end

.for_user(user) ⇒ Object

Get the Scheme for the specified user, or the default

user - User record

Returns a Scheme



71
72
73
74
75
76
77
78
79
# File 'lib/gitlab/color_schemes.rb', line 71

def self.for_user(user)
  return default unless user

  if user_prefers_dark_mode?(user)
    dark_for_user(user)
  else
    light_for_user(user)
  end
end

.light_for_user(user) ⇒ Object

Get the light Scheme for the specified user, or the default

user - User record

Returns a Scheme



90
91
92
93
94
95
96
# File 'lib/gitlab/color_schemes.rb', line 90

def self.light_for_user(user)
  if user
    by_id(user.color_scheme_id)
  else
    default
  end
end

.user_prefers_dark_mode?(user) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/gitlab/color_schemes.rb', line 81

def self.user_prefers_dark_mode?(user)
  user&.color_mode_id == Gitlab::ColorModes::APPLICATION_DARK
end

.valid_idsObject



111
112
113
# File 'lib/gitlab/color_schemes.rb', line 111

def self.valid_ids
  available_schemes.map(&:id)
end