2024-03-22 13:02:01 +00:00
|
|
|
import { ThemeService, ThemeUtils } from 'primevue/themes';
|
2024-03-05 09:22:33 +00:00
|
|
|
|
|
|
|
export default {
|
2024-03-13 12:05:23 +00:00
|
|
|
defaults: {
|
|
|
|
variable: {
|
2024-03-22 13:02:01 +00:00
|
|
|
prefix: 'p',
|
2024-03-13 12:05:23 +00:00
|
|
|
selector: ':root',
|
2024-03-22 13:02:01 +00:00
|
|
|
excludedKeyRegex: /^(primitive|semantic|components|directives|variables|colorscheme|light|dark|common|root|states)$/gi
|
2024-03-13 12:05:23 +00:00
|
|
|
},
|
2024-03-22 13:02:01 +00:00
|
|
|
options: {
|
|
|
|
prefix: 'p',
|
|
|
|
darkModeSelector: 'system',
|
2024-03-31 05:10:59 +00:00
|
|
|
cssLayer: false
|
2024-03-13 12:05:23 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
_theme: undefined,
|
2024-03-18 12:23:53 +00:00
|
|
|
_layerNames: new Set(),
|
2024-03-31 04:44:48 +00:00
|
|
|
_loadedStyleNames: new Set(),
|
2024-04-16 10:15:21 +00:00
|
|
|
_loadingStyles: new Set(),
|
2024-03-19 12:50:54 +00:00
|
|
|
_tokens: {},
|
2024-03-31 04:44:48 +00:00
|
|
|
update(newValues = {}) {
|
|
|
|
const { theme } = newValues;
|
2024-03-13 12:05:23 +00:00
|
|
|
|
2024-03-31 04:44:48 +00:00
|
|
|
if (theme) {
|
|
|
|
this._theme = {
|
|
|
|
...theme,
|
|
|
|
options: {
|
|
|
|
...this.defaults.options,
|
|
|
|
...theme.options
|
|
|
|
}
|
|
|
|
};
|
2024-03-31 05:41:28 +00:00
|
|
|
this._tokens = ThemeUtils.createTokens(this.preset, this.defaults);
|
2024-03-31 04:44:48 +00:00
|
|
|
this.clearLoadedStyleNames();
|
|
|
|
}
|
2024-03-13 12:05:23 +00:00
|
|
|
},
|
|
|
|
get theme() {
|
2024-03-31 04:44:48 +00:00
|
|
|
return this._theme;
|
2024-03-13 12:05:23 +00:00
|
|
|
},
|
|
|
|
get preset() {
|
|
|
|
return this.theme?.preset || {};
|
|
|
|
},
|
|
|
|
get options() {
|
|
|
|
return this.theme?.options || {};
|
|
|
|
},
|
2024-03-19 12:50:54 +00:00
|
|
|
get tokens() {
|
|
|
|
return this._tokens;
|
|
|
|
},
|
2024-03-22 13:02:01 +00:00
|
|
|
getTheme() {
|
|
|
|
return this.theme;
|
|
|
|
},
|
2024-03-13 12:05:23 +00:00
|
|
|
setTheme(newValue) {
|
2024-03-31 04:44:48 +00:00
|
|
|
this.update({ theme: newValue });
|
2024-03-22 13:02:01 +00:00
|
|
|
ThemeService.emit('theme:change', newValue);
|
2024-03-05 09:22:33 +00:00
|
|
|
},
|
2024-03-22 13:02:01 +00:00
|
|
|
getPreset() {
|
|
|
|
return this.preset;
|
2024-03-05 09:22:33 +00:00
|
|
|
},
|
2024-03-22 13:02:01 +00:00
|
|
|
setPreset(newValue) {
|
|
|
|
this._theme = { ...this.theme, preset: newValue };
|
|
|
|
this._tokens = ThemeUtils.createTokens(newValue, this.defaults);
|
|
|
|
|
2024-03-31 04:44:48 +00:00
|
|
|
this.clearLoadedStyleNames();
|
2024-03-22 13:02:01 +00:00
|
|
|
ThemeService.emit('preset:change', newValue);
|
2024-03-31 04:44:48 +00:00
|
|
|
ThemeService.emit('theme:change', this.theme);
|
2024-03-12 09:46:43 +00:00
|
|
|
},
|
2024-05-06 15:50:06 +00:00
|
|
|
getOptions() {
|
|
|
|
return this.options;
|
|
|
|
},
|
|
|
|
setOptions(newValue) {
|
|
|
|
this._theme = { ...this.theme, options: newValue };
|
|
|
|
|
|
|
|
this.clearLoadedStyleNames();
|
|
|
|
ThemeService.emit('options:change', newValue);
|
|
|
|
ThemeService.emit('theme:change', this.theme);
|
|
|
|
},
|
2024-03-18 12:23:53 +00:00
|
|
|
getLayerNames() {
|
|
|
|
return [...this._layerNames];
|
|
|
|
},
|
|
|
|
setLayerNames(layerName) {
|
2024-03-31 04:44:48 +00:00
|
|
|
this._layerNames.add(layerName);
|
|
|
|
},
|
|
|
|
getLoadedStyleNames() {
|
|
|
|
return this._loadedStyleNames;
|
|
|
|
},
|
|
|
|
isStyleNameLoaded(name) {
|
|
|
|
return this._loadedStyleNames.has(name);
|
|
|
|
},
|
|
|
|
setLoadedStyleName(name) {
|
|
|
|
this._loadedStyleNames.add(name);
|
|
|
|
},
|
|
|
|
deleteLoadedStyleName(name) {
|
|
|
|
this._loadedStyleNames.delete(name);
|
|
|
|
},
|
|
|
|
clearLoadedStyleNames() {
|
|
|
|
this._loadedStyleNames.clear();
|
|
|
|
},
|
|
|
|
getTokenValue(tokenPath) {
|
|
|
|
return ThemeUtils.getTokenValue(this.tokens, tokenPath, this.defaults);
|
2024-03-18 12:23:53 +00:00
|
|
|
},
|
2024-05-14 08:35:40 +00:00
|
|
|
getCommon(name = '', params) {
|
2024-03-29 11:25:15 +00:00
|
|
|
return ThemeUtils.getCommon({ name, theme: this.theme, params, defaults: this.defaults, set: { layerNames: this.setLayerNames.bind(this) } });
|
2024-03-13 12:05:23 +00:00
|
|
|
},
|
2024-05-14 08:35:40 +00:00
|
|
|
getComponent(name = '', params) {
|
2024-03-29 11:25:15 +00:00
|
|
|
const options = { name, theme: this.theme, params, defaults: this.defaults, set: { layerNames: this.setLayerNames.bind(this) } };
|
2024-03-05 09:22:33 +00:00
|
|
|
|
2024-05-14 08:35:40 +00:00
|
|
|
return ThemeUtils.getPresetC(options);
|
2024-03-13 12:05:23 +00:00
|
|
|
},
|
2024-05-14 08:35:40 +00:00
|
|
|
getDirective(name = '', params) {
|
2024-03-29 11:25:15 +00:00
|
|
|
const options = { name, theme: this.theme, params, defaults: this.defaults, set: { layerNames: this.setLayerNames.bind(this) } };
|
2024-03-05 09:22:33 +00:00
|
|
|
|
2024-05-14 08:35:40 +00:00
|
|
|
return ThemeUtils.getPresetD(options);
|
2024-03-13 12:05:23 +00:00
|
|
|
},
|
2024-05-14 08:35:40 +00:00
|
|
|
getCustomPreset(name = '', preset, selector, params) {
|
2024-04-01 13:08:53 +00:00
|
|
|
const options = { name, preset, options: this.options, selector, params, defaults: this.defaults, set: { layerNames: this.setLayerNames.bind(this) } };
|
|
|
|
|
2024-05-14 08:35:40 +00:00
|
|
|
return ThemeUtils.getPreset(options);
|
2024-04-01 13:08:53 +00:00
|
|
|
},
|
2024-03-18 10:57:17 +00:00
|
|
|
getLayerOrderCSS(name = '') {
|
2024-03-21 09:08:51 +00:00
|
|
|
return ThemeUtils.getLayerOrder(name, this.options, { names: this.getLayerNames() }, this.defaults);
|
2024-03-18 10:57:17 +00:00
|
|
|
},
|
2024-05-02 22:12:36 +00:00
|
|
|
transformCSS(name = '', css, type = 'style', mode) {
|
|
|
|
return ThemeUtils.transformCSS(name, css, mode, type, this.options, { layerNames: this.setLayerNames.bind(this) }, this.defaults);
|
|
|
|
},
|
2024-03-29 11:25:15 +00:00
|
|
|
getCommonStyleSheet(name = '', params, props = {}) {
|
|
|
|
return ThemeUtils.getCommonStyleSheet({ name, theme: this.theme, params, props, defaults: this.defaults, set: { layerNames: this.setLayerNames.bind(this) } });
|
2024-03-13 12:05:23 +00:00
|
|
|
},
|
2024-03-29 11:25:15 +00:00
|
|
|
getStyleSheet(name, params, props = {}) {
|
|
|
|
return ThemeUtils.getStyleSheet({ name, theme: this.theme, params, props, defaults: this.defaults, set: { layerNames: this.setLayerNames.bind(this) } });
|
2024-04-16 10:15:21 +00:00
|
|
|
},
|
|
|
|
onStyleMounted(name) {
|
|
|
|
this._loadingStyles.add(name);
|
|
|
|
},
|
|
|
|
onStyleUpdated(name) {
|
|
|
|
this._loadingStyles.add(name);
|
|
|
|
},
|
|
|
|
onStyleLoaded(event, { name }) {
|
|
|
|
if (this._loadingStyles.size) {
|
|
|
|
this._loadingStyles.delete(name);
|
|
|
|
|
|
|
|
ThemeService.emit(`theme:${name}:load`, event); // Exp: ThemeService.emit('theme:panel-style:load', event)
|
|
|
|
!this._loadingStyles.size && ThemeService.emit('theme:load');
|
|
|
|
}
|
2024-03-05 09:22:33 +00:00
|
|
|
}
|
|
|
|
};
|