forked from noam3127/github-code-folding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
174 lines (158 loc) · 5.47 KB
/
main.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
(function() {
'use strict';
// In case this script has already been run and modified the DOM on a previous page in github,
// make sure to reset it.
(function resetPage() {
const arrows = document.querySelectorAll('.collapser');
arrows.forEach(a => a.parentNode.removeChild(a));
const ellipsis = document.querySelectorAll('.ellipsis');
ellipsis.forEach(e => e.parentNode.removeChild(e));
const hiddenLines = document.querySelectorAll('.hidden-line');
hiddenLines.forEach(l => l.classList.remove('hidden-line'));
})()
const [...codeLines] = document.querySelectorAll('.file table.highlight .blob-code-inner');
const codeLinesText = codeLines.map(l => l.textContent);
const _arrow =
'<svg version="1.1" width="7px" fill="#969896" xmlns="http://www.w3.org/2000/svg" '+
'xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 1000 1000" enable-background="new 0 0 1000 1000" xml:space="preserve">'+
'<metadata> Svg Vector Icons : http://www.onlinewebfonts.com/icon </metadata>' +
'<g><path d="M579.5,879.8c-43.7,75.7-115.3,75.7-159,0L28.7,201.1c-43.7-75.7-8-137.7,79.5-137.7h783.7c87.5,0,123.2,62,79.5,137.7L579.5,879.8z"></path></g>'+
'</svg>';
class Element {
constructor(name) {
this.element = document.createElement(name);
}
addClass(className) {
this.element.classList.add(className)
return this;
}
setId(id) {
this.element.id = id;
return this;
}
setHTML(str) {
this.element.innerHTML = str;
return this;
}
}
const arrowFactory = (id) => {
return new Element('span')
.addClass('collapser')
.setId(id)
.setHTML(_arrow)
.element;
}
const ellipsisFactory = (id) => {
return new Element('span')
.addClass('pl-smi')
.addClass('ellipsis')
.setId(id)
.setHTML('...')
.element;
};
const spaceMap = new Map();
const pairs = new Map();
const stack = [];
const blockStarts = [];
const countLeadingWhitespace = arr => {
const getWhitespaceIndex = i => {
if (arr[i] !== ' ' && arr[i] !== '\t') {
return i;
}
i++;
return getWhitespaceIndex(i);
}
return getWhitespaceIndex(0);
};
const last = arr => arr[arr.length - 1];
const getPreviousSpaces = (map, lineNum) => {
let prev = map.get(lineNum - 1);
return prev === -1 ? getPreviousSpaces(map, lineNum - 1) : {lineNum: lineNum - 1, count: prev};
};
for (let lineNum = 0; lineNum < codeLinesText.length; lineNum++) {
let line = codeLinesText[lineNum];
let count = line.trim().length ? countLeadingWhitespace(line.split('')) : -1;
spaceMap.set(lineNum, count);
function tryPair() {
let top = last(stack);
if (count !== -1 && count <= spaceMap.get(top)) {
pairs.set(top, lineNum);
codeLines[top].setAttribute('block-start', 'true');
codeLines[top].appendChild(arrowFactory(`gcf-${top + 1}`));
blockStarts.push(codeLines[top]);
stack.pop();
return tryPair();
}
}
tryPair();
let prevSpaces = getPreviousSpaces(spaceMap, lineNum);
if (count > prevSpaces.count) {
stack.push(prevSpaces.lineNum);
}
}
const toggleCode = (action, start, end) => {
if (action === 'hide') {
const sliced = codeLines.slice(start, end);
sliced.forEach(elem => {
let tr = elem.parentNode;
if (tr.classList) {
tr.classList.add('hidden-line');
} else {
tr.className += ' hidden-line';
}
// Check if any children blocks of this parent block have previously
// been collapsed so we can remove ellipsis and reset arrows
let previouslyCollapsed = elem.querySelectorAll('.ellipsis');
previouslyCollapsed.forEach(block => {
block.previousSibling.classList.remove('sideways');
block.parentNode.removeChild(block);
});
});
codeLines[start - 1].appendChild(ellipsisFactory(`ellipsis-${start - 1}`));
} else if (action === 'show') {
const sliced = codeLines.slice(start, end);
const topLine = codeLines[start - 1];
sliced.forEach(elem => {
let tr = elem.parentNode;
if (tr.classList) {
tr.classList.remove('hidden-line');
} else {
tr.className.replace(' hidden-line', '');
}
});
topLine.removeChild(topLine.lastChild);
}
};
const arrows = document.querySelectorAll('.collapser');
function arrowListener(e) {
e.preventDefault();
let svg = e.currentTarget;
let td = e.currentTarget.parentElement;
let id = td.getAttribute('id');
let index = parseInt(id.slice(2)) - 1;
if (svg.classList.contains('sideways')) {
svg.classList.remove('sideways');
toggleCode('show', index + 1, pairs.get(index));
} else {
svg.classList.add('sideways');
toggleCode('hide', index + 1, pairs.get(index));
}
};
arrows.forEach(c => {
c.addEventListener('click', arrowListener);
});
function ellipsisListener(e) {
if (!e.target.parentElement) return;
if (e.target.classList.contains('ellipsis')) {
let td = e.target.parentElement;
let svg = td.querySelector('.sideways');
let id = e.target.parentElement.getAttribute('id');
let index = parseInt(id.slice(2)) - 1;
svg.classList.remove('sideways');
toggleCode('show', index + 1, pairs.get(index));
}
};
blockStarts.forEach(line => {
line.addEventListener('click', ellipsisListener);
});
})()