mirror of
https://github.com/ccmdi/obsidian-map-plus.git
synced 2026-07-22 06:43:14 +00:00
refactor: debloat main base view
This commit is contained in:
parent
f1ed5123fa
commit
5998c99cbe
5 changed files with 54 additions and 51 deletions
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
16
src/utils.ts
Normal file
16
src/utils.ts
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
|
||||
|
||||
export function extractFromFrontmatter(frontmatter: Record<string, unknown>, 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];
|
||||
}
|
||||
|
|
@ -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<string, unknown>, 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
|
||||
|
|
|
|||
Loading…
Reference in a new issue