Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(geomap): stop teardown crashes and fix prod GPX tracks, fixes #10236
Elian Doran4babcc02b79b
diff --git a/apps/client/src/widgets/collections/geomap/marker.tsx b/apps/client/src/widgets/collections/geomap/marker.tsxindex 8b6cb13719..e10268fef0 100644--- a/apps/client/src/widgets/collections/geomap/marker.tsx+++ b/apps/client/src/widgets/collections/geomap/marker.tsx@@ -1,6 +1,6 @@import { useContext, useEffect } from "preact/hooks";import { ParentMap } from "./map";-import { DivIcon, GPX, GPXOptions, Icon, LatLng, Marker as LeafletMarker, LeafletMouseEvent, marker, MarkerOptions } from "leaflet";+import L, { DivIcon, GPXOptions, Icon, LatLng, Marker as LeafletMarker, LeafletMouseEvent, marker, MarkerOptions } from "leaflet";import "leaflet-gpx";export interface MarkerProps {@@ -61,7 +61,12 @@ export function GpxTrack({ gpxXmlString, options }: { gpxXmlString: string, optiuseEffect(() => {if (!parentMap) return;- const track = new GPX(gpxXmlString, options);+ // Use the runtime-augmented `L.GPX` (added by the `leaflet-gpx` side-effect import)+ // rather than a named `{ GPX }` import: in production the Rollup CJS→ESM interop binds+ // named imports to a static facade that never reflects leaflet-gpx's runtime mutation,+ // so `new GPX()` throws "GPX is not a constructor". `L` is leaflet's real export object,+ // which leaflet-gpx actually patches. See TriliumNext/Trilium#10236.+ const track = new L.GPX(gpxXmlString, options);track.addTo(parentMap);return () => track.removeFrom(parentMap);diff --git a/patches/@[email protected] b/patches/@[email protected]new file mode 100644index 0000000000..7547e55074--- /dev/null+++ b/patches/@[email protected]@@ -0,0 +1,46 @@+diff --git a/leaflet-maplibre-gl.js b/leaflet-maplibre-gl.js+index b1bead05c04ad41a9c9255a3b59358fc3fc64dea..731598901b8c1e018411500f0500a844cdcb73e5 100644+--- a/leaflet-maplibre-gl.js++++ b/leaflet-maplibre-gl.js+@@ -262,6 +262,9 @@++ // update the map constantly during a pinch zoom+ _pinchZoom: function (e) {++ if (!this._map || !this._glMap) {++ return;++ }+ this._glMap.jumpTo({+ zoom: this._map.getZoom() - 1,+ center: this._map.getCenter()+@@ -271,6 +274,9 @@+ // borrowed from L.ImageOverlay+ // https://github.com/Leaflet/Leaflet/blob/master/src/layer/ImageOverlay.js#L139-L144+ _animateZoom: function (e) {++ if (!this._map || !this._glMap) {++ return;++ }+ var scale = this._map.getZoomScale(e.zoom);+ var padding = this._map.getSize().multiplyBy(this.options.padding * scale);+ var viewHalf = this.getSize()._divideBy(2);+@@ -295,6 +301,9 @@+ },++ _zoomEnd: function () {++ if (!this._map || !this._glMap) {++ return;++ }+ var scale = this._map.getZoomScale(this._map.getZoom());++ L.DomUtil.setTransform(+@@ -311,6 +320,11 @@++ _transitionEnd: function (e) {+ L.Util.requestAnimFrame(function () {++ // The map/GL layer may have been torn down between scheduling this++ // animation frame and it firing (e.g. navigating away mid-zoom).++ if (!this._map || !this._glMap) {++ return;++ }+ var zoom = this._map.getZoom();+ var center = this._map.getCenter();+ var offset = this._map.latLngToContainerPoint(diff --git a/patches/[email protected] b/patches/[email protected]new file mode 100644index 0000000000..513d52df29--- /dev/null+++ b/patches/[email protected]@@ -0,0 +1,31 @@+diff --git a/dist/leaflet-src.js b/dist/leaflet-src.js+index 90f6db30412aa1219cba21fccd00644b994a51c8..d8d808c1ad314ffa787a8f2703bb466df52effe0 100644+--- a/dist/leaflet-src.js++++ b/dist/leaflet-src.js+@@ -2542,6 +2542,13 @@+ // (used by Leaflet internally to position its layers).+ function setPosition(el, point) {+++ // `el` can be undefined when a deferred interaction callback (a debounced wheel++ // zoom, a queued zoom-transition end, etc.) fires after the map was removed and++ // `_mapPane` was deleted. No-op instead of throwing on `el._leaflet_pos`, mirroring++ // the same guard in getPosition — together these keep a geo-map torn down mid-zoom++ // from breaking app-wide navigation.++ if (!el) { return; }+++ /*eslint-disable */+ el._leaflet_pos = point;+ /* eslint-enable */+@@ -2560,7 +2567,11 @@+ // this method is only used for elements previously positioned using setPosition,+ // so it's safe to cache the position for performance++- return el._leaflet_pos || new Point(0, 0);++ // `el` can be undefined when a deferred interaction callback (a debounced wheel++ // zoom, a queued zoom-transition end, etc.) fires after the map was removed and++ // `_mapPane` was deleted. Tolerate that instead of throwing on `el._leaflet_pos`,++ // which otherwise breaks app-wide navigation when a geo-map is torn down mid-zoom.++ return (el && el._leaflet_pos) || new Point(0, 0);+ }++ // @function disableTextSelection()diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 345983e392..fe88f65593 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -84,6 +84,8 @@ overrides:patchedDependencies:'@ckeditor/ckeditor5-code-block': 1530be090e4f5235e197fb6208a8e8230e3355d5633fc7cad2b2e892659e9c04'@ckeditor/ckeditor5-mention': 70377e48a04e2f45abba79aa35f3d5527ec7aa7ffce44056f9957a49a24a4a60+ '@maplibre/[email protected]': c30b921f19820021e9c5c23d64378cbdf3dfba379cd4b5daff8a3f1f8b3578d5+ [email protected]: 124bf3dd7f7c9674e69724570d6727af9dea5b99a30a56a6e4573a95c49c4402importers:@@ -227,7 +229,7 @@ importers:version: 6.1.20(@fullcalendar/[email protected])'@maplibre/maplibre-gl-leaflet':specifier: 0.1.3+ version: 0.1.3(patch_hash=c30b921f19820021e9c5c23d64378cbdf3dfba379cd4b5daff8a3f1f8b3578d5)(@types/[email protected])([email protected](patch_hash=124bf3dd7f7c9674e69724570d6727af9dea5b99a30a56a6e4573a95c49c4402))([email protected])'@mermaid-js/layout-elk':specifier: 0.2.1version: 0.2.1([email protected])@@ -335,7 +337,7 @@ importers:version: 0.17.0leaflet:specifier: 1.9.4- version: 1.9.4+ version: 1.9.4(patch_hash=124bf3dd7f7c9674e69724570d6727af9dea5b99a30a56a6e4573a95c49c4402)leaflet-gpx:specifier: 2.2.0version: 2.2.0@@ -840,7 +842,7 @@ importers:version: 6.1.20(@fullcalendar/[email protected])'@maplibre/maplibre-gl-leaflet':specifier: 0.1.3+ version: 0.1.3(patch_hash=c30b921f19820021e9c5c23d64378cbdf3dfba379cd4b5daff8a3f1f8b3578d5)(@types/[email protected])([email protected](patch_hash=124bf3dd7f7c9674e69724570d6727af9dea5b99a30a56a6e4573a95c49c4402))([email protected])'@mermaid-js/layout-elk':specifier: 0.2.1version: 0.2.1([email protected])@@ -942,7 +944,7 @@ importers:version: 0.17.0leaflet:specifier: 1.9.4- version: 1.9.4+ version: 1.9.4(patch_hash=124bf3dd7f7c9674e69724570d6727af9dea5b99a30a56a6e4573a95c49c4402)leaflet-gpx:specifier: 2.2.0version: 2.2.0@@ -17330,10 +17332,10 @@ snapshots:dependencies:kdbush: 4.1.0+ '@maplibre/[email protected](patch_hash=c30b921f19820021e9c5c23d64378cbdf3dfba379cd4b5daff8a3f1f8b3578d5)(@types/[email protected])([email protected](patch_hash=124bf3dd7f7c9674e69724570d6727af9dea5b99a30a56a6e4573a95c49c4402))([email protected])':dependencies:'@types/leaflet': 1.9.21- leaflet: 1.9.4+ leaflet: 1.9.4(patch_hash=124bf3dd7f7c9674e69724570d6727af9dea5b99a30a56a6e4573a95c49c4402)maplibre-gl: 5.24.0'@maplibre/[email protected]':@@ -24936,7 +24938,7 @@ snapshots:- [email protected]: {}+ [email protected](patch_hash=124bf3dd7f7c9674e69724570d6727af9dea5b99a30a56a6e4573a95c49c4402): {}dependencies:diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yamlindex eeff1fcb1f..aae4fa2137 100644--- a/pnpm-workspace.yaml+++ b/pnpm-workspace.yaml@@ -101,6 +101,8 @@ overrides:patchedDependencies:"@ckeditor/ckeditor5-mention": patches/@ckeditor__ckeditor5-mention.patch"@ckeditor/ckeditor5-code-block": patches/@ckeditor__ckeditor5-code-block.patch+ '@maplibre/[email protected]': patches/@[email protected]+ [email protected]: patches/[email protected]allowBuilds:better-sqlite3: true