chore(views/geomap): reintroduce viewport storage

This commit is contained in:
Elian Doran 2025-07-06 11:47:37 +03:00
parent 6a5bb1f5c8
commit b8d41b3421
No known key found for this signature in database
2 changed files with 44 additions and 42 deletions

View File

@ -127,34 +127,25 @@ export default class GeoMapTypeWidget extends TypeWidget {
} }
async #onMapInitialized(L: Leaflet) { async #onMapInitialized(L: Leaflet) {
this.L = L; // this.L = L;
const map = this.geoMapWidget.map;
if (!map) {
throw new Error(t("geo-map.unable-to-load-map"));
}
// Restore markers. // // Restore markers.
await this.#reloadMarkers(); // await this.#reloadMarkers();
// This fixes an issue with the map appearing cut off at the beginning, due to the container not being properly attached // // This fixes an issue with the map appearing cut off at the beginning, due to the container not being properly attached
setTimeout(() => { // setTimeout(() => {
map.invalidateSize(); // map.invalidateSize();
}, 100); // }, 100);
const updateFn = () => this.spacedUpdate.scheduleUpdate(); // if (hasTouchBar) {
map.on("moveend", updateFn); // map.on("zoom", () => {
map.on("zoomend", updateFn); // if (!this.ignoreNextZoomEvent) {
map.on("click", (e) => this.#onMapClicked(e)); // this.triggerCommand("refreshTouchBar");
// }
if (hasTouchBar) { // this.ignoreNextZoomEvent = false;
map.on("zoom", () => { // });
if (!this.ignoreNextZoomEvent) { // }
this.triggerCommand("refreshTouchBar");
}
this.ignoreNextZoomEvent = false;
});
}
} }
async #reloadMarkers() { async #reloadMarkers() {
@ -311,24 +302,6 @@ export default class GeoMapTypeWidget extends TypeWidget {
await attributes.setLabel(noteId, LOCATION_ATTRIBUTE, value); await attributes.setLabel(noteId, LOCATION_ATTRIBUTE, value);
} }
getData(): any {
const map = this.geoMapWidget.map;
if (!map) {
return;
}
const data: MapData = {
view: {
center: map.getBounds().getCenter(),
zoom: map.getZoom()
}
};
return {
content: JSON.stringify(data)
};
}
async geoMapCreateChildNoteEvent({ ntxId }: EventData<"geoMapCreateChildNote">) { async geoMapCreateChildNoteEvent({ ntxId }: EventData<"geoMapCreateChildNote">) {
if (!this.isNoteContext(ntxId)) { if (!this.isNoteContext(ntxId)) {
return; return;

View File

@ -1,6 +1,8 @@
import ViewMode, { ViewModeArgs } from "../view_mode.js"; import ViewMode, { ViewModeArgs } from "../view_mode.js";
import L from "leaflet"; import L from "leaflet";
import type { LatLng, Map } from "leaflet"; import type { LatLng, Map } from "leaflet";
import SpacedUpdate from "../../../services/spaced_update.js";
import { t } from "../../../services/i18n.js";
const TPL = /*html*/` const TPL = /*html*/`
<div class="geo-view"> <div class="geo-view">
@ -35,11 +37,13 @@ export default class GeoView extends ViewMode<MapData> {
private $root: JQuery<HTMLElement>; private $root: JQuery<HTMLElement>;
private $container!: JQuery<HTMLElement>; private $container!: JQuery<HTMLElement>;
private map?: Map; private map?: Map;
private spacedUpdate: SpacedUpdate;
constructor(args: ViewModeArgs) { constructor(args: ViewModeArgs) {
super(args, "geoMap"); super(args, "geoMap");
this.$root = $(TPL); this.$root = $(TPL);
this.$container = this.$root.find(".geo-map-container"); this.$container = this.$root.find(".geo-map-container");
this.spacedUpdate = new SpacedUpdate(() => this.onSave(), 5_000);
args.$parent.append(this.$root); args.$parent.append(this.$root);
} }
@ -63,7 +67,17 @@ export default class GeoView extends ViewMode<MapData> {
} }
async #onMapInitialized() { async #onMapInitialized() {
const map = this.map;
if (!map) {
throw new Error(t("geo-map.unable-to-load-map"));
}
this.#restoreViewportAndZoom(); this.#restoreViewportAndZoom();
const updateFn = () => this.spacedUpdate.scheduleUpdate();
map.on("moveend", updateFn);
map.on("zoomend", updateFn);
// map.on("click", (e) => this.#onMapClicked(e));
} }
async #restoreViewportAndZoom() { async #restoreViewportAndZoom() {
@ -80,6 +94,21 @@ export default class GeoView extends ViewMode<MapData> {
map.setView(center, zoom); map.setView(center, zoom);
} }
private onSave() {
const map = this.map;
let data: MapData = {};
if (map) {
data = {
view: {
center: map.getBounds().getCenter(),
zoom: map.getZoom()
}
};
}
this.viewStorage.store(data);
}
get isFullHeight(): boolean { get isFullHeight(): boolean {
return true; return true;
} }