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.
- package/.opencode/opencode.json +14 -14
- package/Slice/Components/Structural/ContextManager/ContextManager.js +420 -369
- package/Slice/Components/Structural/ContextManager/ContextManagerDebugger.js +421 -420
- package/Slice/Components/Structural/Controller/Controller.js +1198 -1199
- package/Slice/Components/Structural/Debugger/Debugger.js +1504 -1497
- package/Slice/Components/Structural/EventManager/EventManager.js +334 -338
- package/Slice/Components/Structural/Logger/Logger.js +200 -145
- package/Slice/Components/Structural/Router/Router.js +775 -752
- package/Slice/Components/Structural/StylesManager/StylesManager.js +82 -78
- package/Slice/Components/Structural/StylesManager/ThemeManager/ThemeManager.js +106 -84
- package/Slice/Slice.js +618 -577
- package/Slice/tests/build-singleton.test.js +244 -221
- package/Slice/tests/bundle-v2-runtime-contract.test.js +738 -728
- package/Slice/tests/context-patch-use.test.js +95 -0
- package/Slice/tests/fixtures/real-runtime-loader.mjs +21 -19
- package/Slice/tests/public-env-typed-accessors.test.js +66 -0
- package/api/index.js +281 -286
- package/api/utils/publicEnvResolver.js +123 -117
- package/package.json +44 -44
|
@@ -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
|
-
|
|
76
|
-
|
|
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
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
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
|
+
}
|