functioning theme selector

This commit is contained in:
Luna D. 2024-07-03 21:32:27 +02:00
parent 774072945a
commit 625c30aaae
No known key found for this signature in database
GPG key ID: 4B1C63448394F688
7 changed files with 58 additions and 127 deletions

View file

@ -11,8 +11,9 @@ export function setupSettings() {
if (!$('#js-setting-table')) return; if (!$('#js-setting-table')) return;
const localCheckboxes = $$<HTMLInputElement>('[data-tab="local"] input[type="checkbox"]'); const localCheckboxes = $$<HTMLInputElement>('[data-tab="local"] input[type="checkbox"]');
const themeSelect = assertNotNull($<HTMLSelectElement>('#user_theme')); const themeSelect = assertNotNull($<HTMLSelectElement>('#user_theme_name'));
const styleSheet = assertNotNull($<HTMLLinkElement>('head link[rel="stylesheet"]')); const themeColorSelect = assertNotNull($<HTMLSelectElement>('#user_theme_color'));
const styleSheet = assertNotNull($<HTMLLinkElement>('#js-theme-stylesheet'));
// Local settings // Local settings
localCheckboxes.forEach(checkbox => { localCheckboxes.forEach(checkbox => {
@ -22,9 +23,13 @@ export function setupSettings() {
}); });
// Theme preview // Theme preview
if (themeSelect) { const themePreviewCallback = () => {
themeSelect.addEventListener('change', () => { const themeName = assertNotUndefined(themeSelect.options[themeSelect.selectedIndex].value);
styleSheet.href = assertNotUndefined(themeSelect.options[themeSelect.selectedIndex].dataset.themePath); const themeColor = assertNotUndefined(themeColorSelect.options[themeColorSelect.selectedIndex].value);
});
} styleSheet.href = `/css/${themeName}-${themeColor}.css`;
};
themeSelect.addEventListener('change', themePreviewCallback);
themeColorSelect.addEventListener('change', themePreviewCallback);
} }

View file

@ -357,7 +357,7 @@ defmodule Philomena.Users.User do
|> TagList.propagate_tag_list(:watched_tag_list, :watched_tag_ids) |> TagList.propagate_tag_list(:watched_tag_list, :watched_tag_ids)
|> validate_inclusion( |> validate_inclusion(
:theme, :theme,
~W(dark-blue dark-red dark-green dark-purple dark-pink dark-yellow dark-cyan dark-grey light-blue light-red light-green light-purple light-pink light-yellow light-cyan light-grey) ~W(dark-red dark-orange dark-yellow dark-blue dark-green dark-purple dark-cyan dark-pink dark-silver light-red light-orange light-yellow light-blue light-green light-purple light-cyan light-pink light-silver)
) )
|> validate_inclusion(:images_per_page, 1..50) |> validate_inclusion(:images_per_page, 1..50)
|> validate_inclusion(:comments_per_page, 1..100) |> validate_inclusion(:comments_per_page, 1..100)

View file

@ -9,6 +9,7 @@ defmodule PhilomenaWeb.SettingController do
def edit(conn, _params) do def edit(conn, _params) do
changeset = changeset =
(conn.assigns.current_user || %User{}) (conn.assigns.current_user || %User{})
|> assign_theme()
|> TagList.assign_tag_list(:watched_tag_ids, :watched_tag_list) |> TagList.assign_tag_list(:watched_tag_ids, :watched_tag_list)
|> Users.change_user() |> Users.change_user()
@ -57,13 +58,21 @@ defmodule PhilomenaWeb.SettingController do
) )
end end
defp determine_theme(%{"theme" => "light", "light_theme" => name} = attrs) when name != nil, defp assign_theme(%{theme: theme} = user) do
do: Map.replace(attrs, "theme", name) [theme_name, theme_color] = String.split(theme, "-")
defp determine_theme(%{"dark_theme" => name} = attrs) when name != nil, user
do: Map.replace(attrs, "theme", name) |> Map.put(:theme_name, theme_name)
|> Map.put(:theme_color, theme_color)
end
defp determine_theme(attrs), do: Map.replace(attrs, "theme", "dark-blue") defp assign_theme(_), do: assign_theme(%{theme: "dark-blue"})
defp determine_theme(%{"theme_name" => name, "theme_color" => color} = attrs)
when name != nil and color != nil,
do: Map.put(attrs, "theme", "#{name}-#{color}")
defp determine_theme(attrs), do: Map.put(attrs, "theme", "dark-blue")
defp maybe_update_user(conn, nil, _user_params), do: {:ok, conn} defp maybe_update_user(conn, nil, _user_params), do: {:ok, conn}

View file

@ -13,9 +13,9 @@ html lang="en"
=<> site_name() =<> site_name()
link rel="preconnect" href="https://#{cdn_host()}" link rel="preconnect" href="https://#{cdn_host()}"
link rel="stylesheet" href=~p"/css/application.css" link rel="stylesheet" href=~p"/css/application.css"
link rel="stylesheet" href=stylesheet_path(@conn, @current_user) link#js-theme-stylesheet rel="stylesheet" href=stylesheet_path(@conn, @current_user)
= if is_nil(@current_user) do = if is_nil(@current_user) do
link rel="stylesheet" href=light_stylesheet_path(@conn) media="(prefers-color-scheme: light)" link#js-theme-stylesheet rel="stylesheet" href=light_stylesheet_path(@conn) media="(prefers-color-scheme: light)"
link rel="icon" href=~p"/favicon.ico" type="image/x-icon" link rel="icon" href=~p"/favicon.ico" type="image/x-icon"
link rel="icon" href=~p"/favicon.svg" type="image/svg+xml" link rel="icon" href=~p"/favicon.svg" type="image/svg+xml"
link rel="search" type="application/opensearchdescription+xml" title="Derpibooru" href="/opensearch.xml" link rel="search" type="application/opensearchdescription+xml" title="Derpibooru" href="/opensearch.xml"

View file

@ -83,22 +83,16 @@ h1 Content Settings
' For 1080p monitors, try 24. ' For 1080p monitors, try 24.
.field .field
label Theme label Theme
select.input#js-theme-selector name="user[theme]" id="user_theme" .with-error
option value="dark" Dark => select f, :theme_name, themes(), class: "input"
option value="light" Light = error_tag f, :theme_name
.fieldlabel: i General appearance of the theme .fieldlabel: i General appearance of the theme
.field#js-theme-dark .field
label Theme color label Theme color
.with-error .with-error
=> select f, :dark_theme, theme_options(), class: "input" => select f, :theme_color, theme_colors(), class: "input"
= error_tag f, :dark_theme = error_tag f, :theme_color
.fieldlabel: i Color of the theme, don't forget to save settings to apply the theme .fieldlabel: i Color of the theme, don't forget to save settings to apply the theme
.field.hidden#js-theme-light
label Theme color
.with-error
=> select f, :light_theme, light_theme_options(), class: "input"
= error_tag f, :light_theme
.fieldlabel: i Preview themes by selecting one from the dropdown. Saving sets the currently selected theme.
.field .field
=> label f, :scale_large_images => label f, :scale_large_images
.with-error .with-error

View file

@ -71,22 +71,24 @@ defmodule PhilomenaWeb.LayoutView do
def stylesheet_path(conn, %{theme: theme}) def stylesheet_path(conn, %{theme: theme})
when theme in [ when theme in [
"dark-blue",
"dark-red", "dark-red",
"dark-orange",
"dark-yellow",
"dark-blue",
"dark-green", "dark-green",
"dark-purple", "dark-purple",
"dark-pink",
"dark-yellow",
"dark-cyan", "dark-cyan",
"dark-grey", "dark-pink",
"light-blue", "dark-silver",
"light-red", "light-red",
"light-orange",
"light-yellow",
"light-blue",
"light-green", "light-green",
"light-purple", "light-purple",
"light-pink",
"light-yellow",
"light-cyan", "light-cyan",
"light-grey" "light-pink",
"light-silver"
], ],
do: static_path(conn, "/css/#{theme}.css") do: static_path(conn, "/css/#{theme}.css")

View file

@ -1,103 +1,24 @@
defmodule PhilomenaWeb.SettingView do defmodule PhilomenaWeb.SettingView do
use PhilomenaWeb, :view use PhilomenaWeb, :view
def theme_options do def themes do
[ [
[ Dark: "dark",
key: "Red", Light: "light"
value: "dark-red",
data: [theme_path: ~p"/css/dark-red.css"]
],
[
key: "Orange",
value: "dark-orange",
data: [theme_path: ~p"/css/dark-orange.css"]
],
[
key: "Yellow",
value: "dark-yellow",
data: [theme_path: ~p"/css/dark-yellow.css"]
],
[
key: "Green",
value: "dark-green",
data: [theme_path: ~p"/css/dark-green.css"]
],
[
key: "Blue",
value: "dark-blue",
data: [theme_path: ~p"/css/dark-blue.css"]
],
[
key: "Purple",
value: "dark-purple",
data: [theme_path: ~p"/css/dark-purple.css"]
],
[
key: "Cyan",
value: "dark-cyan",
data: [theme_path: ~p"/css/dark-cyan.css"]
],
[
key: "Pink",
value: "dark-pink",
data: [theme_path: ~p"/css/dark-pink.css"]
],
[
key: "Grey",
value: "dark-grey",
data: [theme_path: ~p"/css/dark-grey.css"]
]
] ]
end end
def light_theme_options do def theme_colors do
[ [
[ Red: "red",
key: "Red", Orange: "orange",
value: "light-red", Yellow: "yellow",
data: [theme_path: ~p"/css/light-red.css"] Green: "green",
], Blue: "blue",
[ Purple: "purple",
key: "Orange", Cyan: "cyan",
value: "light-orange", Pink: "pink",
data: [theme_path: ~p"/css/light-orange.css"] "Silver/Charcoal": "silver"
],
[
key: "Yellow",
value: "light-yellow",
data: [theme_path: ~p"/css/light-yellow.css"]
],
[
key: "Green",
value: "light-green",
data: [theme_path: ~p"/css/light-green.css"]
],
[
key: "Blue",
value: "light-blue",
data: [theme_path: ~p"/css/light-blue.css"]
],
[
key: "Purple",
value: "light-purple",
data: [theme_path: ~p"/css/light-purple.css"]
],
[
key: "Cyan",
value: "light-cyan",
data: [theme_path: ~p"/css/light-cyan.css"]
],
[
key: "Pink",
value: "light-pink",
data: [theme_path: ~p"/css/light-pink.css"]
],
[
key: "Grey",
value: "light-grey",
data: [theme_path: ~p"/css/light-grey.css"]
]
] ]
end end