bookclub-advr

DSLC Advanced R Book Club
git clone https://git.eamoncaddigan.net/bookclub-advr.git
Log | Files | Refs | README | LICENSE

pdfexport.js (3070B)


      1 var PdfExport = ( function( _Reveal ){
      2 
      3 	var Reveal = _Reveal;
      4 	var setStylesheet = null;
      5 	var installAltKeyBindings = null;
      6 
      7 	function getRevealJsPath(){
      8 		var regex = /\b[^/]+\/reveal.css$/i;
      9 		var script = Array.from( document.querySelectorAll( 'link' ) ).find( function( e ){
     10 			return e.attributes.href && e.attributes.href.value.search( regex ) >= 0;
     11 		});
     12 		if( !script ){
     13 			console.error( 'reveal.css could not be found in included <link> elements. Did you rename this file?' );
     14 			return '';
     15 		}
     16 		return script.attributes.href.value.replace( regex, '' );
     17 	}
     18 
     19 	function setStylesheet3( pdfExport ){
     20 		var link = document.querySelector( '#print' );
     21 		if( !link ){
     22 			link = document.createElement( 'link' );
     23 			link.rel = 'stylesheet';
     24 			link.id = 'print';
     25 			document.querySelector( 'head' ).appendChild( link );
     26 		}
     27 		var style = 'paper';
     28 		if( pdfExport ){
     29 			style = 'pdf';
     30 		}
     31 		link.href = getRevealJsPath() + 'css/print/' + style + '.css';
     32 	}
     33 
     34 	function setStylesheet4( pdfExport ){
     35 	}
     36 
     37 	function installAltKeyBindings3(){
     38 	}
     39 
     40 	function installAltKeyBindings4(){
     41 		if( isPrintingPDF() ){
     42 			var config = Reveal.getConfig();
     43 			var shortcut = config.pdfExportShortcut || 'E';
     44 			window.addEventListener( 'keydown', function( e ){
     45 				if( e.target.nodeName.toUpperCase() == 'BODY'
     46 					&& ( e.key.toUpperCase() == shortcut.toUpperCase() || e.keyCode == shortcut.toUpperCase().charCodeAt( 0 ) ) ){
     47 					e.preventDefault();
     48 					togglePdfExport();
     49 					return false;
     50 				}
     51 			}, true );
     52 		}
     53 	}
     54 	
     55 	function isPrintingPDF(){
     56 		return /print-pdf/gi.test(window.location.search) || /view=print/gi.test(window.location.search);
     57 	}
     58 
     59 	function togglePdfExport(){
     60 		var url_doc = new URL( document.URL );
     61 		var query_doc = new URLSearchParams( url_doc.searchParams );
     62 		if( isPrintingPDF() ){
     63 			if (query_doc.has('print-pdf')) {
     64 				query_doc.delete('print-pdf');
     65 			} else if (query_doc.has('view')) {
     66 				query_doc.delete('view');
     67 			}
     68 		}else{
     69 			query_doc.set( 'view', 'print' );
     70 		}
     71 		url_doc.search = ( query_doc.toString() ? '?' + query_doc.toString() : '' );
     72 		window.location.href = url_doc.toString();
     73 	}
     74 
     75 	function installKeyBindings(){
     76 		var config = Reveal.getConfig();
     77 		var shortcut = config.pdfExportShortcut || 'E';
     78 		Reveal.addKeyBinding({
     79 			keyCode: shortcut.toUpperCase().charCodeAt( 0 ),
     80 			key: shortcut.toUpperCase(),
     81 			description: 'PDF export mode'
     82 		}, togglePdfExport );
     83 		installAltKeyBindings();
     84 	}
     85 
     86 	function install(){
     87 		installKeyBindings();
     88 		setStylesheet( isPrintingPDF() );
     89 	}
     90 
     91 	var Plugin = {
     92 	}
     93 
     94 	if( Reveal && Reveal.VERSION && Reveal.VERSION.length && Reveal.VERSION[ 0 ] == '3' ){
     95 		// reveal 3.x
     96 		setStylesheet = setStylesheet3;
     97 		installAltKeyBindings = installAltKeyBindings3;
     98 		install();
     99 	}else{
    100 		// must be reveal 4.x
    101 		setStylesheet = setStylesheet4;
    102 		installAltKeyBindings = installAltKeyBindings4;
    103 		Plugin.id = 'pdf-export';
    104 		Plugin.init = function( _Reveal ){
    105 			Reveal = _Reveal;
    106 			install();
    107 		};
    108 		Plugin.togglePdfExport = function () {
    109       togglePdfExport();
    110 		};
    111 	}
    112 
    113 	return Plugin;
    114 
    115 })( Reveal );