diff --git a/frontend/react/src/events.ts b/frontend/react/src/events.ts index b8693d13..a6f4f9f3 100644 --- a/frontend/react/src/events.ts +++ b/frontend/react/src/events.ts @@ -892,3 +892,19 @@ export class WeaponsRefreshedEvent { if (DEBUG) console.log(`Event ${this.name} dispatched`); } } + +export class CoordinatesFreezeEvent { + static on(callback: () => void) { + document.addEventListener( + this.name, + (ev: CustomEventInit) => { + callback(); + } + ) + } + + static dispatch() { + document.dispatchEvent(new CustomEvent(this.name)); + if (DEBUG) console.log(`Event ${this.name} dispatched`); + } +} diff --git a/frontend/react/src/map/map.ts b/frontend/react/src/map/map.ts index 48ec8bde..d146c1c9 100644 --- a/frontend/react/src/map/map.ts +++ b/frontend/react/src/map/map.ts @@ -49,6 +49,7 @@ import { ConfigLoadedEvent, ContextActionChangedEvent, ContextActionSetChangedEvent, + CoordinatesFreezeEvent, HiddenTypesChangedEvent, MapContextMenuRequestEvent, MapOptionsChangedEvent, @@ -509,7 +510,7 @@ export class Map extends L.Map { code: "ShiftLeft", altKey: false, ctrlKey: false, - }); + }) } setLayerName(layerName: string) { @@ -1031,6 +1032,7 @@ export class Map extends L.Map { } #onLeftShortClick(e: L.LeafletMouseEvent) { + CoordinatesFreezeEvent.dispatch(); if (Date.now() - this.#leftMouseDownEpoch < SHORT_PRESS_MILLISECONDS) { this.#debounceTimeout = window.setTimeout(() => { if (!this.#isSelecting) { diff --git a/frontend/react/src/other/utils.ts b/frontend/react/src/other/utils.ts index d2b01d07..4b39e816 100644 --- a/frontend/react/src/other/utils.ts +++ b/frontend/react/src/other/utils.ts @@ -769,4 +769,8 @@ export function secondsToTimeString(seconds: number) { const secs = Math.floor(seconds % 60); return `${zeroPad(hours, 2)}:${zeroPad(minutes, 2)}:${zeroPad(secs, 2)}`; +} + +export function isTrustedEnvironment() { + return window.location.protocol === "https:"; } \ No newline at end of file diff --git a/frontend/react/src/ui/components/ollocation.tsx b/frontend/react/src/ui/components/ollocation.tsx index f6a76ef2..f5ab162a 100644 --- a/frontend/react/src/ui/components/ollocation.tsx +++ b/frontend/react/src/ui/components/ollocation.tsx @@ -2,7 +2,7 @@ import React, { useState } from "react"; import { LatLng } from "leaflet"; import { ConvertDDToDMS, DDToDDM, latLngToMGRS, latLngToUTM, zeroAppend } from "../../other/utils"; -export function OlLocation(props: { location: LatLng; className?: string; referenceSystem?: string; onClick?: () => void }) { +export function OlLocation(props: { location: LatLng; className?: string; referenceSystem?: string; onClick?: () => void; onRefSystemChange?: (refSystem: string) => any }) { const [referenceSystem, setReferenceSystem] = props.referenceSystem ? [props.referenceSystem, () => {}] : useState("LatLngDec"); const MGRS = latLngToMGRS(props.location.lat, props.location.lng, 6); if (referenceSystem === "MGRS") { @@ -17,6 +17,7 @@ export function OlLocation(props: { location: LatLng; className?: string; refere ? props.onClick : (ev) => { setReferenceSystem("LatLngDec"); + props.onRefSystemChange ? props.onRefSystemChange("LatLngDec") : null; ev.stopPropagation(); } } @@ -44,6 +45,7 @@ export function OlLocation(props: { location: LatLng; className?: string; refere ? props.onClick : (ev) => { setReferenceSystem("LatLngDMS"); + props.onRefSystemChange ? props.onRefSystemChange("LatLngDMS") : null; ev.stopPropagation(); } } @@ -83,6 +85,7 @@ export function OlLocation(props: { location: LatLng; className?: string; refere ? props.onClick : (ev) => { setReferenceSystem("LatLngDDM"); + props.onRefSystemChange ? props.onRefSystemChange("LatLngDDM") : null; ev.stopPropagation(); } } @@ -122,6 +125,7 @@ export function OlLocation(props: { location: LatLng; className?: string; refere ? props.onClick : (ev) => { setReferenceSystem("MGRS"); + props.onRefSystemChange ? props.onRefSystemChange("MGRS") : null; ev.stopPropagation(); } } diff --git a/frontend/react/src/ui/panels/coordinatespanel.tsx b/frontend/react/src/ui/panels/coordinatespanel.tsx index c1fe6dee..06489307 100644 --- a/frontend/react/src/ui/panels/coordinatespanel.tsx +++ b/frontend/react/src/ui/panels/coordinatespanel.tsx @@ -1,11 +1,12 @@ -import React, { useEffect, useState } from "react"; +import React, { useEffect, useRef, useState } from "react"; import { OlLocation } from "../components/ollocation"; import { LatLng } from "leaflet"; -import { FaBullseye, FaChevronDown, FaChevronUp, FaJetFighter, FaMountain } from "react-icons/fa6"; -import { BullseyesDataChangedEvent, MouseMovedEvent, SelectedUnitsChangedEvent, SelectionClearedEvent } from "../../events"; -import { computeBearingRangeString, mToFt } from "../../other/utils"; +import { FaBullseye, FaChevronDown, FaChevronUp, FaJetFighter, FaMountain, FaCopy, FaXmark } from "react-icons/fa6"; +import { BullseyesDataChangedEvent, CoordinatesFreezeEvent, MouseMovedEvent, SelectedUnitsChangedEvent, SelectionClearedEvent } from "../../events"; +import { computeBearingRangeString, ConvertDDToDMS, DDToDDM, isTrustedEnvironment, latLngToMGRS, mToFt, zeroAppend } from "../../other/utils"; import { Bullseye } from "../../mission/bullseye"; import { Unit } from "../../unit/unit"; +import { getApp } from "../../olympusapp"; export function CoordinatesPanel(props: {}) { const [latlng, setLatlng] = useState(new LatLng(0, 0)); @@ -13,7 +14,10 @@ export function CoordinatesPanel(props: {}) { const [bullseyes, setBullseyes] = useState(null as null | { [name: string]: Bullseye }); const [selectedUnits, setSelectedUnits] = useState([] as Unit[]); const [open, setOpen] = useState(true); - + const [copyCoordsOpen, setCopyCoordsOpen] = useState(false); + const [refSystem, setRefSystem] = useState("LatLngDec"); + const [copyableCoordinates, setCopyableCoordinates] = useState("To start, click any point on the map."); + useEffect(() => { MouseMovedEvent.on((latlng, elevation) => { setLatlng(latlng); @@ -23,18 +27,43 @@ export function CoordinatesPanel(props: {}) { BullseyesDataChangedEvent.on((bullseyes) => setBullseyes(bullseyes)); SelectedUnitsChangedEvent.on((selectedUnits) => setSelectedUnits(selectedUnits)); SelectionClearedEvent.on(() => setSelectedUnits([])); - }, []); + CoordinatesFreezeEvent.on( () => { + setCopyableCoordinates(getCopyableCoordinates()); + }); + }, [refSystem, latlng, elevation]); + + const getCopyableCoordinates = () => { + let returnString = ''; + + switch (refSystem) { + case "LatLngDec": + returnString = `${latlng.lat >= 0 ? "N" : "S"} ${zeroAppend(latlng.lat, 3, true, 6)}°, ${latlng.lng >= 0 ? "E" : "W"} ${zeroAppend(latlng.lng, 3, true, 6)}°,` + break; + case "LatLngDMS": + returnString = `${latlng.lat >= 0 ? "N" : "S"} ${ConvertDDToDMS(latlng.lat, false)}, ${latlng.lng >= 0 ? "E" : "W"} ${ConvertDDToDMS(latlng.lng, false)},` + break; + case "LatLngDDM": + returnString = `${latlng.lat >= 0 ? "N" : "S"} ${DDToDDM(latlng.lat)}, ${latlng.lng >= 0 ? "E" : "W"} ${DDToDDM(latlng.lng)}`; + break; + case "MGRS": + returnString = latLngToMGRS(latlng.lat, latlng.lng, 6)?.string || "Error"; + break; + } + + returnString += ` Elevation: ${Math.round(elevation)}`; + + return returnString; + }; return (
or
{" "} @@ -840,9 +830,7 @@ export function UnitControlMenu(props: { open: boolean; onClose: () => void }) { {" "}