notes.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /**
  2. * Handles opening of and synchronization with the reveal.js
  3. * notes window.
  4. */
  5. var RevealNotes = (function() {
  6. function openNotes() {
  7. var jsFileLocation = document.querySelector('script[src$="notes.js"]').src; // this js file path
  8. jsFileLocation = jsFileLocation.replace(/notes\.js(\?.*)?$/, ''); // the js folder path
  9. var notesPopup = window.open( jsFileLocation + 'notes.html', 'reveal.js - Notes', 'width=1120,height=850' );
  10. // Fires when slide is changed
  11. Reveal.addEventListener( 'slidechanged', post );
  12. // Fires when a fragment is shown
  13. Reveal.addEventListener( 'fragmentshown', post );
  14. // Fires when a fragment is hidden
  15. Reveal.addEventListener( 'fragmenthidden', post );
  16. /**
  17. * Posts the current slide data to the notes window
  18. */
  19. function post() {
  20. var slideElement = Reveal.getCurrentSlide(),
  21. slideIndices = Reveal.getIndices(),
  22. messageData;
  23. var notes = slideElement.querySelector( 'aside.notes' ),
  24. nextindexh,
  25. nextindexv;
  26. if( slideElement.nextElementSibling && slideElement.parentNode.nodeName == 'SECTION' ) {
  27. nextindexh = slideIndices.h;
  28. nextindexv = slideIndices.v + 1;
  29. } else {
  30. nextindexh = slideIndices.h + 1;
  31. nextindexv = 0;
  32. }
  33. messageData = {
  34. notes : notes ? notes.innerHTML : '',
  35. indexh : slideIndices.h,
  36. indexv : slideIndices.v,
  37. indexf : slideIndices.f,
  38. nextindexh : nextindexh,
  39. nextindexv : nextindexv,
  40. markdown : notes ? typeof notes.getAttribute( 'data-markdown' ) === 'string' : false
  41. };
  42. notesPopup.postMessage( JSON.stringify( messageData ), '*' );
  43. }
  44. // Navigate to the current slide when the notes are loaded
  45. notesPopup.addEventListener( 'load', function( event ) {
  46. post();
  47. }, false );
  48. }
  49. // If the there's a 'notes' query set, open directly
  50. if( window.location.search.match( /(\?|\&)notes/gi ) !== null ) {
  51. openNotes();
  52. }
  53. // Open the notes when the 's' key is hit
  54. document.addEventListener( 'keydown', function( event ) {
  55. // Disregard the event if the target is editable or a
  56. // modifier is present
  57. if ( document.querySelector( ':focus' ) !== null || event.shiftKey || event.altKey || event.ctrlKey || event.metaKey ) return;
  58. if( event.keyCode === 83 ) {
  59. event.preventDefault();
  60. openNotes();
  61. }
  62. }, false );
  63. return { open: openNotes };
  64. })();