import { toLonLat } from 'ol/proj'; import React, { FC, useMemo, useCallback } from 'react'; import { StandardEditorProps, SelectableValue } from '@grafana/data'; import { Button, InlineField, InlineFieldRow, Select, VerticalGroup } from '@grafana/ui'; import { NumberInput } from 'app/core/components/OptionsUI/NumberInput'; import { GeomapPanelOptions, MapViewConfig, GeomapInstanceState } from '../types'; import { centerPointRegistry, MapCenterID } from '../view'; export const MapViewEditor: FC< StandardEditorProps > = ({ value, onChange, context }) => { const labelWidth = 10; const views = useMemo(() => { const ids: string[] = []; if (value?.id) { ids.push(value.id); } else { ids.push(centerPointRegistry.list()[0].id); } return centerPointRegistry.selectOptions(ids); }, [value?.id]); const onSetCurrentView = useCallback(() => { const map = context.instanceState?.map; if (map) { const view = map.getView(); const coords = view.getCenter(); if (coords) { const center = toLonLat(coords, view.getProjection()); onChange({ ...value, id: MapCenterID.Coordinates, lon: +center[0].toFixed(6), lat: +center[1].toFixed(6), zoom: +view.getZoom()!.toFixed(2), }); } } }, [value, onChange, context.instanceState]); const onSelectView = useCallback( (selection: SelectableValue) => { const v = centerPointRegistry.getIfExists(selection.value); if (v) { onChange({ ...value, id: v.id, lat: v.lat ?? value?.lat, lon: v.lon ?? value?.lon, zoom: v.zoom ?? value?.zoom, }); } }, [value, onChange] ); return ( <>