Skip to content
This repository has been archived by the owner on Jun 19, 2023. It is now read-only.

Scrolling #7

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,30 @@

* **Do not** initialize code highlighting with `hljs.initHighlightingOnLoad()`.
</section>

<section data-markdown style="text-align: left"><script type="text/template">
### Configuration

`reveal-code-focus` supports one option: whether or not it automatically
scrolls `<code>` elements so that the focused code is centered. This
is enabled by default but can be disabled if desired:

```js
Reveal.initialize({
dependencies: [
{ src: 'path/to/highlight.js' },
{
src: 'node_modules/reveal-code-focus/reveal-code-focus.js',
async: true,
callback: function() {
RevealCodeFocus({ scrollToFocused: false });
}
}
]
});
```
</script></section>

</section>

<section>
Expand Down
34 changes: 29 additions & 5 deletions reveal-code-focus.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
return;
}

var currentSlide, currentFragments, prevSlideData = null;
var currentSlide, currentFragments, scrollToFocused = true, prevSlideData = null;

function forEach(array, callback) {
var i = -1, length = array ? array.length : 0;
Expand Down Expand Up @@ -133,22 +133,46 @@

var lines = fragment.getAttribute('data-code-focus');
if (lines) {
var code = currentSlide.querySelectorAll('pre code .line');
var code = currentSlide.querySelectorAll('pre code .line'), codeParent, linesTop, linesBottom;

function focusLine(lineNumber) {
if (!code[lineNumber - 1]) {
return;
}
code[lineNumber - 1].classList.add('focus');
codeParent = code[lineNumber - 1].parentNode;
if (typeof(linesTop) === 'undefined') {
linesTop = code[lineNumber - 1].offsetTop;
linesBottom = code[lineNumber - 1].offsetTop + code[lineNumber - 1].clientHeight;
} else {
linesTop = Math.min(linesTop, code[lineNumber - 1].offsetTop);
linesBottom = Math.max(linesBottom, code[lineNumber - 1].offsetTop + code[lineNumber - 1].clientHeight);
}
}

forEach(lines.split(','), function(line) {
lines = line.split('-');
if (lines.length == 1) {
code[lines[0] - 1] && code[lines[0] - 1].classList.add('focus');
focusLine(lines[0]);
} else {
var i = lines[0] - 1, j = lines[1];
while (++i <= j) {
code[i - 1] && code[i - 1].classList.add('focus');
focusLine(i);
}
}
});

if (scrollToFocused && typeof(linesTop) !== 'undefined') {
codeParent.scrollTop = linesTop - (codeParent.clientHeight - (linesBottom - linesTop)) / 2;
}
}
}

function RevealCodeFocus() {
function RevealCodeFocus(options) {
options = options || {};
if (typeof(options.scrollToFocused) !== 'undefined') {
scrollToFocused = options.scrollToFocused;
}
if (Reveal.isReady()) {
init({ currentSlide: Reveal.getCurrentSlide() });
} else {
Expand Down