Fixing custom themes.

This commit is contained in:
Dessalines 2022-02-25 11:48:29 -05:00
parent 8a6609170f
commit bd5153f175
3 changed files with 48 additions and 40 deletions

View file

@ -27,40 +27,6 @@ const [hostname, port] = process.env["LEMMY_UI_HOST"]
const extraThemesFolder = const extraThemesFolder =
process.env["LEMMY_UI_EXTRA_THEMES_FOLDER"] || "./extra_themes"; process.env["LEMMY_UI_EXTRA_THEMES_FOLDER"] || "./extra_themes";
export const themeList = buildThemeList();
export const builtinThemes = [
"litera",
"materia",
"minty",
"solar",
"united",
"cyborg",
"darkly",
"journal",
"sketchy",
"vaporwave",
"vaporwave-dark",
"i386",
"litely",
"nord",
];
function buildThemeList(): string[] {
let data = fs.readdirSync(extraThemesFolder);
if (data != null) {
data = data
.filter(d => d.endsWith(".min.css"))
.map(d => d.replace(".min.css", ""));
data = builtinThemes.concat(data);
// use set to remove duplicate values
data = Array.from(new Set(data));
return data;
} else {
return builtinThemes;
}
}
server.use(express.json()); server.use(express.json());
server.use(express.urlencoded({ extended: false })); server.use(express.urlencoded({ extended: false }));
server.use("/static", express.static(path.resolve("./dist"))); server.use("/static", express.static(path.resolve("./dist")));
@ -99,6 +65,38 @@ server.get("/css/themes/:name", async (req, res) => {
} }
}); });
function buildThemeList(): string[] {
let themes = [
"litera",
"materia",
"minty",
"solar",
"united",
"cyborg",
"darkly",
"journal",
"sketchy",
"vaporwave",
"vaporwave-dark",
"i386",
"litely",
"nord",
];
let dirThemes = fs.readdirSync(extraThemesFolder);
if (dirThemes != null) {
let minCssThemes = dirThemes
.filter(d => d.endsWith(".min.css"))
.map(d => d.replace(".min.css", ""));
themes.push(...minCssThemes);
}
return themes;
}
server.get("/css/themelist", async (_req, res) => {
res.type("json");
res.send(JSON.stringify(buildThemeList()));
});
// server.use(cookieParser()); // server.use(cookieParser());
server.get("/*", async (req, res) => { server.get("/*", async (req, res) => {
try { try {

View file

@ -19,7 +19,6 @@ import {
} from "lemmy-js-client"; } from "lemmy-js-client";
import { Subscription } from "rxjs"; import { Subscription } from "rxjs";
import { i18n, languages } from "../../i18next"; import { i18n, languages } from "../../i18next";
import { themeList } from "../../../server/index";
import { UserService, WebSocketService } from "../../services"; import { UserService, WebSocketService } from "../../services";
import { import {
authField, authField,
@ -30,6 +29,7 @@ import {
debounce, debounce,
elementUrl, elementUrl,
fetchCommunities, fetchCommunities,
fetchThemeList,
fetchUsers, fetchUsers,
getLanguages, getLanguages,
isBrowser, isBrowser,
@ -78,6 +78,7 @@ interface SettingsState {
blockCommunity?: CommunityView; blockCommunity?: CommunityView;
currentTab: string; currentTab: string;
siteRes: GetSiteResponse; siteRes: GetSiteResponse;
themeList: string[];
} }
export class Settings extends Component<any, SettingsState> { export class Settings extends Component<any, SettingsState> {
@ -109,6 +110,7 @@ export class Settings extends Component<any, SettingsState> {
blockCommunityId: 0, blockCommunityId: 0,
currentTab: "settings", currentTab: "settings",
siteRes: this.isoData.site_res, siteRes: this.isoData.site_res,
themeList: [],
}; };
constructor(props: any, context: any) { constructor(props: any, context: any) {
@ -131,8 +133,10 @@ export class Settings extends Component<any, SettingsState> {
this.setUserInfo(); this.setUserInfo();
} }
componentDidMount() { async componentDidMount() {
setupTippy(); setupTippy();
this.state.themeList = await fetchThemeList();
this.setState(this.state);
} }
componentWillUnmount() { componentWillUnmount() {
@ -545,7 +549,7 @@ export class Settings extends Component<any, SettingsState> {
{i18n.t("theme")} {i18n.t("theme")}
</option> </option>
<option value="browser">{i18n.t("browser_default")}</option> <option value="browser">{i18n.t("browser_default")}</option>
{themeList.map(theme => ( {this.state.themeList.map(theme => (
<option value={theme}>{theme}</option> <option value={theme}>{theme}</option>
))} ))}
</select> </select>

View file

@ -36,7 +36,6 @@ import markdown_it_sup from "markdown-it-sup";
import moment from "moment"; import moment from "moment";
import { Subscription } from "rxjs"; import { Subscription } from "rxjs";
import { delay, retryWhen, take } from "rxjs/operators"; import { delay, retryWhen, take } from "rxjs/operators";
import { themeList } from "server";
import tippy from "tippy.js"; import tippy from "tippy.js";
import Toastify from "toastify-js"; import Toastify from "toastify-js";
import { httpBase } from "./env"; import { httpBase } from "./env";
@ -349,7 +348,11 @@ function getBrowserLanguages(): string[] {
return allowedLangs; return allowedLangs;
} }
export function setTheme(theme: string, forceReload = false) { export async function fetchThemeList(): Promise<string[]> {
return fetch("/css/themelist").then(res => res.json());
}
export async function setTheme(theme: string, forceReload = false) {
if (!isBrowser()) { if (!isBrowser()) {
return; return;
} }
@ -361,6 +364,8 @@ export function setTheme(theme: string, forceReload = false) {
theme = "darkly"; theme = "darkly";
} }
let themeList = await fetchThemeList();
// Unload all the other themes // Unload all the other themes
for (var i = 0; i < themeList.length; i++) { for (var i = 0; i < themeList.length; i++) {
let styleSheet = document.getElementById(themeList[i]); let styleSheet = document.getElementById(themeList[i]);
@ -375,7 +380,8 @@ export function setTheme(theme: string, forceReload = false) {
document.getElementById("default-dark")?.setAttribute("disabled", "disabled"); document.getElementById("default-dark")?.setAttribute("disabled", "disabled");
// Load the theme dynamically // Load the theme dynamically
let cssLoc = `/static/assets/css/themes/${theme}.min.css`; let cssLoc = `/css/themes/${theme}.min.css`;
loadCss(theme, cssLoc); loadCss(theme, cssLoc);
document.getElementById(theme).removeAttribute("disabled"); document.getElementById(theme).removeAttribute("disabled");
} }