1
- import { fetchJsonOnce } from "../util/FetchHelper.js" ;
1
+ import { fetchJsonOnce , fetchTextOnce } from "../util/FetchHelper.js" ;
2
2
import { DEFAULT_THEME } from "../generated/AssetParameters.js" ;
3
+ import getFileExtension from "../util/getFileExtension.js" ;
3
4
4
5
const themeURLs = new Map ( ) ;
5
6
const themeStyles = new Map ( ) ;
@@ -16,13 +17,15 @@ const registeredThemes = new Set();
16
17
* registerThemeProperties("my-package", "my_theme", ":root{--var1: red;}");
17
18
* 2) Pass the CSS Vars as an object directly
18
19
* registerThemeProperties("my-package", "my_theme", {"_": ":root{--var1: red;}"});
19
- * 3) Pass a URL to a JSON file, containing the CSS Vars in its "_" property. Will be fetched on demand, not upon registration.
20
+ * 3) Pass a URL to a CSS file, containing the CSS Vars. Will be fetched on demand, not upon registration.
21
+ * registerThemeProperties("my-package", "my_theme", "http://url/to/my/theme.css");
22
+ * 4) Pass a URL to a JSON file, containing the CSS Vars in its "_" property. Will be fetched on demand, not upon registration.
20
23
* registerThemeProperties("my-package", "my_theme", "http://url/to/my/theme.json");
21
24
*
22
25
* @public
23
26
* @param packageName - the NPM package for which CSS Vars are registered
24
27
* @param themeName - the theme which the CSS Vars implement
25
- * @param style - can be one of three options: a string, an object with a "_" property or a URL to a JSON file with a "_" property
28
+ * @param style - can be one of four options: a string, an object with a "_" property, URL to a CSS file, or URL to a JSON file with a "_" property
26
29
*/
27
30
const registerThemeProperties = ( packageName , themeName , style ) => {
28
31
if ( style . _ ) {
@@ -52,8 +55,10 @@ const getThemeProperties = async (packageName, themeName) => {
52
55
}
53
56
54
57
const data = await fetchThemeProperties ( packageName , themeName ) ;
55
- themeStyles . set ( `${ packageName } _${ themeName } ` , data . _ ) ;
56
- return data . _ ;
58
+ const themeProps = data . _ || data ;
59
+
60
+ themeStyles . set ( `${ packageName } _${ themeName } ` , themeProps ) ;
61
+ return themeProps ;
57
62
} ;
58
63
59
64
const fetchThemeProperties = async ( packageName , themeName ) => {
@@ -62,7 +67,8 @@ const fetchThemeProperties = async (packageName, themeName) => {
62
67
if ( ! url ) {
63
68
throw new Error ( `You have to import the ${ packageName } /dist/Assets.js module to switch to additional themes` ) ;
64
69
}
65
- return fetchJsonOnce ( url ) ;
70
+
71
+ return getFileExtension ( url ) === ".css" ? fetchTextOnce ( url ) : fetchJsonOnce ( url ) ;
66
72
} ;
67
73
68
74
const getRegisteredPackages = ( ) => {
0 commit comments