Live-update url (fixes #3905)

live-updates url after moving map
This commit is contained in:
k26pl 2024-02-27 23:33:01 +01:00 committed by GitHub
parent ea24554033
commit 1a7e702a39
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -41,6 +41,8 @@ componentconstructors['coord'] = function(dynmap, configuration) {
var coord = new Coord();
dynmap.map.addControl(coord);
var prev=[0,0];
var update_url=true;
dynmap.map.on('mousemove', function(mevent) {
if(!dynmap.map) return;
var loc = dynmap.getProjection().fromLatLngToLocation(mevent.latlng, dynmap.world.sealevel+1);
@ -52,6 +54,32 @@ componentconstructors['coord'] = function(dynmap, configuration) {
coord.mcrfield.text('r.' + Math.floor(loc.x/512) + '.' + Math.floor(loc.z/512) + '.mca');
if(configuration['show-chunk'])
coord.chunkfield.text('Chunk: ' + Math.floor(loc.x/16) + ',' + Math.floor(loc.z/16));
if(update_url){
var center = dynmap.maptype.getProjection().fromLatLngToLocation(dynmap.map.getCenter(), 64);
if(
prev[0]==center.x &&
prev[1]==center.z
)
return;
prev[0]=center.x;
prev[1]=center.y;
var url = window.location.pathname;
if(dynmap.options['round-coordinates'])
url = url + "?worldname=" + dynmap.world.name + "&mapname=" + dynmap.maptype.options.name + "&zoom=" + dynmap.map.getZoom() + "&x=" + center.x + "&y=" +
center.y + "&z=" + center.z;
else
url = url + "?worldname=" + dynmap.world.name + "&mapname=" + dynmap.maptype.options.name + "&zoom=" + dynmap.map.getZoom() + "&x=" +
Math.round(center.x) + "&y=" + Math.round(center.y) + "&z=" + Math.round(center.z);
// history api can send error because it is called too many times
try{
history.replaceState(null,"",url);
}catch(e){}
// prevent calling the api too often
update_url=false;
setTimeout(()=>{
update_url=true;
},100)
}
});
dynmap.map.on('mouseout', function(mevent) {
if(!dynmap.map) return;