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
feat(geomap): draw the places that answer a click nearly solid
Elian Doran393b58b0bee4
diff --git a/apps/client/src/widgets/collections/geomap/Pois.spec.tsx b/apps/client/src/widgets/collections/geomap/Pois.spec.tsxindex eff13f477d..6651afc4ed 100644--- a/apps/client/src/widgets/collections/geomap/Pois.spec.tsx+++ b/apps/client/src/widgets/collections/geomap/Pois.spec.tsx@@ -12,7 +12,7 @@ import { CLUSTER_LAYER } from "./clusters";import type { GeoSearchResult } from "./geocoding";import { MapStyleLoaded, ParentMap } from "./map";import { MARKER_LAYER } from "./Markers";-import Pois, { clickableTint, poiFromFeature, poiLayers } from "./Pois";+import Pois, { clickableOpacity, clickableTint, poiFromFeature, poiLayers } from "./Pois";import { PLACE_MARKER_COLOR } from "./PlaceMarker";/**@@ -69,11 +69,13 @@ function fakeMap({poiLayerIds = [ "poi-amenity", "poi-shop" ],ownLayerIds = [ MARKER_LAYER ],zoom = 18,- styleColor = "rgb(85,85,85)" as unknown+ styleColor = "rgb(85,85,85)" as unknown,+ styleOpacity = { stops: [ [ 16, 0 ], [ 17, 0.4 ] ] } as unknown} = {}) {const listeners = new Map<string, Set<Listener>>();const canvas = { style: { cursor: "" } };const paint = new Map<string, unknown>(poiLayerIds.map((id) => [ id, styleColor ]));+ const opacities = new Map<string, unknown>(poiLayerIds.map((id) => [ id, styleOpacity ]));let onPoi: unknown[] = [];let onOwn: unknown[] = [];@@ -125,9 +127,13 @@ function fakeMap({},/** What a layer's places are currently painted in. */iconColor: (layer: string) => paint.get(layer),- getPaintProperty: (layer: string, name: string) => name === "icon-color" ? paint.get(layer) : undefined,+ /** How solid a layer's places are currently drawn. */+ iconOpacity: (layer: string) => opacities.get(layer),+ getPaintProperty: (layer: string, name: string) =>+ name === "icon-color" ? paint.get(layer) : opacities.get(layer),setPaintProperty(layer: string, name: string, value: unknown) {if (name === "icon-color") paint.set(layer, value);+ else opacities.set(layer, value);},getLayersOrder: () => [ ...layers.keys() ],getLayer: (id: string) => layers.get(id),@@ -533,6 +539,85 @@ describe("geo map place colouring", () => {});});+describe("geo map place solidity", () => {+ /** What the styles ask for: nothing at zoom 16, climbing to a fraction of solid by 17. */+ const STYLE_RAMP = { stops: [ [ 16, 0 ], [ 17, 0.4 ] ] };++ it("brings the places that answer a click up to nearly solid", async () => {+ const map = fakeMap();+ await renderPois(map);++ expect(map.iconOpacity("poi-amenity")).toEqual(clickableOpacity(STYLE_RAMP));+ expect(clickableOpacity(STYLE_RAMP)).toEqual([+ "interpolate",+ [ "linear" ],+ [ "zoom" ],+ // Below the zoom a place can be picked from, what the style asked for stands.+ 16, 0,+ // At it, a place carrying a name is drawn nearly solid and the rest stay a background.+ 17, [ "case", NAMED, 0.95, 0.4 ]+ ]);+ });++ it("keeps the curve the style asked to be read between its stops by", () => {+ const exponential = clickableOpacity({ stops: [ [ 16, 0 ], [ 18, 0.5 ] ], base: 1.5 });++ expect((exponential as unknown[])[1]).toEqual([ "exponential", 1.5 ]);+ });++ it("raises the places at the zoom they can be picked from, whatever the style's ramp says", () => {+ // A ramp that has finished climbing before a place can be picked says nothing about the+ // zooms that matter, so the raise is added as a stop of its own.+ expect(clickableOpacity({ stops: [ [ 14, 0 ], [ 15, 0.4 ] ] })).toEqual([+ "interpolate",+ [ "linear" ],+ [ "zoom" ],+ 14, 0,+ 15, 0.4,+ 17, [ "case", NAMED, 0.95, 0.4 ]+ ]);++ // A flat number has no zoom to it, and is stepped in where the colour is.+ expect(clickableOpacity(0.4)).toEqual([+ "step", [ "zoom" ], 0.4, 17, [ "case", NAMED, 0.95, 0.4 ]+ ]);+ });++ it("leaves alone what it cannot read a ramp of numbers out of", async () => {+ expect(clickableOpacity({ stops: [ [ 16, "half" ] ] })).toBeNull();+ expect(clickableOpacity({ stops: [] })).toBeNull();+ expect(clickableOpacity(undefined)).toBeNull();+ expect(clickableOpacity([ "case", NAMED, 1, 0 ])).toBeNull();++ // A style this cannot be read out of keeps the solidity it asked for, colour or no colour.+ const map = fakeMap({ styleOpacity: [ "case", NAMED, 1, 0 ] });+ await renderPois(map);++ expect(map.iconOpacity("poi-amenity")).toEqual([ "case", NAMED, 1, 0 ]);+ expect(map.iconColor("poi-amenity")).toEqual(clickableTint("rgb(85,85,85)"));+ });++ it("asks the zoom only where MapLibre takes it, at the top of the expression", () => {+ const solid = clickableOpacity(STYLE_RAMP) as unknown[];++ expect(solid[0]).toBe("interpolate");+ expect(solid[2]).toEqual([ "zoom" ]);+ expect(mentionsZoom(solid.slice(3))).toBe(false);+ });++ it("puts the style's own solidity back when it goes", async () => {+ const map = fakeMap({ styleOpacity: STYLE_RAMP });+ const { unmount } = await renderPois(map);++ await unmount();++ expect(map.iconOpacity("poi-amenity")).toBe(STYLE_RAMP);+ });+});++/** What both builders ask of a place: that it carries a name to be kept under. */+const NAMED = [ "to-boolean", [ "coalesce", [ "get", "name_en" ], [ "get", "name" ] ] ];+/** Whether `["zoom"]` is asked anywhere within a value, however deeply it is nested. */function mentionsZoom(value: unknown): boolean {if (!Array.isArray(value)) {diff --git a/apps/client/src/widgets/collections/geomap/Pois.tsx b/apps/client/src/widgets/collections/geomap/Pois.tsxindex c40d55db3b..a75d578c16 100644--- a/apps/client/src/widgets/collections/geomap/Pois.tsx+++ b/apps/client/src/widgets/collections/geomap/Pois.tsx@@ -20,6 +20,12 @@ const POI_SOURCE_LAYER = "pois";*/const MIN_POI_ZOOM = 17;+/**+ * How solid a place that answers a click is drawn. The styles hold their places at a fraction of+ * this, which is what keeps them a background; a place that can be kept is not one.+ */+const CLICKABLE_OPACITY = 0.95;+/*** How long the pointer has to rest on a place before its name is shown.*@@ -224,16 +230,17 @@ export default function Pois({ placing, onPick }: PoisProps) {if (!parentMap) return;// Aliased so the narrowing above carries into the functions below.const map = parentMap;- // What the style painted its places with, to be put back when this is taken off.- let painted: { layer: string; color: string }[] = [];+ // What the style painted its places with, to be put back when this is taken off. Either+ // may be absent, a style being composable in one and not the other.+ let painted: { layer: string; color?: unknown; opacity?: unknown }[] = [];/** Puts back what the style painted, for a map that keeps its layers after this goes. */function restore() {- for (const { layer, color } of painted) {+ for (const { layer, color, opacity } of painted) {try {- if (map.getLayer(layer)) {- map.setPaintProperty(layer, "icon-color", color);- }+ if (!map.getLayer(layer)) continue;+ if (color !== undefined) map.setPaintProperty(layer, "icon-color", color);+ if (opacity !== undefined) map.setPaintProperty(layer, "icon-opacity", opacity);} catch {// The style has moved on, and took the layer with it.}@@ -248,15 +255,23 @@ export default function Pois({ placing, onPick }: PoisProps) {for (const layer of poiLayers(map)) {const color = map.getPaintProperty(layer, "icon-color");+ const opacity = map.getPaintProperty(layer, "icon-opacity");// Only a plain colour can be composed with: the old function syntax a style may// paint with is an object rather than an expression, and cannot stand inside one.- if (typeof color !== "string") continue;+ const tinted = typeof color === "string" ? clickableTint(color) : null;+ const solid = clickableOpacity(opacity);+ if (!tinted && !solid) continue;try {- map.setPaintProperty(layer, "icon-color", clickableTint(color));- painted.push({ layer, color });+ if (tinted) map.setPaintProperty(layer, "icon-color", tinted);+ if (solid) map.setPaintProperty(layer, "icon-opacity", solid);+ painted.push({+ layer,+ color: tinted ? color : undefined,+ opacity: solid ? opacity : undefined+ });} catch (e) {- console.warn("Geo map: could not colour the places that answer a click --", e);+ console.warn("Geo map: could not draw the places that answer a click --", e);}}}@@ -340,16 +355,92 @@ function isOwnUnderPointer(map: MapLibreGLMap, point: MapMouseEvent["point"]) {* is where the name is then asked about.*/export function clickableTint(styleColor: string) {- const named = [ "to-boolean", [ "coalesce", [ "get", "name_en" ], [ "get", "name" ] ] ];-return ["step",[ "zoom" ],styleColor,- MIN_POI_ZOOM, [ "case", named, PLACE_MARKER_COLOR, styleColor ]+ MIN_POI_ZOOM, [ "case", namedPlace(), PLACE_MARKER_COLOR, styleColor ]];}+/**+ * How solid a place is drawn: what the style asked for, with the places that answer a click brought+ * up to nearly solid at the zooms they can be picked from. `null` where the style asks for something+ * this cannot be read out of, and the layer is then left as it is.+ *+ * The styles paint this with the old function syntax -- `{stops: [[16, 0], [17, 0.4]]}` -- which is+ * an object rather than an expression and so cannot be composed with. It is read and rebuilt as the+ * expression saying the same thing, rather than written out here from what the styles say today: a+ * style is fetched now rather than shipped (see map_layer), and one that lifts its places out of the+ * background on its own should not be argued with about how far.+ */+export function clickableOpacity(styleOpacity: unknown) {+ // A flat number has no zoom to it, so the raise is stepped in where the colour is (see+ // `clickableTint`) rather than standing at every zoom the style draws its places at.+ if (typeof styleOpacity === "number") {+ return [+ "step",+ [ "zoom" ],+ styleOpacity,+ MIN_POI_ZOOM, [ "case", namedPlace(), CLICKABLE_OPACITY, styleOpacity ]+ ];+ }++ const ramp = readRamp(styleOpacity);+ if (!ramp) {+ return null;+ }++ const { stops, interpolation } = ramp;+ const raised: unknown[][] = stops.map(([ zoom, value ]) => [+ zoom,+ zoom >= MIN_POI_ZOOM ? [ "case", namedPlace(), CLICKABLE_OPACITY, value ] : value+ ]);++ // A ramp that has finished climbing before a place can be picked says nothing about the zooms+ // that matter here, so the raise is added as a stop of its own at the first zoom that does.+ const [ lastZoom, lastValue ] = stops[stops.length - 1];+ if (lastZoom < MIN_POI_ZOOM) {+ raised.push([ MIN_POI_ZOOM, [ "case", namedPlace(), CLICKABLE_OPACITY, lastValue ] ]);+ }++ return [ "interpolate", interpolation, [ "zoom" ], ...raised.flat() ];+}++/** Whether a place carries a name, which is what it would be kept under (see `poiFromFeature`). */+function namedPlace() {+ return [ "to-boolean", [ "coalesce", [ "get", "name_en" ], [ "get", "name" ] ] ];+}++/**+ * The zoom ramp behind the old function syntax -- its stops and how it reads between them -- or+ * `null` for anything that is not one of numbers.+ */+function readRamp(value: unknown) {+ if (!value || typeof value !== "object" || !("stops" in value)) {+ return null;+ }++ const { stops, base } = value as { stops?: unknown; base?: unknown };+ if (!Array.isArray(stops) || !stops.length) {+ return null;+ }++ const read: [number, number][] = [];+ for (const stop of stops) {+ if (!Array.isArray(stop) || typeof stop[0] !== "number" || typeof stop[1] !== "number") {+ return null;+ }+ read.push([ stop[0], stop[1] ]);+ }++ // A base of one climbs evenly, which is what `linear` says; anything else is the curve the old+ // syntax spells `base`.+ const interpolation = typeof base === "number" && base !== 1 ? [ "exponential", base ] : [ "linear" ];++ return { stops: read, interpolation };+}+/** The name of a place and the icon it would wear as a marker, as the tooltip shows them. */function placeLabel(place: GeoSearchResult) {const label = document.createElement("span");diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Collections/Geo Map.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Collections/Geo Map.htmlindex ae86682d13..90bb286099 100644--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Collections/Geo Map.html+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Collections/Geo Map.html@@ -181,8 +181,9 @@zoomed in far enough for these to be drawn, they can be clicked:</p><ol><li>Look for the places drawn in orange — the same colour a searched place- is pinned in. Those are the ones that can be clicked; the rest of the map's- places stay grey.</li>+ is pinned in. Those are the ones that can be clicked, and they are drawn+ nearly solid, while the rest of the map's places stay a faint grey+ background.</li><li>Rest the mouse on one of them. Its name appears above it, and the cursorturns into a pointer. The map draws these places as bare icons, so hoveringis how their names are read — showing them all at once would crowd out thediff --git a/docs/User Guide/User Guide/Collections/Geo Map.md b/docs/User Guide/User Guide/Collections/Geo Map.mdindex 5409d011d3..380124e881 100644--- a/docs/User Guide/User Guide/Collections/Geo Map.md+++ b/docs/User Guide/User Guide/Collections/Geo Map.md@@ -128,7 +128,7 @@ This works for:When a vector map style is used, the map itself draws the shops, cafés, museums and other places around the area being viewed. Once the map is zoomed in far enough for these to be drawn, they can be clicked:-1. Look for the places drawn in orange — the same colour a searched place is pinned in. Those are the ones that can be clicked; the rest of the map's places stay grey.+1. Look for the places drawn in orange — the same colour a searched place is pinned in. Those are the ones that can be clicked, and they are drawn nearly solid, while the rest of the map's places stay a faint grey background.2. Rest the mouse on one of them. Its name appears above it, and the cursor turns into a pointer. The map draws these places as bare icons, so hovering is how their names are read — showing them all at once would crowd out the titles of your own markers.3. Click it. The same panel used for a searched place appears, showing the place's name and its coordinates, and a pin is placed on it.4. Press _Add as marker_ to keep it. The note is created with the place's name as its title, and the icon matching the kind of place it is.