Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
91 lines (79 loc) · 2.55 KB
/
Copy pathmain.js
File metadata and controls
91 lines (79 loc) · 2.55 KB
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
// @ts-check
'use strict';
(function () {
function onLoad() {
const code = document.getElementsByTagName('code')[0];
const [...childs] = code.childNodes;
// @ts-ignore
childs.filter(n => n.nodeType === 1 && n.hasAttribute('range-start')).forEach(n => {
n.addEventListener('click', toggleLines);
});
}
function toggleLines(e, collapse) {
let lineSpan, recursive, isExpandAction;
if (arguments.length === 2) {
lineSpan = e;
recursive = true;
isExpandAction = !collapse;
if (isExpandAction) {
lineSpan.classList.remove('collapsed');
} else {
lineSpan.classList.add('collapsed');
}
} else {
lineSpan = e.target.parentNode;
recursive = e.shiftKey;
isExpandAction = isCollapspedLine(lineSpan);
lineSpan.classList.toggle('collapsed');
}
const blockEndNum = getFoldingRangeEnd(lineSpan);
let span = lineSpan;
let currentLineNum = getLineNum(lineSpan);
let skipLineEndNum = -1;
while ((span = span.nextElementSibling) && ++currentLineNum <= blockEndNum) {
if (isExpandAction) {
if (currentLineNum > skipLineEndNum || recursive) {
span.classList.remove('hidden-line');
span.nextSibling.textContent = '\n';
if (isCollapspedLine(span)) {
skipLineEndNum = getFoldingRangeEnd(span);
if (recursive) {
span.classList.remove('collapsed');
}
}
}
} else {
if (isRangeStartLine(span) && recursive) {
span.classList.add('collapsed');
}
span.classList.add('hidden-line');
span.nextSibling.textContent = '';
}
}
}
function getLineNum(element) {
return parseInt(element.attributes.getNamedItem('start').value);
}
function isRangeStartLine(element) {
return element.hasAttribute('range-start');
}
function isCollapspedLine(element) {
return element.classList.contains('collapsed');
}
function getFoldingRangeEnd(element) {
return parseInt(element.attributes.getNamedItem('range-end').value);
}
window.addEventListener('message', event => {
const message = event.data;
const code = document.getElementsByTagName('code')[0];
const [...childs] = code.childNodes;
// @ts-ignore
const lineSpan = childs.find(n => n.nodeType === 1 && n.hasAttribute('range-start'));
toggleLines(lineSpan, message.command === 'foldAll');
});
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', onLoad);
} else {
onLoad();
}
})()