notes.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * Handles opening of and synchronization with the reveal.js
  3. * notes window.
  4. */
  5. var RevealNotes = (function() {
  6. function openNotes() {
  7. var notesPopup = window.open( 'plugin/notes/notes.html', 'reveal.js - Notes', 'width=1120,height=850' );
  8. // Fires when slide is changed
  9. Reveal.addEventListener( 'slidechanged', function( event ) {
  10. post('slidechanged');
  11. } );
  12. // Fires when a fragment is shown
  13. Reveal.addEventListener( 'fragmentshown', function( event ) {
  14. post('fragmentshown');
  15. } );
  16. // Fires when a fragment is hidden
  17. Reveal.addEventListener( 'fragmenthidden', function( event ) {
  18. post('fragmenthidden');
  19. } );
  20. /**
  21. * @description Posts the current slide data to the notes window
  22. *
  23. * @param {string} eventType Expecting 'slidechanged', 'fragmentshown' or 'fragmenthidden'
  24. * set in the events above to define the needed slideDate.
  25. */
  26. function post(eventType) {
  27. console.log(eventType);
  28. var slideElement = Reveal.getCurrentSlide(),
  29. indexh = Reveal.getIndices().h,
  30. indexv = Reveal.getIndices().v,
  31. notes = slideElement.querySelector( 'aside.notes' ),
  32. nextindexh,
  33. nextindexv,
  34. slideData;
  35. if( slideElement.nextElementSibling && slideElement.parentNode.nodeName == 'SECTION' ) {
  36. nextindexh = indexh;
  37. nextindexv = indexv + 1;
  38. } else {
  39. nextindexh = indexh + 1;
  40. nextindexv = 0;
  41. }
  42. if (eventType === 'slidechanged') {
  43. slideData = {
  44. notes : notes ? notes.innerHTML : '',
  45. indexh : indexh,
  46. indexv : indexv,
  47. nextindexh : nextindexh,
  48. nextindexv : nextindexv,
  49. markdown : notes ? typeof notes.getAttribute( 'data-markdown' ) === 'string' : false
  50. };
  51. }
  52. else if (eventType === 'fragmentshown') {
  53. slideData = {
  54. fragment : 'next'
  55. };
  56. }
  57. else if (eventType === 'fragmenthidden') {
  58. slideData = {
  59. fragment : 'prev'
  60. };
  61. }
  62. notesPopup.postMessage( JSON.stringify( slideData ), '*' );
  63. }
  64. // The main presentation is kept in sync when navigating the
  65. // note slides so that the popup may be used as a remote
  66. window.addEventListener( 'message', function( event ) {
  67. var data = JSON.parse( event.data );
  68. if( data && typeof data.indexh === 'number' && typeof data.indexv === 'number' ) {
  69. Reveal.slide( data.indexh, data.indexv );
  70. }
  71. } );
  72. // Navigate to the current slide when the notes are loaded
  73. notesPopup.addEventListener( 'load', function( event ) {
  74. post('slidechanged');
  75. }, false );
  76. }
  77. // If the there's a 'notes' query set, open directly
  78. if( window.location.search.match(/(\?|\&)notes/gi ) !== null ) {
  79. openNotes();
  80. }
  81. // Open the notes when the 's' key is hit
  82. document.addEventListener( 'keydown', function( event ) {
  83. // Disregard the event if the target is editable or a
  84. // modifier is present
  85. if ( document.querySelector( ':focus' ) !== null || event.shiftKey || event.altKey || event.ctrlKey || event.metaKey ) return;
  86. if( event.keyCode === 83 ) {
  87. event.preventDefault();
  88. openNotes();
  89. }
  90. }, false );
  91. return { open: openNotes };
  92. })();