-
Notifications
You must be signed in to change notification settings - Fork 853
/
slideView.js
329 lines (265 loc) · 9.61 KB
/
slideView.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
var SlideNumber = require('../components/slide-number/slide-number')
, converter = require('../converter')
, highlighter = require('../highlighter')
, utils = require('../utils')
;
module.exports = SlideView;
function SlideView (events, slideshow, scaler, slide) {
var self = this;
self.events = events;
self.slideshow = slideshow;
self.scaler = scaler;
self.slide = slide;
self.slideNumber = new SlideNumber(slide, slideshow);
self.configureElements();
self.updateDimensions();
self.events.on('propertiesChanged', function (changes) {
if (changes.hasOwnProperty('ratio')) {
self.updateDimensions();
}
});
}
SlideView.prototype.updateDimensions = function () {
var self = this
, dimensions = self.scaler.dimensions
;
self.scalingElement.style.width = dimensions.width + 'px';
self.scalingElement.style.height = dimensions.height + 'px';
};
SlideView.prototype.scale = function (containerElement) {
var self = this;
self.scaler.scaleToFit(self.scalingElement, containerElement);
};
SlideView.prototype.show = function () {
utils.addClass(this.containerElement, 'remark-visible');
utils.removeClass(this.containerElement, 'remark-fading');
};
SlideView.prototype.hide = function () {
var self = this;
utils.removeClass(this.containerElement, 'remark-visible');
// Don't just disappear the slide. Mark it as fading, which
// keeps it on the screen, but at a reduced z-index.
// Then set a timer to remove the fading state in 1s.
utils.addClass(this.containerElement, 'remark-fading');
setTimeout(function(){
utils.removeClass(self.containerElement, 'remark-fading');
}, 1000);
};
SlideView.prototype.configureElements = function () {
var self = this;
self.containerElement = document.createElement('div');
self.containerElement.className = 'remark-slide-container';
self.scalingElement = document.createElement('div');
self.scalingElement.className = 'remark-slide-scaler';
self.element = createSlideElement(self.slide);
self.contentElement = createContentElement(self.events, self.slideshow, self.slide);
self.notesElement = createNotesElement(self.slideshow, self.slide.notes);
self.contentElement.appendChild(self.slideNumber.element);
self.element.appendChild(self.contentElement);
self.scalingElement.appendChild(self.element);
self.containerElement.appendChild(self.scalingElement);
self.containerElement.appendChild(self.notesElement);
};
SlideView.prototype.scaleBackgroundImage = function (dimensions) {
var self = this
, styles = window.getComputedStyle(this.contentElement)
, backgroundImage = styles.backgroundImage
, backgroundSize = styles.backgroundSize
, backgroundPosition = styles.backgroundPosition
, match
, image
, scale
;
// If the user explicitly sets the backgroundSize or backgroundPosition, let
// that win and early return here.
if ((backgroundSize || backgroundPosition) && !self.backgroundSizeSet) {
return;
}
if ((match = /^url\(("?)([^\)]+?)\1\)/.exec(backgroundImage)) !== null) {
image = new Image();
image.onload = function () {
if (image.width > dimensions.width ||
image.height > dimensions.height) {
// Background image is larger than slide
if (!self.originalBackgroundSize) {
// No custom background size has been set
self.originalBackgroundSize = self.contentElement.style.backgroundSize;
self.originalBackgroundPosition = self.contentElement.style.backgroundPosition;
self.backgroundSizeSet = true;
if (dimensions.width / image.width < dimensions.height / image.height) {
scale = dimensions.width / image.width;
}
else {
scale = dimensions.height / image.height;
}
self.contentElement.style.backgroundSize = image.width * scale +
'px ' + image.height * scale + 'px';
self.contentElement.style.backgroundPosition = '50% ' +
((dimensions.height - (image.height * scale)) / 2) + 'px';
}
}
else {
// Revert to previous background size setting
if (self.backgroundSizeSet) {
self.contentElement.style.backgroundSize = self.originalBackgroundSize;
self.contentElement.style.backgroundPosition = self.originalBackgroundPosition;
self.backgroundSizeSet = false;
}
}
};
image.src = match[2];
}
};
function createSlideElement(slide) {
var element = document.createElement('div');
element.className = 'remark-slide';
if (slide.properties.continued === 'true') {
utils.addClass(element, 'remark-slide-incremental');
}
return element;
}
function createContentElement (events, slideshow, slide) {
var element = document.createElement('div');
if (slide.properties.name) {
element.id = 'slide-' + slide.properties.name;
}
styleContentElement(slideshow, element, slide.properties);
element.innerHTML = converter.convertMarkdown(slide.content, slideshow.getLinks());
highlightCodeBlocks(element, slideshow);
return element;
}
function styleContentElement (slideshow, element, properties) {
element.className = '';
setClassFromProperties(element, properties);
setHighlightStyleFromProperties(element, properties, slideshow);
setBackgroundFromProperties(element, properties);
}
function createNotesElement (slideshow, notes) {
var element = document.createElement('div');
element.className = 'remark-slide-notes';
element.innerHTML = converter.convertMarkdown(notes, slideshow.getLinks());
highlightCodeBlocks(element, slideshow);
return element;
}
function setBackgroundFromProperties (element, properties) {
var backgroundImage = properties['background-image'];
var backgroundColor = properties['background-color'];
var backgroundSize = properties['background-size'];
var backgroundPosition = properties['background-position'];
if (backgroundImage) {
element.style.backgroundImage = backgroundImage;
}
if (backgroundColor) {
element.style.backgroundColor = backgroundColor;
}
if (backgroundSize) {
element.style.backgroundSize = backgroundSize;
}
if (backgroundPosition) {
element.style.backgroundPosition = backgroundPosition;
}
}
function setHighlightStyleFromProperties (element, properties, slideshow) {
var highlightStyle = properties['highlight-style'] ||
slideshow.getHighlightStyle();
if (highlightStyle) {
utils.addClass(element, 'hljs-' + highlightStyle);
}
}
function setClassFromProperties (element, properties) {
utils.addClass(element, 'remark-slide-content');
(properties['class'] || '').split(/,| /)
.filter(function (s) { return s !== ''; })
.forEach(function (c) { utils.addClass(element, c); });
}
function highlightCodeBlocks (content, slideshow) {
var codeBlocks = content.getElementsByTagName('code'),
highlightLines = slideshow.getHighlightLines(),
highlightSpans = slideshow.getHighlightSpans(),
highlightInline = slideshow.getHighlightInlineCode(),
meta;
codeBlocks.forEach(function (block) {
if (block.className === '') {
block.className = slideshow.getHighlightLanguage();
}
if (block.parentElement.tagName !== 'PRE') {
utils.addClass(block, 'remark-inline-code');
if (highlightInline) {
highlighter.engine.highlightBlock(block, '');
}
return;
}
if (highlightLines) {
meta = extractMetadata(block);
}
if (block.className !== '') {
highlighter.engine.highlightBlock(block, ' ');
}
wrapLines(block);
if (highlightLines) {
highlightBlockLines(block, meta.highlightedLines);
}
if (highlightSpans) {
// highlightSpans is either true or a RegExp
highlightBlockSpans(block, highlightSpans);
}
utils.addClass(block, 'remark-code');
});
}
function extractMetadata (block) {
var highlightedLines = [];
block.innerHTML = block.innerHTML.split(/\r?\n/).map(function (line, i) {
if (line.indexOf('*') === 0) {
highlightedLines.push(i);
return line.replace(/^\*( )?/, '$1$1');
}
return line;
}).join('\n');
return {
highlightedLines: highlightedLines
};
}
function wrapLines (block) {
var lines = block.innerHTML.split(/\r?\n/).map(function (line) {
return '<div class="remark-code-line">' + line + '</div>';
});
// Remove empty last line (due to last \n)
if (lines.length && lines[lines.length - 1].indexOf('><') !== -1) {
lines.pop();
}
block.innerHTML = lines.join('');
}
function highlightBlockLines (block, lines) {
lines.forEach(function (i) {
utils.addClass(block.childNodes[i], 'remark-code-line-highlighted');
});
}
/**
* @param highlightSpans `true` or a RegExp
*/
function highlightBlockSpans (block, highlightSpans) {
var pattern;
if (highlightSpans === true) {
pattern = /([^`])`([^`]+?)`/g;
} else if (highlightSpans instanceof RegExp) {
if (! highlightSpans.global) {
throw new Error('The regular expression in `highlightSpans` must have flag /g');
}
// Use [^] instead of dot (.) so that even newlines match
// We prefix the escape group, so users can provide nicer regular expressions
var flags = highlightSpans.flags || 'g'; // ES6 feature; use if it’s available
pattern = new RegExp('([^])' + highlightSpans.source, flags);
} else {
throw new Error('Illegal value for `highlightSpans`');
}
block.childNodes.forEach(function (element) {
element.innerHTML = element.innerHTML.replace(pattern,
function (m,e,c) {
if (e === '\\') {
return m.substr(1);
}
return e + '<span class="remark-code-span-highlighted">' +
c + '</span>';
});
});
}