Cleanup code style.

This commit is contained in:
Benjamin Tan 2018-02-08 11:22:01 +08:00
parent fb97d99bff
commit 18e7dd2173
3 changed files with 99 additions and 101 deletions

View File

@ -403,13 +403,13 @@
} }
/** /**
* Loads the dependencies of reveal.js. Dependencies are * Loads the dependencies of reveal.js. Dependencies are
* defined via the configuration option 'dependencies' * defined via the configuration option 'dependencies'
* and will be loaded prior to starting/binding reveal.js. * and will be loaded prior to starting/binding reveal.js.
* Some dependencies may have an 'async' flag, if so they * Some dependencies may have an 'async' flag, if so they
* will load after reveal.js has been started up. * will load after reveal.js has been started up.
*/ */
function load() { function load() {
var scripts = [], var scripts = [],

View File

@ -42,28 +42,26 @@ probePage.open( inputFile, function( status ) {
printPage.open( inputFile, function( status ) { printPage.open( inputFile, function( status ) {
console.log( 'Export PDF: Preparing pdf [3/4]') console.log( 'Export PDF: Preparing pdf [3/4]')
printPage.evaluate(function() { printPage.evaluate( function() {
Reveal.isReady() ? window.callPhantom() : Reveal.addEventListener( 'pdf-ready', window.callPhantom ); Reveal.isReady() ? window.callPhantom() : Reveal.addEventListener( 'pdf-ready', window.callPhantom );
}); } );
} ); } );
printPage.onCallback = function(data) { printPage.onCallback = function( data ) {
// For some reason we need to "jump the queue" for syntax highlighting to work. // For some reason we need to "jump the queue" for syntax highlighting to work.
// See: http://stackoverflow.com/a/3580132/129269 // See: http://stackoverflow.com/a/3580132/129269
setTimeout(function() { setTimeout( function() {
console.log( 'Export PDF: Writing file [4/4]' ); console.log( 'Export PDF: Writing file [4/4]' );
printPage.render( outputFile ); printPage.render( outputFile );
console.log( 'Export PDF: Finished successfully!' ); console.log( 'Export PDF: Finished successfully!' );
phantom.exit(); phantom.exit();
}, 0); }, 0 );
}; };
} }
else { else {
console.log( 'Export PDF: Unable to read reveal.js config. Make sure the input address points to a reveal.js page.' ); console.log( 'Export PDF: Unable to read reveal.js config. Make sure the input address points to a reveal.js page.' );
phantom.exit(1); phantom.exit( 1 );
} }
} ); } );

View File

@ -19,92 +19,92 @@ var RevealSearch = (function() {
function Hilitor(id, tag) function Hilitor(id, tag)
{ {
var targetNode = document.getElementById(id) || document.body; var targetNode = document.getElementById(id) || document.body;
var hiliteTag = tag || "EM"; var hiliteTag = tag || "EM";
var skipTags = new RegExp("^(?:" + hiliteTag + "|SCRIPT|FORM)$"); var skipTags = new RegExp("^(?:" + hiliteTag + "|SCRIPT|FORM)$");
var colors = ["#ff6", "#a0ffff", "#9f9", "#f99", "#f6f"]; var colors = ["#ff6", "#a0ffff", "#9f9", "#f99", "#f6f"];
var wordColor = []; var wordColor = [];
var colorIdx = 0; var colorIdx = 0;
var matchRegex = ""; var matchRegex = "";
var matchingSlides = []; var matchingSlides = [];
this.setRegex = function(input) this.setRegex = function(input)
{ {
input = input.replace(/^[^\w]+|[^\w]+$/g, "").replace(/[^\w'-]+/g, "|"); input = input.replace(/^[^\w]+|[^\w]+$/g, "").replace(/[^\w'-]+/g, "|");
matchRegex = new RegExp("(" + input + ")","i"); matchRegex = new RegExp("(" + input + ")","i");
} }
this.getRegex = function() this.getRegex = function()
{ {
return matchRegex.toString().replace(/^\/\\b\(|\)\\b\/i$/g, "").replace(/\|/g, " "); return matchRegex.toString().replace(/^\/\\b\(|\)\\b\/i$/g, "").replace(/\|/g, " ");
} }
// recursively apply word highlighting // recursively apply word highlighting
this.hiliteWords = function(node) this.hiliteWords = function(node)
{ {
if(node == undefined || !node) return; if(node == undefined || !node) return;
if(!matchRegex) return; if(!matchRegex) return;
if(skipTags.test(node.nodeName)) return; if(skipTags.test(node.nodeName)) return;
if(node.hasChildNodes()) { if(node.hasChildNodes()) {
for(var i=0; i < node.childNodes.length; i++) for(var i=0; i < node.childNodes.length; i++)
this.hiliteWords(node.childNodes[i]); this.hiliteWords(node.childNodes[i]);
} }
if(node.nodeType == 3) { // NODE_TEXT if(node.nodeType == 3) { // NODE_TEXT
if((nv = node.nodeValue) && (regs = matchRegex.exec(nv))) { if((nv = node.nodeValue) && (regs = matchRegex.exec(nv))) {
//find the slide's section element and save it in our list of matching slides //find the slide's section element and save it in our list of matching slides
var secnode = node; var secnode = node;
while (secnode != null && secnode.nodeName != 'SECTION') { while (secnode != null && secnode.nodeName != 'SECTION') {
secnode = secnode.parentNode; secnode = secnode.parentNode;
} }
var slideIndex = Reveal.getIndices(secnode); var slideIndex = Reveal.getIndices(secnode);
var slidelen = matchingSlides.length; var slidelen = matchingSlides.length;
var alreadyAdded = false; var alreadyAdded = false;
for (var i=0; i < slidelen; i++) { for (var i=0; i < slidelen; i++) {
if ( (matchingSlides[i].h === slideIndex.h) && (matchingSlides[i].v === slideIndex.v) ) { if ( (matchingSlides[i].h === slideIndex.h) && (matchingSlides[i].v === slideIndex.v) ) {
alreadyAdded = true; alreadyAdded = true;
} }
} }
if (! alreadyAdded) { if (! alreadyAdded) {
matchingSlides.push(slideIndex); matchingSlides.push(slideIndex);
} }
if(!wordColor[regs[0].toLowerCase()]) { if(!wordColor[regs[0].toLowerCase()]) {
wordColor[regs[0].toLowerCase()] = colors[colorIdx++ % colors.length]; wordColor[regs[0].toLowerCase()] = colors[colorIdx++ % colors.length];
} }
var match = document.createElement(hiliteTag); var match = document.createElement(hiliteTag);
match.appendChild(document.createTextNode(regs[0])); match.appendChild(document.createTextNode(regs[0]));
match.style.backgroundColor = wordColor[regs[0].toLowerCase()]; match.style.backgroundColor = wordColor[regs[0].toLowerCase()];
match.style.fontStyle = "inherit"; match.style.fontStyle = "inherit";
match.style.color = "#000"; match.style.color = "#000";
var after = node.splitText(regs.index); var after = node.splitText(regs.index);
after.nodeValue = after.nodeValue.substring(regs[0].length); after.nodeValue = after.nodeValue.substring(regs[0].length);
node.parentNode.insertBefore(match, after); node.parentNode.insertBefore(match, after);
} }
} }
}; };
// remove highlighting // remove highlighting
this.remove = function() this.remove = function()
{ {
var arr = document.getElementsByTagName(hiliteTag); var arr = document.getElementsByTagName(hiliteTag);
while(arr.length && (el = arr[0])) { while(arr.length && (el = arr[0])) {
el.parentNode.replaceChild(el.firstChild, el); el.parentNode.replaceChild(el.firstChild, el);
} }
}; };
// start highlighting at target node // start highlighting at target node
this.apply = function(input) this.apply = function(input)
{ {
if(input == undefined || !input) return; if(input == undefined || !input) return;
this.remove(); this.remove();
this.setRegex(input); this.setRegex(input);
this.hiliteWords(targetNode); this.hiliteWords(targetNode);
return matchingSlides; return matchingSlides;
}; };
} }
@ -150,7 +150,7 @@ function Hilitor(id, tag)
} }
} }
if (matchedSlides) { if (matchedSlides) {
//navigate to the next slide that has the keyword, wrapping to the first if necessary //navigate to the next slide that has the keyword, wrapping to the first if necessary
if (matchedSlides.length && (matchedSlides.length <= currentMatchedIndex)) { if (matchedSlides.length && (matchedSlides.length <= currentMatchedIndex)) {
currentMatchedIndex = 0; currentMatchedIndex = 0;
@ -169,20 +169,20 @@ function Hilitor(id, tag)
var searchElement = document.createElement( 'div' ); var searchElement = document.createElement( 'div' );
searchElement.id = "searchinputdiv"; searchElement.id = "searchinputdiv";
searchElement.classList.add( 'searchdiv' ); searchElement.classList.add( 'searchdiv' );
searchElement.style.position = 'absolute'; searchElement.style.position = 'absolute';
searchElement.style.top = '10px'; searchElement.style.top = '10px';
searchElement.style.right = '10px'; searchElement.style.right = '10px';
searchElement.style.zIndex = 10; searchElement.style.zIndex = 10;
//embedded base64 search icon Designed by Sketchdock - http://www.sketchdock.com/: //embedded base64 search icon Designed by Sketchdock - http://www.sketchdock.com/:
searchElement.innerHTML = '<span><input type="search" id="searchinput" class="searchinput" style="vertical-align: top;"/><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAJiSURBVHjatFZNaxNBGH5md+Mmu92NVdKDRipSAyqCghgQD4L4cRe86UUtAQ+eFCxoa4/25EXBFi8eBE+eRPoDhB6KgiiixdAPCEkx2pjvTXadd9yNsflwuyUDD/O+u8PzzDPvzOwyx3EwyCZhwG3gAkp7MnpjgbopjsltcD4gjuXZZKeAR348MYLYTm3LzOs/y3j3JTfZxgXWXmTuwPHIc4VmoOmv5IrI53+AO2DdHLjkDWQ3GoEEVFXtXQOvkSnPWcyUceviLhwbDYv8/XIVj97kse7TodLvZXxYxrPUHkQ1ufXs3FEdybEIxucySOesoNvUgWU1cP3MkCBfTFdw9fGaAMVmRELq7LBw2Q3/FaAxxWIRpw+ZIr/7IouPqzUBiqmdHAv7EuhRAwf1er2Vy4x1jW3b2d5Jfvu5IPp7l2LYbcgCFFNb+FoJ7oBqEAqFMPNqFcmEgVMJDfMT+1tvN0pNjERlMS6QA5pFOKxiKVPFhakPeL3It+WGJUDxt2wFR+JhzI7v5ctkd8DXOZAkCYYxhO+lKm4+Xfqz/rIixBuNBl7eOYzkQQNzqX249mRl6zUgEcYkaJrGhUwBinVdh6IouPzwE6/DL5w4oLkH8y981aDf+uq6hlKpJESiUdNfDZi7/ehG9K6KfiA3pml0PLcsq+cSMTj2NL9ukc4UOmz7AZ3+crkC4mHujFvXNaMFB3bEr8xPS6p5O+jXxq4VZtaen7/PwzrntjcLUE0iHPS1Ud1cdiEJl/8WivZk0wXd7zWOMkeF8s0CcAmkNrC2nvXZDbbbN73ccYnZoH9bfgswAFzAe9/h3dbKAAAAAElFTkSuQmCC" id="searchbutton" class="searchicon" style="vertical-align: top; margin-top: -1px;"/></span>'; searchElement.innerHTML = '<span><input type="search" id="searchinput" class="searchinput" style="vertical-align: top;"/><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAJiSURBVHjatFZNaxNBGH5md+Mmu92NVdKDRipSAyqCghgQD4L4cRe86UUtAQ+eFCxoa4/25EXBFi8eBE+eRPoDhB6KgiiixdAPCEkx2pjvTXadd9yNsflwuyUDD/O+u8PzzDPvzOwyx3EwyCZhwG3gAkp7MnpjgbopjsltcD4gjuXZZKeAR348MYLYTm3LzOs/y3j3JTfZxgXWXmTuwPHIc4VmoOmv5IrI53+AO2DdHLjkDWQ3GoEEVFXtXQOvkSnPWcyUceviLhwbDYv8/XIVj97kse7TodLvZXxYxrPUHkQ1ufXs3FEdybEIxucySOesoNvUgWU1cP3MkCBfTFdw9fGaAMVmRELq7LBw2Q3/FaAxxWIRpw+ZIr/7IouPqzUBiqmdHAv7EuhRAwf1er2Vy4x1jW3b2d5Jfvu5IPp7l2LYbcgCFFNb+FoJ7oBqEAqFMPNqFcmEgVMJDfMT+1tvN0pNjERlMS6QA5pFOKxiKVPFhakPeL3It+WGJUDxt2wFR+JhzI7v5ctkd8DXOZAkCYYxhO+lKm4+Xfqz/rIixBuNBl7eOYzkQQNzqX249mRl6zUgEcYkaJrGhUwBinVdh6IouPzwE6/DL5w4oLkH8y981aDf+uq6hlKpJESiUdNfDZi7/ehG9K6KfiA3pml0PLcsq+cSMTj2NL9ukc4UOmz7AZ3+crkC4mHujFvXNaMFB3bEr8xPS6p5O+jXxq4VZtaen7/PwzrntjcLUE0iHPS1Ud1cdiEJl/8WivZk0wXd7zWOMkeF8s0CcAmkNrC2nvXZDbbbN73ccYnZoH9bfgswAFzAe9/h3dbKAAAAAElFTkSuQmCC" id="searchbutton" class="searchicon" style="vertical-align: top; margin-top: -1px;"/></span>';
dom.wrapper.appendChild( searchElement ); dom.wrapper.appendChild( searchElement );
} }
document.getElementById("searchbutton").addEventListener( 'click', function(event) { document.getElementById( 'searchbutton' ).addEventListener( 'click', function(event) {
doSearch(); doSearch();
}, false ); }, false );
document.getElementById("searchinput").addEventListener( 'keyup', function( event ) { document.getElementById( 'searchinput' ).addEventListener( 'keyup', function( event ) {
switch (event.keyCode) { switch (event.keyCode) {
case 13: case 13:
event.preventDefault(); event.preventDefault();
@ -195,7 +195,7 @@ function Hilitor(id, tag)
}, false ); }, false );
document.addEventListener( 'keydown', function( event ) { document.addEventListener( 'keydown', function( event ) {
if( event.key == "F" && (event.ctrlKey || event.metaKey) ) {//Control+Shift+f if( event.key == "F" && (event.ctrlKey || event.metaKey) ) { //Control+Shift+f
event.preventDefault(); event.preventDefault();
toggleSearch(); toggleSearch();
} }