-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Save/Load location from local storage
Fixes #187
- Loading branch information
Showing
4 changed files
with
104 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
export const locationParameter = "map"; | ||
|
||
export interface MapLocation { | ||
zoom: number; | ||
latitude: number; | ||
longitude: number; | ||
} | ||
|
||
export function getMapLocation(): MapLocation { | ||
const defaultLocation: MapLocation = { | ||
zoom: 3, | ||
latitude: 47.74, | ||
longitude: -8, | ||
}; | ||
return ( | ||
getLocationFromUrl() ?? getLocationFromLocalStorage() ?? defaultLocation | ||
); | ||
} | ||
|
||
function getLocationStringFromUrl(): string | null { | ||
const paramsFromHash = parseParametersFromUrl(); | ||
return paramsFromHash[locationParameter] ?? null; | ||
} | ||
|
||
function getLocationFromUrl(): MapLocation | null { | ||
const locationString = getLocationStringFromUrl(); | ||
return locationString !== null | ||
? parseLocationFromString(locationString) | ||
: null; | ||
} | ||
|
||
function getLocationFromLocalStorage(): MapLocation | null { | ||
const locationString = localStorage.getItem(locationParameter); | ||
return locationString !== null | ||
? parseLocationFromString(locationString) | ||
: null; | ||
} | ||
|
||
function parseLocationFromString(locationString: string): MapLocation | null { | ||
try { | ||
const [zoom, latitude, longitude] = locationString.split("/").map(Number); | ||
return { zoom, latitude, longitude }; | ||
} catch (e) { | ||
return null; | ||
} | ||
} | ||
|
||
export function parseParametersFromUrl(): Record<string, string> { | ||
const parameters: Record<string, string> = {}; | ||
for (const part of window.location.hash.slice(1).split("&")) { | ||
const [key, value] = part.split("=", 2); | ||
parameters[key] = value; | ||
} | ||
return parameters; | ||
} | ||
|
||
function serializeParametersToUrlTarget(parameters: Record<string, string>) { | ||
return Object.entries(parameters) | ||
.map(([key, value]) => `${key}=${value}`) | ||
.join("&"); | ||
} | ||
|
||
export function removeNodeIdFromHash() { | ||
const params = parseParametersFromUrl(); | ||
// biome-ignore lint/performance/noDelete: using undefined assignment causes it to be part of url | ||
delete params.node_id; | ||
window.location.hash = serializeParametersToUrlTarget(params); | ||
} | ||
|
||
export function addNodeIdToHash(nodeId: string) { | ||
const params = parseParametersFromUrl(); | ||
params.node_id = nodeId; | ||
window.location.hash = serializeParametersToUrlTarget(params); | ||
} | ||
|
||
export function saveLocationToLocalStorage() { | ||
const location = getLocationStringFromUrl(); | ||
if (location !== null) { | ||
localStorage.setItem(locationParameter, location ?? ""); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters