leaflet-theme-control 0.0.2 → 0.0.4
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/package.json +1 -1
- package/src/leaflet-theme-control.js +12 -2
- package/src/leaflet-theme-editor.js +37 -35
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "leaflet-theme-control",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"description": "A Leaflet control for switching between visual themes (light, dark, grayscale, custom, etc.) using CSS filters",
|
|
5
5
|
"main": "src/leaflet-theme-control.js",
|
|
6
6
|
"module": "src/leaflet-theme-control.js",
|
|
@@ -33,8 +33,18 @@ export class ThemeControl extends Control {
|
|
|
33
33
|
initialize(options) {
|
|
34
34
|
Util.setOptions(this, options)
|
|
35
35
|
|
|
36
|
-
// Create
|
|
37
|
-
|
|
36
|
+
// Create deep copies of themes to avoid mutating DEFAULT_THEMES
|
|
37
|
+
// This is necessary because spread operator only does shallow copies
|
|
38
|
+
if (this.options.themes) {
|
|
39
|
+
// User provided custom themes - create deep copies of each theme
|
|
40
|
+
const themesCopy = {}
|
|
41
|
+
Object.keys(this.options.themes).forEach((key) => {
|
|
42
|
+
themesCopy[key] = { ...this.options.themes[key] }
|
|
43
|
+
})
|
|
44
|
+
this.options.themes = themesCopy
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
// No themes provided - use copies of DEFAULT_THEMES
|
|
38
48
|
this.options.themes = {}
|
|
39
49
|
Object.keys(DEFAULT_THEMES).forEach((key) => {
|
|
40
50
|
this.options.themes[key] = { ...DEFAULT_THEMES[key] }
|
|
@@ -332,20 +332,23 @@ export class ThemeEditor {
|
|
|
332
332
|
selectBtn.appendChild(this._el('span', { 'className': 'theme-active-badge', 'aria-hidden': 'true' }, '✓'))
|
|
333
333
|
}
|
|
334
334
|
|
|
335
|
-
// Create edit button
|
|
336
|
-
const editBtn = this._el('button', {
|
|
337
|
-
'className': 'theme-edit-btn',
|
|
338
|
-
'data-theme': themeKey,
|
|
339
|
-
'aria-label': `${this._getLabel('customizeTheme')}: ${themeLabel}`,
|
|
340
|
-
}, '⚙️')
|
|
341
|
-
|
|
342
335
|
// Create row container
|
|
343
336
|
const row = this._el('div', {
|
|
344
337
|
'className': `theme-row ${isActive ? 'active' : ''}`,
|
|
345
338
|
'data-theme': themeKey,
|
|
346
339
|
})
|
|
347
340
|
row.appendChild(selectBtn)
|
|
348
|
-
|
|
341
|
+
|
|
342
|
+
// Only show edit button for the active theme
|
|
343
|
+
// User needs to see the theme to edit it meaningfully
|
|
344
|
+
if (isActive) {
|
|
345
|
+
const editBtn = this._el('button', {
|
|
346
|
+
'className': 'theme-edit-btn',
|
|
347
|
+
'data-theme': themeKey,
|
|
348
|
+
'aria-label': `${this._getLabel('customizeTheme')}: ${themeLabel}`,
|
|
349
|
+
}, '⚙️')
|
|
350
|
+
row.appendChild(editBtn)
|
|
351
|
+
}
|
|
349
352
|
|
|
350
353
|
return row
|
|
351
354
|
}
|
|
@@ -402,14 +405,9 @@ export class ThemeEditor {
|
|
|
402
405
|
// Get current filter values or defaults
|
|
403
406
|
const filterValues = this.customFilters[themeKey] || this._parseFilterString(theme.filter)
|
|
404
407
|
|
|
405
|
-
// Get control style preference
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
controlStyle = this.customFilters[themeKey].controlStyle
|
|
409
|
-
}
|
|
410
|
-
else if (theme.controlStyle) {
|
|
411
|
-
controlStyle = theme.controlStyle
|
|
412
|
-
}
|
|
408
|
+
// Get control style preference - always start fresh from theme
|
|
409
|
+
// Don't check customFilters first, as it may have stale data
|
|
410
|
+
const controlStyle = theme.controlStyle || 'light'
|
|
413
411
|
|
|
414
412
|
// Build panel structure
|
|
415
413
|
const header = this._createPanelHeader(`${this._getLabel('customize')}: ${themeLabel}`, true)
|
|
@@ -594,33 +592,37 @@ export class ThemeEditor {
|
|
|
594
592
|
delete this.customFilters[themeKey]
|
|
595
593
|
this._saveCustomFilters()
|
|
596
594
|
|
|
597
|
-
// Restore default
|
|
595
|
+
// Restore default filter and controlStyle from DEFAULT_THEMES
|
|
596
|
+
// but KEEP user-defined properties like applyToSelectors
|
|
598
597
|
const defaultTheme = DEFAULT_THEMES[themeKey]
|
|
598
|
+
const currentTheme = this.themeControl.options.themes[themeKey]
|
|
599
599
|
|
|
600
|
-
if (defaultTheme) {
|
|
601
|
-
//
|
|
602
|
-
|
|
600
|
+
if (defaultTheme && currentTheme) {
|
|
601
|
+
// Only reset the editable properties (filter, controlStyle)
|
|
602
|
+
// Keep other properties like applyToSelectors, icon, label, className
|
|
603
|
+
currentTheme.filter = defaultTheme.filter
|
|
604
|
+
currentTheme.controlStyle = defaultTheme.controlStyle
|
|
605
|
+
}
|
|
606
|
+
else if (!currentTheme) {
|
|
607
|
+
// Theme doesn't exist - this shouldn't happen
|
|
608
|
+
console.warn(`Theme "${themeKey}" not found`)
|
|
609
|
+
return
|
|
603
610
|
}
|
|
604
611
|
else {
|
|
605
|
-
// For custom themes not in DEFAULT_THEMES,
|
|
606
|
-
|
|
607
|
-
const theme = this.themeControl.options.themes[themeKey]
|
|
608
|
-
if (theme) {
|
|
609
|
-
// Keep the theme but clear any custom modifications by re-parsing
|
|
610
|
-
// the original filter (which may still be modified, so this is a no-op for custom themes)
|
|
611
|
-
console.warn(`Theme "${themeKey}" has no default in DEFAULT_THEMES, cannot fully reset`)
|
|
612
|
-
}
|
|
612
|
+
// For custom themes not in DEFAULT_THEMES, we can't reset
|
|
613
|
+
console.warn(`Theme "${themeKey}" has no default in DEFAULT_THEMES, cannot reset filter values`)
|
|
613
614
|
}
|
|
614
615
|
|
|
615
|
-
// Reapply theme
|
|
616
|
-
//
|
|
616
|
+
// Reapply theme if it's currently active
|
|
617
|
+
// Use setTheme to ensure proper application
|
|
617
618
|
if (this.themeControl.getCurrentTheme() === themeKey) {
|
|
618
|
-
this.themeControl.
|
|
619
|
+
this.themeControl.setTheme(themeKey)
|
|
619
620
|
}
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
621
|
+
else {
|
|
622
|
+
// Fire onChange callback even if theme is not currently active
|
|
623
|
+
if (this.themeControl.options.onChange) {
|
|
624
|
+
this.themeControl.options.onChange(themeKey, this.themeControl.options.themes[themeKey])
|
|
625
|
+
}
|
|
624
626
|
}
|
|
625
627
|
|
|
626
628
|
// Re-render editor panel with default values
|