Skip to content

Build your personal knowledge base with Trilium Notes

feat(collections/map): cache icon renderings

Elian Doran638beb5b039e
diff --git a/apps/client/src/widgets/collections/geomap/marker_data.ts b/apps/client/src/widgets/collections/geomap/marker_data.ts
index 8a7bc088a6..331021712c 100644
--- a/apps/client/src/widgets/collections/geomap/marker_data.ts
+++ b/apps/client/src/widgets/collections/geomap/marker_data.ts
@@ -9,6 +9,7 @@ const DEFAULT_MARKER_COLOR = "#2A81CB";
// SVG marker pin shape (replaces the Leaflet marker PNG).
export const MARKER_SVG = "foo"; // TODO: Fix
+const iconSvgCache = new Map<string, string>();
export function useMarkerData(note: FNote | null | undefined, apiRef: MutableRef<maplibregl.Map>) {
const childNotes = useChildNotes(note?.noteId);
@@ -17,7 +18,6 @@ export function useMarkerData(note: FNote | null | undefined, apiRef: MutableRef
const map = apiRef.current as maplibregl.Map | undefined;
if (!map) return;
- const iconSvgCache = new Map<string, string>();
async function ensureIcon(color: string, iconClass: string) {
const key = `marker-${color}-${iconClass}`;
diff --git a/apps/client/src/widgets/collections/geomap/marker_renderer.ts b/apps/client/src/widgets/collections/geomap/marker_renderer.ts
index 074877d7fb..21cc968ea6 100644
--- a/apps/client/src/widgets/collections/geomap/marker_renderer.ts
+++ b/apps/client/src/widgets/collections/geomap/marker_renderer.ts
@@ -1,5 +1,7 @@
+const iconClassToBitmapCache = new Map<string, string | undefined>();
+
export async function buildMarkerIcon(color: string, iconClass: string, scale = window.devicePixelRatio || 1) {
- const iconUrl = await snapshotIcon(iconClass, 16 * scale);
+ const iconUrl = iconClassToBitmapCache.get(iconClass) ?? await snapshotIcon(iconClass, 16 * scale);
return `\
<svg width="${25 * scale}" height="${41 * scale}" viewBox="0 0 25 41" xmlns="http://www.w3.org/2000/svg">
<path d="M12.5 0C5.6 0 0 5.6 0 12.5C0 21.9 12.5 41 12.5 41S25 21.9 25 12.5C25 5.6 19.4 0 12.5 0Z" fill="${color}" />
@@ -10,6 +12,9 @@ export async function buildMarkerIcon(color: string, iconClass: string, scale =
}
async function snapshotIcon(iconClass: string, size: number) {
+ const cachedIcon = iconClassToBitmapCache.get(iconClass);
+ if (cachedIcon) return cachedIcon;
+
await document.fonts.ready;
const glyph = getGlyphFromClass(iconClass);
const rendered = renderMarkerCanvas({
@@ -17,7 +22,9 @@ async function snapshotIcon(iconClass: string, size: number) {
glyph,
size
});
- return rendered?.toDataURL();
+ const dataUrl = rendered?.toDataURL();
+ iconClassToBitmapCache.set(iconClass, dataUrl);
+ return dataUrl;
}
function renderMarkerCanvas({
@@ -88,7 +95,7 @@ function getGlyphFromClass(iconClass: string) {
};
}
-export function svgToImage(svgString){
+export function svgToImage(svgString: string){
return new Promise<HTMLImageElement>(resolve => {
const svgBlob = new Blob([svgString], { type: "image/svg+xml" });
const url = URL.createObjectURL(svgBlob);
@@ -101,6 +108,5 @@ export function svgToImage(svgString){
};
img.src = url;
- document.body.appendChild(img);
});
}