@kupola/kupola 1.7.1 → 1.7.3

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.
@@ -94,8 +94,53 @@ const config = {
94
94
  },
95
95
  };
96
96
 
97
+ const configChangeListeners = [];
98
+
99
+ function loadConfigFromGlobal() {
100
+ if (typeof window !== 'undefined' && window.kupolaConfig) {
101
+ try {
102
+ mergeDeep(config, window.kupolaConfig);
103
+ notifyConfigChange();
104
+ } catch (e) {
105
+ console.warn('[Kupola] Failed to parse window.kupolaConfig:', e);
106
+ }
107
+ }
108
+ }
109
+
110
+ function notifyConfigChange() {
111
+ configChangeListeners.forEach(listener => {
112
+ try {
113
+ listener(config);
114
+ } catch (e) {
115
+ console.warn('[Kupola] Error in config change listener:', e);
116
+ }
117
+ });
118
+ }
119
+
120
+ export function onConfigChange(listener) {
121
+ if (typeof listener === 'function') {
122
+ configChangeListeners.push(listener);
123
+ }
124
+ }
125
+
126
+ export function offConfigChange(listener) {
127
+ const index = configChangeListeners.indexOf(listener);
128
+ if (index > -1) {
129
+ configChangeListeners.splice(index, 1);
130
+ }
131
+ }
132
+
133
+ if (typeof document !== 'undefined') {
134
+ if (document.readyState === 'loading') {
135
+ document.addEventListener('DOMContentLoaded', loadConfigFromGlobal);
136
+ } else {
137
+ loadConfigFromGlobal();
138
+ }
139
+ }
140
+
97
141
  export function setConfig(options) {
98
142
  mergeDeep(config, options);
143
+ notifyConfigChange();
99
144
  }
100
145
 
101
146
  export function getConfig(key) {
package/js/theme.js CHANGED
@@ -1,9 +1,18 @@
1
- import { getIconsPath, getDefaultTheme, getDefaultBrand, setConfig } from './kupola-config.js';
1
+ import { getIconsPath, getDefaultTheme, getDefaultBrand, setConfig, onConfigChange } from './kupola-config.js';
2
2
 
3
3
  const THEME_KEY = 'kupola-theme';
4
4
  const BRAND_KEY = 'kupola-brand';
5
5
  let _themeInitialized = false;
6
6
 
7
+ onConfigChange((newConfig) => {
8
+ const toggleBtn = document.querySelector('[data-theme-toggle]');
9
+ if (toggleBtn) {
10
+ const currentTheme = getTheme();
11
+ updateThemeIcon(toggleBtn);
12
+ setTheme(currentTheme);
13
+ }
14
+ });
15
+
7
16
  const BRAND_OPTIONS = [
8
17
  { id: 'green', name: '翠绿', color: '#32F08C' },
9
18
  { id: 'xionghuang', name: '雄黄', color: '#FF9900' },
@@ -92,26 +101,6 @@ function initTheme() {
92
101
 
93
102
  if (!_themeInitialized) {
94
103
  _themeInitialized = true;
95
-
96
- if (toggleBtn) {
97
- const iconEl = toggleBtn.querySelector('.theme-icon');
98
- if (iconEl && iconEl.src) {
99
- const currentIconsPath = getIconsPath();
100
- if (currentIconsPath === '/icons/') {
101
- const iconPath = iconEl.src.substring(0, iconEl.src.lastIndexOf('/') + 1);
102
- const basePath = window.location.pathname.substring(0, window.location.pathname.lastIndexOf('/') + 1);
103
- const relativeIconsPath = iconPath.replace(window.location.origin + basePath, '');
104
- if (relativeIconsPath !== iconPath) {
105
- setConfig({
106
- paths: {
107
- icons: relativeIconsPath,
108
- base: basePath
109
- }
110
- });
111
- }
112
- }
113
- }
114
- }
115
104
  }
116
105
 
117
106
  const savedTheme = getTheme();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kupola/kupola",
3
- "version": "1.7.1",
3
+ "version": "1.7.3",
4
4
  "description": "A lightweight UI toolkit for any web project. No heavy frontend frameworks required.",
5
5
  "main": "dist/kupola.cjs.js",
6
6
  "module": "dist/kupola.esm.js",