diff options
author | Hakim El Hattab <hakim.elhattab@gmail.com> | 2020-04-17 14:10:56 +0200 |
---|---|---|
committer | Hakim El Hattab <hakim.elhattab@gmail.com> | 2020-04-17 14:10:56 +0200 |
commit | e58502b3fb43bf86555ec970bfade389d039b2c7 (patch) | |
tree | 54037dd2210aae78be03f654e11ee67e53a452e5 /plugin | |
parent | 7e72b10fa5c56ff8d0cd88390475d1fef1be2b0c (diff) | |
download | fosdem-2021-minimalism-presentation-e58502b3fb43bf86555ec970bfade389d039b2c7.tar fosdem-2021-minimalism-presentation-e58502b3fb43bf86555ec970bfade389d039b2c7.tar.gz |
tweak plugin initialization to enable multi-instance plugins
Diffstat (limited to 'plugin')
-rw-r--r-- | plugin/highlight/highlight.js | 136 | ||||
-rwxr-xr-x | plugin/markdown/markdown.js | 5 | ||||
-rwxr-xr-x | plugin/math/math.js | 4 | ||||
-rw-r--r-- | plugin/notes/notes.js | 4 | ||||
-rw-r--r-- | plugin/search/search.js | 4 | ||||
-rw-r--r-- | plugin/zoom/zoom.js | 4 |
6 files changed, 81 insertions, 76 deletions
diff --git a/plugin/highlight/highlight.js b/plugin/highlight/highlight.js index 620b8f4..9fa4fa2 100644 --- a/plugin/highlight/highlight.js +++ b/plugin/highlight/highlight.js @@ -5,54 +5,7 @@ import './highlight-line-numbers.js' * reveal.js plugin that adds syntax highlight support. */ -// Function to perform a better "data-trim" on code snippets -// Will slice an indentation amount on each line of the snippet (amount based on the line having the lowest indentation length) -function betterTrim(snippetEl) { - // Helper functions - function trimLeft(val) { - // Adapted from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim#Polyfill - return val.replace(/^[\s\uFEFF\xA0]+/g, ''); - } - function trimLineBreaks(input) { - var lines = input.split('\n'); - - // Trim line-breaks from the beginning - for (var i = 0; i < lines.length; i++) { - if (lines[i].trim() === '') { - lines.splice(i--, 1); - } else break; - } - - // Trim line-breaks from the end - for (var i = lines.length-1; i >= 0; i--) { - if (lines[i].trim() === '') { - lines.splice(i, 1); - } else break; - } - - return lines.join('\n'); - } - - // Main function for betterTrim() - return (function(snippetEl) { - var content = trimLineBreaks(snippetEl.innerHTML); - var lines = content.split('\n'); - // Calculate the minimum amount to remove on each line start of the snippet (can be 0) - var pad = lines.reduce(function(acc, line) { - if (line.length > 0 && trimLeft(line).length > 0 && acc > line.length - trimLeft(line).length) { - return line.length - trimLeft(line).length; - } - return acc; - }, Number.POSITIVE_INFINITY); - // Slice each line with this amount - return lines.map(function(line, index) { - return line.slice(pad); - }) - .join('\n'); - })(snippetEl); -} - -var RevealHighlight = { +let Plugin = { id: 'highlight', @@ -85,7 +38,7 @@ var RevealHighlight = { }, false ); if( config.highlightOnLoad ) { - RevealHighlight.highlightBlock( block ); + Plugin.highlightBlock( block ); } } ); @@ -94,7 +47,7 @@ var RevealHighlight = { // all blocks in the deck into view at once deck.on( 'pdf-ready', function() { [].slice.call( document.querySelectorAll( '.reveal pre code[data-line-numbers].current-fragment' ) ).forEach( function( block ) { - RevealHighlight.scrollHighlightedLineIntoView( block, {}, true ); + Plugin.scrollHighlightedLineIntoView( block, {}, true ); } ); } ); @@ -122,7 +75,7 @@ var RevealHighlight = { // If there is at least one highlight step, generate // fragments - var highlightSteps = RevealHighlight.deserializeHighlightSteps( block.getAttribute( 'data-line-numbers' ) ); + var highlightSteps = Plugin.deserializeHighlightSteps( block.getAttribute( 'data-line-numbers' ) ); if( highlightSteps.length > 1 ) { // If the original code block has a fragment-index, @@ -137,10 +90,10 @@ var RevealHighlight = { highlightSteps.slice(1).forEach( function( highlight ) { var fragmentBlock = block.cloneNode( true ); - fragmentBlock.setAttribute( 'data-line-numbers', RevealHighlight.serializeHighlightSteps( [ highlight ] ) ); + fragmentBlock.setAttribute( 'data-line-numbers', Plugin.serializeHighlightSteps( [ highlight ] ) ); fragmentBlock.classList.add( 'fragment' ); block.parentNode.appendChild( fragmentBlock ); - RevealHighlight.highlightLines( fragmentBlock ); + Plugin.highlightLines( fragmentBlock ); if( typeof fragmentIndex === 'number' ) { fragmentBlock.setAttribute( 'data-fragment-index', fragmentIndex ); @@ -151,13 +104,13 @@ var RevealHighlight = { } // Scroll highlights into view as we step through them - fragmentBlock.addEventListener( 'visible', RevealHighlight.scrollHighlightedLineIntoView.bind( RevealHighlight, fragmentBlock, scrollState ) ); - fragmentBlock.addEventListener( 'hidden', RevealHighlight.scrollHighlightedLineIntoView.bind( RevealHighlight, fragmentBlock.previousSibling, scrollState ) ); + fragmentBlock.addEventListener( 'visible', Plugin.scrollHighlightedLineIntoView.bind( Plugin, fragmentBlock, scrollState ) ); + fragmentBlock.addEventListener( 'hidden', Plugin.scrollHighlightedLineIntoView.bind( Plugin, fragmentBlock.previousSibling, scrollState ) ); } ); block.removeAttribute( 'data-fragment-index' ) - block.setAttribute( 'data-line-numbers', RevealHighlight.serializeHighlightSteps( [ highlightSteps[0] ] ) ); + block.setAttribute( 'data-line-numbers', Plugin.serializeHighlightSteps( [ highlightSteps[0] ] ) ); } @@ -167,13 +120,13 @@ var RevealHighlight = { var slide = typeof block.closest === 'function' ? block.closest( 'section:not(.stack)' ) : null; if( slide ) { var scrollFirstHighlightIntoView = function() { - RevealHighlight.scrollHighlightedLineIntoView( block, scrollState, true ); + Plugin.scrollHighlightedLineIntoView( block, scrollState, true ); slide.removeEventListener( 'visible', scrollFirstHighlightIntoView ); } slide.addEventListener( 'visible', scrollFirstHighlightIntoView ); } - RevealHighlight.highlightLines( block ); + Plugin.highlightLines( block ); } @@ -229,7 +182,7 @@ var RevealHighlight = { time = Math.min( time + 0.02, 1 ); // Update our eased scroll position - block.scrollTop = startTop + ( targetTop - startTop ) * RevealHighlight.easeInOutQuart( time ); + block.scrollTop = startTop + ( targetTop - startTop ) * Plugin.easeInOutQuart( time ); // Keep animating unless we've reached the end if( time < 1 ) { @@ -284,7 +237,7 @@ var RevealHighlight = { */ highlightLines: function( block, linesToHighlight ) { - var highlightSteps = RevealHighlight.deserializeHighlightSteps( linesToHighlight || block.getAttribute( 'data-line-numbers' ) ); + var highlightSteps = Plugin.deserializeHighlightSteps( linesToHighlight || block.getAttribute( 'data-line-numbers' ) ); if( highlightSteps.length ) { @@ -320,7 +273,7 @@ var RevealHighlight = { * numbers to highlight. * * @example - * RevealHighlight.deserializeHighlightSteps( '1,2|3,5-10' ) + * Plugin.deserializeHighlightSteps( '1,2|3,5-10' ) * // [ * // [ { start: 1 }, { start: 2 } ], * // [ { start: 3 }, { start: 5, end: 10 } ] @@ -332,16 +285,16 @@ var RevealHighlight = { highlightSteps = highlightSteps.replace( /\s/g, '' ); // Divide up our line number groups - highlightSteps = highlightSteps.split( RevealHighlight.HIGHLIGHT_STEP_DELIMITER ); + highlightSteps = highlightSteps.split( Plugin.HIGHLIGHT_STEP_DELIMITER ); return highlightSteps.map( function( highlights ) { - return highlights.split( RevealHighlight.HIGHLIGHT_LINE_DELIMITER ).map( function( highlight ) { + return highlights.split( Plugin.HIGHLIGHT_LINE_DELIMITER ).map( function( highlight ) { // Parse valid line numbers if( /^[\d-]+$/.test( highlight ) ) { - highlight = highlight.split( RevealHighlight.HIGHLIGHT_LINE_RANGE_DELIMITER ); + highlight = highlight.split( Plugin.HIGHLIGHT_LINE_RANGE_DELIMITER ); var lineStart = parseInt( highlight[0], 10 ), lineEnd = parseInt( highlight[1], 10 ); @@ -384,7 +337,7 @@ var RevealHighlight = { // Line range if( typeof highlight.end === 'number' ) { - return highlight.start + RevealHighlight.HIGHLIGHT_LINE_RANGE_DELIMITER + highlight.end; + return highlight.start + Plugin.HIGHLIGHT_LINE_RANGE_DELIMITER + highlight.end; } // Single line else if( typeof highlight.start === 'number' ) { @@ -395,12 +348,59 @@ var RevealHighlight = { return ''; } - } ).join( RevealHighlight.HIGHLIGHT_LINE_DELIMITER ); + } ).join( Plugin.HIGHLIGHT_LINE_DELIMITER ); - } ).join( RevealHighlight.HIGHLIGHT_STEP_DELIMITER ); + } ).join( Plugin.HIGHLIGHT_STEP_DELIMITER ); } } -export default RevealHighlight;
\ No newline at end of file +// Function to perform a better "data-trim" on code snippets +// Will slice an indentation amount on each line of the snippet (amount based on the line having the lowest indentation length) +function betterTrim(snippetEl) { + // Helper functions + function trimLeft(val) { + // Adapted from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim#Polyfill + return val.replace(/^[\s\uFEFF\xA0]+/g, ''); + } + function trimLineBreaks(input) { + var lines = input.split('\n'); + + // Trim line-breaks from the beginning + for (var i = 0; i < lines.length; i++) { + if (lines[i].trim() === '') { + lines.splice(i--, 1); + } else break; + } + + // Trim line-breaks from the end + for (var i = lines.length-1; i >= 0; i--) { + if (lines[i].trim() === '') { + lines.splice(i, 1); + } else break; + } + + return lines.join('\n'); + } + + // Main function for betterTrim() + return (function(snippetEl) { + var content = trimLineBreaks(snippetEl.innerHTML); + var lines = content.split('\n'); + // Calculate the minimum amount to remove on each line start of the snippet (can be 0) + var pad = lines.reduce(function(acc, line) { + if (line.length > 0 && trimLeft(line).length > 0 && acc > line.length - trimLeft(line).length) { + return line.length - trimLeft(line).length; + } + return acc; + }, Number.POSITIVE_INFINITY); + // Slice each line with this amount + return lines.map(function(line, index) { + return line.slice(pad); + }) + .join('\n'); + })(snippetEl); +} + +export default () => Plugin; diff --git a/plugin/markdown/markdown.js b/plugin/markdown/markdown.js index f305104..9114078 100755 --- a/plugin/markdown/markdown.js +++ b/plugin/markdown/markdown.js @@ -6,7 +6,7 @@ import marked from './marked.js' -export default { +let Plugin = { id: 'markdown', @@ -15,6 +15,7 @@ export default { * current reveal.js deck. */ init: function( deck ) { + // This should no longer be needed, as long as the highlight.js // plugin is included after the markdown plugin // if( typeof window.hljs !== 'undefined' ) { @@ -43,6 +44,8 @@ export default { }; +export default () => Plugin; + var DEFAULT_SLIDE_SEPARATOR = '^\r?\n---\r?\n$', DEFAULT_NOTES_SEPARATOR = 'notes?:', DEFAULT_ELEMENT_ATTRIBUTES_SEPARATOR = '\\\.element\\\s*?(.+?)$', diff --git a/plugin/math/math.js b/plugin/math/math.js index 6ef9bd9..25f50cb 100755 --- a/plugin/math/math.js +++ b/plugin/math/math.js @@ -4,7 +4,7 @@ * * @author Hakim El Hattab */ -var RevealMath = (function(){ +let Plugin = (function(){ var options = Reveal.getConfig().math || {}; var mathjax = options.mathjax || 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js'; @@ -91,4 +91,4 @@ var RevealMath = (function(){ })(); -export default RevealMath; +export default () => Plugin; diff --git a/plugin/notes/notes.js b/plugin/notes/notes.js index 8184936..b4bbbc2 100644 --- a/plugin/notes/notes.js +++ b/plugin/notes/notes.js @@ -9,7 +9,7 @@ * 3. This window proceeds to send the current presentation state * to the notes window */ -var RevealNotes = (function() { +let Plugin = (function() { var notesPopup = null; @@ -182,4 +182,4 @@ var RevealNotes = (function() { })(); -export default RevealNotes; +export default () => Plugin; diff --git a/plugin/search/search.js b/plugin/search/search.js index d4cefcb..cc39ef0 100644 --- a/plugin/search/search.js +++ b/plugin/search/search.js @@ -5,7 +5,7 @@ * By Jon Snyder <snyder.jon@gmail.com>, February 2013 */ -var RevealSearch = (function() { +var Plugin = (function() { var matchedSlides; var currentMatchedIndex; @@ -218,4 +218,4 @@ function Hilitor(id, tag) } })(); -export default RevealSearch;
\ No newline at end of file +export default () => Plugin;
\ No newline at end of file diff --git a/plugin/zoom/zoom.js b/plugin/zoom/zoom.js index 1b1f3ed..a4de3f3 100644 --- a/plugin/zoom/zoom.js +++ b/plugin/zoom/zoom.js @@ -1,7 +1,7 @@ /*! * reveal.js Zoom plugin */ -export default { +var Plugin = { id: 'zoom', @@ -29,6 +29,8 @@ export default { }; +export default () => Plugin; + /*! * zoom.js 0.3 (modified for use with reveal.js) * http://lab.hakim.se/zoom-js |