2023-01-13 05:40:33 +01:00
|
|
|
import { v4 as uuid } from "uuid";
|
2020-07-11 05:12:35 +02:00
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
import { themeProps, Theme } from "./theme";
|
2020-07-11 05:12:35 +02:00
|
|
|
|
|
|
|
export type Default = null;
|
|
|
|
export type Color = string;
|
2023-01-13 05:40:33 +01:00
|
|
|
export type FuncName = "alpha" | "darken" | "lighten";
|
|
|
|
export type Func = { type: "func"; name: FuncName; arg: number; value: string };
|
|
|
|
export type RefProp = { type: "refProp"; key: string };
|
|
|
|
export type RefConst = { type: "refConst"; key: string };
|
|
|
|
export type Css = { type: "css"; value: string };
|
2020-07-11 05:12:35 +02:00
|
|
|
|
2020-10-17 13:12:00 +02:00
|
|
|
export type ThemeValue = Color | Func | RefProp | RefConst | Css | Default;
|
2020-07-11 05:12:35 +02:00
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
export type ThemeViewModel = [string, ThemeValue][];
|
2020-07-11 05:12:35 +02:00
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
export const fromThemeString = (str?: string): ThemeValue => {
|
2020-07-11 05:12:35 +02:00
|
|
|
if (!str) return null;
|
2023-01-13 05:40:33 +01:00
|
|
|
if (str.startsWith(":")) {
|
|
|
|
const parts = str.slice(1).split("<");
|
2020-07-11 05:12:35 +02:00
|
|
|
const name = parts[0] as FuncName;
|
|
|
|
const arg = parseFloat(parts[1]);
|
2023-01-13 05:40:33 +01:00
|
|
|
const value = parts[2].startsWith("@") ? parts[2].slice(1) : "";
|
|
|
|
return { type: "func", name, arg, value };
|
|
|
|
} else if (str.startsWith("@")) {
|
2020-07-11 05:12:35 +02:00
|
|
|
return {
|
2023-01-13 05:40:33 +01:00
|
|
|
type: "refProp",
|
2020-07-11 05:12:35 +02:00
|
|
|
key: str.slice(1),
|
|
|
|
};
|
2023-01-13 05:40:33 +01:00
|
|
|
} else if (str.startsWith("$")) {
|
2020-07-11 05:12:35 +02:00
|
|
|
return {
|
2023-01-13 05:40:33 +01:00
|
|
|
type: "refConst",
|
2020-07-11 05:12:35 +02:00
|
|
|
key: str.slice(1),
|
|
|
|
};
|
2020-10-17 13:12:00 +02:00
|
|
|
} else if (str.startsWith('"')) {
|
|
|
|
return {
|
2023-01-13 05:40:33 +01:00
|
|
|
type: "css",
|
2020-10-17 13:12:00 +02:00
|
|
|
value: str.substr(1).trim(),
|
|
|
|
};
|
2020-07-11 05:12:35 +02:00
|
|
|
} else {
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
export const toThemeString = (
|
|
|
|
value: Color | Func | RefProp | RefConst | Css,
|
|
|
|
) => {
|
|
|
|
if (typeof value === "string") return value;
|
2020-07-11 05:12:35 +02:00
|
|
|
switch (value.type) {
|
2023-01-13 05:40:33 +01:00
|
|
|
case "func":
|
|
|
|
return `:${value.name}<${value.arg}<@${value.value}`;
|
|
|
|
case "refProp":
|
|
|
|
return `@${value.key}`;
|
|
|
|
case "refConst":
|
|
|
|
return `$${value.key}`;
|
|
|
|
case "css":
|
|
|
|
return `" ${value.value}`;
|
2020-07-11 05:12:35 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
export const convertToMisskeyTheme = (
|
|
|
|
vm: ThemeViewModel,
|
|
|
|
name: string,
|
|
|
|
desc: string,
|
|
|
|
author: string,
|
|
|
|
base: "dark" | "light",
|
|
|
|
): Theme => {
|
|
|
|
const props = {} as { [key: string]: string };
|
|
|
|
for (const [key, value] of vm) {
|
2020-07-11 05:12:35 +02:00
|
|
|
if (value === null) continue;
|
|
|
|
props[key] = toThemeString(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
id: uuid(),
|
2023-01-13 05:40:33 +01:00
|
|
|
name,
|
|
|
|
desc,
|
|
|
|
author,
|
|
|
|
props,
|
|
|
|
base,
|
2020-07-11 05:12:35 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const convertToViewModel = (theme: Theme): ThemeViewModel => {
|
|
|
|
const vm: ThemeViewModel = [];
|
|
|
|
// プロパティの登録
|
2023-01-13 05:40:33 +01:00
|
|
|
vm.push(
|
|
|
|
...themeProps.map(
|
|
|
|
(key) => [key, fromThemeString(theme.props[key])] as [string, ThemeValue],
|
|
|
|
),
|
|
|
|
);
|
2020-07-11 05:12:35 +02:00
|
|
|
|
|
|
|
// 定数の登録
|
2023-01-13 05:40:33 +01:00
|
|
|
const consts = Object.keys(theme.props)
|
|
|
|
.filter((k) => k.startsWith("$"))
|
|
|
|
.map((k) => [k, fromThemeString(theme.props[k])] as [string, ThemeValue]);
|
2020-07-11 05:12:35 +02:00
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
vm.push(...consts);
|
2020-07-11 05:12:35 +02:00
|
|
|
return vm;
|
|
|
|
};
|