From a2673e9d59a0e5817621890daf7cbbe3644e8e53 Mon Sep 17 00:00:00 2001 From: Tony Grosinger Date: Tue, 9 Dec 2025 15:11:17 -0800 Subject: [PATCH 1/2] Support primitives in coordinateFromValue --- src/map-view.ts | 2 +- src/map/markers.ts | 4 ++-- src/map/utils.ts | 21 +++++++++++++++++---- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/map-view.ts b/src/map-view.ts index 5dedcf6..1bcf345 100644 --- a/src/map-view.ts +++ b/src/map-view.ts @@ -17,7 +17,7 @@ import { BackgroundSwitcherControl } from './map/controls/background-switcher'; import { StyleManager } from './map/style'; import { PopupManager } from './map/popup'; import { MarkerManager } from './map/markers'; -import { hasOwnProperty, coordinateFromValue } from './map/utils'; +import { hasOwnProperty, parseLatLng } from './map/utils'; import { rtlPluginCode } from './map/rtl-plugin-code'; interface MapConfig { diff --git a/src/map/markers.ts b/src/map/markers.ts index 7007a5e..0223751 100644 --- a/src/map/markers.ts +++ b/src/map/markers.ts @@ -1,7 +1,7 @@ import { App, BasesEntry, BasesPropertyId, Keymap, Menu, setIcon } from 'obsidian'; import { Map, LngLatBounds, GeoJSONSource, MapLayerMouseEvent } from 'maplibre-gl'; import { MapMarker, MapMarkerProperties } from './types'; -import { coordinateFromValue } from './utils'; +import { parseLatLng } from './utils'; import { PopupManager } from './popup'; export class MarkerManager { @@ -65,7 +65,7 @@ export class MarkerManager { let coordinates: [number, number] | null = null; try { const value = entry.getValue(mapConfig.coordinatesProp); - coordinates = coordinateFromValue(value); + coordinates = parseLatLng(value); } catch (error) { console.error(`Error extracting coordinates for ${entry.file.name}:`, error); diff --git a/src/map/utils.ts b/src/map/utils.ts index 9e70d0c..95c6a8d 100644 --- a/src/map/utils.ts +++ b/src/map/utils.ts @@ -1,9 +1,9 @@ -import { Value, NumberValue, StringValue, ListValue } from 'obsidian'; +import { NumberValue, StringValue, ListValue } from 'obsidian'; /** - * Converts a Value to coordinate tuple [lat, lng] + * Converts a Value, string, or array to coordinate tuple [lat, lng] */ -export function coordinateFromValue(value: Value | null): [number, number] | null { +export function parseLatLng(value: unknown | null): [number, number] | null { let lat: number | null = null; let lng: number | null = null; @@ -23,6 +23,19 @@ export function coordinateFromValue(value: Value | null): [number, number] | nul lng = parseCoordinate(parts[1].trim()); } } + else if (String.isString(value)) { + const parts = value.trim().split(','); + if (parts.length >= 2) { + lat = parseCoordinate(parts[0].trim()); + lng = parseCoordinate(parts[1].trim()); + } + } + else if (Array.isArray(value)) { + if (value.length >= 2) { + lat = parseCoordinate(value[0]); + lng = parseCoordinate(value[1]); + } + } if (lat && lng && verifyLatLng(lat, lng)) { return [lat, lng]; @@ -39,7 +52,7 @@ export function verifyLatLng(lat: number, lng: number): boolean { } /** - * Parses a coordinate value from various formats + * Parses a single coordinate value from various formats */ export function parseCoordinate(value: unknown): number | null { if (value instanceof NumberValue) { From 76a7f1ec62417b5a2b2f98d3c3d70650be57e454 Mon Sep 17 00:00:00 2001 From: Tony Grosinger Date: Mon, 24 Nov 2025 14:53:23 -0800 Subject: [PATCH 2/2] Update to support new formula evaluation for view config values --- src/map-view.ts | 35 ++++++++++------------------------- 1 file changed, 10 insertions(+), 25 deletions(-) diff --git a/src/map-view.ts b/src/map-view.ts index 1bcf345..b7b742b 100644 --- a/src/map-view.ts +++ b/src/map-view.ts @@ -4,8 +4,6 @@ import { debounce, Menu, QueryController, - Value, - StringValue, NullValue, ViewOption, } from 'obsidian'; @@ -532,33 +530,19 @@ export class MapView extends BasesView { } private getCenterFromConfig(): [number, number] { - let centerConfig: Value; - + let centerConfig: unknown = null; + try { centerConfig = this.config.getEvaluatedFormula(this, 'center'); - } catch (error) { - // Formula evaluation failed (e.g., this.file is null when no active file) - // Fall back to raw config value - const centerConfigStr = this.config.get('center'); - if (String.isString(centerConfigStr)) { - centerConfig = new StringValue(centerConfigStr); - } - else { - return DEFAULT_MAP_CENTER; + if (centerConfig === null || centerConfig === undefined || centerConfig === NullValue.value) { + // If a formula is not specified, then get the static value. + centerConfig = this.config.get('center'); } + } catch (error) { + return DEFAULT_MAP_CENTER; } - // Support for legacy string format. - if (Value.equals(centerConfig, NullValue.value)) { - const centerConfigStr = this.config.get('center'); - if (String.isString(centerConfigStr)) { - centerConfig = new StringValue(centerConfigStr); - } - else { - return DEFAULT_MAP_CENTER; - } - } - return coordinateFromValue(centerConfig) || DEFAULT_MAP_CENTER; + return parseLatLng(centerConfig) || DEFAULT_MAP_CENTER; } private getConfigSnapshot(): string { @@ -695,9 +679,10 @@ export class MapView extends BasesView { { displayName: 'Center coordinates', - type: 'formula', + type: 'text', key: 'center', placeholder: '[latitude, longitude]', + allowFormula: true, }, { displayName: 'Default zoom',