notes.js 2.6 KB

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