کار اولیه
This commit is contained in:
@@ -0,0 +1,356 @@
|
||||
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,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
import { reactive, readonly } from "vue";
|
||||
|
||||
// یک state مشترک برای تمام کامپوننتها
|
||||
const confirmState = reactive({
|
||||
isOpen: false,
|
||||
title: "تأیید عملیات",
|
||||
message: "آیا از انجام این عملیات اطمینان دارید؟",
|
||||
confirmText: "تأیید",
|
||||
cancelText: "انصراف",
|
||||
resolve: null as ((value: boolean) => void) | null,
|
||||
});
|
||||
|
||||
export const useConfirmState = () => readonly(confirmState);
|
||||
|
||||
export const useConfirm = () => {
|
||||
const ucoShowConfirmModal = (
|
||||
options: {
|
||||
title?: string;
|
||||
message?: string;
|
||||
confirmText?: string;
|
||||
cancelText?: string;
|
||||
} = {}
|
||||
): Promise<boolean> => {
|
||||
confirmState.title = options.title || "تأیید عملیات";
|
||||
confirmState.message = options.message || "آیا از انجام این عملیات اطمینان دارید؟";
|
||||
confirmState.confirmText = options.confirmText || "تأیید";
|
||||
confirmState.cancelText = options.cancelText || "انصراف";
|
||||
confirmState.isOpen = true;
|
||||
|
||||
return new Promise((resolve) => {
|
||||
confirmState.resolve = resolve;
|
||||
});
|
||||
};
|
||||
|
||||
return { ucoShowConfirmModal };
|
||||
};
|
||||
|
||||
export const useConfirmActions = () => {
|
||||
const ucoConfirm = () => {
|
||||
confirmState.isOpen = false;
|
||||
if (confirmState.resolve) confirmState.resolve(true);
|
||||
confirmState.resolve = null;
|
||||
};
|
||||
|
||||
const ucoCancelConfirmModal = () => {
|
||||
confirmState.isOpen = false;
|
||||
if (confirmState.resolve) confirmState.resolve(false);
|
||||
confirmState.resolve = null;
|
||||
};
|
||||
|
||||
return { ucoConfirm, ucoCancelConfirmModal };
|
||||
};
|
||||
Reference in New Issue
Block a user