357 lines
11 KiB
TypeScript
357 lines
11 KiB
TypeScript
import { ref, computed } from "vue";
|
|
import defaultSchoolTheme from "~/assets/school/theme.json";
|
|
|
|
export interface ThemeFontFile {
|
|
weight: string;
|
|
style: string;
|
|
src: string;
|
|
}
|
|
|
|
export interface ThemeColors {
|
|
primary?: Record<string, string>;
|
|
secondary?: Record<string, string>;
|
|
[key: string]: Record<string, string> | undefined;
|
|
}
|
|
|
|
export interface ThemeConfig {
|
|
name?: string;
|
|
title?: string;
|
|
subTitle?: string;
|
|
description?: string;
|
|
version?: string;
|
|
logo?: {
|
|
light?: string;
|
|
dark?: string;
|
|
favicon?: string;
|
|
icon?: string;
|
|
};
|
|
font?: string;
|
|
fontFallback?: string;
|
|
fontFiles?: ThemeFontFile[];
|
|
colors?: ThemeColors;
|
|
rtl?: boolean;
|
|
direction?: "rtl" | "ltr";
|
|
icons?: Record<string, string>;
|
|
layout?: {
|
|
containerWidth?: string;
|
|
sidebarWidth?: string;
|
|
headerHeight?: string;
|
|
footerHeight?: string;
|
|
};
|
|
darkMode?: {
|
|
enabled?: boolean;
|
|
default?: "light" | "dark" | "system";
|
|
storageKey?: string;
|
|
};
|
|
}
|
|
|
|
export function composSystemTheme() {
|
|
const nuxtApp = useNuxtApp();
|
|
|
|
// دسترسی به تم تزریقشده توسط پلاگین بر اساس NUXT_PUBLIC_SYSTEM یا تم پیشفرض school
|
|
const initialTheme: ThemeConfig = (nuxtApp.$theme as ThemeConfig) || defaultSchoolTheme;
|
|
|
|
// وضعیت سراسری تم
|
|
const currentTheme = useState<ThemeConfig>("system_theme", () => initialTheme);
|
|
const themeKey = useState<string>("system_theme_key", () => (nuxtApp.$themeKey as string) || "school");
|
|
const isDark = useState<boolean>("system_is_dark", () => false);
|
|
const isRtl = useState<boolean>("system_is_rtl", () => currentTheme.value?.rtl ?? true);
|
|
|
|
// وضعیت reactive آیکونها
|
|
const icons = useState<Record<string, string>>("system_icons", () => ({
|
|
sun: "i-lucide-sun",
|
|
moon: "i-lucide-moon",
|
|
menu: "i-lucide-menu",
|
|
close: "i-lucide-x",
|
|
check: "i-lucide-check",
|
|
chevronRight: "i-lucide-chevron-right",
|
|
chevronLeft: "i-lucide-chevron-left",
|
|
search: "i-lucide-search",
|
|
user: "i-lucide-user",
|
|
palette: "i-lucide-palette",
|
|
font: "i-lucide-type",
|
|
direction: "i-lucide-arrow-left-right",
|
|
network: "i-lucide-wifi",
|
|
settings: "i-lucide-settings",
|
|
bell: "i-lucide-bell",
|
|
dashboard: "i-lucide-layout-dashboard",
|
|
logout: "i-lucide-log-out",
|
|
bookOpen: "i-lucide-book-open",
|
|
graduationCap: "i-lucide-graduation-cap",
|
|
users: "i-lucide-users",
|
|
calendar: "i-lucide-calendar",
|
|
award: "i-lucide-award",
|
|
library: "i-lucide-library",
|
|
fileText: "i-lucide-file-text",
|
|
phone: "i-lucide-phone",
|
|
mapPin: "i-lucide-map-pin",
|
|
...(currentTheme.value?.icons || {}),
|
|
}));
|
|
|
|
// جهت صفحه
|
|
const direction = computed(() => (isRtl.value ? "rtl" : "ltr"));
|
|
|
|
// لوگوی متناسب با تم روز/شب
|
|
const logo = computed(() => {
|
|
if (!currentTheme.value?.logo) return "";
|
|
return isDark.value
|
|
? currentTheme.value.logo.dark || currentTheme.value.logo.light || ""
|
|
: currentTheme.value.logo.light || currentTheme.value.logo.dark || "";
|
|
});
|
|
|
|
// رنگ اصلی جاری (shade 500)
|
|
const primaryColor = computed(() => {
|
|
return currentTheme.value?.colors?.primary?.["500"] || "#d99058";
|
|
});
|
|
|
|
// پالت کامل رنگ اصلی
|
|
const primaryPalette = computed(() => {
|
|
return currentTheme.value?.colors?.primary || {};
|
|
});
|
|
|
|
// نام فونت جاری
|
|
const fontName = computed(() => {
|
|
return currentTheme.value?.font || "Vazirmatn";
|
|
});
|
|
|
|
// متد استخراج آیکون
|
|
const getIcon = (key: string, fallback = "i-lucide-circle"): string => {
|
|
return icons.value[key] || fallback;
|
|
};
|
|
|
|
// تغییر یک آیکون
|
|
const setIcon = (key: string, iconClass: string) => {
|
|
icons.value = {
|
|
...icons.value,
|
|
[key]: iconClass,
|
|
};
|
|
if (currentTheme.value.icons) {
|
|
currentTheme.value.icons[key] = iconClass;
|
|
}
|
|
};
|
|
|
|
// بهروزرسانی گروهی آیکونها
|
|
const setIcons = (newIcons: Record<string, string>) => {
|
|
icons.value = {
|
|
...icons.value,
|
|
...newIcons,
|
|
};
|
|
currentTheme.value.icons = {
|
|
...(currentTheme.value.icons || {}),
|
|
...newIcons,
|
|
};
|
|
};
|
|
|
|
// تغییر دارک مود
|
|
const toggleDarkMode = () => {
|
|
setDarkMode(!isDark.value);
|
|
};
|
|
|
|
const setDarkMode = (dark: boolean) => {
|
|
isDark.value = dark;
|
|
if (import.meta.client) {
|
|
const storageKey = currentTheme.value?.darkMode?.storageKey || "theme-mode";
|
|
localStorage.setItem(storageKey, dark ? "dark" : "light");
|
|
const root = document.documentElement;
|
|
if (dark) {
|
|
root.classList.add("dark");
|
|
} else {
|
|
root.classList.remove("dark");
|
|
}
|
|
}
|
|
};
|
|
|
|
// تغییر جهت RTL / LTR
|
|
const toggleDirection = () => {
|
|
setDirection(!isRtl.value);
|
|
};
|
|
|
|
const setDirection = (rtl: boolean) => {
|
|
isRtl.value = rtl;
|
|
currentTheme.value.rtl = rtl;
|
|
currentTheme.value.direction = rtl ? "rtl" : "ltr";
|
|
if (import.meta.client) {
|
|
const root = document.documentElement;
|
|
root.setAttribute("dir", rtl ? "rtl" : "ltr");
|
|
root.style.direction = rtl ? "rtl" : "ltr";
|
|
}
|
|
};
|
|
|
|
// تولید پالت رنگی داینامیک از روی یک رنگ hex
|
|
const generateShades = (hex: string): Record<string, string> => {
|
|
return {
|
|
"50": hex,
|
|
"100": hex,
|
|
"200": hex,
|
|
"300": hex,
|
|
"400": hex,
|
|
"500": hex,
|
|
"600": hex,
|
|
"700": hex,
|
|
"800": hex,
|
|
"900": hex,
|
|
"950": hex,
|
|
};
|
|
};
|
|
|
|
// تغییر پالت رنگ primary
|
|
const setPrimaryPalette = (palette: Record<string, string>) => {
|
|
if (!currentTheme.value.colors) {
|
|
currentTheme.value.colors = {};
|
|
}
|
|
currentTheme.value.colors.primary = palette;
|
|
|
|
if (import.meta.client) {
|
|
const root = document.documentElement;
|
|
Object.entries(palette).forEach(([shade, val]) => {
|
|
root.style.setProperty(`--color-primary-${shade}`, val);
|
|
root.style.setProperty(`--ui-color-primary-${shade}`, val);
|
|
});
|
|
if (palette["500"]) {
|
|
root.style.setProperty("--color-primary", palette["500"]);
|
|
root.style.setProperty("--ui-primary", palette["500"]);
|
|
}
|
|
}
|
|
};
|
|
|
|
// تغییر رنگ اصلی فقط با ارسال کد رنگ
|
|
const setPrimaryColor = (hex: string) => {
|
|
const defaultShades = currentTheme.value?.colors?.primary;
|
|
const newShades = defaultShades ? { ...defaultShades, "500": hex, "600": hex } : generateShades(hex);
|
|
newShades["500"] = hex;
|
|
setPrimaryPalette(newShades);
|
|
};
|
|
|
|
// تغییر فونت پروژه
|
|
const setFont = (fontFamily: string, fallback?: string, fontFiles?: ThemeFontFile[]) => {
|
|
currentTheme.value.font = fontFamily;
|
|
if (fallback) currentTheme.value.fontFallback = fallback;
|
|
if (fontFiles) currentTheme.value.fontFiles = fontFiles;
|
|
|
|
if (import.meta.client) {
|
|
const root = document.documentElement;
|
|
const fullFont = fallback ? `"${fontFamily}", ${fallback}` : `"${fontFamily}", sans-serif`;
|
|
root.style.setProperty("--app-font", fullFont);
|
|
|
|
if (fontFiles && fontFiles.length > 0) {
|
|
const fontStyleId = `font-style-${fontFamily.replace(/\s+/g, "-")}`;
|
|
let styleEl = document.getElementById(fontStyleId) as HTMLStyleElement | null;
|
|
if (!styleEl) {
|
|
styleEl = document.createElement("style");
|
|
styleEl.id = fontStyleId;
|
|
document.head.appendChild(styleEl);
|
|
}
|
|
let cssRules = "";
|
|
fontFiles.forEach((f) => {
|
|
cssRules += `
|
|
@font-face {
|
|
font-family: "${fontFamily}";
|
|
src: url("${f.src}") format("woff2");
|
|
font-weight: ${f.weight || "normal"};
|
|
font-style: ${f.style || "normal"};
|
|
font-display: swap;
|
|
}
|
|
`;
|
|
});
|
|
styleEl.textContent = cssRules;
|
|
}
|
|
}
|
|
};
|
|
|
|
// اعمال جامع تم روی DOM و متغیرهای CSS
|
|
const applyTheme = async (newThemeConfig?: ThemeConfig) => {
|
|
if (newThemeConfig) {
|
|
currentTheme.value = { ...currentTheme.value, ...newThemeConfig };
|
|
}
|
|
const theme = currentTheme.value;
|
|
|
|
if (import.meta.client) {
|
|
const root = document.documentElement;
|
|
|
|
// 1. تنظیم RTL / Direction
|
|
const rtl = theme.rtl ?? (theme.direction === "rtl");
|
|
isRtl.value = rtl;
|
|
root.setAttribute("dir", rtl ? "rtl" : "ltr");
|
|
root.style.direction = rtl ? "rtl" : "ltr";
|
|
|
|
// 2. تنظیم رنگهای Primary
|
|
if (theme.colors?.primary) {
|
|
Object.entries(theme.colors.primary).forEach(([shade, val]) => {
|
|
root.style.setProperty(`--color-primary-${shade}`, val);
|
|
root.style.setProperty(`--ui-color-primary-${shade}`, val);
|
|
});
|
|
if (theme.colors.primary["500"]) {
|
|
root.style.setProperty("--color-primary", theme.colors.primary["500"]);
|
|
root.style.setProperty("--ui-primary", theme.colors.primary["500"]);
|
|
}
|
|
}
|
|
|
|
// 3. تنظیم فونت
|
|
if (theme.font) {
|
|
setFont(theme.font, theme.fontFallback, theme.fontFiles);
|
|
}
|
|
|
|
// 4. تنظیم ابعاد Layout
|
|
if (theme.layout) {
|
|
if (theme.layout.containerWidth) root.style.setProperty("--container-width", theme.layout.containerWidth);
|
|
if (theme.layout.sidebarWidth) root.style.setProperty("--sidebar-width", theme.layout.sidebarWidth);
|
|
if (theme.layout.headerHeight) root.style.setProperty("--header-height", theme.layout.headerHeight);
|
|
if (theme.layout.footerHeight) root.style.setProperty("--footer-height", theme.layout.footerHeight);
|
|
}
|
|
|
|
// 5. تنظیم آیکونها
|
|
if (theme.icons) {
|
|
setIcons(theme.icons);
|
|
}
|
|
|
|
// 6. همگامسازی دارک مود
|
|
const storageKey = theme.darkMode?.storageKey || "theme-mode";
|
|
const savedDark = localStorage.getItem(storageKey);
|
|
if (savedDark !== null) {
|
|
setDarkMode(savedDark === "dark");
|
|
} else if (theme.darkMode?.default === "dark") {
|
|
setDarkMode(true);
|
|
}
|
|
}
|
|
|
|
return currentTheme.value;
|
|
};
|
|
|
|
// سوئیچ داینامیک به تم یک پروژه دیگر
|
|
const switchSystemTheme = (newSystemKey: string) => {
|
|
const allThemes = (nuxtApp.$allThemes as Record<string, ThemeConfig>) || {};
|
|
if (allThemes[newSystemKey]) {
|
|
themeKey.value = newSystemKey;
|
|
applyTheme(allThemes[newSystemKey]);
|
|
}
|
|
};
|
|
|
|
return {
|
|
// Reactive State
|
|
currentTheme,
|
|
themeKey,
|
|
isDark,
|
|
isRtl,
|
|
direction,
|
|
logo,
|
|
fontName,
|
|
primaryColor,
|
|
primaryPalette,
|
|
icons,
|
|
|
|
// Methods
|
|
applyTheme,
|
|
switchSystemTheme,
|
|
toggleDarkMode,
|
|
setDarkMode,
|
|
toggleDirection,
|
|
setDirection,
|
|
setPrimaryColor,
|
|
setPrimaryPalette,
|
|
setFont,
|
|
getIcon,
|
|
setIcon,
|
|
setIcons,
|
|
};
|
|
}
|