notes.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. Reveal.addEventListener( 'slidechanged', post );
  9. // Posts the current slide data to the notes window
  10. function post() {
  11. var slideElement = Reveal.getCurrentSlide(),
  12. indexh = Reveal.getIndices().h,
  13. indexv = Reveal.getIndices().v,
  14. nextindexh,
  15. nextindexv;
  16. if( slideElement.nextElementSibling && slideElement.parentNode.nodeName == 'SECTION' ) {
  17. nextindexh = indexh;
  18. nextindexv = indexv + 1;
  19. } else {
  20. nextindexh = indexh + 1;
  21. nextindexv = 0;
  22. }
  23. var notes = slideElement.querySelector( 'aside.notes' );
  24. var slideData = {
  25. notes : notes ? notes.innerHTML : '',
  26. indexh : indexh,
  27. indexv : indexv,
  28. nextindexh : nextindexh,
  29. nextindexv : nextindexv,
  30. markdown : notes ? typeof notes.getAttribute( 'data-markdown' ) === 'string' : false
  31. };
  32. notesPopup.postMessage( JSON.stringify( slideData ), '*' );
  33. }
  34. // The main presentation is kept in sync when navigating the
  35. // note slides so that the popup may be used as a remote
  36. window.addEventListener( 'message', function( event ) {
  37. var data = JSON.parse( event.data );
  38. if( data && typeof data.indexh === 'number' && typeof data.indexv === 'number' ) {
  39. Reveal.slide( data.indexh, data.indexv );
  40. }
  41. } );
  42. // Navigate to the current slide when the notes are loaded
  43. notesPopup.addEventListener( 'load', post, false );
  44. }
  45. // If the there's a 'notes' query set, open directly
  46. if( window.location.search.match(/(\?|\&)notes/gi ) !== null ) {
  47. openNotes();
  48. }
  49. // Open the notes when the 's' key is hit
  50. document.addEventListener( 'keydown', function( event ) {
  51. // Disregard the event if the target is editable or a
  52. // modifier is present
  53. if ( document.querySelector( ':focus' ) !== null || event.shiftKey || event.altKey || event.ctrlKey || event.metaKey ) return;
  54. if( event.keyCode === 83 ) {
  55. event.preventDefault();
  56. openNotes();
  57. }
  58. }, false );
  59. return { open: openNotes }
  60. })();