Skip to content

Commit

Permalink
Initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
McShelby authored Jun 7, 2018
1 parent 499a18a commit 4202099
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 1 deletion.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2018 Sören Weber
Copyright (c) 2018 Sören Weber, https://soeren-weber.de

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
53 changes: 53 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# PdfExport

A [reveal.js](https://github.com/hakimel/reveal.js/) plugin to easly switch between screen and the built-in PDF export mode.

## Installation

Copy this repository into the plugins folder of your reveal.js presentation, ie ```plugin/pdfexport```.

Add the plugin to the dependencies in your presentation, as below.

```javascript
Reveal.initialize({
// ...
dependencies: [
// ...
{ src: 'plugin/pdfexport/pdfexport.js', async: true },
]
});
```

You need to remove all of the following or similar lines from your presentation. Paper or PDF stylesheets will be set by the plugin.

```html
<!-- Printing and PDF exports -->
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? 'css/print/pdf.css' : 'css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
```

## Usage

To toggle between screen and PDF mode you can press the ```E``` shortcut on the keyboard.

### Parameter

```javascript
Reveal.initialize({
// ...

// Shortcut for toggling between screen and PDF mode
pdfExportShortcut: 'E',
});
```

## License

[MIT licensed](https://en.wikipedia.org/wiki/MIT_License).

Copyright (C) 2018 [Sören Weber](https://soeren-weber.de)
80 changes: 80 additions & 0 deletions pdfexport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
var PdfExport = ( function( Reveal ){

var defMode = false;

function getRevealJsPath(){
var regex = /\/js\/reveal.js$/i;
var script = Array.from( document.querySelectorAll( 'script' ) ).find( function( e ){
return e.attributes.src && e.attributes.src.value.search( regex ) >= 0;
});
if( !script ){
console.error( 'reveal.js script could not be found in included <script> elements. Did you rename this file?' );
return '';
}
return script.attributes.src.value.replace( regex, '' );
}

function setStylesheet( pdf ){
var link = document.querySelector( '#print' );
if( !link ){
link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.id = 'print';
document.querySelector( 'head' ).appendChild( link );
}
var style = 'paper';
if( pdf ){
style = 'pdf';
}
link.href = getRevealJsPath() + '/css/print/' + style + '.css';
}

function setPdfExport( pdfExport ){
var config = Reveal.getConfig();
pdfExport = pdfExport !== null ? pdfExport : config.pdfExport !== undefined ? !!config.pdfExport : defMode;
setStylesheet( pdfExport );
}

function isPrintingPDF(){
return ( /print-pdf/gi ).test( window.location.search );
}

function togglePdfExport(){
var url_doc = new URL( document.URL );
var query_doc = new URLSearchParams( url_doc.searchParams );
if( isPrintingPDF() ){
query_doc.delete( 'print-pdf' );
}else{
query_doc.set( 'print-pdf', '' );
}
url_doc.search = ( query_doc.toString() ? '?' + query_doc.toString() : '' );
window.location.href = url_doc.toString();
}

function applyPdfExportParameter(){
setPdfExport( isPrintingPDF() );
}

function installKeyBindings(){
var config = Reveal.getConfig();
var shortcut = config.pdfExportShortcut || 'E';
var keyboard = {};
keyboard[ shortcut.toUpperCase().charCodeAt( 0 ) ] = togglePdfExport;

Reveal.registerKeyboardShortcut( shortcut, 'PDF export mode' );
Reveal.configure({
keyboard: keyboard
});
}

function install(){
installKeyBindings();
applyPdfExportParameter();
}

install();

return {
};

})( Reveal );

0 comments on commit 4202099

Please sign in to comment.