Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 42 additions & 9 deletions src/map-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { rtlPluginCode } from './map/rtl-plugin-code';

interface MapConfig {
coordinatesProp: BasesPropertyId | null;
zoomProp: BasesPropertyId | null;
markerIconProp: BasesPropertyId | null;
markerColorProp: BasesPropertyId | null;
mapHeight: number;
Expand Down Expand Up @@ -237,7 +238,6 @@ export class MapView extends BasesView {
if (isRestoringState || this.pendingMapState) return;

const hasConfiguredCenter = this.mapConfig.center[0] !== 0 || this.mapConfig.center[1] !== 0;
const hasConfiguredZoom = this.config.get('defaultZoom') && Number.isNumber(this.config.get('defaultZoom'));

// Set center based on configuration
if (hasConfiguredCenter) {
Expand All @@ -250,15 +250,27 @@ export class MapView extends BasesView {
}
}

// Set zoom based on configuration
if (hasConfiguredZoom) {
this.map.setZoom(this.mapConfig.defaultZoom); // Use configured zoom
}
else {
const bounds = this.markerManager.getBounds();
if (bounds) {
this.map.fitBounds(bounds, { padding: 20 }); // Fit all markers
const zoomStrategy = this.config.get('zoomStrategy');

// Set the zoom based on the selected strategy
switch (zoomStrategy) {
case 'bounds': {
const bounds = this.markerManager.getBounds();

if (bounds) {
this.map.fitBounds(bounds, { padding: 20 });
}

break;
}

case 'properties':
this.map.setZoom(this.markerManager.getWidestMarkerZoom() ?? DEFAULT_MAP_ZOOM);
break;

default:
this.map.setZoom(this.mapConfig.defaultZoom);
break;
}
});

Expand Down Expand Up @@ -440,6 +452,7 @@ export class MapView extends BasesView {
private loadConfig(currentTileSetId: string | null): MapConfig {
// Load property configurations
const coordinatesProp = this.config.getAsPropertyId('coordinates');
const zoomProp = this.config.getAsPropertyId('zoom');
const markerIconProp = this.config.getAsPropertyId('markerIcon');
const markerColorProp = this.config.getAsPropertyId('markerColor');

Expand Down Expand Up @@ -491,6 +504,7 @@ export class MapView extends BasesView {

return {
coordinatesProp,
zoomProp,
markerIconProp,
markerColorProp,
mapHeight,
Expand Down Expand Up @@ -699,6 +713,25 @@ export class MapView extends BasesView {
key: 'center',
placeholder: '[latitude, longitude]',
},
{
displayName: 'Zoom strategy',
type: 'dropdown',
key: 'zoomStrategy',
default: '',
options: {
'': 'Use default zoom',
'bounds': 'Use marker bounds',
'properties': 'Use widest zoom defined by markers',
},
},
{
displayName: 'Zoom property',
type: 'property',
key: 'zoom',
filter: prop => !prop.startsWith('file.'),
placeholder: 'Property',
shouldHide: (config) => config.get('zoomStrategy') !== 'properties',
},
{
displayName: 'Default zoom',
type: 'slider',
Expand Down
20 changes: 20 additions & 0 deletions src/map/markers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ export class MarkerManager {
return this.bounds;
}

getWidestMarkerZoom(): number | undefined {
const validZoomLevels = this.markers
.map(marker => marker.zoom)
.filter((z): z is number => z !== undefined);

if (validZoomLevels.length === 0) return undefined;

return Math.min(...validZoomLevels);
}

clearLoadedIcons(): void {
this.loadedIcons.clear();
}
Expand All @@ -63,6 +73,7 @@ export class MarkerManager {
if (!entry) continue;

let coordinates: [number, number] | null = null;
let zoom: number | undefined = undefined;
try {
const value = entry.getValue(mapConfig.coordinatesProp);
coordinates = coordinateFromValue(value);
Expand All @@ -71,10 +82,19 @@ export class MarkerManager {
console.error(`Error extracting coordinates for ${entry.file.name}:`, error);
}

try {
const value = entry.getValue(mapConfig.zoomProp);
zoom = value !== undefined ? Number(value) : undefined;
}
catch (error) {
console.error(`Error extracting zoom for ${entry.file.name}:`, error);
}

if (coordinates) {
validMarkers.push({
entry,
coordinates,
zoom,
});
}
}
Expand Down
1 change: 1 addition & 0 deletions src/map/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { BasesEntry } from 'obsidian';
export interface MapMarker {
entry: BasesEntry;
coordinates: [number, number];
zoom?: number;
}

export interface MapMarkerProperties {
Expand Down