@kupola/kupola 1.7.0 → 1.7.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.
@@ -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,7 +1,17 @@
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
+ let _themeInitialized = false;
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
+ });
5
15
 
6
16
  const BRAND_OPTIONS = [
7
17
  { id: 'green', name: '翠绿', color: '#32F08C' },
@@ -88,21 +98,26 @@ function updateThemeIcon(toggleBtn) {
88
98
  function initTheme() {
89
99
 
90
100
  const toggleBtn = document.querySelector('[data-theme-toggle]');
91
- if (toggleBtn) {
92
- const iconEl = toggleBtn.querySelector('.theme-icon');
93
- if (iconEl && iconEl.src) {
94
- const currentIconsPath = getIconsPath();
95
- if (currentIconsPath === '/icons/') {
96
- const iconPath = iconEl.src.substring(0, iconEl.src.lastIndexOf('/') + 1);
97
- const basePath = window.location.pathname.substring(0, window.location.pathname.lastIndexOf('/') + 1);
98
- const relativeIconsPath = iconPath.replace(window.location.origin + basePath, '');
99
- if (relativeIconsPath !== iconPath) {
100
- setConfig({
101
- paths: {
102
- icons: relativeIconsPath,
103
- base: basePath
104
- }
105
- });
101
+
102
+ if (!_themeInitialized) {
103
+ _themeInitialized = true;
104
+
105
+ if (toggleBtn) {
106
+ const iconEl = toggleBtn.querySelector('.theme-icon');
107
+ if (iconEl && iconEl.src) {
108
+ const currentIconsPath = getIconsPath();
109
+ if (currentIconsPath === '/icons/') {
110
+ const iconPath = iconEl.src.substring(0, iconEl.src.lastIndexOf('/') + 1);
111
+ const basePath = window.location.pathname.substring(0, window.location.pathname.lastIndexOf('/') + 1);
112
+ const relativeIconsPath = iconPath.replace(window.location.origin + basePath, '');
113
+ if (relativeIconsPath !== iconPath) {
114
+ setConfig({
115
+ paths: {
116
+ icons: relativeIconsPath,
117
+ base: basePath
118
+ }
119
+ });
120
+ }
106
121
  }
107
122
  }
108
123
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kupola/kupola",
3
- "version": "1.7.0",
3
+ "version": "1.7.2",
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",