Skip to content

Build your personal knowledge base with Trilium Notes

Merge remote-tracking branch 'origin/main' into copilot/switch-to-mapbox-gl

Elian Doranea09018acda1
diff --cc apps/client/src/widgets/collections/geomap/index.tsx
index 28f2a65561,8be547fa3a..023775a311
--- a/apps/client/src/widgets/collections/geomap/index.tsx
+++ b/apps/client/src/widgets/collections/geomap/index.tsx
@@@ -1,5 -1,8 +1,6 @@@
import "./index.css";
-import { divIcon, GPXOptions, LatLng, LeafletMouseEvent } from "leaflet";
-import markerIcon from "leaflet/dist/images/marker-icon.png";
-import markerIconShadow from "leaflet/dist/images/marker-shadow.png";
++import type maplibregl from "maplibre-gl";
import { useCallback, useContext, useEffect, useMemo, useRef, useState } from "preact/hooks";
import appContext from "../../../components/app_context";
@@@ -18,10 -21,9 +19,9 @@@ import TouchBar, { TouchBarButton, Touc
import { ViewModeProps } from "../interface";
import { createNewNote, moveMarker } from "./api";
import openContextMenu, { openMapContextMenu } from "./context_menu";
-import Map from "./map";
+import Map, { GeoMouseEvent } from "./map";
- import { DEFAULT_MAP_LAYER_NAME } from "./map_layer";
+ import { DEFAULT_MAP_LAYER_NAME, MAP_LAYERS, MapLayer } from "./map_layer";
import Marker, { GpxTrack } from "./marker";
- import type maplibregl from "maplibre-gl";
const DEFAULT_COORDINATES: [number, number] = [3.878638227135724, 446.6630455551659];
const DEFAULT_ZOOM = 2;
@@@ -199,9 -228,10 +226,10 @@@ function NoteMarker({ note, editable, l
const [ archived ] = useNoteLabelBoolean(note, "archived");
const title = useNoteProperty(note, "title");
- const icon = useMemo(() => {
+ const iconHtml = useMemo(() => {
- return buildIconHtml(note.getIcon(), note.getColorClass() ?? undefined, title, note.noteId, archived);
- }, [ iconClass, color, title, note.noteId, archived]);
+ const titleOrNone = hideLabels ? undefined : title;
- return buildIcon(note.getIcon(), note.getColorClass() ?? undefined, titleOrNone, note.noteId, archived);
- }, [ iconClass, color, title, note.noteId, archived, hideLabels ]);
++ return buildIconHtml(note.getIcon(), note.getColorClass() ?? undefined, titleOrNone, note.noteId, archived);
++ }, [ note, iconClass, color, title, archived, hideLabels ]);
const onClick = useCallback(() => {
appContext.triggerCommand("openInPopup", { noteIdOrPath: note.noteId });
diff --cc apps/client/src/widgets/collections/geomap/map.tsx
index 44bd66d5f3,19a4586c83..edac08e053
--- a/apps/client/src/widgets/collections/geomap/map.tsx
+++ b/apps/client/src/widgets/collections/geomap/map.tsx
@@@ -1,89 -1,41 +1,88 @@@
-import { useEffect, useImperativeHandle, useRef, useState } from "preact/hooks";
-import L, { control, LatLng, Layer, LeafletMouseEvent } from "leaflet";
-import "leaflet/dist/leaflet.css";
-import { MAP_LAYERS, type MapLayer } from "./map_layer";
+import "maplibre-gl/dist/maplibre-gl.css";
+
+import maplibregl from "maplibre-gl";
import { ComponentChildren, createContext, RefObject } from "preact";
+import { useEffect, useImperativeHandle, useRef } from "preact/hooks";
+
import { useElementSize, useSyncedRef } from "../../react/hooks";
- import { MAP_LAYERS } from "./map_layer";
++import { MapLayer } from "./map_layer";
+
+export interface GeoMouseEvent {
+ latlng: { lat: number; lng: number };
+ originalEvent: MouseEvent;
+}
-export const ParentMap = createContext<L.Map | null>(null);
+export const ParentMap = createContext<maplibregl.Map | null>(null);
interface MapProps {
- apiRef?: RefObject<L.Map | null>;
+ apiRef?: RefObject<maplibregl.Map | null>;
containerRef?: RefObject<HTMLDivElement>;
- coordinates: LatLng | [number, number];
+ coordinates: { lat: number; lng: number } | [number, number];
zoom: number;
- layerName: string;
+ layerData: MapLayer;
- viewportChanged: (coordinates: LatLng, zoom: number) => void;
+ viewportChanged: (coordinates: { lat: number; lng: number }, zoom: number) => void;
children: ComponentChildren;
- onClick?: (e: LeafletMouseEvent) => void;
- onContextMenu?: (e: LeafletMouseEvent) => void;
+ onClick?: (e: GeoMouseEvent) => void;
+ onContextMenu?: (e: GeoMouseEvent) => void;
onZoom?: () => void;
scale: boolean;
}
+function toMapLibreEvent(e: maplibregl.MapMouseEvent): GeoMouseEvent {
+ return {
+ latlng: { lat: e.lngLat.lat, lng: e.lngLat.lng },
+ originalEvent: e.originalEvent
+ };
+}
+
- export default function Map({ coordinates, zoom, layerName, viewportChanged, children, onClick, onContextMenu, scale, apiRef, containerRef: _containerRef, onZoom }: MapProps) {
+ export default function Map({ coordinates, zoom, layerData, viewportChanged, children, onClick, onContextMenu, scale, apiRef, containerRef: _containerRef, onZoom }: MapProps) {
- const mapRef = useRef<L.Map>(null);
+ const mapRef = useRef<maplibregl.Map>(null);
const containerRef = useSyncedRef<HTMLDivElement>(_containerRef);
useImperativeHandle(apiRef ?? null, () => mapRef.current);
+ // Initialize the map.
useEffect(() => {
if (!containerRef.current) return;
- const mapInstance = L.map(containerRef.current, {
- worldCopyJump: false,
- maxBounds: [
- [-90, -180],
- [90, 180]
- ],
- minZoom: 2
+
- const layerData = MAP_LAYERS[layerName];
+ let style: maplibregl.StyleSpecification | string;
+
+ if (layerData.type === "vector") {
+ style = typeof layerData.style === "string"
+ ? layerData.style
+ : layerData.styleFallback;
+ } else {
+ style = {
+ version: 8,
+ sources: {
+ "raster-tiles": {
+ type: "raster",
+ tiles: [layerData.url],
+ tileSize: 256,
+ attribution: layerData.attribution
+ }
+ },
+ layers: [
+ {
+ id: "raster-layer",
+ type: "raster",
+ source: "raster-tiles"
+ }
+ ]
+ };
+ }
+
+ const center = Array.isArray(coordinates)
+ ? [coordinates[1], coordinates[0]] as [number, number]
+ : [coordinates.lng, coordinates.lat] as [number, number];
+
+ const mapInstance = new maplibregl.Map({
+ container: containerRef.current,
+ style,
+ center,
+ zoom,
+ minZoom: 1,
+ renderWorldCopies: false
});
mapRef.current = mapInstance;
@@@ -101,42 -45,38 +100,41 @@@
};
}, []);
- // Load the layer asynchronously.
- const [ layer, setLayer ] = useState<Layer>();
+ // React to layer changes.
useEffect(() => {
- async function load() {
- if (layerData.type === "vector") {
- const style = (typeof layerData.style === "string" ? layerData.style : await layerData.style());
- await import("@maplibre/maplibre-gl-leaflet");
-
- setLayer(L.maplibreGL({
- style: style as any
- }));
+ const map = mapRef.current;
+ if (!map) return;
+
- const layerData = MAP_LAYERS[layerName];
+ if (layerData.type === "vector") {
+ if (typeof layerData.style === "string") {
+ map.setStyle(layerData.style);
} else {
- setLayer(L.tileLayer(layerData.url, {
- attribution: layerData.attribution,
- detectRetina: true,
- noWrap: true
- }));
+ layerData.style().then(asyncStyle => {
+ map.setStyle(asyncStyle as maplibregl.StyleSpecification);
+ });
}
+ } else {
+ map.setStyle({
+ version: 8,
+ sources: {
+ "raster-tiles": {
+ type: "raster",
+ tiles: [layerData.url],
+ tileSize: 256,
+ attribution: layerData.attribution
+ }
+ },
+ layers: [
+ {
+ id: "raster-layer",
+ type: "raster",
+ source: "raster-tiles"
+ }
+ ]
+ });
}
- }, [ layerName ]);
-
- load();
+ }, [ layerData ]);
- // Attach layer to the map.
- useEffect(() => {
- const map = mapRef.current;
- const layerToAdd = layer;
- if (!map || !layerToAdd) return;
- layerToAdd.addTo(map);
- return () => layerToAdd.removeFrom(map);
- }, [ mapRef, layer ]);
-
// React to coordinate changes.
useEffect(() => {
if (!mapRef.current) return;
diff --cc apps/client/src/widgets/collections/geomap/map_layer.ts
index baa5c07774,bb5f6174e6..3859122f28
--- a/apps/client/src/widgets/collections/geomap/map_layer.ts
+++ b/apps/client/src/widgets/collections/geomap/map_layer.ts
@@@ -1,24 -1,17 +1,23 @@@
- export interface MapLayer {
- name: string;
- isDarkTheme?: boolean;
- }
++import { type StyleSpecification } from "maplibre-gl";
+
- interface VectorLayer extends MapLayer {
+ export type MapLayer = ({
type: "vector";
- style: string | (() => Promise<{}>);
- styleFallback: {};
- }
-
- interface RasterLayer extends MapLayer {
- style: string | (() => Promise<{}>)
++ style: string | (() => Promise<StyleSpecification>);
++ styleFallback: StyleSpecification;
+ } | {
type: "raster";
url: string;
attribution: string;
- }
+ }) & {
+ // Common properties
+ name: string;
+ isDarkTheme?: boolean;
+ };
+// Minimal empty style used as a placeholder while the real style loads asynchronously.
- const EMPTY_STYLE = { version: 8, sources: {}, layers: [] };
++const EMPTY_STYLE: StyleSpecification = { version: 8, sources: {}, layers: [] };
+
- export const MAP_LAYERS: Record<string, VectorLayer | RasterLayer> = {
+ export const MAP_LAYERS: Record<string, MapLayer> = {
"openstreetmap": {
name: "OpenStreetMap",
type: "raster",
@@@ -28,33 -21,28 +27,33 @@@
"versatiles-colorful": {
name: "VersaTiles Colorful",
type: "vector",
- style: async () => (await import("./styles/colorful/en.json")).default,
- style: async () => (await import("./styles/colorful/en.json")).default
++ style: async () => (await import("./styles/colorful/en.json")).default as unknown as StyleSpecification,
+ styleFallback: EMPTY_STYLE
},
"versatiles-eclipse": {
name: "VersaTiles Eclipse",
type: "vector",
-- style: async () => (await import("./styles/eclipse/en.json")).default,
++ style: async () => (await import("./styles/eclipse/en.json")).default as unknown as StyleSpecification,
+ styleFallback: EMPTY_STYLE,
isDarkTheme: true
},
"versatiles-graybeard": {
name: "VersaTiles Graybeard",
type: "vector",
- style: async () => (await import("./styles/graybeard/en.json")).default,
- styleFallback: EMPTY_STYLE
- style: async () => (await import("./styles/graybeard/en.json")).default
++ style: async () => (await import("./styles/graybeard/en.json")).default as unknown as StyleSpecification,
++ styleFallback: EMPTY_STYLE,
},
"versatiles-neutrino": {
name: "VersaTiles Neutrino",
type: "vector",
- style: async () => (await import("./styles/neutrino/en.json")).default,
- styleFallback: EMPTY_STYLE
- style: async () => (await import("./styles/neutrino/en.json")).default
++ style: async () => (await import("./styles/neutrino/en.json")).default as unknown as StyleSpecification,
++ styleFallback: EMPTY_STYLE,
},
"versatiles-shadow": {
name: "VersaTiles Shadow",
type: "vector",
-- style: async () => (await import("./styles/shadow/en.json")).default,
++ style: async () => (await import("./styles/shadow/en.json")).default as unknown as StyleSpecification,
+ styleFallback: EMPTY_STYLE,
isDarkTheme: true
}
};