From 5998c99cbeab8313a5d21c973f5e94871be9b6bf Mon Sep 17 00:00:00 2001 From: ccmdi Date: Sun, 23 Nov 2025 17:05:10 -0500 Subject: [PATCH] refactor: debloat main base view --- src/map-renderer.ts | 2 +- src/pointutils.ts | 2 +- src/{ => types}/latlng.ts | 7 +++- src/utils.ts | 16 ++++++++ src/views/map-bases-view.ts | 78 ++++++++++++++----------------------- 5 files changed, 54 insertions(+), 51 deletions(-) rename src/{ => types}/latlng.ts (97%) create mode 100644 src/utils.ts diff --git a/src/map-renderer.ts b/src/map-renderer.ts index 7b474cd..0293ab3 100644 --- a/src/map-renderer.ts +++ b/src/map-renderer.ts @@ -9,7 +9,7 @@ import { MapView as MapViewType } from '@deck.gl/core'; import { MapTagSettings } from './settings/map-tag-settings'; import type MapPlugin from './main'; import type { ThumbnailCacheManager } from './thumbnail-cache'; -import { LatLng } from './latlng'; +import { LatLng } from './types/latlng'; function easeCubic(t: number): number { return t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2; diff --git a/src/pointutils.ts b/src/pointutils.ts index 303fe81..4f152b5 100644 --- a/src/pointutils.ts +++ b/src/pointutils.ts @@ -1,5 +1,5 @@ import { MapPoint } from "./map-renderer"; -import { LatLng } from "./latlng"; +import { LatLng } from "./types/latlng"; export function haveLocationsChanged(points1: MapPoint[], points2: MapPoint[]): boolean { return !arePointsEqual(points1, points2); diff --git a/src/latlng.ts b/src/types/latlng.ts similarity index 97% rename from src/latlng.ts rename to src/types/latlng.ts index 0b1177a..920fc27 100644 --- a/src/latlng.ts +++ b/src/types/latlng.ts @@ -143,8 +143,13 @@ export namespace LatLng { // Parse coordinate value from various sources (Obsidian-specific) // Handles NumberValue, StringValue, and primitive types export function parseCoordinate(value: unknown): number | null { + // Handle null/undefined + if (value === null || value === undefined) { + return null; + } + // Handle Obsidian's NumberValue/StringValue types - if (value && typeof value === 'object' && 'toString' in value) { + if (typeof value === 'object' && 'toString' in value) { const strValue = (value as { toString: () => string }).toString(); const num = parseFloat(strValue); return isNaN(num) ? null : num; diff --git a/src/utils.ts b/src/utils.ts new file mode 100644 index 0000000..bb1da40 --- /dev/null +++ b/src/utils.ts @@ -0,0 +1,16 @@ + + +export function extractFromFrontmatter(frontmatter: Record, key: string): unknown { + const arrayMatch = key.match(/^(.+)\[(\d+)\]$/); + if (arrayMatch) { + const arrayKey = arrayMatch[1]; + const index = parseInt(arrayMatch[2]); + const arrayValue = frontmatter[arrayKey]; + if (Array.isArray(arrayValue) && index >= 0 && index < arrayValue.length) { + return arrayValue[index]; + } + return undefined; + } + + return frontmatter[key]; +} \ No newline at end of file diff --git a/src/views/map-bases-view.ts b/src/views/map-bases-view.ts index 15c4b00..64f5bca 100644 --- a/src/views/map-bases-view.ts +++ b/src/views/map-bases-view.ts @@ -15,9 +15,11 @@ import { createMapRenderer, MapPoint, updateMapPoints } from '../map-renderer'; import MapPlugin from '../main'; import { haveLocationsChanged } from '../pointutils'; import { currentViewState } from '../map-renderer'; -import { LatLng } from '../latlng'; +import { LatLng } from '../types/latlng'; +import { extractFromFrontmatter } from '../utils'; export const MapBasesViewType = 'map'; +export const SEARCH_DEBOUNCE_TIME = 300; const DEFAULT_MAP_HEIGHT = 400; const DEFAULT_MAP_ZOOM = 4; @@ -449,18 +451,13 @@ export class MapBasesView extends BasesView { // Fallback to global settings if coordinates property not set or failed if (this.plugin.settings.latKey && this.plugin.settings.lngKey) { try { + //TODO: use fm cache - location[0] and location[1] not accessible via entry.getValue? const fileCache = this.app.metadataCache.getFileCache(entry.file); if (fileCache?.frontmatter) { - const latValue = this.extractFromFrontmatter(fileCache.frontmatter, this.plugin.settings.latKey); - const lngValue = this.extractFromFrontmatter(fileCache.frontmatter, this.plugin.settings.lngKey); + const latValue = extractFromFrontmatter(fileCache.frontmatter, this.plugin.settings.latKey); + const lngValue = extractFromFrontmatter(fileCache.frontmatter, this.plugin.settings.lngKey); - if (latValue !== undefined && lngValue !== undefined) { - const lat = LatLng.parseCoordinate(latValue); - const lng = LatLng.parseCoordinate(lngValue); - if (lat !== null && lng !== null) { - return LatLng.from(lat, lng); - } - } + return LatLng.parse(`${latValue}, ${lngValue}`); } } catch (error) { console.error(`Error extracting coordinates from frontmatter for ${entry.file.name}:`, error); @@ -470,55 +467,40 @@ export class MapBasesView extends BasesView { return null; } - private extractFromFrontmatter(frontmatter: Record, key: string): unknown { - const arrayMatch = key.match(/^(.+)\[(\d+)\]$/); - if (arrayMatch) { - const arrayKey = arrayMatch[1]; - const index = parseInt(arrayMatch[2]); - const arrayValue = frontmatter[arrayKey]; - if (Array.isArray(arrayValue) && index >= 0 && index < arrayValue.length) { - return arrayValue[index]; - } - return undefined; - } - - // Regular property access - return frontmatter[key]; - } - private extractPolygonCoordinates(value: unknown): LatLng.Verified[] | null { try { + let items: unknown[] = []; + // Handle ListValue (array from frontmatter) if (value instanceof ListValue) { - const coords: LatLng.Verified[] = []; for (let i = 0; i < value.length(); i++) { - const coord = LatLng.parse(value.get(i)); - if (coord) coords.push(coord); + items.push(value.get(i)); } - return coords.length > 0 ? coords : null; + } + // Handle string value as JSON array + else if (value instanceof StringValue) { + const parsed = JSON.parse(value.toString().trim()); + if (Array.isArray(parsed)) { + items = parsed; + } + } + // Handle plain array + else if (Array.isArray(value)) { + items = value; } - // Handle string value as JSON array - if (value instanceof StringValue) { - try { - const parsed = JSON.parse(value.toString().trim()) as unknown[]; - if (Array.isArray(parsed)) { - const coords: LatLng.Verified[] = []; - for (const item of parsed) { - const coord = LatLng.parse(item); - if (coord) coords.push(coord); - } - return coords.length > 0 ? coords : null; - } - } catch { - // Not JSON, ignore - } + // Parse each item as a coordinate + const coords: LatLng.Verified[] = []; + for (const item of items) { + const coord = LatLng.parse(item); + if (coord) coords.push(coord); } + + return coords.length > 0 ? coords : null; } catch (error) { console.error('Error extracting polygon coordinates:', error); + return null; } - - return null; } private createSearchBox(): void { @@ -620,7 +602,7 @@ export class MapBasesView extends BasesView { searchTimeout = window.setTimeout(() => { void performSearch(query); - }, 300); + }, SEARCH_DEBOUNCE_TIME); }); // Close results when clicking outside