|
| 1 | +const themeKey = 'currentThemeId'; |
| 2 | + |
| 3 | +const themeLoaders = import.meta.glob('../artifacts/css/dx.*.css', { as: 'url' }); |
| 4 | + |
| 5 | +const themeList = Object.keys(themeLoaders).map((path) => { |
| 6 | + const match = path.match(/dx\.(.+)\.css$/); |
| 7 | + return match ? match[1] : null; |
| 8 | +}).filter(Boolean) as string[]; |
| 9 | + |
| 10 | +function groupThemes(themes: string[]) { |
| 11 | + const groups: Record<string, string[]> = {}; |
| 12 | + themes.forEach((theme) => { |
| 13 | + const [group] = theme.split('.'); |
| 14 | + const groupName = group.charAt(0).toUpperCase() + group.slice(1); |
| 15 | + if (!groups[groupName]) groups[groupName] = []; |
| 16 | + groups[groupName].push(theme); |
| 17 | + }); |
| 18 | + return groups; |
| 19 | +} |
| 20 | + |
| 21 | +const groupedThemes = groupThemes(themeList); |
| 22 | + |
| 23 | +function initThemes(dropDownList: HTMLSelectElement) { |
| 24 | + Object.entries(groupedThemes).forEach(([group, themes]) => { |
| 25 | + const parent = document.createElement('optgroup'); |
| 26 | + parent.label = group; |
| 27 | + |
| 28 | + themes.forEach((theme) => { |
| 29 | + const child = document.createElement('option'); |
| 30 | + child.value = theme; |
| 31 | + child.text = theme.replaceAll('.', ' '); |
| 32 | + parent.appendChild(child); |
| 33 | + }); |
| 34 | + |
| 35 | + dropDownList.appendChild(parent); |
| 36 | + }); |
| 37 | +} |
| 38 | + |
| 39 | +function loadThemeCss(themeId: string): Promise<void> { |
| 40 | + return new Promise((resolve, reject) => { |
| 41 | + const oldLink = document.getElementById('theme-stylesheet'); |
| 42 | + if (oldLink) oldLink.remove(); |
| 43 | + |
| 44 | + const key = Object.keys(themeLoaders).find((p) => p.includes(`dx.${themeId}.css`)); |
| 45 | + if (!key) { |
| 46 | + reject(new Error(`Theme not found: ${themeId}`)); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + themeLoaders[key]().then((cssUrl: string) => { |
| 51 | + const link = document.createElement('link'); |
| 52 | + link.id = 'theme-stylesheet'; |
| 53 | + link.rel = 'stylesheet'; |
| 54 | + link.href = cssUrl; |
| 55 | + |
| 56 | + link.onload = () => resolve(); |
| 57 | + link.onerror = () => reject(new Error(`Failed to load theme: ${themeId}`)); |
| 58 | + |
| 59 | + document.head.appendChild(link); |
| 60 | + }); |
| 61 | + }); |
| 62 | +} |
| 63 | + |
| 64 | +export function setupThemeSelector(selectorId: string): Promise<void> { |
| 65 | + return new Promise((resolve) => { |
| 66 | + const dropDownList = document.querySelector<HTMLSelectElement>(`#${selectorId}`); |
| 67 | + if (!dropDownList) { |
| 68 | + resolve(); |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + initThemes(dropDownList); |
| 73 | + |
| 74 | + const savedTheme = window.localStorage.getItem(themeKey) || themeList[0]; |
| 75 | + dropDownList.value = savedTheme; |
| 76 | + |
| 77 | + loadThemeCss(savedTheme).then(() => { |
| 78 | + dropDownList.addEventListener('change', () => { |
| 79 | + const newTheme = dropDownList.value; |
| 80 | + window.localStorage.setItem(themeKey, newTheme); |
| 81 | + loadThemeCss(newTheme); |
| 82 | + }); |
| 83 | + |
| 84 | + resolve(); |
| 85 | + }); |
| 86 | + }); |
| 87 | +} |
0 commit comments