[bug] somehow "=" starts getting added to hashes, add some quick handling

This commit is contained in:
Andrea 2022-04-26 20:53:15 +02:00
parent 7109961d48
commit 483b479890
1 changed files with 13 additions and 8 deletions

View File

@ -1,12 +1,15 @@
export default { export default {
methods: { methods: {
getHash() {
if (!process.client) { return null; }
return decodeURIComponent(window.location.hash.substr(1).replace(/=$/, ''));
},
handleHash(namespace, callback, checkAnchor = true) { handleHash(namespace, callback, checkAnchor = true) {
if (!process.client) { if (!process.client) { return; }
return;
}
const doHandle = () => { const doHandle = () => {
const anchor = decodeURIComponent(window.location.hash.substr(1)); const anchor = this.getHash();
const $anchor = document.getElementById(anchor); const $anchor = document.getElementById(anchor);
if (checkAnchor && $anchor) { if (checkAnchor && $anchor) {
$anchor.scrollIntoView(); $anchor.scrollIntoView();
@ -25,15 +28,17 @@ export default {
}, 500); }, 500);
}, },
setHash(namespace, value) { setHash(namespace, value) {
if (!process.client) { if (!process.client) { return; }
return; const current = this.getHash();
} const fullValue = namespace ? namespace + '/' + value : value
if (fullValue === current) { return; }
history.pushState( history.pushState(
'', '',
document.title, document.title,
window.location.pathname + window.location.search window.location.pathname + window.location.search
+ (value ? '#' + (namespace ? namespace + '/' + value : value) : '') + (value ? '#' + fullValue : '')
); );
}, },
} }