slicejs-web-framework 3.3.6 → 3.3.8

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.
@@ -1,78 +1,82 @@
1
-
2
- export default class StylesManager {
3
- constructor() {
4
- this.componentStyles = document.createElement('style');
5
- this.componentStyles.id = 'slice-component-styles';
6
- document.head.appendChild(this.componentStyles);
7
-
8
- }
9
-
10
- /**
11
- * Load global styles and initialize ThemeManager if enabled.
12
- * @returns {Promise<void>}
13
- */
14
- async init() {
15
- const requestedStyles = Array.isArray(slice.stylesConfig.requestedStyles)
16
- ? slice.stylesConfig.requestedStyles
17
- : [];
18
-
19
- const styleResults = await Promise.all(
20
- requestedStyles.map(async (styleName) => {
21
- const styles = await slice.controller.fetchText(styleName, 'styles');
22
- return { styleName, styles };
23
- })
24
- );
25
-
26
- for (const { styleName, styles } of styleResults) {
27
- this.appendComponentStyles(styles);
28
- slice.logger.logInfo('StylesManager', `${styleName} styles loaded`);
29
- }
30
-
31
- if (slice.themeConfig.enabled) {
32
- const ThemeManagerClass = slice.frameworkClasses?.ThemeManager
33
- || await slice.getClass(`${slice.paths.structuralComponentFolderPath}/StylesManager/ThemeManager/ThemeManager.js`);
34
- if (!ThemeManagerClass) {
35
- throw new Error('ThemeManager not available');
36
- }
37
-
38
- this.themeManager = new ThemeManagerClass();
39
- let theme;
40
-
41
- if (slice.themeConfig.saveThemeLocally) {
42
- theme = localStorage.getItem('sliceTheme');
43
- }
44
-
45
- if (!theme) {
46
- theme = slice.themeConfig.defaultTheme;
47
- }
48
-
49
- if (slice.themeConfig.useBrowserTheme) {
50
- const browserTheme = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'Dark' : 'Light';
51
- theme = browserTheme;
52
- }
53
-
54
- await this.themeManager.applyTheme(theme);
55
- }
56
- }
57
-
58
- //add a method that will add css as text to the componentStyles element
59
- /**
60
- * Append raw CSS to the global component style tag.
61
- * @param {string} cssText
62
- * @returns {void}
63
- */
64
- appendComponentStyles(cssText) {
65
- this.componentStyles.appendChild(document.createTextNode(cssText));
66
- }
67
-
68
- /**
69
- * Register CSS for a component.
70
- * @param {string} componentName
71
- * @param {string} cssText
72
- * @returns {void}
73
- */
74
- registerComponentStyles(componentName, cssText) {
75
- slice.controller.requestedStyles.add(componentName);
76
- this.appendComponentStyles(cssText);
77
- }
78
- }
1
+
2
+ export default class StylesManager {
3
+ constructor() {
4
+ this.componentStyles = document.createElement('style');
5
+ this.componentStyles.id = 'slice-component-styles';
6
+ document.head.appendChild(this.componentStyles);
7
+
8
+ }
9
+
10
+ /**
11
+ * Load global styles and initialize ThemeManager if enabled.
12
+ * @returns {Promise<void>}
13
+ */
14
+ async init() {
15
+ const requestedStyles = Array.isArray(slice.stylesConfig.requestedStyles)
16
+ ? slice.stylesConfig.requestedStyles
17
+ : [];
18
+
19
+ const styleResults = await Promise.all(
20
+ requestedStyles.map(async (styleName) => {
21
+ const styles = await slice.controller.fetchText(styleName, 'styles');
22
+ return { styleName, styles };
23
+ })
24
+ );
25
+
26
+ for (const { styleName, styles } of styleResults) {
27
+ this.appendComponentStyles(styles);
28
+ slice.logger.logInfo('StylesManager', `${styleName} styles loaded`);
29
+ }
30
+
31
+ if (slice.themeConfig.enabled) {
32
+ const ThemeManagerClass = slice.frameworkClasses?.ThemeManager
33
+ || await slice.getClass(`${slice.paths.structuralComponentFolderPath}/StylesManager/ThemeManager/ThemeManager.js`);
34
+ if (!ThemeManagerClass) {
35
+ throw new Error('ThemeManager not available');
36
+ }
37
+
38
+ this.themeManager = new ThemeManagerClass();
39
+ let theme;
40
+
41
+ if (slice.themeConfig.saveThemeLocally) {
42
+ theme = localStorage.getItem('sliceTheme');
43
+ }
44
+
45
+ if (!theme) {
46
+ theme = slice.themeConfig.defaultTheme;
47
+ }
48
+
49
+ if (slice.themeConfig.useBrowserTheme) {
50
+ const browserTheme = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'Dark' : 'Light';
51
+ theme = browserTheme;
52
+ }
53
+
54
+ await this.themeManager.applyTheme(theme);
55
+ }
56
+ }
57
+
58
+ //add a method that will add css as text to the componentStyles element
59
+ /**
60
+ * Append raw CSS to the global component style tag.
61
+ * @param {string} cssText
62
+ * @returns {void}
63
+ */
64
+ appendComponentStyles(cssText) {
65
+ this.componentStyles.appendChild(document.createTextNode(cssText));
66
+ }
67
+
68
+ /**
69
+ * Register CSS for a component.
70
+ * @param {string} componentName
71
+ * @param {string} cssText
72
+ * @returns {void}
73
+ */
74
+ registerComponentStyles(componentName, cssText) {
75
+ // Idempotent: a component's CSS is injected at most once, no matter how many
76
+ // times it is built or how a caller reaches here (Controller guards upstream,
77
+ // but the debuggers and the resource loader do not).
78
+ if (slice.controller.requestedStyles.has(componentName)) return;
79
+ slice.controller.requestedStyles.add(componentName);
80
+ this.appendComponentStyles(cssText);
81
+ }
82
+ }
@@ -1,84 +1,106 @@
1
- /**
2
- * Manages theme CSS loading and persistence.
3
- */
4
- export default class ThemeManager {
5
- constructor() {
6
- this.themeStyles = new Map();
7
- this.currentTheme = null;
8
- this.themeStyle = document.createElement('style');
9
- document.head.appendChild(this.themeStyle);
10
- }
11
-
12
- /**
13
- * Apply a theme by name.
14
- * @param {string} themeName
15
- * @returns {Promise<void>}
16
- */
17
- async applyTheme(themeName) {
18
- if (!themeName) {
19
- slice.logger.logError('ThemeManager', 'Invalid theme name');
20
- return;
21
- }
22
-
23
- if (!this.themeStyles.has(themeName)) {
24
- await this.loadThemeCSS(themeName);
25
- } else {
26
- this.setThemeStyle(themeName);
27
- this.saveThemeLocally(themeName, this.themeStyles.get(themeName));
28
- }
29
- }
30
-
31
- /**
32
- * Load theme CSS and cache it.
33
- * @param {string} themeName
34
- * @returns {Promise<void>}
35
- */
36
- async loadThemeCSS(themeName) {
37
- let themeContent =
38
- localStorage.getItem(`sliceTheme-${themeName}`) || (await slice.controller.fetchText(themeName, 'theme'));
39
-
40
- if (!themeContent) {
41
- slice.logger.logError('ThemeManager', `Failed to load theme: ${themeName}`);
42
- return;
43
- }
44
-
45
- this.themeStyles.set(themeName, themeContent);
46
- this.setThemeStyle(themeName);
47
- this.saveThemeLocally(themeName, themeContent);
48
- }
49
-
50
- /**
51
- * Persist a theme in localStorage when enabled.
52
- * @param {string} themeName
53
- * @param {string} themeContent
54
- * @returns {void}
55
- */
56
- saveThemeLocally(themeName, themeContent) {
57
- if (slice.themeConfig.saveThemeLocally) {
58
- localStorage.setItem('sliceTheme', themeName);
59
- localStorage.setItem(`sliceTheme-${themeName}`, themeContent);
60
- slice.logger.logInfo('ThemeManager', `Theme ${themeName} saved locally`);
61
- }
62
- }
63
-
64
- /**
65
- * Clear currently applied theme styles.
66
- * @returns {void}
67
- */
68
- removeCurrentTheme() {
69
- if (this.currentTheme) {
70
- this.themeStyle.textContent = '';
71
- }
72
- }
73
-
74
- /**
75
- * Set theme style text and mark current theme.
76
- * @param {string} themeName
77
- * @returns {void}
78
- */
79
- setThemeStyle(themeName) {
80
- this.themeStyle.textContent = this.themeStyles.get(themeName);
81
- this.currentTheme = themeName;
82
- slice.logger.logInfo('ThemeManager', `Theme ${themeName} applied`);
83
- }
84
- }
1
+ /**
2
+ * Manages theme CSS loading and persistence.
3
+ */
4
+ export default class ThemeManager {
5
+ constructor() {
6
+ this.themeStyles = new Map();
7
+ this.currentTheme = null;
8
+ // In-flight loads, keyed by theme name, so concurrent applyTheme() calls for
9
+ // the same theme share one fetch instead of racing and double-fetching.
10
+ this._loadingThemes = new Map();
11
+ this.themeStyle = document.createElement('style');
12
+ document.head.appendChild(this.themeStyle);
13
+ }
14
+
15
+ /**
16
+ * Apply a theme by name.
17
+ * @param {string} themeName
18
+ * @returns {Promise<void>}
19
+ */
20
+ async applyTheme(themeName) {
21
+ if (!themeName) {
22
+ slice.logger.logError('ThemeManager', 'Invalid theme name');
23
+ return;
24
+ }
25
+
26
+ // Already the active theme — nothing to re-inject.
27
+ if (themeName === this.currentTheme) {
28
+ return;
29
+ }
30
+
31
+ if (this.themeStyles.has(themeName)) {
32
+ this.setThemeStyle(themeName);
33
+ this.saveThemeLocally(themeName, this.themeStyles.get(themeName));
34
+ return;
35
+ }
36
+
37
+ // Coalesce concurrent loads of the same theme into a single fetch.
38
+ if (!this._loadingThemes.has(themeName)) {
39
+ this._loadingThemes.set(
40
+ themeName,
41
+ this.loadThemeCSS(themeName).finally(() => this._loadingThemes.delete(themeName))
42
+ );
43
+ }
44
+ await this._loadingThemes.get(themeName);
45
+ }
46
+
47
+ /**
48
+ * Load theme CSS and cache it.
49
+ * @param {string} themeName
50
+ * @returns {Promise<void>}
51
+ */
52
+ async loadThemeCSS(themeName) {
53
+ let themeContent =
54
+ localStorage.getItem(`sliceTheme-${themeName}`) || (await slice.controller.fetchText(themeName, 'theme'));
55
+
56
+ if (!themeContent) {
57
+ slice.logger.logError('ThemeManager', `Failed to load theme: ${themeName}`);
58
+ return;
59
+ }
60
+
61
+ this.themeStyles.set(themeName, themeContent);
62
+ this.setThemeStyle(themeName);
63
+ this.saveThemeLocally(themeName, themeContent);
64
+ }
65
+
66
+ /**
67
+ * Persist a theme in localStorage when enabled.
68
+ * @param {string} themeName
69
+ * @param {string} themeContent
70
+ * @returns {void}
71
+ */
72
+ saveThemeLocally(themeName, themeContent) {
73
+ if (slice.themeConfig.saveThemeLocally) {
74
+ localStorage.setItem('sliceTheme', themeName);
75
+ localStorage.setItem(`sliceTheme-${themeName}`, themeContent);
76
+ slice.logger.logInfo('ThemeManager', `Theme ${themeName} saved locally`);
77
+ }
78
+ }
79
+
80
+ /**
81
+ * Clear currently applied theme styles.
82
+ * @returns {void}
83
+ */
84
+ removeCurrentTheme() {
85
+ if (this.currentTheme) {
86
+ this.themeStyle.textContent = '';
87
+ }
88
+ }
89
+
90
+ /**
91
+ * Set theme style text and mark current theme.
92
+ * @param {string} themeName
93
+ * @returns {void}
94
+ */
95
+ setThemeStyle(themeName) {
96
+ this.themeStyle.textContent = this.themeStyles.get(themeName);
97
+ this.currentTheme = themeName;
98
+ // Expose the active theme on the root element so CSS can react per theme
99
+ // without any JS — e.g. [data-slice-theme="Dark"] .logo { filter: ... }.
100
+ // Non-breaking: apps opt in only if they reference the attribute.
101
+ if (typeof document !== 'undefined' && document.documentElement) {
102
+ document.documentElement.setAttribute('data-slice-theme', themeName);
103
+ }
104
+ slice.logger.logInfo('ThemeManager', `Theme ${themeName} applied`);
105
+ }
106
+ }