location.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { enterFullscreen } from '../utils/util.js'
  2. /**
  3. * Handles all reveal.js keyboard interactions.
  4. */
  5. export default class Location {
  6. constructor( Reveal ) {
  7. this.Reveal = Reveal;
  8. }
  9. /**
  10. * Return a hash URL that will resolve to the given slide location.
  11. *
  12. * @param {HTMLElement} [slide=currentSlide] The slide to link to
  13. */
  14. getHash( slide = this.Reveal.getCurrentSlide() ) {
  15. let url = '/';
  16. // Attempt to create a named link based on the slide's ID
  17. let id = slide ? slide.getAttribute( 'id' ) : null;
  18. if( id ) {
  19. id = encodeURIComponent( id );
  20. }
  21. let index = this.Reveal.getIndices( slide );
  22. if( !this.Reveal.getConfig().fragmentInURL ) {
  23. index.f = undefined;
  24. }
  25. // If the current slide has an ID, use that as a named link,
  26. // but we don't support named links with a fragment index
  27. if( typeof id === 'string' && id.length && index.f === undefined ) {
  28. url = '/' + id;
  29. }
  30. // Otherwise use the /h/v index
  31. else {
  32. let hashIndexBase = this.Reveal.getConfig().hashOneBasedIndex ? 1 : 0;
  33. if( index.h > 0 || index.v > 0 || index.f !== undefined ) url += index.h + hashIndexBase;
  34. if( index.v > 0 || index.f !== undefined ) url += '/' + (index.v + hashIndexBase );
  35. if( index.f !== undefined ) url += '/' + index.f;
  36. }
  37. return url;
  38. }
  39. }