Legacy url support, allow world/world+map only new urls

This commit is contained in:
James Lyne 2020-12-21 18:40:28 +00:00
parent 8e18a7393f
commit 614f0b9774
3 changed files with 68 additions and 26 deletions

View File

@ -39,8 +39,7 @@ export default defineComponent({
}, },
setup() { setup() {
const initialUrl = window.location.hash.replace('#', ''), const store = useStore(),
store = useStore(),
updateInterval = computed(() => store.state.configuration.updateInterval), updateInterval = computed(() => store.state.configuration.updateInterval),
title = computed(() => store.state.configuration.title), title = computed(() => store.state.configuration.title),
currentUrl = computed(() => store.getters.url), currentUrl = computed(() => store.getters.url),
@ -85,15 +84,10 @@ export default defineComponent({
}, },
parseUrl = () => { parseUrl = () => {
if(!initialUrl) { const parsedUrl = Util.parseUrl();
return;
}
try { if(parsedUrl) {
const result = Util.parseMapHash(initialUrl); store.commit(MutationTypes.SET_PARSED_URL, parsedUrl);
store.commit(MutationTypes.SET_PARSED_URL, result);
} catch(e) {
console.warn('Ignoring invalid url ' + e);
} }
}; };

1
src/dynmap.d.ts vendored
View File

@ -293,6 +293,7 @@ interface DynmapParsedUrl {
map?: string; map?: string;
location?: Coordinate; location?: Coordinate;
zoom?: number; zoom?: number;
legacy: boolean;
} }
interface DynmapChat { interface DynmapChat {

View File

@ -76,24 +76,39 @@ export default {
}; };
}, },
parseUrl() {
const query = new URLSearchParams(window.location.search),
hash = window.location.hash.replace('#', '');
if(hash) {
try {
return this.parseMapHash(hash);
} catch (e) {
console.warn('Ignoring invalid url ' + e);
}
}
try {
return this.parseMapSearchParams(query);
} catch(e) {
console.warn('Ignoring invalid legacy url ' + e);
}
return null;
},
parseMapHash(hash: string) { parseMapHash(hash: string) {
const parts = hash.replace('#', '').split(';'); const parts = hash.replace('#', '').split(';');
if(parts.length < 3) { const world = parts[0] || undefined,
throw new TypeError('Not enough parts'); map = parts[1] || undefined,
} location = (parts[2] || '').split(',')
.map(item => parseFloat(item))
.filter(item => !isNaN(item) && isFinite(item)),
zoom = typeof parts[3] !== 'undefined' ? parseInt(parts[3]) : undefined;
const world = parts[0], if(location.length && location.length !== 3) {
map = parts[1], throw new TypeError('Location should contain exactly 3 valid numbers');
location = parts[2].split(',').map(item => parseFloat(item)),
zoom = parts[3] ? parseInt(parts[3]) : undefined;
if(location.length !== 3) {
throw new TypeError('Location should contain exactly 3 numbers');
}
if(location.filter(item => isNaN(item) || !isFinite(item)).length) {
throw new TypeError('Invalid value in location');
} }
if(typeof zoom !== 'undefined' && (isNaN(zoom) || zoom < 0 || !isFinite(zoom))) { if(typeof zoom !== 'undefined' && (isNaN(zoom) || zoom < 0 || !isFinite(zoom))) {
@ -103,12 +118,44 @@ export default {
return { return {
world, world,
map, map,
location: { location: location.length ? {
x: location[0], x: location[0],
y: location[1], y: location[1],
z: location[2], z: location[2],
}, } : undefined,
zoom, zoom,
legacy: false,
}
},
parseMapSearchParams(query: URLSearchParams) {
const world = query.get('worldname') || undefined,
map = query.get('mapname') || undefined,
location = [
query.get('x') || '',
query.get('y') || '',
query.get('z') || ''
].map(item => parseFloat(item)).filter(item => !isNaN(item) && isFinite(item)),
zoom = query.has('zoom') ? parseInt(query.get('zoom') as string) : undefined;
if(location.length && location.length !== 3) {
throw new TypeError('Location should contain exactly 3 valid numbers');
}
if(typeof zoom !== 'undefined' && (isNaN(zoom) || zoom < 0 || !isFinite(zoom))) {
throw new TypeError('Invalid value for zoom');
}
return {
world,
map,
location: location.length ? {
x: location[0],
y: location[1],
z: location[2],
} : undefined,
zoom,
legacy: true,
} }
} }
} }