mirror of
https://git.bsd.gay/fef/nyastodon.git
synced 2024-12-28 04:13:44 +01:00
Merge pull request #1660 from ClearlyClaire/glitch-soc/features/themes-multiple-packs
Refactor glitch-soc's theme handling
This commit is contained in:
commit
f6acc039ec
4 changed files with 108 additions and 92 deletions
|
@ -10,6 +10,7 @@ class ApplicationController < ActionController::Base
|
|||
include SessionTrackingConcern
|
||||
include CacheConcern
|
||||
include DomainControlHelper
|
||||
include ThemingConcern
|
||||
|
||||
helper_method :current_account
|
||||
helper_method :current_session
|
||||
|
@ -73,75 +74,6 @@ class ApplicationController < ActionController::Base
|
|||
new_user_session_path
|
||||
end
|
||||
|
||||
def pack(data, pack_name, skin = 'default')
|
||||
return nil unless pack?(data, pack_name)
|
||||
pack_data = {
|
||||
common: pack_name == 'common' ? nil : resolve_pack(data['name'] ? Themes.instance.flavour(current_flavour) : Themes.instance.core, 'common', skin),
|
||||
flavour: data['name'],
|
||||
pack: pack_name,
|
||||
preload: nil,
|
||||
skin: nil,
|
||||
supported_locales: data['locales'],
|
||||
}
|
||||
if data['pack'][pack_name].is_a?(Hash)
|
||||
pack_data[:common] = nil if data['pack'][pack_name]['use_common'] == false
|
||||
pack_data[:pack] = nil unless data['pack'][pack_name]['filename']
|
||||
if data['pack'][pack_name]['preload']
|
||||
pack_data[:preload] = [data['pack'][pack_name]['preload']] if data['pack'][pack_name]['preload'].is_a?(String)
|
||||
pack_data[:preload] = data['pack'][pack_name]['preload'] if data['pack'][pack_name]['preload'].is_a?(Array)
|
||||
end
|
||||
if skin != 'default' && data['skin'][skin]
|
||||
pack_data[:skin] = skin if data['skin'][skin].include?(pack_name)
|
||||
else # default skin
|
||||
pack_data[:skin] = 'default' if data['pack'][pack_name]['stylesheet']
|
||||
end
|
||||
end
|
||||
pack_data
|
||||
end
|
||||
|
||||
def pack?(data, pack_name)
|
||||
if data['pack'].is_a?(Hash) && data['pack'].key?(pack_name)
|
||||
return true if data['pack'][pack_name].is_a?(String) || data['pack'][pack_name].is_a?(Hash)
|
||||
end
|
||||
false
|
||||
end
|
||||
|
||||
def nil_pack(data, pack_name, skin = 'default')
|
||||
{
|
||||
common: pack_name == 'common' ? nil : resolve_pack(data['name'] ? Themes.instance.flavour(current_flavour) : Themes.instance.core, 'common', skin),
|
||||
flavour: data['name'],
|
||||
pack: nil,
|
||||
preload: nil,
|
||||
skin: nil,
|
||||
supported_locales: data['locales'],
|
||||
}
|
||||
end
|
||||
|
||||
def resolve_pack(data, pack_name, skin = 'default')
|
||||
result = pack(data, pack_name, skin)
|
||||
unless result
|
||||
if data['name'] && data.key?('fallback')
|
||||
if data['fallback'].nil?
|
||||
return nil_pack(data, pack_name, skin)
|
||||
elsif data['fallback'].is_a?(String) && Themes.instance.flavour(data['fallback'])
|
||||
return resolve_pack(Themes.instance.flavour(data['fallback']), pack_name)
|
||||
elsif data['fallback'].is_a?(Array)
|
||||
data['fallback'].each do |fallback|
|
||||
return resolve_pack(Themes.instance.flavour(fallback), pack_name) if Themes.instance.flavour(fallback)
|
||||
end
|
||||
end
|
||||
return nil_pack(data, pack_name, skin)
|
||||
end
|
||||
return data.key?('name') && data['name'] != Setting.default_settings['flavour'] ? resolve_pack(Themes.instance.flavour(Setting.default_settings['flavour']), pack_name) : nil_pack(data, pack_name, skin)
|
||||
end
|
||||
result
|
||||
end
|
||||
|
||||
def use_pack(pack_name)
|
||||
@core = resolve_pack(Themes.instance.core, pack_name)
|
||||
@theme = resolve_pack(Themes.instance.flavour(current_flavour), pack_name, current_skin)
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def truthy_param?(key)
|
||||
|
|
80
app/controllers/concerns/theming_concern.rb
Normal file
80
app/controllers/concerns/theming_concern.rb
Normal file
|
@ -0,0 +1,80 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module ThemingConcern
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
def use_pack(pack_name)
|
||||
@core = resolve_pack_with_common(Themes.instance.core, pack_name)
|
||||
@theme = resolve_pack_with_common(Themes.instance.flavour(current_flavour), pack_name, current_skin)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def valid_pack_data?(data, pack_name)
|
||||
data['pack'].is_a?(Hash) && [String, Hash].any? { |c| data['pack'][pack_name].is_a?(c) }
|
||||
end
|
||||
|
||||
def nil_pack(data)
|
||||
{
|
||||
use_common: true,
|
||||
flavour: data['name'],
|
||||
pack: nil,
|
||||
preload: nil,
|
||||
skin: nil,
|
||||
supported_locales: data['locales'],
|
||||
}
|
||||
end
|
||||
|
||||
def pack(data, pack_name, skin)
|
||||
pack_data = {
|
||||
use_common: true,
|
||||
flavour: data['name'],
|
||||
pack: pack_name,
|
||||
preload: nil,
|
||||
skin: nil,
|
||||
supported_locales: data['locales'],
|
||||
}
|
||||
|
||||
return pack_data unless data['pack'][pack_name].is_a?(Hash)
|
||||
|
||||
pack_data[:use_common] = false if data['pack'][pack_name]['use_common'] == false
|
||||
pack_data[:pack] = nil unless data['pack'][pack_name]['filename']
|
||||
|
||||
preloads = data['pack'][pack_name]['preload']
|
||||
pack_data[:preload] = [preloads] if preloads.is_a?(String)
|
||||
pack_data[:preload] = preloads if preloads.is_a?(Array)
|
||||
|
||||
if skin != 'default' && data['skin'][skin]
|
||||
pack_data[:skin] = skin if data['skin'][skin].include?(pack_name)
|
||||
elsif data['pack'][pack_name]['stylesheet']
|
||||
pack_data[:skin] = 'default'
|
||||
end
|
||||
|
||||
pack_data
|
||||
end
|
||||
|
||||
def resolve_pack(data, pack_name, skin)
|
||||
return pack(data, pack_name, skin) if valid_pack_data?(data, pack_name)
|
||||
return if data['name'].blank?
|
||||
|
||||
fallbacks = []
|
||||
if data.key?('fallback')
|
||||
fallbacks = data['fallback'] if data['fallback'].is_a?(Array)
|
||||
fallbacks = [data['fallback']] if data['fallback'].is_a?(String)
|
||||
elsif data['name'] != Setting.default_settings['flavour']
|
||||
fallbacks = [Setting.default_settings['flavour']]
|
||||
end
|
||||
|
||||
fallbacks.each do |fallback|
|
||||
return resolve_pack(Themes.instance.flavour(fallback), pack_name) if Themes.instance.flavour(fallback)
|
||||
end
|
||||
|
||||
nil
|
||||
end
|
||||
|
||||
def resolve_pack_with_common(data, pack_name, skin = 'default')
|
||||
result = resolve_pack(data, pack_name, skin) || nil_pack(data)
|
||||
result[:common] = resolve_pack(data, 'common', skin) if result.delete(:use_common)
|
||||
result
|
||||
end
|
||||
end
|
|
@ -14,16 +14,20 @@ class Themes
|
|||
result = Hash.new
|
||||
Dir.glob(Rails.root.join('app', 'javascript', 'flavours', '*', 'theme.yml')) do |path|
|
||||
data = YAML.load_file(path)
|
||||
next unless data['pack']
|
||||
|
||||
dir = File.dirname(path)
|
||||
name = File.basename(dir)
|
||||
locales = []
|
||||
screenshots = []
|
||||
|
||||
if data['locales']
|
||||
Dir.glob(File.join(dir, data['locales'], '*.{js,json}')) do |locale|
|
||||
localeName = File.basename(locale, File.extname(locale))
|
||||
locales.push(localeName) unless localeName.match(/defaultMessages|whitelist|index/)
|
||||
end
|
||||
end
|
||||
|
||||
if data['screenshot']
|
||||
if data['screenshot'].is_a? Array
|
||||
screenshots = data['screenshot']
|
||||
|
@ -31,38 +35,37 @@ class Themes
|
|||
screenshots.push(data['screenshot'])
|
||||
end
|
||||
end
|
||||
if data['pack']
|
||||
data['name'] = name
|
||||
data['locales'] = locales
|
||||
data['screenshot'] = screenshots
|
||||
data['skin'] = { 'default' => [] }
|
||||
result[name] = data
|
||||
end
|
||||
|
||||
data['name'] = name
|
||||
data['locales'] = locales
|
||||
data['screenshot'] = screenshots
|
||||
data['skin'] = { 'default' => [] }
|
||||
result[name] = data
|
||||
end
|
||||
|
||||
Dir.glob(Rails.root.join('app', 'javascript', 'skins', '*', '*')) do |path|
|
||||
ext = File.extname(path)
|
||||
skin = File.basename(path)
|
||||
name = File.basename(File.dirname(path))
|
||||
if result[name]
|
||||
if File.directory?(path)
|
||||
pack = []
|
||||
Dir.glob(File.join(path, '*.{css,scss}')) do |sheet|
|
||||
pack.push(File.basename(sheet, File.extname(sheet)))
|
||||
end
|
||||
elsif ext.match(/^\.s?css$/i)
|
||||
skin = File.basename(path, ext)
|
||||
pack = ['common']
|
||||
end
|
||||
if skin != 'default'
|
||||
result[name]['skin'][skin] = pack
|
||||
next unless result[name]
|
||||
|
||||
if File.directory?(path)
|
||||
pack = []
|
||||
Dir.glob(File.join(path, '*.{css,scss}')) do |sheet|
|
||||
pack.push(File.basename(sheet, File.extname(sheet)))
|
||||
end
|
||||
elsif ext.match(/^\.s?css$/i)
|
||||
skin = File.basename(path, ext)
|
||||
pack = ['common']
|
||||
end
|
||||
|
||||
if skin != 'default'
|
||||
result[name]['skin'][skin] = pack
|
||||
end
|
||||
end
|
||||
|
||||
@core = core
|
||||
@conf = result
|
||||
|
||||
end
|
||||
|
||||
def core
|
||||
|
|
|
@ -2,12 +2,13 @@
|
|||
- if theme[:pack] != 'common' && theme[:common]
|
||||
= render partial: 'layouts/theme', object: theme[:common]
|
||||
- if theme[:pack]
|
||||
= javascript_pack_tag theme[:flavour] ? "flavours/#{theme[:flavour]}/#{theme[:pack]}" : "core/#{theme[:pack]}", crossorigin: 'anonymous'
|
||||
- pack_path = theme[:flavour] ? "flavours/#{theme[:flavour]}/#{theme[:pack]}" : "core/#{theme[:pack]}"
|
||||
= javascript_pack_tag pack_path, crossorigin: 'anonymous'
|
||||
- if theme[:skin]
|
||||
- if !theme[:flavour] || theme[:skin] == 'default'
|
||||
= stylesheet_pack_tag theme[:flavour] ? "flavours/#{theme[:flavour]}/#{theme[:pack]}" : "core/#{theme[:pack]}", media: 'all', crossorigin: 'anonymous'
|
||||
= stylesheet_pack_tag pack_path, media: 'all', crossorigin: 'anonymous'
|
||||
- else
|
||||
= stylesheet_pack_tag "skins/#{theme[:flavour]}/#{theme[:skin]}/#{theme[:pack]}", crossorigin: 'anonymous'
|
||||
= stylesheet_pack_tag "skins/#{theme[:flavour]}/#{theme[:skin]}/#{theme[:pack]}", media: 'all', crossorigin: 'anonymous'
|
||||
- if theme[:preload]
|
||||
- theme[:preload].each do |link|
|
||||
%link{ href: asset_pack_path("#{link}.js"), crossorigin: 'anonymous', rel: 'preload', as: 'script' }/
|
||||
|
|
Loading…
Reference in a new issue