18x
Version:
util js for build web site
1 lines • 864 B
JavaScript
var e,t;import r from"x/On.js";export const nowUrl=()=>location.pathname.slice(1);t=nowUrl(),location.hash&&(t+=location.hash),e=[];export default(t=>(e.push(t),()=>{e=e.filter(e=>e!==t)}));export const setPre=e=>{t=e};export const preUrl=()=>t;export const refresh=r=>{var o;for(o of e)o(r,t);setPre(r)};export const removeSlash=e=>("/"===e[0]&&(e=e.slice(1)),e);export const split=(e,t)=>{var r,o;return(r=e.indexOf(t))>=0?(o=e.slice(r+1),e=e.slice(0,r)):o="",[e,o]};export const setUrl=e=>{var r,o,s,n;if((e=removeSlash(e))!==t){if([n,o]=split(e,"#"),[s,r]=split(t,"#"),setPre(e),n!==s)return history.pushState(null,"","/"+e),1;if(location.hash.slice(1)!==o){location.hash=o;return}}window.dispatchEvent(new HashChangeEvent("hashchange"))};export const goto=e=>{setUrl(e)&&refresh(e)};r(window,{popstate:()=>{var e;(e=nowUrl())!==split(t,"#")[0]&&refresh(e)}});