mirror of
https://github.com/discourse/discourse.git
synced 2025-03-25 12:55:48 +08:00
FIX: Detect dark scheme server-side for better dark logo support (#10490)
* FIX: Use dark logo when dark scheme is default * Small refactor
This commit is contained in:
parent
f495fca7e8
commit
3c06dd9b99
@ -84,11 +84,7 @@ export default {
|
|||||||
'link[media="(prefers-color-scheme: dark)"]'
|
'link[media="(prefers-color-scheme: dark)"]'
|
||||||
).length > 0;
|
).length > 0;
|
||||||
|
|
||||||
session.darkColorScheme =
|
session.defaultColorSchemeIsDark = setupData.colorSchemeIsDark === "true";
|
||||||
!window.matchMedia("(prefers-color-scheme: dark)").matches &&
|
|
||||||
getComputedStyle(document.documentElement)
|
|
||||||
.getPropertyValue("--scheme-type")
|
|
||||||
.trim() === "dark";
|
|
||||||
|
|
||||||
session.highlightJsPath = setupData.highlightJsPath;
|
session.highlightJsPath = setupData.highlightJsPath;
|
||||||
session.svgSpritePath = setupData.svgSpritePath;
|
session.svgSpritePath = setupData.svgSpritePath;
|
||||||
|
@ -463,6 +463,10 @@ module ApplicationHelper
|
|||||||
result.html_safe
|
result.html_safe
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def dark_color_scheme?
|
||||||
|
ColorScheme.find_by_id(scheme_id)&.is_dark?
|
||||||
|
end
|
||||||
|
|
||||||
def preloaded_json
|
def preloaded_json
|
||||||
return '{}' if @preloaded.blank?
|
return '{}' if @preloaded.blank?
|
||||||
@preloaded.transform_values { |value| escape_unicode(value) }.to_json
|
@preloaded.transform_values { |value| escape_unicode(value) }.to_json
|
||||||
@ -485,6 +489,7 @@ module ApplicationHelper
|
|||||||
highlight_js_path: HighlightJs.path,
|
highlight_js_path: HighlightJs.path,
|
||||||
svg_sprite_path: SvgSprite.path(theme_ids),
|
svg_sprite_path: SvgSprite.path(theme_ids),
|
||||||
enable_js_error_reporting: GlobalSetting.enable_js_error_reporting,
|
enable_js_error_reporting: GlobalSetting.enable_js_error_reporting,
|
||||||
|
color_scheme_is_dark: dark_color_scheme?
|
||||||
}
|
}
|
||||||
|
|
||||||
if Rails.env.development?
|
if Rails.env.development?
|
||||||
|
@ -296,6 +296,18 @@ class ColorScheme < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def is_dark?
|
||||||
|
primary_b = brightness(colors_by_name["primary"].hex)
|
||||||
|
secondary_b = brightness(colors_by_name["secondary"].hex)
|
||||||
|
|
||||||
|
primary_b > secondary_b
|
||||||
|
end
|
||||||
|
|
||||||
|
# Equivalent to dc-color-brightness() in variables.scss
|
||||||
|
def brightness(color)
|
||||||
|
rgb = color.scan(/../).map { |c| c.to_i(16) }
|
||||||
|
(rgb[0].to_i * 299 + rgb[1].to_i * 587 + rgb[2].to_i * 114) / 1000.0
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# == Schema Information
|
# == Schema Information
|
||||||
|
@ -401,4 +401,17 @@ describe ApplicationHelper do
|
|||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "dark_color_scheme?" do
|
||||||
|
it 'returns nil for the base color scheme' do
|
||||||
|
expect(helper.dark_color_scheme?).to eq(nil)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'works correctly for a dark scheme' do
|
||||||
|
dark_theme = Theme.where(name: "Dark").first
|
||||||
|
helper.request.env[:resolved_theme_ids] = [dark_theme.id]
|
||||||
|
|
||||||
|
expect(helper.dark_color_scheme?).to eq(true)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
@ -86,4 +86,15 @@ describe ColorScheme do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "is_dark?" do
|
||||||
|
it "works as expected" do
|
||||||
|
scheme = ColorScheme.create_from_base(name: 'Tester')
|
||||||
|
ColorSchemeRevisor.revise(scheme, colors: [{ name: 'primary', hex: '333333' }, { name: 'secondary', hex: 'DDDDDD' }])
|
||||||
|
expect(scheme.is_dark?).to eq(false)
|
||||||
|
|
||||||
|
ColorSchemeRevisor.revise(scheme, colors: [{ name: 'primary', hex: 'F8F8F8' }, { name: 'secondary', hex: '232323' }])
|
||||||
|
expect(scheme.is_dark?).to eq(true)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user