filter out extension layers on load

This commit is contained in:
vcoppe committed 2026-11-08 17:34:35 +01:00
1 parent 4050420b3c
commit 57c34b388b
4 files changed
+52 -42

No files matched your search

@@ -399,13 +399,43 @@ describe('ExtensionAPI v2', () => {
assert.deepEqual(calls, [{ a: true }]);
});
it('does not tell the extension that all is hidden when the page closes', () => {
const calls: unknown[] = [];
api.onVisibilityChange('Heatmaps', (visible) => calls.push(visible));
api.setOverlays('Heatmaps', [v2('a', { visible: true })]);
(api as unknown as { destroy(): void }).destroy();
assert.deepEqual(calls, []);
it('forgets the state of the overlays with the next session', () => {
const saved = () => ({
currentOverlays: JSON.stringify(get(settings.currentOverlays)),
previousOverlays: JSON.stringify(get(settings.previousOverlays)),
selectedOverlayTree: JSON.stringify(get(settings.selectedOverlayTree)),
opacities: JSON.stringify(get(settings.opacities)),
});
// what was saved, not the default trees, which are the ones that are allowed
settings.connect(saved());
api.setOverlays('Heatmaps', [v2('a', { visible: true }), v2('b')]);
api.addOrUpdateOverlay(overlay('one'));
settings.selectedOverlayTree.update((tree) => {
group(tree)![id('b')] = false;
return tree;
});
settings.opacities.update((opacities) => ({ ...opacities, [id('a')]: 0.3 }));
const stored = saved();
assert.ok(stored.currentOverlays.includes(id('a')));
settings.connect(stored);
for (const tree of [
get(settings.currentOverlays),
get(settings.previousOverlays),
get(settings.selectedOverlayTree),
]) {
assert.ok(!JSON.stringify(tree).includes('extension-'));
}
assert.equal(get(settings.opacities)[id('a')], 0.3);
// an extension that is still there starts again, as the source of what it remembers
const next = new ExtensionAPI();
next.setOverlays('Heatmaps', [v2('a'), v2('b', { visible: true })]);
assert.equal(isSelected(get(settings.currentOverlays)!, id('a')), false);
assert.equal(isSelected(get(settings.currentOverlays)!, id('b')), true);
assert.equal(isSelected(get(settings.selectedOverlayTree), id('b')), true);
next.setOverlays('Heatmaps', []);
});
it('leaves a style URL for the map to fetch', () => {
@@ -1,7 +1,7 @@
import { settings } from '$lib/state/settings';
import type { StyleSpecification } from 'maplibre-gl';
import { derived, get, writable, type Writable } from 'svelte/store';
import { isSelected, prefixStyle, remove, removeAll } from './utils';
import { isSelected, prefixStyle, removeAll } from './utils';
import { layerGroup, overlays, overlayTree, type LayerTreeType } from '$lib/data/layers';
import { browser } from '$app/environment';
import { map } from '$lib/components/map/map';
@@ -101,9 +101,6 @@ export class ExtensionAPI {
Object.defineProperty(window, 'gpxstudio', {
value: this,
});
addEventListener('beforeunload', () => {
this.destroy();
});
}
}
@@ -432,30 +429,6 @@ export class ExtensionAPI {
const names = new Set(Array.from($overlays.values(), (overlay) => overlay.extensionName));
return (id: string) => names.has(id);
});
private destroy() {
// the page is closing: the user has not hidden anything, which extensions would then save
this._visibilityCallbacks.clear();
const ids = Array.from(get(this._overlays).keys());
currentOverlays.update((current) => {
ids.forEach((id) => {
remove(current, id);
});
return current;
});
previousOverlays.update((previous) => {
ids.forEach((id) => {
remove(previous, id);
});
return previous;
});
selectedOverlayTree.update((selected) => {
ids.forEach((id) => {
remove(selected, id);
});
return selected;
});
}
}
export const extensionAPI = new ExtensionAPI();