-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodeblock-resizer.js
51 lines (46 loc) · 1.59 KB
/
codeblock-resizer.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
+function($) {
'use strict';
// Resize code blocks to fit the screen width
var CodeBlockResizer = function(elem) {
this.$codeBlocks = $(elem);
};
CodeBlockResizer.prototype = {
/**
* Run main feature
*/
run: function() {
var self = this;
// resize all codeblocks
self.resize();
// resize codeblocks when window is resized
$(window).smartresize(function() {
self.resize();
});
},
/**
* Resize codeblocks
*/
resize: function() {
var self = this;
self.$codeBlocks.each(function() {
var $gutter = $(this).find('.gutter');
var $code = $(this).find('.code');
// get padding of code div
var codePaddings = $code.width() - $code.innerWidth();
// code block div width with padding - gutter div with padding + code div padding
var width = $(this).outerWidth() - $gutter.outerWidth() + codePaddings;
// apply new width
$code.css('width', width);
$code.children('pre').css('width', width);
});
}
};
$(document).ready(function() {
// register jQuery function to check if an element has an horizontal scroll bar
$.fn.hasHorizontalScrollBar = function() {
return this.get(0).scrollWidth > this.innerWidth();
};
var resizer = new CodeBlockResizer('figure.highlight');
resizer.run();
});
}(jQuery);