2020-03-10 20:08:11 +00:00
|
|
|
/**
|
2020-03-11 07:13:53 +00:00
|
|
|
* Reads and writes the URL based on reveal.js' current state.
|
2020-03-10 20:08:11 +00:00
|
|
|
*/
|
|
|
|
export default class Location {
|
|
|
|
|
2022-03-09 08:44:19 +00:00
|
|
|
// The minimum number of milliseconds that must pass between
|
|
|
|
// calls to history.replaceState
|
2022-03-09 09:25:30 +00:00
|
|
|
MAX_REPLACE_STATE_FREQUENCY = 1000
|
2022-03-09 08:44:19 +00:00
|
|
|
|
2020-03-10 20:08:11 +00:00
|
|
|
constructor( Reveal ) {
|
|
|
|
|
|
|
|
this.Reveal = Reveal;
|
|
|
|
|
2020-03-10 20:30:39 +00:00
|
|
|
// Delays updates to the URL due to a Chrome thumbnailer bug
|
|
|
|
this.writeURLTimeout = 0;
|
|
|
|
|
2022-03-09 08:44:19 +00:00
|
|
|
this.replaceStateTimestamp = 0;
|
|
|
|
|
2020-04-22 09:11:14 +00:00
|
|
|
this.onWindowHashChange = this.onWindowHashChange.bind( this );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bind() {
|
|
|
|
|
|
|
|
window.addEventListener( 'hashchange', this.onWindowHashChange, false );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
unbind() {
|
|
|
|
|
|
|
|
window.removeEventListener( 'hashchange', this.onWindowHashChange, false );
|
|
|
|
|
2020-03-10 20:30:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-11-10 19:08:37 +00:00
|
|
|
* Returns the slide indices for the given hash link.
|
|
|
|
*
|
|
|
|
* @param {string} [hash] the hash string that we want to
|
|
|
|
* find the indices for
|
|
|
|
*
|
|
|
|
* @returns slide indices or null
|
2020-03-10 20:30:39 +00:00
|
|
|
*/
|
2023-01-17 08:49:49 +00:00
|
|
|
getIndicesFromHash( hash=window.location.hash, options={} ) {
|
2020-03-10 20:30:39 +00:00
|
|
|
|
|
|
|
// Attempt to parse the hash as either an index or name
|
2021-11-10 19:08:37 +00:00
|
|
|
let name = hash.replace( /^#\/?/, '' );
|
|
|
|
let bits = name.split( '/' );
|
2020-03-10 20:30:39 +00:00
|
|
|
|
|
|
|
// If the first bit is not fully numeric and there is a name we
|
|
|
|
// can assume that this is a named link
|
|
|
|
if( !/^[0-9]*$/.test( bits[0] ) && name.length ) {
|
|
|
|
let element;
|
|
|
|
|
2020-04-22 09:11:14 +00:00
|
|
|
let f;
|
|
|
|
|
|
|
|
// Parse named links with fragments (#/named-link/2)
|
|
|
|
if( /\/[-\d]+$/g.test( name ) ) {
|
|
|
|
f = parseInt( name.split( '/' ).pop(), 10 );
|
|
|
|
f = isNaN(f) ? undefined : f;
|
|
|
|
name = name.split( '/' ).shift();
|
|
|
|
}
|
|
|
|
|
2020-03-10 20:30:39 +00:00
|
|
|
// Ensure the named link is a valid HTML ID attribute
|
|
|
|
try {
|
|
|
|
element = document.getElementById( decodeURIComponent( name ) );
|
|
|
|
}
|
|
|
|
catch ( error ) { }
|
|
|
|
|
|
|
|
if( element ) {
|
2021-11-10 19:08:37 +00:00
|
|
|
return { ...this.Reveal.getIndices( element ), f };
|
2020-03-10 20:30:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2021-11-10 19:08:37 +00:00
|
|
|
const config = this.Reveal.getConfig();
|
2023-01-17 08:49:49 +00:00
|
|
|
let hashIndexBase = config.hashOneBasedIndex || options.oneBasedIndex ? 1 : 0;
|
2020-03-10 20:30:39 +00:00
|
|
|
|
|
|
|
// Read the index components of the hash
|
|
|
|
let h = ( parseInt( bits[0], 10 ) - hashIndexBase ) || 0,
|
|
|
|
v = ( parseInt( bits[1], 10 ) - hashIndexBase ) || 0,
|
|
|
|
f;
|
|
|
|
|
|
|
|
if( config.fragmentInURL ) {
|
|
|
|
f = parseInt( bits[2], 10 );
|
|
|
|
if( isNaN( f ) ) {
|
|
|
|
f = undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-10 19:08:37 +00:00
|
|
|
return { h, v, f };
|
|
|
|
}
|
|
|
|
|
|
|
|
// The hash couldn't be parsed or no matching named link was found
|
|
|
|
return null
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reads the current URL (hash) and navigates accordingly.
|
|
|
|
*/
|
|
|
|
readURL() {
|
|
|
|
|
|
|
|
const currentIndices = this.Reveal.getIndices();
|
|
|
|
const newIndices = this.getIndicesFromHash();
|
|
|
|
|
2021-11-24 10:30:12 +00:00
|
|
|
if( newIndices ) {
|
|
|
|
if( ( newIndices.h !== currentIndices.h || newIndices.v !== currentIndices.v || newIndices.f !== undefined ) ) {
|
|
|
|
this.Reveal.slide( newIndices.h, newIndices.v, newIndices.f );
|
|
|
|
}
|
2021-11-10 19:08:37 +00:00
|
|
|
}
|
2021-11-24 10:30:12 +00:00
|
|
|
// If no new indices are available, we're trying to navigate to
|
|
|
|
// a slide hash that does not exist
|
2021-11-10 19:08:37 +00:00
|
|
|
else {
|
|
|
|
this.Reveal.slide( currentIndices.h || 0, currentIndices.v || 0 );
|
2020-03-10 20:30:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the page URL (hash) to reflect the current
|
|
|
|
* state.
|
|
|
|
*
|
|
|
|
* @param {number} delay The time in ms to wait before
|
|
|
|
* writing the hash
|
|
|
|
*/
|
|
|
|
writeURL( delay ) {
|
|
|
|
|
|
|
|
let config = this.Reveal.getConfig();
|
|
|
|
let currentSlide = this.Reveal.getCurrentSlide();
|
|
|
|
|
|
|
|
// Make sure there's never more than one timeout running
|
|
|
|
clearTimeout( this.writeURLTimeout );
|
|
|
|
|
|
|
|
// If a delay is specified, timeout this call
|
|
|
|
if( typeof delay === 'number' ) {
|
|
|
|
this.writeURLTimeout = setTimeout( this.writeURL, delay );
|
|
|
|
}
|
|
|
|
else if( currentSlide ) {
|
2020-06-02 07:08:00 +00:00
|
|
|
|
|
|
|
let hash = this.getHash();
|
|
|
|
|
2020-03-10 20:30:39 +00:00
|
|
|
// If we're configured to push to history OR the history
|
2023-01-25 13:13:31 +00:00
|
|
|
// API is not available.
|
2020-06-02 07:08:00 +00:00
|
|
|
if( config.history ) {
|
|
|
|
window.location.hash = hash;
|
2020-03-10 20:30:39 +00:00
|
|
|
}
|
|
|
|
// If we're configured to reflect the current slide in the
|
|
|
|
// URL without pushing to history.
|
|
|
|
else if( config.hash ) {
|
2020-06-02 07:08:00 +00:00
|
|
|
// If the hash is empty, don't add it to the URL
|
|
|
|
if( hash === '/' ) {
|
2022-03-09 08:44:19 +00:00
|
|
|
this.debouncedReplaceState( window.location.pathname + window.location.search );
|
2020-06-02 07:08:00 +00:00
|
|
|
}
|
|
|
|
else {
|
2022-03-09 08:44:19 +00:00
|
|
|
this.debouncedReplaceState( '#' + hash );
|
2020-06-02 07:08:00 +00:00
|
|
|
}
|
2020-03-10 20:30:39 +00:00
|
|
|
}
|
2020-05-08 18:53:05 +00:00
|
|
|
// UPDATE: The below nuking of all hash changes breaks
|
|
|
|
// anchors on pages where reveal.js is running. Removed
|
|
|
|
// in 4.0. Why was it here in the first place? ¯\_(ツ)_/¯
|
|
|
|
//
|
2020-03-10 20:30:39 +00:00
|
|
|
// If history and hash are both disabled, a hash may still
|
|
|
|
// be added to the URL by clicking on a href with a hash
|
|
|
|
// target. Counter this by always removing the hash.
|
2020-05-08 18:53:05 +00:00
|
|
|
// else {
|
|
|
|
// window.history.replaceState( null, null, window.location.pathname + window.location.search );
|
|
|
|
// }
|
2020-06-02 07:08:00 +00:00
|
|
|
|
2020-03-10 20:30:39 +00:00
|
|
|
}
|
|
|
|
|
2020-03-10 20:08:11 +00:00
|
|
|
}
|
|
|
|
|
2022-03-09 08:44:19 +00:00
|
|
|
replaceState( url ) {
|
|
|
|
|
|
|
|
window.history.replaceState( null, null, url );
|
|
|
|
this.replaceStateTimestamp = Date.now();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
debouncedReplaceState( url ) {
|
|
|
|
|
|
|
|
clearTimeout( this.replaceStateTimeout );
|
|
|
|
|
|
|
|
if( Date.now() - this.replaceStateTimestamp > this.MAX_REPLACE_STATE_FREQUENCY ) {
|
|
|
|
this.replaceState( url );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.replaceStateTimeout = setTimeout( () => this.replaceState( url ), this.MAX_REPLACE_STATE_FREQUENCY );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-03-10 20:08:11 +00:00
|
|
|
/**
|
|
|
|
* Return a hash URL that will resolve to the given slide location.
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} [slide=currentSlide] The slide to link to
|
|
|
|
*/
|
2020-03-10 20:30:39 +00:00
|
|
|
getHash( slide ) {
|
2020-03-10 20:08:11 +00:00
|
|
|
|
|
|
|
let url = '/';
|
|
|
|
|
|
|
|
// Attempt to create a named link based on the slide's ID
|
2020-03-10 20:30:39 +00:00
|
|
|
let s = slide || this.Reveal.getCurrentSlide();
|
|
|
|
let id = s ? s.getAttribute( 'id' ) : null;
|
2020-03-10 20:08:11 +00:00
|
|
|
if( id ) {
|
|
|
|
id = encodeURIComponent( id );
|
|
|
|
}
|
|
|
|
|
|
|
|
let index = this.Reveal.getIndices( slide );
|
|
|
|
if( !this.Reveal.getConfig().fragmentInURL ) {
|
|
|
|
index.f = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the current slide has an ID, use that as a named link,
|
|
|
|
// but we don't support named links with a fragment index
|
2020-04-22 09:11:14 +00:00
|
|
|
if( typeof id === 'string' && id.length ) {
|
2020-03-10 20:08:11 +00:00
|
|
|
url = '/' + id;
|
2020-04-22 09:11:14 +00:00
|
|
|
|
|
|
|
// If there is also a fragment, append that at the end
|
|
|
|
// of the named link, like: #/named-link/2
|
|
|
|
if( index.f >= 0 ) url += '/' + index.f;
|
2020-03-10 20:08:11 +00:00
|
|
|
}
|
|
|
|
// Otherwise use the /h/v index
|
|
|
|
else {
|
|
|
|
let hashIndexBase = this.Reveal.getConfig().hashOneBasedIndex ? 1 : 0;
|
2020-04-22 09:11:14 +00:00
|
|
|
if( index.h > 0 || index.v > 0 || index.f >= 0 ) url += index.h + hashIndexBase;
|
|
|
|
if( index.v > 0 || index.f >= 0 ) url += '/' + (index.v + hashIndexBase );
|
|
|
|
if( index.f >= 0 ) url += '/' + index.f;
|
2020-03-10 20:08:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return url;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-04-22 09:11:14 +00:00
|
|
|
/**
|
|
|
|
* Handler for the window level 'hashchange' event.
|
|
|
|
*
|
|
|
|
* @param {object} [event]
|
|
|
|
*/
|
|
|
|
onWindowHashChange( event ) {
|
|
|
|
|
|
|
|
this.readURL();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-03-10 20:08:11 +00:00
|
|
|
}
|