Cleanup App.vue and parseUrl

- Fix double event listener add in unmount hook
- Remove onUrlChange as it does the same thing as handleUrl
- Merge duplicate mount/unmount hooks
- Pass a URL to parseUrl instead of directly using window.location
This commit is contained in:
James Lyne 2021-09-10 15:29:27 +01:00
parent 969e9db5cc
commit f90b32c41a

View File

@ -103,9 +103,9 @@ const tickHeadQueue = () => {
tickHeadQueue(); tickHeadQueue();
} }
export const parseUrl = () => { export const parseUrl = (url: URL) => {
const query = new URLSearchParams(window.location.search), const query = new URLSearchParams(url.search),
hash = window.location.hash.replace('#', ''); hash = url.hash.replace('#', '');
if(hash) { if(hash) {
try { try {
@ -128,18 +128,21 @@ export const parseMapHash = (hash: string) => {
const parts = hash.replace('#', '').split(';'); const parts = hash.replace('#', '').split(';');
const world = parts[0] || undefined, const world = parts[0] || undefined,
map = parts[1] || undefined, map = parts[1] || undefined;
location = (parts[2] || '').split(',')
let location = (parts[2] || '').split(',')
.map(item => parseFloat(item)) .map(item => parseFloat(item))
.filter(item => !isNaN(item) && isFinite(item)), .filter(item => !isNaN(item) && isFinite(item)),
zoom = typeof parts[3] !== 'undefined' ? parseInt(parts[3]) : undefined; zoom = typeof parts[3] !== 'undefined' ? parseInt(parts[3]) : undefined;
if(location.length && location.length !== 3) { if(location.length && location.length !== 3) {
throw new TypeError('Location should contain exactly 3 valid numbers'); location = [];
console.warn('Ignoring invalid URL location');
} }
if(typeof zoom !== 'undefined' && (isNaN(zoom) || zoom < 0 || !isFinite(zoom))) { if(typeof zoom !== 'undefined' && (isNaN(zoom) || zoom < 0 || !isFinite(zoom))) {
throw new TypeError('Invalid value for zoom'); zoom = undefined;
console.warn('Ignoring invalid URL value for zoom');
} }
return { return {
@ -157,8 +160,9 @@ export const parseMapHash = (hash: string) => {
export const parseMapSearchParams = (query: URLSearchParams) => { export const parseMapSearchParams = (query: URLSearchParams) => {
const world = query.get('worldname') /* Dynmap */ || query.get('world') /* Pl3xmap */ || undefined, const world = query.get('worldname') /* Dynmap */ || query.get('world') /* Pl3xmap */ || undefined,
map = query.get('mapname') || undefined, map = query.get('mapname') || undefined;
location = [
let location = [
query.get('x') || '', query.get('x') || '',
query.get('y') || '64', query.get('y') || '64',
query.get('z') || '' query.get('z') || ''
@ -166,11 +170,13 @@ export const parseMapSearchParams = (query: URLSearchParams) => {
zoom = query.has('zoom') ? parseInt(query.get('zoom') as string) : undefined; zoom = query.has('zoom') ? parseInt(query.get('zoom') as string) : undefined;
if(location.length && location.length !== 3) { if(location.length && location.length !== 3) {
throw new TypeError('Location should contain exactly 3 valid numbers'); location = [];
console.warn('Ignoring invalid URL location');
} }
if(typeof zoom !== 'undefined' && (isNaN(zoom) || zoom < 0 || !isFinite(zoom))) { if(typeof zoom !== 'undefined' && (isNaN(zoom) || zoom < 0 || !isFinite(zoom))) {
throw new TypeError('Invalid value for zoom'); zoom = undefined;
console.warn('Ignoring invalid URL value for zoom');
} }
return { return {