forked from bnjmnt4n/reveal-code-focus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reveal-code-focus.js
254 lines (213 loc) · 7.3 KB
/
reveal-code-focus.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*!
* reveal-code-focus v1.1.0
* Copyright 2015-2018 Benjamin Tan <https://bnjmnt4n.now.sh/>
* Available under MIT license <https://github.com/bnjmnt4n/reveal-code-focus/blob/master/LICENSE>
*/
;(function(window, Reveal, hljs) {
if (typeof window.RevealCodeFocus == 'function') {
return;
}
var currentSlide, currentFragmentsList, scrollToFocused = true, prevSlideData = null;
// Iterates through `array`, running `callback` for each `array` element.
function forEach(array, callback) {
var i = -1, length = array ? array.length : 0;
while (++i < length) {
callback(array[i]);
}
}
var initialized = false;
function initialize(e) {
// Initialize code only once.
// TODO: figure out why `initialize` is being called twice.
if (initialized) {
return;
}
initialized = true;
parseCode();
Reveal.addEventListener('slidechanged', updateCurrentSlide);
Reveal.addEventListener('fragmentshown', function(e) {
focusFragments(e.fragments);
});
// TODO: make this configurable.
// When fragments are hidden, clear the current focused fragments,
// and focus on the previous fragments.
Reveal.addEventListener('fragmenthidden', function(e) {
var index = e.fragment.getAttribute('data-fragment-index');
focusFragments(currentFragmentsList[index - 1]);
});
updateCurrentSlide(e);
}
// Highlight code and transform it into individual lines.
function parseCode() {
// TODO: mark as parsed.
forEach(document.querySelectorAll('pre code'), function(element) {
// Trim whitespace if the `data-trim` attribute is present.
if (element.hasAttribute('data-trim') && typeof element.innerHTML.trim == 'function') {
element.innerHTML = element.innerHTML.trim();
}
// Highlight code using highlight.js.
// TODO: avoid touching the element twice (when highlighting and generating lines).
hljs.highlightBlock(element);
// Split highlighted code into lines.
var openTags = [];
var reHtmlTag = /<(\/?)span(?:\s+(?:class=(['"])hljs-.*?\2)?\s*|\s*)>/g;
// Ensure that last line ends in a newline as our line-splitting algorithm
// requires lines to end in new lines.
var html = element.innerHTML;
if (html.charCodeAt(html.length - 1) != 10) {
html += '\n';
}
element.innerHTML = html.replace(/(.*?)\r?\n/g, function(_, string) {
if (!string) {
return '<span class=line> </span>';
}
var openTag, stringPrepend;
// Re-open all tags that were previously closed.
if (openTags.length) {
stringPrepend = openTags.join('');
}
// Match all HTML `<span>` tags.
reHtmlTag.lastIndex = 0;
while (openTag = reHtmlTag.exec(string)) {
// If it is a closing tag, remove the opening tag from the list.
if (openTag[1]) {
openTags.pop();
}
// Otherwise if it is an opening tag, push it to the list.
else {
openTags.push(openTag[0]);
}
}
// Close all opened tags, so that strings can be wrapped with `span.line`.
if (openTags.length) {
string += Array(openTags.length + 1).join('</span>');
}
if (stringPrepend) {
string = stringPrepend + string;
}
return '<span class=line>' + string + '</span>';
});
});
}
function updateCurrentSlide(e) {
currentSlide = e.currentSlide;
currentFragmentsList = [];
forEach(currentSlide.getElementsByClassName('fragment'), function(fragment) {
var fragmentIndex = fragment.getAttribute('data-fragment-index');
(
currentFragmentsList[fragmentIndex] ||
(currentFragmentsList[fragmentIndex] = [])
).push(fragment);
});
clearPreviousFocus();
// If moving back to a previous slide…
if (
currentFragmentsList.length &&
prevSlideData &&
(
prevSlideData.indexh > e.indexh ||
(prevSlideData.indexh == e.indexh && prevSlideData.indexv > e.indexv)
)
) {
// …return to the last fragment and highlight the code.
while (Reveal.nextFragment()) {}
var currentFragment = currentFragmentsList[currentFragmentsList.length - 1];
forEach(currentFragment, function(currentFragment) {
currentFragment.classList.add('current-fragment');
});
focusFragments(currentFragment);
}
// Update previous slide information.
prevSlideData = {
'indexh': e.indexh,
'indexv': e.indexv
};
}
// Removes any previously focused lines.
function clearPreviousFocus() {
forEach(currentSlide.querySelectorAll('pre code .line.focus'), function(line) {
line.classList.remove('focus');
});
}
function focusFragments(fragments) {
clearPreviousFocus();
if (!fragments) {
return;
}
forEach(fragments, function(fragment) {
var lines = fragment.getAttribute('data-code-focus');
if (!lines) {
return;
}
var codeBlock = parseInt(fragment.getAttribute('data-code-block'));
if (isNaN(codeBlock)) {
codeBlock = 1;
}
var preElems = currentSlide.querySelectorAll('pre');
if (!preElems.length) {
return;
}
var pre = preElems[codeBlock - 1];
var code = pre.querySelectorAll('code .line');
if (!code.length) {
return;
}
forEach(lines.split(','), function(line) {
lines = line.split('-');
if (lines.length == 1) {
focusLine(lines[0]);
} else {
var i = lines[0] - 1, j = lines[1];
while (++i <= j) {
focusLine(i);
}
}
});
var topLineNumber, bottomLineNumber;
function focusLine(lineNumber) {
// Convert from 1-based index to 0-based index.
lineNumber -= 1;
var line = code[lineNumber];
if (!line) {
return;
}
line.classList.add('focus');
if (scrollToFocused) {
if (topLineNumber == null) {
topLineNumber = bottomLineNumber = lineNumber;
} else {
if (lineNumber < topLineNumber) {
topLineNumber = lineNumber;
}
if (lineNumber > bottomLineNumber) {
bottomLineNumber = lineNumber;
}
}
}
}
// TODO: avoid touching the DOM layout properties multiple times for each fragment
if (scrollToFocused && topLineNumber != null) {
var topLine = code[topLineNumber];
var bottomLine = code[bottomLineNumber];
var codeParent = topLine.parentNode;
var scrollTop = topLine.offsetTop;
var scrollBottom = bottomLine.offsetTop + bottomLine.clientHeight;
codeParent.scrollTop = scrollTop - (codeParent.clientHeight - (scrollBottom - scrollTop)) / 2;
}
});
}
function RevealCodeFocus(options) {
if (!options) {
options = { 'scrollToFocused': true };
}
if (options.scrollToFocused != null) {
scrollToFocused = options.scrollToFocused;
}
if (Reveal.isReady()) {
initialize({ 'currentSlide': Reveal.getCurrentSlide() });
} else {
Reveal.addEventListener('ready', initialize);
}
}
window.RevealCodeFocus = RevealCodeFocus;
}(this, this.Reveal, this.hljs));