@rxap/ngx-theme 21.0.1-dev.1 → 22.0.0-dev.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +26 -0
- package/compodoc/changelog.html +24 -0
- package/compodoc/dependencies.html +7 -7
- package/compodoc/js/search/search_index.js +2 -2
- package/compodoc/properties.html +1 -1
- package/docs/documentation.json +126 -126
- package/docs/html/classes/ThemeModeService.html +6 -6
- package/docs/html/classes/ThemeService.html +2 -2
- package/docs/html/enums/ColorPaletteAlgorithm.html +2 -2
- package/docs/html/functions/ComputeColorPalette.html +1 -1
- package/docs/html/functions/ObserveCurrentThemeDensity.html +1 -1
- package/docs/html/functions/provideTheme.html +1 -1
- package/docs/html/hierarchy.html +1 -1
- package/docs/html/index.html +2 -2
- package/docs/html/interfaces/ColorPalette.html +2 -2
- package/docs/html/interfaces/ColorPaletteConfig.html +2 -2
- package/docs/html/interfaces/ColorPaletteConfigWithName.html +2 -2
- package/docs/html/interfaces/ThemeConfig.html +2 -2
- package/docs/html/modules.html +1 -1
- package/docs/html/types/ActiveTheme.html +1 -1
- package/docs/html/types/ThemeSetting.html +1 -1
- package/docs/html/variables/THEME_SETTING_KEY.html +1 -1
- package/docs/markdown/README.md +1 -1
- package/docs/markdown/classes/ThemeModeService.md +22 -22
- package/docs/markdown/classes/ThemeService.md +31 -31
- package/docs/markdown/enumerations/ColorPaletteAlgorithm.md +4 -4
- package/docs/markdown/functions/ComputeColorPalette.md +2 -2
- package/docs/markdown/functions/ObserveCurrentThemeDensity.md +2 -2
- package/docs/markdown/functions/provideTheme.md +2 -2
- package/docs/markdown/globals.md +2 -2
- package/docs/markdown/interfaces/ColorPalette.md +16 -16
- package/docs/markdown/interfaces/ColorPaletteConfig.md +5 -5
- package/docs/markdown/interfaces/ColorPaletteConfigWithName.md +6 -6
- package/docs/markdown/interfaces/ThemeConfig.md +7 -7
- package/docs/markdown/type-aliases/ActiveTheme.md +2 -2
- package/docs/markdown/type-aliases/ThemeSetting.md +2 -2
- package/docs/markdown/variables/THEME_SETTING_KEY.md +2 -2
- package/docs/wiki/Class.ThemeModeService.md +21 -21
- package/docs/wiki/Class.ThemeService.md +30 -30
- package/docs/wiki/Enumeration.ColorPaletteAlgorithm.md +3 -3
- package/docs/wiki/Function.ComputeColorPalette.md +1 -1
- package/docs/wiki/Function.ObserveCurrentThemeDensity.md +1 -1
- package/docs/wiki/Function.provideTheme.md +1 -1
- package/docs/wiki/Interface.ColorPalette.md +15 -15
- package/docs/wiki/Interface.ColorPaletteConfig.md +4 -4
- package/docs/wiki/Interface.ColorPaletteConfigWithName.md +5 -5
- package/docs/wiki/Interface.ThemeConfig.md +6 -6
- package/docs/wiki/TypeAlias.ActiveTheme.md +1 -1
- package/docs/wiki/TypeAlias.ThemeSetting.md +1 -1
- package/docs/wiki/Variable.THEME_SETTING_KEY.md +1 -1
- package/docs/wiki/globals.md +1 -1
- package/fesm2022/rxap-ngx-theme.mjs +433 -526
- package/fesm2022/rxap-ngx-theme.mjs.map +1 -1
- package/package.json +21 -17
- package/types/rxap-ngx-theme.d.ts +129 -134
|
@@ -1,537 +1,444 @@
|
|
|
1
|
-
import { TinyColor } from
|
|
2
|
-
import { Observable, Subscription, debounceTime, map, tap } from
|
|
3
|
-
import * as i0 from
|
|
4
|
-
import {
|
|
5
|
-
import { MediaMatcher } from
|
|
6
|
-
import { ConfigService } from
|
|
7
|
-
import { PubSubService, RXAP_TOPICS } from
|
|
8
|
-
import { isDefined } from
|
|
9
|
-
|
|
1
|
+
import { TinyColor } from "@ctrl/tinycolor";
|
|
2
|
+
import { Observable, Subscription, debounceTime, map, tap } from "rxjs";
|
|
3
|
+
import * as i0 from "@angular/core";
|
|
4
|
+
import { DOCUMENT, Injectable, computed, effect, inject, isDevMode, provideAppInitializer, signal } from "@angular/core";
|
|
5
|
+
import { MediaMatcher } from "@angular/cdk/layout";
|
|
6
|
+
import { ConfigService } from "@rxap/config";
|
|
7
|
+
import { PubSubService, RXAP_TOPICS } from "@rxap/ngx-pub-sub";
|
|
8
|
+
import { isDefined } from "@rxap/rxjs";
|
|
10
9
|
var ColorPaletteAlgorithm;
|
|
11
|
-
(function
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
(function(ColorPaletteAlgorithm) {
|
|
11
|
+
ColorPaletteAlgorithm["CONSTANTIN"] = "constantin";
|
|
12
|
+
ColorPaletteAlgorithm["BUCKNER"] = "buckner";
|
|
14
13
|
})(ColorPaletteAlgorithm || (ColorPaletteAlgorithm = {}));
|
|
15
14
|
function ComputeColorPalette(color, colorPalette = {}, algorithm) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
return colorPalette;
|
|
15
|
+
colorPalette = { ...colorPalette };
|
|
16
|
+
const baseLight = new TinyColor("#ffffff");
|
|
17
|
+
const baseDark = multiply(new TinyColor(color).toRgb(), new TinyColor(color).toRgb());
|
|
18
|
+
const baseTriad = new TinyColor(color).tetrad();
|
|
19
|
+
switch (algorithm) {
|
|
20
|
+
case ColorPaletteAlgorithm.CONSTANTIN:
|
|
21
|
+
colorPalette[50] ??= baseLight.mix(color, 12).toHexString();
|
|
22
|
+
colorPalette[100] ??= baseLight.mix(color, 30).toHexString();
|
|
23
|
+
colorPalette[200] ??= baseLight.mix(color, 50).toHexString();
|
|
24
|
+
colorPalette[300] ??= baseLight.mix(color, 70).toHexString();
|
|
25
|
+
colorPalette[400] ??= baseLight.mix(color, 85).toHexString();
|
|
26
|
+
colorPalette[500] ??= baseLight.mix(color, 100).toHexString();
|
|
27
|
+
colorPalette[600] ??= baseDark.mix(color, 87).toHexString();
|
|
28
|
+
colorPalette[700] ??= baseDark.mix(color, 70).toHexString();
|
|
29
|
+
colorPalette[800] ??= baseDark.mix(color, 54).toHexString();
|
|
30
|
+
colorPalette[900] ??= baseDark.mix(color, 25).toHexString();
|
|
31
|
+
colorPalette.A100 ??= baseDark.mix(baseTriad[4], 15).saturate(80).lighten(65).toHexString();
|
|
32
|
+
colorPalette.A200 ??= baseDark.mix(baseTriad[4], 15).saturate(80).lighten(55).toHexString();
|
|
33
|
+
colorPalette.A400 ??= baseDark.mix(baseTriad[4], 15).saturate(100).lighten(45).toHexString();
|
|
34
|
+
colorPalette.A700 ??= baseDark.mix(baseTriad[4], 15).saturate(100).lighten(40).toHexString();
|
|
35
|
+
break;
|
|
36
|
+
case ColorPaletteAlgorithm.BUCKNER:
|
|
37
|
+
colorPalette[50] ??= baseLight.mix(color, 12).toHexString();
|
|
38
|
+
colorPalette[100] ??= baseLight.mix(color, 30).toHexString();
|
|
39
|
+
colorPalette[200] ??= baseLight.mix(color, 50).toHexString();
|
|
40
|
+
colorPalette[300] ??= baseLight.mix(color, 70).toHexString();
|
|
41
|
+
colorPalette[400] ??= baseLight.mix(color, 85).toHexString();
|
|
42
|
+
colorPalette[500] ??= baseLight.mix(color, 100).toHexString();
|
|
43
|
+
colorPalette[600] ??= baseDark.mix(color, 87).toHexString();
|
|
44
|
+
colorPalette[700] ??= baseDark.mix(color, 70).toHexString();
|
|
45
|
+
colorPalette[800] ??= baseDark.mix(color, 54).toHexString();
|
|
46
|
+
colorPalette[900] ??= baseDark.mix(color, 25).toHexString();
|
|
47
|
+
colorPalette.A100 ??= baseDark.mix(baseTriad[3], 15).saturate(80).lighten(48).toHexString();
|
|
48
|
+
colorPalette.A200 ??= baseDark.mix(baseTriad[3], 15).saturate(80).lighten(36).toHexString();
|
|
49
|
+
colorPalette.A400 ??= baseDark.mix(baseTriad[3], 15).saturate(100).lighten(31).toHexString();
|
|
50
|
+
colorPalette.A700 ??= baseDark.mix(baseTriad[3], 15).saturate(100).lighten(28).toHexString();
|
|
51
|
+
break;
|
|
52
|
+
default:
|
|
53
|
+
colorPalette[50] ??= new TinyColor(color).lighten(52).toHexString();
|
|
54
|
+
colorPalette[100] ??= new TinyColor(color).lighten(37).toHexString();
|
|
55
|
+
colorPalette[200] ??= new TinyColor(color).lighten(26).toHexString();
|
|
56
|
+
colorPalette[300] ??= new TinyColor(color).lighten(12).toHexString();
|
|
57
|
+
colorPalette[400] ??= new TinyColor(color).lighten(6).toHexString();
|
|
58
|
+
colorPalette[500] ??= new TinyColor(color).toHexString();
|
|
59
|
+
colorPalette[600] ??= new TinyColor(color).darken(6).toHexString();
|
|
60
|
+
colorPalette[700] ??= new TinyColor(color).darken(12).toHexString();
|
|
61
|
+
colorPalette[800] ??= new TinyColor(color).darken(18).toHexString();
|
|
62
|
+
colorPalette[900] ??= new TinyColor(color).darken(24).toHexString();
|
|
63
|
+
colorPalette.A100 ??= new TinyColor(color).lighten(50).saturate(30).toHexString();
|
|
64
|
+
colorPalette.A200 ??= new TinyColor(color).lighten(30).saturate(30).toHexString();
|
|
65
|
+
colorPalette.A400 ??= new TinyColor(color).lighten(10).saturate(15).toHexString();
|
|
66
|
+
colorPalette.A700 ??= new TinyColor(color).lighten(5).saturate(5).toHexString();
|
|
67
|
+
}
|
|
68
|
+
return colorPalette;
|
|
71
69
|
}
|
|
72
70
|
function multiply(rgb1, rgb2) {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
71
|
+
rgb1.b = Math.floor(rgb1.b * rgb2.b / 255);
|
|
72
|
+
rgb1.g = Math.floor(rgb1.g * rgb2.g / 255);
|
|
73
|
+
rgb1.r = Math.floor(rgb1.r * rgb2.r / 255);
|
|
74
|
+
return new TinyColor("rgb " + rgb1.r + " " + rgb1.g + " " + rgb1.b);
|
|
77
75
|
}
|
|
78
|
-
|
|
79
76
|
function ObserveCurrentThemeDensity() {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
observer.observe(document.body, {
|
|
100
|
-
attributes: true,
|
|
101
|
-
attributeFilter: ['class'],
|
|
102
|
-
});
|
|
103
|
-
});
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
const THEME_SETTING_KEY = 'rxap_theme_settings';
|
|
107
|
-
/**
|
|
108
|
-
* Service to manage theme mode (light/dark/system) for the application.
|
|
109
|
-
* It provides functionality to handle theme preferences, persist them, and synchronize changes.
|
|
110
|
-
*
|
|
111
|
-
* This service integrates with media queries to detect system-level theme changes and allows the user
|
|
112
|
-
* or application logic to toggle or set a specific theme.
|
|
113
|
-
*
|
|
114
|
-
* It utilizes dependency injection to access various required services and the document object.
|
|
115
|
-
* Additionally, it publishes theme-related events for other parts of the application to react to
|
|
116
|
-
* theme changes.
|
|
117
|
-
*/
|
|
118
|
-
class ThemeModeService {
|
|
119
|
-
constructor() {
|
|
120
|
-
this.mediaMatcher = inject(MediaMatcher);
|
|
121
|
-
this.document = inject(DOCUMENT);
|
|
122
|
-
this.config = inject(ConfigService);
|
|
123
|
-
this.pubSub = inject(PubSubService);
|
|
124
|
-
this.darkMode = computed(() => this.activeTheme() === 'dark', ...(ngDevMode ? [{ debugName: "darkMode" }] : /* istanbul ignore next */ []));
|
|
125
|
-
this.subscriptions = null;
|
|
126
|
-
this.darkColorSchemaMediaQuery = this.mediaMatcher.matchMedia('(prefers-color-scheme: dark)');
|
|
127
|
-
this.themeSetting = signal(this.restoreThemeSetting(), ...(ngDevMode ? [{ debugName: "themeSetting" }] : /* istanbul ignore next */ []));
|
|
128
|
-
let activeTheme;
|
|
129
|
-
const themeSetting = this.themeSetting();
|
|
130
|
-
switch (themeSetting) {
|
|
131
|
-
case 'system':
|
|
132
|
-
activeTheme = this.darkColorSchemaMediaQuery.matches ? 'dark' : 'light';
|
|
133
|
-
break;
|
|
134
|
-
case 'light':
|
|
135
|
-
activeTheme = 'light';
|
|
136
|
-
break;
|
|
137
|
-
case 'dark':
|
|
138
|
-
activeTheme = 'dark';
|
|
139
|
-
break;
|
|
140
|
-
default:
|
|
141
|
-
activeTheme = this.config.get('theme.active', 'light');
|
|
142
|
-
break;
|
|
143
|
-
}
|
|
144
|
-
this.activeTheme = signal(activeTheme, ...(ngDevMode ? [{ debugName: "activeTheme" }] : /* istanbul ignore next */ []));
|
|
145
|
-
effect(() => {
|
|
146
|
-
const setting = this.themeSetting();
|
|
147
|
-
this.storeThemeSetting(setting);
|
|
148
|
-
});
|
|
149
|
-
effect(() => {
|
|
150
|
-
const active = this.activeTheme();
|
|
151
|
-
this.setThemeClasses(active);
|
|
152
|
-
});
|
|
153
|
-
this.mediaQueryListener = (event) => {
|
|
154
|
-
if (this.themeSetting() === 'system') {
|
|
155
|
-
this.activeTheme.set(event.matches ? 'dark' : 'light');
|
|
156
|
-
}
|
|
157
|
-
};
|
|
158
|
-
this.darkColorSchemaMediaQuery.addEventListener('change', this.mediaQueryListener);
|
|
159
|
-
this.restoreFromPubSub();
|
|
160
|
-
}
|
|
161
|
-
get darkModeLocalStorageKey() {
|
|
162
|
-
return window?.['__rxap__']?.['ngx']?.['theme']?.['darkMode']?.['key'] ?? THEME_SETTING_KEY;
|
|
163
|
-
}
|
|
164
|
-
restoreFromPubSub() {
|
|
165
|
-
if (this.syncSubscription) {
|
|
166
|
-
return;
|
|
167
|
-
}
|
|
168
|
-
this.syncSubscription = new Subscription();
|
|
169
|
-
this.syncSubscription.add(this.pubSub.subscribe(RXAP_TOPICS.theme.darkMode.restore).pipe(debounceTime(1000), map(event => event.data), isDefined(), tap(data => {
|
|
170
|
-
const active = data ? 'dark' : 'light';
|
|
171
|
-
this.themeSetting.set(active);
|
|
172
|
-
this.activeTheme.set(active);
|
|
173
|
-
})).subscribe());
|
|
174
|
-
}
|
|
175
|
-
/**
|
|
176
|
-
* Cleans up subscriptions and event listeners when the service is destroyed.
|
|
177
|
-
*/
|
|
178
|
-
ngOnDestroy() {
|
|
179
|
-
// Remove the media query listener if it exists.
|
|
180
|
-
this.darkColorSchemaMediaQuery.removeEventListener('change', this.mediaQueryListener);
|
|
181
|
-
// Unsubscribe from all RxJS subscriptions.
|
|
182
|
-
this.subscriptions?.unsubscribe();
|
|
183
|
-
this.syncSubscription?.unsubscribe();
|
|
184
|
-
}
|
|
185
|
-
restoreThemeSetting() {
|
|
186
|
-
const setting = localStorage.getItem(this.darkModeLocalStorageKey) ?? this.config.get('theme.setting', 'system');
|
|
187
|
-
if (setting && ['system', 'dark', 'light'].includes(setting)) {
|
|
188
|
-
return setting;
|
|
189
|
-
}
|
|
190
|
-
return 'system';
|
|
191
|
-
}
|
|
192
|
-
storeThemeSetting(setting) {
|
|
193
|
-
localStorage.setItem(this.darkModeLocalStorageKey, setting);
|
|
194
|
-
}
|
|
195
|
-
removeThemeClasses() {
|
|
196
|
-
this.document.body.classList.remove('light');
|
|
197
|
-
this.document.body.classList.remove('dark');
|
|
198
|
-
}
|
|
199
|
-
setThemeClasses(active) {
|
|
200
|
-
this.document.body.classList.remove(active === 'light' ? 'dark' : 'light');
|
|
201
|
-
this.document.body.classList.add(active);
|
|
202
|
-
}
|
|
203
|
-
/**
|
|
204
|
-
* Toggles the application theme between 'light' and 'dark' modes.
|
|
205
|
-
* It checks the currently active theme and switches to the opposite mode.
|
|
206
|
-
*
|
|
207
|
-
* @return {void} Does not return a value.
|
|
208
|
-
*/
|
|
209
|
-
toggleTheme() {
|
|
210
|
-
const currentActive = this.activeTheme();
|
|
211
|
-
// Determine the new setting based on the *currently active* theme.
|
|
212
|
-
const newActive = currentActive === 'light' ? 'dark' : 'light';
|
|
213
|
-
this.themeSetting.set(newActive);
|
|
214
|
-
this.activeTheme.set(newActive);
|
|
215
|
-
}
|
|
216
|
-
/**
|
|
217
|
-
* Sets the active theme for the application and optionally publishes the change.
|
|
218
|
-
*
|
|
219
|
-
* @param {ActiveTheme} active - The theme to be set as active.
|
|
220
|
-
* @param {boolean} [publish=true] - Indicates whether to publish the theme change event.
|
|
221
|
-
* @return {void}
|
|
222
|
-
*/
|
|
223
|
-
setTheme(active, publish = true) {
|
|
224
|
-
this.themeSetting.set(active);
|
|
225
|
-
this.activeTheme.set(active);
|
|
226
|
-
if (publish) {
|
|
227
|
-
this.pubSub.publish(RXAP_TOPICS.theme.darkMode.changed, this.darkMode());
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.9", ngImport: i0, type: ThemeModeService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
|
|
231
|
-
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "21.2.9", ngImport: i0, type: ThemeModeService, providedIn: 'root' }); }
|
|
232
|
-
}
|
|
233
|
-
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.9", ngImport: i0, type: ThemeModeService, decorators: [{
|
|
234
|
-
type: Injectable,
|
|
235
|
-
args: [{ providedIn: 'root' }]
|
|
236
|
-
}], ctorParameters: () => [] });
|
|
237
|
-
|
|
238
|
-
class ThemeService {
|
|
239
|
-
constructor() {
|
|
240
|
-
this.mediaMatcher = inject(MediaMatcher);
|
|
241
|
-
this.config = inject(ConfigService);
|
|
242
|
-
this.pubSub = inject(PubSubService);
|
|
243
|
-
this.themeModeService = inject(ThemeModeService);
|
|
244
|
-
this.darkMode = this.themeModeService.darkMode;
|
|
245
|
-
this.themeName = signal(this.getTheme(), ...(ngDevMode ? [{ debugName: "themeName" }] : /* istanbul ignore next */ []));
|
|
246
|
-
this.density = signal(this.getDensity(), ...(ngDevMode ? [{ debugName: "density" }] : /* istanbul ignore next */ []));
|
|
247
|
-
this.typography = signal(this.getTypography(), ...(ngDevMode ? [{ debugName: "typography" }] : /* istanbul ignore next */ []));
|
|
248
|
-
}
|
|
249
|
-
restore() {
|
|
250
|
-
if (isDevMode()) {
|
|
251
|
-
console.log('Restore theme settings from local storage');
|
|
252
|
-
}
|
|
253
|
-
this.restoreThemeName();
|
|
254
|
-
this.restoreDensity();
|
|
255
|
-
this.restoreTypography();
|
|
256
|
-
this.restoreFromPubSub();
|
|
257
|
-
}
|
|
258
|
-
restoreFromPubSub() {
|
|
259
|
-
if (this.syncSubscription) {
|
|
260
|
-
return;
|
|
261
|
-
}
|
|
262
|
-
this.syncSubscription = new Subscription();
|
|
263
|
-
this.syncSubscription.add(this.pubSub.subscribe(RXAP_TOPICS.theme.density.restore).pipe(debounceTime(1000), map(event => event.data), isDefined(), tap(data => this.setDensity(data, false, false))).subscribe());
|
|
264
|
-
this.syncSubscription.add(this.pubSub.subscribe(RXAP_TOPICS.theme.preset.restore).pipe(debounceTime(1000), map(event => event.data), isDefined(), tap(data => this.setTheme(data, false, false))).subscribe());
|
|
265
|
-
this.syncSubscription.add(this.pubSub.subscribe(RXAP_TOPICS.theme.typography.restore).pipe(debounceTime(1000), map(event => event.data), isDefined(), tap(data => this.setTypography(data, false, false))).subscribe());
|
|
266
|
-
}
|
|
267
|
-
get themeNameLocalStorageKey() {
|
|
268
|
-
return window?.['__rxap__']?.['ngx']?.['theme']?.['name']?.['key'] ?? `rxap-theme-name`;
|
|
269
|
-
}
|
|
270
|
-
get densityLocalStorageKey() {
|
|
271
|
-
return window?.['__rxap__']?.['ngx']?.['theme']?.['density']?.['key'] ?? `rxap-theme-density`;
|
|
272
|
-
}
|
|
273
|
-
get typographyLocalStorageKey() {
|
|
274
|
-
return window?.['__rxap__']?.['ngx']?.['theme']?.['typography']?.['key'] ?? `rxap-theme-typography`;
|
|
275
|
-
}
|
|
276
|
-
// region restore
|
|
277
|
-
restoreThemeName() {
|
|
278
|
-
const themeName = localStorage.getItem(this.themeNameLocalStorageKey);
|
|
279
|
-
if (themeName) {
|
|
280
|
-
this.setTheme(themeName, true);
|
|
281
|
-
}
|
|
282
|
-
return themeName;
|
|
283
|
-
}
|
|
284
|
-
restoreTypography() {
|
|
285
|
-
const typography = localStorage.getItem(this.typographyLocalStorageKey);
|
|
286
|
-
if (typography) {
|
|
287
|
-
this.setTypography(typography, true);
|
|
288
|
-
}
|
|
289
|
-
return typography;
|
|
290
|
-
}
|
|
291
|
-
restoreDensity() {
|
|
292
|
-
const density = localStorage.getItem(this.densityLocalStorageKey);
|
|
293
|
-
if (density) {
|
|
294
|
-
const value = Number(density);
|
|
295
|
-
if (value <= 0 && value >= -3) {
|
|
296
|
-
this.setDensity(Number(density), true);
|
|
297
|
-
return value;
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
return null;
|
|
301
|
-
}
|
|
302
|
-
// endregion
|
|
303
|
-
toggleDarkTheme() {
|
|
304
|
-
this.themeModeService.toggleTheme();
|
|
305
|
-
}
|
|
306
|
-
// region set theme configuration state
|
|
307
|
-
setDarkTheme(darkMode, silent = false, publish = true) {
|
|
308
|
-
this.themeModeService.setTheme(darkMode ? 'dark' : 'light', publish);
|
|
309
|
-
}
|
|
310
|
-
setDensity(density, silent = false, publish = true) {
|
|
311
|
-
this.applyDensity(density);
|
|
312
|
-
if (this.density() !== density) {
|
|
313
|
-
this.density.set(density);
|
|
314
|
-
if (!silent) {
|
|
315
|
-
localStorage.setItem(this.densityLocalStorageKey, String(density));
|
|
316
|
-
if (publish) {
|
|
317
|
-
this.pubSub.publish(RXAP_TOPICS.theme.density.changed, density);
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
}
|
|
322
|
-
setTypography(typography, silent = false, publish = true) {
|
|
323
|
-
this.applyTypography(typography);
|
|
324
|
-
if (this.typography() !== typography) {
|
|
325
|
-
this.typography.set(typography);
|
|
326
|
-
if (!silent) {
|
|
327
|
-
localStorage.setItem(this.typographyLocalStorageKey, typography);
|
|
328
|
-
if (publish) {
|
|
329
|
-
this.pubSub.publish(RXAP_TOPICS.theme.typography.changed, typography);
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
setTheme(themeName, silent = false, publish = true) {
|
|
335
|
-
this.applyTheme(themeName);
|
|
336
|
-
this.density.set(this.getDensity());
|
|
337
|
-
this.typography.set(this.getTypography());
|
|
338
|
-
if (this.themeName() !== themeName) {
|
|
339
|
-
this.themeName.set(themeName);
|
|
340
|
-
if (!silent) {
|
|
341
|
-
localStorage.setItem(this.themeNameLocalStorageKey, themeName);
|
|
342
|
-
if (publish) {
|
|
343
|
-
this.pubSub.publish(RXAP_TOPICS.theme.preset.changed, themeName);
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
// endregion
|
|
349
|
-
// region apply theme configuration state
|
|
350
|
-
applyDensity(density) {
|
|
351
|
-
document.body.classList.remove('density-0', 'density-1', 'density-2', 'density-3');
|
|
352
|
-
if (density < 0) {
|
|
353
|
-
document.body.classList.add(`density${density}`);
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
applyTypography(typography) {
|
|
357
|
-
document.body.style.setProperty('--font-family', `var(--font-family-${typography})`);
|
|
358
|
-
}
|
|
359
|
-
applyTheme(themeName) {
|
|
360
|
-
if (themeName === 'default') {
|
|
361
|
-
this.resetToDefaultTheme();
|
|
362
|
-
return;
|
|
363
|
-
}
|
|
364
|
-
const theme = this.getThemeConfig(themeName);
|
|
365
|
-
if (theme.primaryColor?.color) {
|
|
366
|
-
this.setCssColorVariables('primary', theme.primaryColor.color);
|
|
367
|
-
}
|
|
368
|
-
if (theme.accentColor?.color) {
|
|
369
|
-
this.setCssColorVariables('accent', theme.accentColor.color);
|
|
370
|
-
}
|
|
371
|
-
if (theme.warnColor?.color) {
|
|
372
|
-
this.setCssColorVariables('warn', theme.warnColor.color);
|
|
373
|
-
}
|
|
374
|
-
if (theme.density !== undefined) {
|
|
375
|
-
this.applyDensity(theme.density);
|
|
376
|
-
}
|
|
377
|
-
if (theme.typography) {
|
|
378
|
-
this.applyTypography(theme.typography);
|
|
379
|
-
}
|
|
380
|
-
document.body.style.setProperty(`--theme-name`, themeName);
|
|
381
|
-
}
|
|
382
|
-
// endregion
|
|
383
|
-
// region get theme configuration state
|
|
384
|
-
getDensity() {
|
|
385
|
-
let density = 0;
|
|
386
|
-
document.body.classList.forEach((className) => {
|
|
387
|
-
const match = className.match(/density-([123])/);
|
|
388
|
-
if (match) {
|
|
389
|
-
density = Number(match[1]) * -1;
|
|
390
|
-
}
|
|
391
|
-
});
|
|
392
|
-
return density;
|
|
393
|
-
}
|
|
394
|
-
getTypography() {
|
|
395
|
-
const variable = document.body.style.getPropertyValue('--font-family');
|
|
396
|
-
const match = variable.match(/var\(--font-family-(.*)\)/);
|
|
397
|
-
if (match) {
|
|
398
|
-
return match[1];
|
|
399
|
-
}
|
|
400
|
-
return 'default';
|
|
401
|
-
}
|
|
402
|
-
getTheme() {
|
|
403
|
-
return document.body.style.getPropertyValue('--theme-name') || 'default';
|
|
404
|
-
}
|
|
405
|
-
// endregion
|
|
406
|
-
// region get available
|
|
407
|
-
getAvailableColorPalettes() {
|
|
408
|
-
const colorPalettesConfigs = this.config.get('colorPalettes', false);
|
|
409
|
-
if (!colorPalettesConfigs) {
|
|
410
|
-
return null;
|
|
411
|
-
}
|
|
412
|
-
const availableColorPalettes = Object.keys(colorPalettesConfigs);
|
|
413
|
-
availableColorPalettes.unshift('default');
|
|
414
|
-
return availableColorPalettes;
|
|
415
|
-
}
|
|
416
|
-
getAvailableThemes() {
|
|
417
|
-
const themeConfigs = this.config.get('themes', false);
|
|
418
|
-
if (!themeConfigs) {
|
|
419
|
-
return null;
|
|
420
|
-
}
|
|
421
|
-
const availableThemes = Object.keys(themeConfigs);
|
|
422
|
-
availableThemes.unshift('default');
|
|
423
|
-
return availableThemes;
|
|
424
|
-
}
|
|
425
|
-
getAvailableTypographies() {
|
|
426
|
-
const availableTypographies = this.config.get('typographies', false);
|
|
427
|
-
if (!availableTypographies) {
|
|
428
|
-
return null;
|
|
429
|
-
}
|
|
430
|
-
return Array
|
|
431
|
-
.from(document.styleSheets)
|
|
432
|
-
.filter(sheet => sheet.href === null || sheet.href.startsWith(window.location.origin))
|
|
433
|
-
.flatMap(sheet => Array.from(sheet.cssRules || []))
|
|
434
|
-
.filter((rule) => rule.selectorText === ':root')
|
|
435
|
-
.flatMap((rule) => Array.from(rule.style))
|
|
436
|
-
.filter((prop) => prop.startsWith('--'))
|
|
437
|
-
.filter((prop) => prop.startsWith('--font-family-'))
|
|
438
|
-
.map((prop) => prop.replace('--font-family-', ''))
|
|
439
|
-
.sort();
|
|
440
|
-
}
|
|
441
|
-
// endregion
|
|
442
|
-
getColorPalette(colorPaletteName) {
|
|
443
|
-
const colorPaletteConfig = this.config.getOrThrow(`colorPalettes.${colorPaletteName}`);
|
|
444
|
-
return this.coerceColorPalette(colorPaletteConfig);
|
|
445
|
-
}
|
|
446
|
-
// region utility
|
|
447
|
-
coerceColorPalette(colorPaletteConfig) {
|
|
448
|
-
let colorPalette = {};
|
|
449
|
-
if (colorPaletteConfig.color) {
|
|
450
|
-
if (Object.keys(colorPaletteConfig.color).length !== 14) {
|
|
451
|
-
// the color palette is not complete
|
|
452
|
-
if (colorPaletteConfig.base) {
|
|
453
|
-
colorPalette =
|
|
454
|
-
ComputeColorPalette(colorPaletteConfig.base, colorPaletteConfig.color, colorPaletteConfig.algorithm);
|
|
455
|
-
}
|
|
456
|
-
else {
|
|
457
|
-
colorPalette = colorPaletteConfig.color;
|
|
458
|
-
}
|
|
459
|
-
}
|
|
460
|
-
else {
|
|
461
|
-
colorPalette = colorPaletteConfig.color;
|
|
462
|
-
}
|
|
463
|
-
}
|
|
464
|
-
else if (colorPaletteConfig.base) {
|
|
465
|
-
colorPalette = ComputeColorPalette(colorPaletteConfig.base, {}, colorPaletteConfig.algorithm);
|
|
466
|
-
}
|
|
467
|
-
if (Object.keys(colorPalette).length === 0) {
|
|
468
|
-
throw new Error('FATAL: The color palette has neither a base nor a color property');
|
|
469
|
-
}
|
|
470
|
-
return colorPalette;
|
|
471
|
-
}
|
|
472
|
-
getThemeConfig(themeName) {
|
|
473
|
-
const themeConfig = this.config.getOrThrow(`themes.${themeName}`);
|
|
474
|
-
if (themeConfig.accentColor) {
|
|
475
|
-
themeConfig.accentColor.color = this.coerceColorPalette(themeConfig.accentColor);
|
|
476
|
-
}
|
|
477
|
-
if (themeConfig.primaryColor) {
|
|
478
|
-
themeConfig.primaryColor.color = this.coerceColorPalette(themeConfig.primaryColor);
|
|
479
|
-
}
|
|
480
|
-
if (themeConfig.warnColor) {
|
|
481
|
-
themeConfig.warnColor.color = this.coerceColorPalette(themeConfig.warnColor);
|
|
482
|
-
}
|
|
483
|
-
return themeConfig;
|
|
484
|
-
}
|
|
485
|
-
setCssColorVariables(name, colorPalette) {
|
|
486
|
-
this.clearCssColorVariables(name);
|
|
487
|
-
for (const [index, color] of Object.entries(colorPalette)) {
|
|
488
|
-
document.body.style.setProperty(`--${name}-${index}`, color);
|
|
489
|
-
}
|
|
490
|
-
}
|
|
491
|
-
clearCssColorVariables(name) {
|
|
492
|
-
document.body.style.removeProperty(`--${name}-50`);
|
|
493
|
-
for (let index = 100; index <= 900; index += 100) {
|
|
494
|
-
document.body.style.removeProperty(`--${name}-${index}`);
|
|
495
|
-
}
|
|
496
|
-
document.body.style.removeProperty(`--${name}-a100`);
|
|
497
|
-
document.body.style.removeProperty(`--${name}-a200`);
|
|
498
|
-
document.body.style.removeProperty(`--${name}-a400`);
|
|
499
|
-
document.body.style.removeProperty(`--${name}-a700`);
|
|
500
|
-
}
|
|
501
|
-
resetToDefaultTheme() {
|
|
502
|
-
this.clearCssColorVariables('primary');
|
|
503
|
-
this.clearCssColorVariables('accent');
|
|
504
|
-
this.clearCssColorVariables('warn');
|
|
505
|
-
this.setDensity(0);
|
|
506
|
-
this.setTypography('default');
|
|
507
|
-
document.body.style.removeProperty(`--theme-name`);
|
|
508
|
-
localStorage.removeItem(this.themeNameLocalStorageKey);
|
|
509
|
-
localStorage.removeItem(this.densityLocalStorageKey);
|
|
510
|
-
localStorage.removeItem(this.typographyLocalStorageKey);
|
|
511
|
-
}
|
|
512
|
-
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.9", ngImport: i0, type: ThemeService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
|
|
513
|
-
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "21.2.9", ngImport: i0, type: ThemeService, providedIn: 'root' }); }
|
|
77
|
+
return new Observable((subscriber) => {
|
|
78
|
+
const observer = new MutationObserver((mutations) => {
|
|
79
|
+
mutations.forEach((mutation) => {
|
|
80
|
+
if (mutation.type === "attributes" && mutation.attributeName === "class") {
|
|
81
|
+
const classList = mutation.target.classList;
|
|
82
|
+
const matchingClasses = Array.from(classList).filter((className) => /density-\d/.test(className));
|
|
83
|
+
if (matchingClasses.length > 0) {
|
|
84
|
+
const match = matchingClasses[0].match(/density-(\d)/);
|
|
85
|
+
if (match) subscriber.next(Number(match[1]) * -1);
|
|
86
|
+
} else subscriber.next(0);
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
subscriber.add(() => observer.disconnect());
|
|
91
|
+
observer.observe(document.body, {
|
|
92
|
+
attributes: true,
|
|
93
|
+
attributeFilter: ["class"]
|
|
94
|
+
});
|
|
95
|
+
});
|
|
514
96
|
}
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
97
|
+
const THEME_SETTING_KEY = "rxap_theme_settings";
|
|
98
|
+
var ThemeModeService = class ThemeModeService {
|
|
99
|
+
constructor() {
|
|
100
|
+
this.mediaMatcher = inject(MediaMatcher);
|
|
101
|
+
this.document = inject(DOCUMENT);
|
|
102
|
+
this.config = inject(ConfigService);
|
|
103
|
+
this.pubSub = inject(PubSubService);
|
|
104
|
+
this.darkMode = computed(() => this.activeTheme() === "dark", ...ngDevMode ? [{ debugName: "darkMode" }] : /* istanbul ignore next */ []);
|
|
105
|
+
this.subscriptions = null;
|
|
106
|
+
this.darkColorSchemaMediaQuery = this.mediaMatcher.matchMedia("(prefers-color-scheme: dark)");
|
|
107
|
+
this.themeSetting = signal(this.restoreThemeSetting(), ...ngDevMode ? [{ debugName: "themeSetting" }] : /* istanbul ignore next */ []);
|
|
108
|
+
let activeTheme;
|
|
109
|
+
switch (this.themeSetting()) {
|
|
110
|
+
case "system":
|
|
111
|
+
activeTheme = this.darkColorSchemaMediaQuery.matches ? "dark" : "light";
|
|
112
|
+
break;
|
|
113
|
+
case "light":
|
|
114
|
+
activeTheme = "light";
|
|
115
|
+
break;
|
|
116
|
+
case "dark":
|
|
117
|
+
activeTheme = "dark";
|
|
118
|
+
break;
|
|
119
|
+
default: activeTheme = this.config.get("theme.active", "light");
|
|
120
|
+
}
|
|
121
|
+
this.activeTheme = signal(activeTheme, ...ngDevMode ? [{ debugName: "activeTheme" }] : /* istanbul ignore next */ []);
|
|
122
|
+
effect(() => {
|
|
123
|
+
const setting = this.themeSetting();
|
|
124
|
+
this.storeThemeSetting(setting);
|
|
125
|
+
});
|
|
126
|
+
effect(() => {
|
|
127
|
+
const active = this.activeTheme();
|
|
128
|
+
this.setThemeClasses(active);
|
|
129
|
+
});
|
|
130
|
+
this.mediaQueryListener = (event) => {
|
|
131
|
+
if (this.themeSetting() === "system") this.activeTheme.set(event.matches ? "dark" : "light");
|
|
132
|
+
};
|
|
133
|
+
this.darkColorSchemaMediaQuery.addEventListener("change", this.mediaQueryListener);
|
|
134
|
+
this.restoreFromPubSub();
|
|
135
|
+
}
|
|
136
|
+
get darkModeLocalStorageKey() {
|
|
137
|
+
return window?.["__rxap__"]?.["ngx"]?.["theme"]?.["darkMode"]?.["key"] ?? "rxap_theme_settings";
|
|
138
|
+
}
|
|
139
|
+
restoreFromPubSub() {
|
|
140
|
+
if (this.syncSubscription) return;
|
|
141
|
+
this.syncSubscription = new Subscription();
|
|
142
|
+
this.syncSubscription.add(this.pubSub.subscribe(RXAP_TOPICS.theme.darkMode.restore).pipe(debounceTime(1e3), map((event) => event.data), isDefined(), tap((data) => {
|
|
143
|
+
const active = data ? "dark" : "light";
|
|
144
|
+
this.themeSetting.set(active);
|
|
145
|
+
this.activeTheme.set(active);
|
|
146
|
+
})).subscribe());
|
|
147
|
+
}
|
|
148
|
+
ngOnDestroy() {
|
|
149
|
+
this.darkColorSchemaMediaQuery.removeEventListener("change", this.mediaQueryListener);
|
|
150
|
+
this.subscriptions?.unsubscribe();
|
|
151
|
+
this.syncSubscription?.unsubscribe();
|
|
152
|
+
}
|
|
153
|
+
restoreThemeSetting() {
|
|
154
|
+
const setting = localStorage.getItem(this.darkModeLocalStorageKey) ?? this.config.get("theme.setting", "system");
|
|
155
|
+
if (setting && [
|
|
156
|
+
"system",
|
|
157
|
+
"dark",
|
|
158
|
+
"light"
|
|
159
|
+
].includes(setting)) return setting;
|
|
160
|
+
return "system";
|
|
161
|
+
}
|
|
162
|
+
storeThemeSetting(setting) {
|
|
163
|
+
localStorage.setItem(this.darkModeLocalStorageKey, setting);
|
|
164
|
+
}
|
|
165
|
+
removeThemeClasses() {
|
|
166
|
+
this.document.body.classList.remove("light");
|
|
167
|
+
this.document.body.classList.remove("dark");
|
|
168
|
+
}
|
|
169
|
+
setThemeClasses(active) {
|
|
170
|
+
this.document.body.classList.remove(active === "light" ? "dark" : "light");
|
|
171
|
+
this.document.body.classList.add(active);
|
|
172
|
+
}
|
|
173
|
+
toggleTheme() {
|
|
174
|
+
const newActive = this.activeTheme() === "light" ? "dark" : "light";
|
|
175
|
+
this.themeSetting.set(newActive);
|
|
176
|
+
this.activeTheme.set(newActive);
|
|
177
|
+
}
|
|
178
|
+
setTheme(active, publish = true) {
|
|
179
|
+
this.themeSetting.set(active);
|
|
180
|
+
this.activeTheme.set(active);
|
|
181
|
+
if (publish) this.pubSub.publish(RXAP_TOPICS.theme.darkMode.changed, this.darkMode());
|
|
182
|
+
}
|
|
183
|
+
static {
|
|
184
|
+
this.ɵfac = i0.ɵɵngDeclareFactory({
|
|
185
|
+
minVersion: "12.0.0",
|
|
186
|
+
version: "22.3.0",
|
|
187
|
+
ngImport: i0,
|
|
188
|
+
type: ThemeModeService,
|
|
189
|
+
deps: [],
|
|
190
|
+
target: i0.ɵɵFactoryTarget.Injectable
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
static {
|
|
194
|
+
this.ɵprov = i0.ɵɵngDeclareInjectable({
|
|
195
|
+
minVersion: "12.0.0",
|
|
196
|
+
version: "22.3.0",
|
|
197
|
+
ngImport: i0,
|
|
198
|
+
type: ThemeModeService,
|
|
199
|
+
providedIn: "root"
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
i0.ɵɵngDeclareClassMetadata({
|
|
204
|
+
minVersion: "12.0.0",
|
|
205
|
+
version: "22.3.0",
|
|
206
|
+
ngImport: i0,
|
|
207
|
+
type: ThemeModeService,
|
|
208
|
+
decorators: [{
|
|
209
|
+
type: Injectable,
|
|
210
|
+
args: [{ providedIn: "root" }]
|
|
211
|
+
}],
|
|
212
|
+
ctorParameters: () => []
|
|
213
|
+
});
|
|
214
|
+
var ThemeService = class ThemeService {
|
|
215
|
+
constructor() {
|
|
216
|
+
this.mediaMatcher = inject(MediaMatcher);
|
|
217
|
+
this.config = inject(ConfigService);
|
|
218
|
+
this.pubSub = inject(PubSubService);
|
|
219
|
+
this.themeModeService = inject(ThemeModeService);
|
|
220
|
+
this.darkMode = this.themeModeService.darkMode;
|
|
221
|
+
this.themeName = signal(this.getTheme(), ...ngDevMode ? [{ debugName: "themeName" }] : /* istanbul ignore next */ []);
|
|
222
|
+
this.density = signal(this.getDensity(), ...ngDevMode ? [{ debugName: "density" }] : /* istanbul ignore next */ []);
|
|
223
|
+
this.typography = signal(this.getTypography(), ...ngDevMode ? [{ debugName: "typography" }] : /* istanbul ignore next */ []);
|
|
224
|
+
}
|
|
225
|
+
restore() {
|
|
226
|
+
if (isDevMode()) console.log("Restore theme settings from local storage");
|
|
227
|
+
this.restoreThemeName();
|
|
228
|
+
this.restoreDensity();
|
|
229
|
+
this.restoreTypography();
|
|
230
|
+
this.restoreFromPubSub();
|
|
231
|
+
}
|
|
232
|
+
restoreFromPubSub() {
|
|
233
|
+
if (this.syncSubscription) return;
|
|
234
|
+
this.syncSubscription = new Subscription();
|
|
235
|
+
this.syncSubscription.add(this.pubSub.subscribe(RXAP_TOPICS.theme.density.restore).pipe(debounceTime(1e3), map((event) => event.data), isDefined(), tap((data) => this.setDensity(data, false, false))).subscribe());
|
|
236
|
+
this.syncSubscription.add(this.pubSub.subscribe(RXAP_TOPICS.theme.preset.restore).pipe(debounceTime(1e3), map((event) => event.data), isDefined(), tap((data) => this.setTheme(data, false, false))).subscribe());
|
|
237
|
+
this.syncSubscription.add(this.pubSub.subscribe(RXAP_TOPICS.theme.typography.restore).pipe(debounceTime(1e3), map((event) => event.data), isDefined(), tap((data) => this.setTypography(data, false, false))).subscribe());
|
|
238
|
+
}
|
|
239
|
+
get themeNameLocalStorageKey() {
|
|
240
|
+
return window?.["__rxap__"]?.["ngx"]?.["theme"]?.["name"]?.["key"] ?? `rxap-theme-name`;
|
|
241
|
+
}
|
|
242
|
+
get densityLocalStorageKey() {
|
|
243
|
+
return window?.["__rxap__"]?.["ngx"]?.["theme"]?.["density"]?.["key"] ?? `rxap-theme-density`;
|
|
244
|
+
}
|
|
245
|
+
get typographyLocalStorageKey() {
|
|
246
|
+
return window?.["__rxap__"]?.["ngx"]?.["theme"]?.["typography"]?.["key"] ?? `rxap-theme-typography`;
|
|
247
|
+
}
|
|
248
|
+
restoreThemeName() {
|
|
249
|
+
const themeName = localStorage.getItem(this.themeNameLocalStorageKey);
|
|
250
|
+
if (themeName) this.setTheme(themeName, true);
|
|
251
|
+
return themeName;
|
|
252
|
+
}
|
|
253
|
+
restoreTypography() {
|
|
254
|
+
const typography = localStorage.getItem(this.typographyLocalStorageKey);
|
|
255
|
+
if (typography) this.setTypography(typography, true);
|
|
256
|
+
return typography;
|
|
257
|
+
}
|
|
258
|
+
restoreDensity() {
|
|
259
|
+
const density = localStorage.getItem(this.densityLocalStorageKey);
|
|
260
|
+
if (density) {
|
|
261
|
+
const value = Number(density);
|
|
262
|
+
if (value <= 0 && value >= -3) {
|
|
263
|
+
this.setDensity(Number(density), true);
|
|
264
|
+
return value;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
return null;
|
|
268
|
+
}
|
|
269
|
+
toggleDarkTheme() {
|
|
270
|
+
this.themeModeService.toggleTheme();
|
|
271
|
+
}
|
|
272
|
+
setDarkTheme(darkMode, silent = false, publish = true) {
|
|
273
|
+
this.themeModeService.setTheme(darkMode ? "dark" : "light", publish);
|
|
274
|
+
}
|
|
275
|
+
setDensity(density, silent = false, publish = true) {
|
|
276
|
+
this.applyDensity(density);
|
|
277
|
+
if (this.density() !== density) {
|
|
278
|
+
this.density.set(density);
|
|
279
|
+
if (!silent) {
|
|
280
|
+
localStorage.setItem(this.densityLocalStorageKey, String(density));
|
|
281
|
+
if (publish) this.pubSub.publish(RXAP_TOPICS.theme.density.changed, density);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
setTypography(typography, silent = false, publish = true) {
|
|
286
|
+
this.applyTypography(typography);
|
|
287
|
+
if (this.typography() !== typography) {
|
|
288
|
+
this.typography.set(typography);
|
|
289
|
+
if (!silent) {
|
|
290
|
+
localStorage.setItem(this.typographyLocalStorageKey, typography);
|
|
291
|
+
if (publish) this.pubSub.publish(RXAP_TOPICS.theme.typography.changed, typography);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
setTheme(themeName, silent = false, publish = true) {
|
|
296
|
+
this.applyTheme(themeName);
|
|
297
|
+
this.density.set(this.getDensity());
|
|
298
|
+
this.typography.set(this.getTypography());
|
|
299
|
+
if (this.themeName() !== themeName) {
|
|
300
|
+
this.themeName.set(themeName);
|
|
301
|
+
if (!silent) {
|
|
302
|
+
localStorage.setItem(this.themeNameLocalStorageKey, themeName);
|
|
303
|
+
if (publish) this.pubSub.publish(RXAP_TOPICS.theme.preset.changed, themeName);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
applyDensity(density) {
|
|
308
|
+
document.body.classList.remove("density-0", "density-1", "density-2", "density-3");
|
|
309
|
+
if (density < 0) document.body.classList.add(`density${density}`);
|
|
310
|
+
}
|
|
311
|
+
applyTypography(typography) {
|
|
312
|
+
document.body.style.setProperty("--font-family", `var(--font-family-${typography})`);
|
|
313
|
+
}
|
|
314
|
+
applyTheme(themeName) {
|
|
315
|
+
if (themeName === "default") {
|
|
316
|
+
this.resetToDefaultTheme();
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
const theme = this.getThemeConfig(themeName);
|
|
320
|
+
if (theme.primaryColor?.color) this.setCssColorVariables("primary", theme.primaryColor.color);
|
|
321
|
+
if (theme.accentColor?.color) this.setCssColorVariables("accent", theme.accentColor.color);
|
|
322
|
+
if (theme.warnColor?.color) this.setCssColorVariables("warn", theme.warnColor.color);
|
|
323
|
+
if (theme.density !== void 0) this.applyDensity(theme.density);
|
|
324
|
+
if (theme.typography) this.applyTypography(theme.typography);
|
|
325
|
+
document.body.style.setProperty(`--theme-name`, themeName);
|
|
326
|
+
}
|
|
327
|
+
getDensity() {
|
|
328
|
+
let density = 0;
|
|
329
|
+
document.body.classList.forEach((className) => {
|
|
330
|
+
const match = className.match(/density-([123])/);
|
|
331
|
+
if (match) density = Number(match[1]) * -1;
|
|
332
|
+
});
|
|
333
|
+
return density;
|
|
334
|
+
}
|
|
335
|
+
getTypography() {
|
|
336
|
+
const match = document.body.style.getPropertyValue("--font-family").match(/var\(--font-family-(.*)\)/);
|
|
337
|
+
if (match) return match[1];
|
|
338
|
+
return "default";
|
|
339
|
+
}
|
|
340
|
+
getTheme() {
|
|
341
|
+
return document.body.style.getPropertyValue("--theme-name") || "default";
|
|
342
|
+
}
|
|
343
|
+
getAvailableColorPalettes() {
|
|
344
|
+
const colorPalettesConfigs = this.config.get("colorPalettes", false);
|
|
345
|
+
if (!colorPalettesConfigs) return null;
|
|
346
|
+
const availableColorPalettes = Object.keys(colorPalettesConfigs);
|
|
347
|
+
availableColorPalettes.unshift("default");
|
|
348
|
+
return availableColorPalettes;
|
|
349
|
+
}
|
|
350
|
+
getAvailableThemes() {
|
|
351
|
+
const themeConfigs = this.config.get("themes", false);
|
|
352
|
+
if (!themeConfigs) return null;
|
|
353
|
+
const availableThemes = Object.keys(themeConfigs);
|
|
354
|
+
availableThemes.unshift("default");
|
|
355
|
+
return availableThemes;
|
|
356
|
+
}
|
|
357
|
+
getAvailableTypographies() {
|
|
358
|
+
if (!this.config.get("typographies", false)) return null;
|
|
359
|
+
return Array.from(document.styleSheets).filter((sheet) => sheet.href === null || sheet.href.startsWith(window.location.origin)).flatMap((sheet) => Array.from(sheet.cssRules || [])).filter((rule) => rule.selectorText === ":root").flatMap((rule) => Array.from(rule.style)).filter((prop) => prop.startsWith("--")).filter((prop) => prop.startsWith("--font-family-")).map((prop) => prop.replace("--font-family-", "")).sort();
|
|
360
|
+
}
|
|
361
|
+
getColorPalette(colorPaletteName) {
|
|
362
|
+
const colorPaletteConfig = this.config.getOrThrow(`colorPalettes.${colorPaletteName}`);
|
|
363
|
+
return this.coerceColorPalette(colorPaletteConfig);
|
|
364
|
+
}
|
|
365
|
+
coerceColorPalette(colorPaletteConfig) {
|
|
366
|
+
let colorPalette = {};
|
|
367
|
+
if (colorPaletteConfig.color) {
|
|
368
|
+
if (Object.keys(colorPaletteConfig.color).length !== 14) {
|
|
369
|
+
if (colorPaletteConfig.base) colorPalette = ComputeColorPalette(colorPaletteConfig.base, colorPaletteConfig.color, colorPaletteConfig.algorithm);
|
|
370
|
+
else colorPalette = colorPaletteConfig.color;
|
|
371
|
+
} else colorPalette = colorPaletteConfig.color;
|
|
372
|
+
} else if (colorPaletteConfig.base) colorPalette = ComputeColorPalette(colorPaletteConfig.base, {}, colorPaletteConfig.algorithm);
|
|
373
|
+
if (Object.keys(colorPalette).length === 0) throw new Error("FATAL: The color palette has neither a base nor a color property");
|
|
374
|
+
return colorPalette;
|
|
375
|
+
}
|
|
376
|
+
getThemeConfig(themeName) {
|
|
377
|
+
const themeConfig = this.config.getOrThrow(`themes.${themeName}`);
|
|
378
|
+
if (themeConfig.accentColor) themeConfig.accentColor.color = this.coerceColorPalette(themeConfig.accentColor);
|
|
379
|
+
if (themeConfig.primaryColor) themeConfig.primaryColor.color = this.coerceColorPalette(themeConfig.primaryColor);
|
|
380
|
+
if (themeConfig.warnColor) themeConfig.warnColor.color = this.coerceColorPalette(themeConfig.warnColor);
|
|
381
|
+
return themeConfig;
|
|
382
|
+
}
|
|
383
|
+
setCssColorVariables(name, colorPalette) {
|
|
384
|
+
this.clearCssColorVariables(name);
|
|
385
|
+
for (const [index, color] of Object.entries(colorPalette)) document.body.style.setProperty(`--${name}-${index}`, color);
|
|
386
|
+
}
|
|
387
|
+
clearCssColorVariables(name) {
|
|
388
|
+
document.body.style.removeProperty(`--${name}-50`);
|
|
389
|
+
for (let index = 100; index <= 900; index += 100) document.body.style.removeProperty(`--${name}-${index}`);
|
|
390
|
+
document.body.style.removeProperty(`--${name}-a100`);
|
|
391
|
+
document.body.style.removeProperty(`--${name}-a200`);
|
|
392
|
+
document.body.style.removeProperty(`--${name}-a400`);
|
|
393
|
+
document.body.style.removeProperty(`--${name}-a700`);
|
|
394
|
+
}
|
|
395
|
+
resetToDefaultTheme() {
|
|
396
|
+
this.clearCssColorVariables("primary");
|
|
397
|
+
this.clearCssColorVariables("accent");
|
|
398
|
+
this.clearCssColorVariables("warn");
|
|
399
|
+
this.setDensity(0);
|
|
400
|
+
this.setTypography("default");
|
|
401
|
+
document.body.style.removeProperty(`--theme-name`);
|
|
402
|
+
localStorage.removeItem(this.themeNameLocalStorageKey);
|
|
403
|
+
localStorage.removeItem(this.densityLocalStorageKey);
|
|
404
|
+
localStorage.removeItem(this.typographyLocalStorageKey);
|
|
405
|
+
}
|
|
406
|
+
static {
|
|
407
|
+
this.ɵfac = i0.ɵɵngDeclareFactory({
|
|
408
|
+
minVersion: "12.0.0",
|
|
409
|
+
version: "22.3.0",
|
|
410
|
+
ngImport: i0,
|
|
411
|
+
type: ThemeService,
|
|
412
|
+
deps: [],
|
|
413
|
+
target: i0.ɵɵFactoryTarget.Injectable
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
static {
|
|
417
|
+
this.ɵprov = i0.ɵɵngDeclareInjectable({
|
|
418
|
+
minVersion: "12.0.0",
|
|
419
|
+
version: "22.3.0",
|
|
420
|
+
ngImport: i0,
|
|
421
|
+
type: ThemeService,
|
|
422
|
+
providedIn: "root"
|
|
423
|
+
});
|
|
424
|
+
}
|
|
425
|
+
};
|
|
426
|
+
i0.ɵɵngDeclareClassMetadata({
|
|
427
|
+
minVersion: "12.0.0",
|
|
428
|
+
version: "22.3.0",
|
|
429
|
+
ngImport: i0,
|
|
430
|
+
type: ThemeService,
|
|
431
|
+
decorators: [{
|
|
432
|
+
type: Injectable,
|
|
433
|
+
args: [{ providedIn: "root" }]
|
|
434
|
+
}],
|
|
435
|
+
ctorParameters: () => []
|
|
436
|
+
});
|
|
520
437
|
function provideTheme() {
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
return initializerFn();
|
|
525
|
-
})
|
|
526
|
-
];
|
|
438
|
+
return [provideAppInitializer(() => {
|
|
439
|
+
return ((themeService) => () => themeService.restore())(inject(ThemeService))();
|
|
440
|
+
})];
|
|
527
441
|
}
|
|
528
|
-
|
|
529
|
-
// region
|
|
530
|
-
// endregion
|
|
531
|
-
|
|
532
|
-
/**
|
|
533
|
-
* Generated bundle index. Do not edit.
|
|
534
|
-
*/
|
|
535
|
-
|
|
536
442
|
export { ColorPaletteAlgorithm, ComputeColorPalette, ObserveCurrentThemeDensity, THEME_SETTING_KEY, ThemeModeService, ThemeService, provideTheme };
|
|
537
|
-
|
|
443
|
+
|
|
444
|
+
//# sourceMappingURL=rxap-ngx-theme.mjs.map
|