notes.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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', function( event ) {
  12. post('slidechanged');
  13. } );
  14. // Fires when a fragment is shown
  15. Reveal.addEventListener( 'fragmentshown', function( event ) {
  16. post('fragmentshown');
  17. } );
  18. // Fires when a fragment is hidden
  19. Reveal.addEventListener( 'fragmenthidden', function( event ) {
  20. post('fragmenthidden');
  21. } );
  22. /**
  23. * Posts the current slide data to the notes window
  24. *
  25. * @param {String} eventType Expecting 'slidechanged', 'fragmentshown'
  26. * or 'fragmenthidden' set in the events above to define the needed
  27. * slideDate.
  28. */
  29. function post( eventType ) {
  30. var slideElement = Reveal.getCurrentSlide(),
  31. messageData;
  32. if( eventType === 'slidechanged' ) {
  33. var notes = slideElement.querySelector( 'aside.notes' ),
  34. indexh = Reveal.getIndices().h,
  35. indexv = Reveal.getIndices().v,
  36. nextindexh,
  37. nextindexv;
  38. if( slideElement.nextElementSibling && slideElement.parentNode.nodeName == 'SECTION' ) {
  39. nextindexh = indexh;
  40. nextindexv = indexv + 1;
  41. } else {
  42. nextindexh = indexh + 1;
  43. nextindexv = 0;
  44. }
  45. messageData = {
  46. notes : notes ? notes.innerHTML : '',
  47. indexh : indexh,
  48. indexv : indexv,
  49. nextindexh : nextindexh,
  50. nextindexv : nextindexv,
  51. markdown : notes ? typeof notes.getAttribute( 'data-markdown' ) === 'string' : false
  52. };
  53. }
  54. else if( eventType === 'fragmentshown' ) {
  55. messageData = {
  56. fragment : 'next'
  57. };
  58. }
  59. else if( eventType === 'fragmenthidden' ) {
  60. messageData = {
  61. fragment : 'prev'
  62. };
  63. }
  64. notesPopup.postMessage( JSON.stringify( messageData ), '*' );
  65. }
  66. // Navigate to the current slide when the notes are loaded
  67. notesPopup.addEventListener( 'load', function( event ) {
  68. post('slidechanged');
  69. }, false );
  70. }
  71. // If the there's a 'notes' query set, open directly
  72. if( window.location.search.match( /(\?|\&)notes/gi ) !== null ) {
  73. openNotes();
  74. }
  75. // Open the notes when the 's' key is hit
  76. document.addEventListener( 'keydown', function( event ) {
  77. // Disregard the event if the target is editable or a
  78. // modifier is present
  79. if ( document.querySelector( ':focus' ) !== null || event.shiftKey || event.altKey || event.ctrlKey || event.metaKey ) return;
  80. if( event.keyCode === 83 ) {
  81. event.preventDefault();
  82. openNotes();
  83. }
  84. }, false );
  85. return { open: openNotes };
  86. })();