From f25cbae8daf85d71e78677a18516f2bfd73facd5 Mon Sep 17 00:00:00 2001 From: Liia Alice Menke Date: Thu, 6 Jun 2024 16:35:56 +0200 Subject: [PATCH] Added options to disable the display of controls and marker creation on right click --- README.md | 9 ++ src/map/map.ts | 180 +++++++++++++++++++++++---------------- src/renderer/renderer.ts | 11 ++- types/main.d.ts | 11 +++ types/map.d.ts | 11 +++ 5 files changed, 147 insertions(+), 75 deletions(-) diff --git a/README.md b/README.md index 095d6da..8f8216b 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,15 @@ darkMode: true | lock | Control whether the map will start locked or unlocked | false | | recenter | Forces map to stay re-center itself after panning. | false | | noScrollZoom | Turns off scrollwheel zooming. | false | +| disableCreateMarkerOnRightClick | Disable creation of markers when right clicking on the map | false | +| disableMapViewControl | Disables display of the map view control (Show all markers) | false | +| disableResetZoomControl | Disables display of the reset zoom control | false | +| disableFilterControl | Disables display of the filter control | false | +| disableGpxControl | Disables display of the gpx control (Zoom to GPX track) | false | +| disableLockControl | Disables display of the lock control | false | +| disableLayerControl | Disables display of the layer control | false | +| disableSaveMapControl | Disables display of the save map control | false | +| disableDistanceDisplay | Disables display of the distance widget | false | > \*: Requires the [DataView plugin](https://github.com/blacksmithgu/obsidian-dataview). diff --git a/src/map/map.ts b/src/map/map.ts index 01d9c29..74d122d 100644 --- a/src/map/map.ts +++ b/src/map/map.ts @@ -538,10 +538,15 @@ export abstract class BaseMap extends Events implements BaseMapDefinition { addLayerControl() { if (this.layerControlAdded) return; this.layerControlAdded = true; - this.filterControl?.remove(); - this.layerControl.addTo(this.leafletInstance); - this.filterControl?.addTo(this.leafletInstance); + if(!this.options.disableFilterControl) { + this.filterControl?.remove(); + this.filterControl?.addTo(this.leafletInstance); + } + if(!this.options.disableLayerControl) { + this.layerControl.addTo(this.leafletInstance); + } } + onFirstLayerReady(callback: (...args: any[]) => any) { if (this.mapLayers.length) { callback(); @@ -577,10 +582,12 @@ export abstract class BaseMap extends Events implements BaseMapDefinition { ); geo.leafletInstance.addTo(this.geojsonLayer); - this.layerControl.addOverlay( - geo.leafletInstance, - alias && alias.length ? alias : `GeoJSON ${added + 1}` - ); + if(!this.options.disableLayerControl) { + this.layerControl.addOverlay( + geo.leafletInstance, + alias && alias.length ? alias : `GeoJSON ${added + 1}` + ); + } added++; } catch (e) { @@ -614,10 +621,12 @@ export abstract class BaseMap extends Events implements BaseMapDefinition { ); gpxInstance.show(); gpxInstance.leafletInstance.addTo(this.gpxLayer); - this.layerControl.addOverlay( - gpxInstance.leafletInstance, - alias ?? `GPX ${added + 1}` - ); + if(!this.options.disableLayerControl) { + this.layerControl.addOverlay( + gpxInstance.leafletInstance, + alias ?? `GPX ${added + 1}` + ); + } added++; } catch (e) { console.error(e); @@ -629,10 +638,12 @@ export abstract class BaseMap extends Events implements BaseMapDefinition { } } - this.gpxControl = gpxControl( - { position: "bottomleft" }, - this - ).addTo(this.leafletInstance); + if(!this.options.disableGpxControl) { + this.gpxControl = gpxControl( + { position: "bottomleft" }, + this + ).addTo(this.leafletInstance); + } } if (this.geojsonData.length || this.gpxData.length) { @@ -663,7 +674,9 @@ export abstract class BaseMap extends Events implements BaseMapDefinition { pane: "image-overlay" }); - this.layerControl.addOverlay(image, overlay.alias); + if(!this.options.disableLayerControl) { + this.layerControl.addOverlay(image, overlay.alias); + } } }); } @@ -679,10 +692,12 @@ export abstract class BaseMap extends Events implements BaseMapDefinition { if (on && on == "on") { layer.addTo(this.tileOverlayLayer); } - this.layerControl.addOverlay( - layer, - name && name.length ? name : `Layer ${index}` - ); + if(!this.options.disableLayerControl) { + this.layerControl.addOverlay( + layer, + name && name.length ? name : `Layer ${index}` + ); + } } }); } @@ -721,32 +736,42 @@ export abstract class BaseMap extends Events implements BaseMapDefinition { }); } } - this.filterControl = filterMarkerControl( - { position: "topright" }, - this - ).addTo(this.leafletInstance); - this.lockControl = lockControl({ position: "topright" }, this).addTo( - this.leafletInstance - ); - zoomControl({ position: "topleft" }, this).addTo(this.leafletInstance); - resetZoomControl({ position: "topleft" }, this).addTo( - this.leafletInstance - ); - this.distanceDisplay = distanceDisplay( - { - position: "bottomleft" - }, - this - ).addTo(this.leafletInstance); + if(!this.options.disableFilterControl) { + this.filterControl = filterMarkerControl( + { position: "topright" }, + this + ).addTo(this.leafletInstance); + } + if(!this.options.disableLockControl) { + this.lockControl = lockControl({ position: "topright" }, this).addTo( + this.leafletInstance + ); + } + if(!this.options.disableMapViewControl) { + zoomControl({ position: "topleft" }, this).addTo(this.leafletInstance); + } + if(!this.options.disableResetZoomControl) { + resetZoomControl({ position: "topleft" }, this).addTo( + this.leafletInstance + ); + } + if(!this.options.disableDistanceDisplay) { + this.distanceDisplay = distanceDisplay( + { + position: "bottomleft" + }, + this + ).addTo(this.leafletInstance); + } - if (this.options.isMapView) { + if (this.options.isMapView && !this.options.disableMapViewControl) { mapViewControl( { position: "bottomright" }, this ).addTo(this.leafletInstance); - } else if (!this.options.isInitiativeView) { + } else if (!this.options.isInitiativeView && !this.options.disableSaveMapControl) { saveMapParametersControl( { position: "bottomright" @@ -765,7 +790,9 @@ export abstract class BaseMap extends Events implements BaseMapDefinition { updateLockState(state: boolean): void { this.options.lock = state; - this.lockControl.setState(this.options.lock); + if(!this.options.disableLockControl) { + this.lockControl.setState(this.options.lock); + } this.trigger("lock"); } @@ -918,7 +945,9 @@ export abstract class BaseMap extends Events implements BaseMapDefinition { ]); this.distanceTooltips.last().open(`${display} (${segment})`); - this.distanceDisplay.setText(display); + if(!this.options.disableDistanceDisplay) { + this.distanceDisplay.setText(display); + } this.distanceLines.last().redraw(); }); @@ -1080,44 +1109,47 @@ export abstract class BaseMap extends Events implements BaseMapDefinition { return; } - if (this.markerIcons.size <= 1) { - this.log( - `No additional marker types defined. Adding default marker.` - ); - this.createMarker( - this.defaultIcon.type, - [evt.latlng.lat, evt.latlng.lng], - undefined - ); - return; - } - - let contextMenu = new Menu(); - - contextMenu.setNoIcon(); - - this.log(`Opening marker context menu.`); - - this.markerIcons.forEach((marker: MarkerIcon) => { - if (!marker.type || !marker.html) return; - contextMenu.addItem((item) => { - item.setTitle( - marker.type == "default" ? "Default" : marker.type + if(!this.options.disableCreateMarkerOnRightClick) { + if (this.markerIcons.size <= 1) { + this.log( + `No additional marker types defined. Adding default marker.` ); - item.onClick(async () => { - this.log(`${marker.type} selected. Creating marker.`); - this.createMarker( - marker.type, - [evt.latlng.lat, evt.latlng.lng], - undefined + this.createMarker( + this.defaultIcon.type, + [evt.latlng.lat, evt.latlng.lng], + undefined + ); + return; + } + + let contextMenu = new Menu(); + + contextMenu.setNoIcon(); + + this.log(`Opening marker context menu.`); + + this.markerIcons.forEach((marker: MarkerIcon) => { + if (!marker.type || !marker.html) return; + contextMenu.addItem((item) => { + item.setTitle( + marker.type == "default" ? "Default" : marker.type ); - this.trigger("should-save"); + item.onClick(async () => { + this.log(`${marker.type} selected. Creating marker.`); + this.createMarker( + marker.type, + [evt.latlng.lat, evt.latlng.lng], + undefined + ); + this.trigger("should-save"); + }); }); }); - }); - - contextMenu.showAtMouseEvent(evt.originalEvent); + + contextMenu.showAtMouseEvent(evt.originalEvent); + } } + handleMapContextMobile(evt: L.LeafletMouseEvent, overlay?: Overlay) { let contextMenu = new Menu(); diff --git a/src/renderer/renderer.ts b/src/renderer/renderer.ts index 916b482..eb4cc6d 100644 --- a/src/renderer/renderer.ts +++ b/src/renderer/renderer.ts @@ -173,7 +173,16 @@ export class LeafletRenderer extends MarkdownRenderChild { verbose: this.params.verbose, zoomDelta: +this.params.zoomDelta, zoomFeatures: this.params.zoomFeatures, - zoomMarkers: this.params.showAllMarkers + zoomMarkers: this.params.showAllMarkers, + disableCreateMarkerOnRightClick: this.params.disableCreateMarkerOnRightClick, + disableMapViewControl: this.params.disableMapViewControl, + disableResetZoomControl: this.params.disableResetZoomControl, + disableFilterControl: this.params.disableFilterControl, + disableGpxControl: this.params.disableGpxControl, + disableLockControl: this.params.disableLockControl, + disableLayerControl: this.params.disableLayerControl, + disableSaveMapControl: this.params.disableSaveMapControl, + disableDistanceDisplay: this.params.disableDistanceDisplay }; this.preserveAspect = this.params.preserveAspect ?? false; diff --git a/types/main.d.ts b/types/main.d.ts index 7cf9d36..1190e97 100644 --- a/types/main.d.ts +++ b/types/main.d.ts @@ -78,5 +78,16 @@ export interface BlockParameters { recenter?: boolean; noScrollZoom?: boolean; lock?: boolean; + + disableCreateMarkerOnRightClick?: boolean; + + disableMapViewControl?: boolean; + disableResetZoomControl?: boolean; + disableFilterControl?: boolean; + disableGpxControl?: boolean; + disableLockControl?: boolean; + disableLayerControl?: boolean; + disableSaveMapControl?: boolean; + disableDistanceDisplay?: boolean; } diff --git a/types/map.d.ts b/types/map.d.ts index 5ccea87..a7d3e52 100644 --- a/types/map.d.ts +++ b/types/map.d.ts @@ -115,6 +115,17 @@ export interface LeafletMapOptions { zoomDelta?: number; zoomFeatures?: boolean; zoomMarkers?: boolean; + + disableCreateMarkerOnRightClick?: boolean; + + disableMapViewControl?: boolean; + disableResetZoomControl?: boolean; + disableFilterControl?: boolean; + disableGpxControl?: boolean; + disableLockControl?: boolean; + disableLayerControl?: boolean; + disableSaveMapControl?: boolean; + disableDistanceDisplay?: boolean; } declare class Popup {