slicejs-web-framework 2.3.1 → 2.3.2

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,49 +1,64 @@
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
- async init() {
11
- for (let i = 0; i < slice.stylesConfig.requestedStyles.length; i++) {
12
- const styles = await slice.controller.fetchText(slice.stylesConfig.requestedStyles[i], 'styles');
13
- this.componentStyles.innerText += styles;
14
- slice.logger.logInfo('StylesManager', `${slice.stylesConfig.requestedStyles[i]} styles loaded`);
15
- }
16
-
17
- if (slice.themeConfig.enabled) {
18
- const module = await import(`${slice.paths.structuralComponentFolderPath}/StylesManager/ThemeManager/ThemeManager.js`);
19
-
20
- this.themeManager = new module.default();
21
- let theme;
22
-
23
- if (slice.themeConfig.saveThemeLocally) {
24
- theme = localStorage.getItem('sliceTheme');
25
- }
26
-
27
- if (!theme) {
28
- theme = slice.themeConfig.defaultTheme;
29
- }
30
-
31
- if (slice.themeConfig.useBrowserTheme) {
32
- const browserTheme = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'Dark' : 'Light';
33
- theme = browserTheme;
34
- }
35
-
36
- await this.themeManager.applyTheme(theme);
37
- }
38
- }
39
-
40
- //add a method that will add css as text to the componentStyles element
41
- appendComponentStyles(cssText) {
42
- this.componentStyles.appendChild(document.createTextNode(cssText));
43
- }
44
-
45
- registerComponentStyles(componentName, cssText) {
46
- slice.controller.requestedStyles.add(componentName);
47
- this.appendComponentStyles(cssText);
48
- }
49
- }
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
+ for (let i = 0; i < slice.stylesConfig.requestedStyles.length; i++) {
16
+ const styles = await slice.controller.fetchText(slice.stylesConfig.requestedStyles[i], 'styles');
17
+ this.componentStyles.innerText += styles;
18
+ slice.logger.logInfo('StylesManager', `${slice.stylesConfig.requestedStyles[i]} styles loaded`);
19
+ }
20
+
21
+ if (slice.themeConfig.enabled) {
22
+ const module = await import(`${slice.paths.structuralComponentFolderPath}/StylesManager/ThemeManager/ThemeManager.js`);
23
+
24
+ this.themeManager = new module.default();
25
+ let theme;
26
+
27
+ if (slice.themeConfig.saveThemeLocally) {
28
+ theme = localStorage.getItem('sliceTheme');
29
+ }
30
+
31
+ if (!theme) {
32
+ theme = slice.themeConfig.defaultTheme;
33
+ }
34
+
35
+ if (slice.themeConfig.useBrowserTheme) {
36
+ const browserTheme = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'Dark' : 'Light';
37
+ theme = browserTheme;
38
+ }
39
+
40
+ await this.themeManager.applyTheme(theme);
41
+ }
42
+ }
43
+
44
+ //add a method that will add css as text to the componentStyles element
45
+ /**
46
+ * Append raw CSS to the global component style tag.
47
+ * @param {string} cssText
48
+ * @returns {void}
49
+ */
50
+ appendComponentStyles(cssText) {
51
+ this.componentStyles.appendChild(document.createTextNode(cssText));
52
+ }
53
+
54
+ /**
55
+ * Register CSS for a component.
56
+ * @param {string} componentName
57
+ * @param {string} cssText
58
+ * @returns {void}
59
+ */
60
+ registerComponentStyles(componentName, cssText) {
61
+ slice.controller.requestedStyles.add(componentName);
62
+ this.appendComponentStyles(cssText);
63
+ }
64
+ }
@@ -1,56 +1,84 @@
1
- export default class ThemeManager {
2
- constructor() {
3
- this.themeStyles = new Map();
4
- this.currentTheme = null;
5
- this.themeStyle = document.createElement('style');
6
- document.head.appendChild(this.themeStyle);
7
- }
8
-
9
- async applyTheme(themeName) {
10
- if (!themeName) {
11
- slice.logger.logError('ThemeManager', 'Invalid theme name');
12
- return;
13
- }
14
-
15
- if (!this.themeStyles.has(themeName)) {
16
- await this.loadThemeCSS(themeName);
17
- } else {
18
- this.setThemeStyle(themeName);
19
- this.saveThemeLocally(themeName, this.themeStyles.get(themeName));
20
- }
21
- }
22
-
23
- async loadThemeCSS(themeName) {
24
- let themeContent =
25
- localStorage.getItem(`sliceTheme-${themeName}`) || (await slice.controller.fetchText(themeName, 'theme'));
26
-
27
- if (!themeContent) {
28
- slice.logger.logError('ThemeManager', `Failed to load theme: ${themeName}`);
29
- return;
30
- }
31
-
32
- this.themeStyles.set(themeName, themeContent);
33
- this.setThemeStyle(themeName);
34
- this.saveThemeLocally(themeName, themeContent);
35
- }
36
-
37
- saveThemeLocally(themeName, themeContent) {
38
- if (slice.themeConfig.saveThemeLocally) {
39
- localStorage.setItem('sliceTheme', themeName);
40
- localStorage.setItem(`sliceTheme-${themeName}`, themeContent);
41
- slice.logger.logInfo('ThemeManager', `Theme ${themeName} saved locally`);
42
- }
43
- }
44
-
45
- removeCurrentTheme() {
46
- if (this.currentTheme) {
47
- this.themeStyle.textContent = '';
48
- }
49
- }
50
-
51
- setThemeStyle(themeName) {
52
- this.themeStyle.textContent = this.themeStyles.get(themeName);
53
- this.currentTheme = themeName;
54
- slice.logger.logInfo('ThemeManager', `Theme ${themeName} applied`);
55
- }
56
- }
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
+ }