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
|
const codeActionButtons = [
{
icon: 'copy',
id: 'copy',
title: 'Copy Code',
show: true
},
{
icon: 'order',
id: 'lines',
title: 'Toggle Line Numbers',
show: true
},
{
icon: 'carly',
id: 'wrap',
title: 'Toggle Line Wrap',
show: false
},
{
icon: 'expand',
id: 'expand',
title: 'Toggle code block expand',
show: false
}
];
const body = elem('body');
const maxLines = parseInt(body.dataset.code);
const copyId = 'panel_copy';
const wrapId = 'panel_wrap';
const linesId = 'panel_lines';
const panelExpand = 'panel_expand';
const panelExpanded = 'panel_expanded';
const panelHide = 'panel_hide';
const panelFrom = 'panel_from';
const panelBox = 'panel_box';
const fullHeight = 'initial';
const highlightWrap = 'highlight_wrap'
function codeBlocks() {
const markedCodeBlocks = elems('code');
const blocks = Array.from(markedCodeBlocks).filter(function(block){
return hasClasses(block) && !Array.from(block.classList).includes('noClass');
}).map(function(block){
return block
});
return blocks;
}
function codeBlockFits(block) {
// return false if codeblock overflows
const blockWidth = block.offsetWidth;
const highlightBlockWidth = block.parentNode.parentNode.offsetWidth;
return blockWidth <= highlightBlockWidth ? true : false;
}
function maxHeightIsSet(elem) {
let maxHeight = elem.style.maxHeight;
return maxHeight.includes('px')
}
function restrainCodeBlockHeight(lines) {
const lastLine = lines[maxLines-1];
let maxCodeBlockHeight = fullHeight;
if(lastLine) {
const lastLinePos = lastLine.offsetTop;
if(lastLinePos !== 0) {
maxCodeBlockHeight = `${lastLinePos}px`;
const codeBlock = lines[0].parentNode;
const outerBlock = codeBlock.closest('.highlight');
const isExpanded = containsClass(outerBlock, panelExpanded);
if(!isExpanded) {
codeBlock.dataset.height = maxCodeBlockHeight;
codeBlock.style.maxHeight = maxCodeBlockHeight;
}
}
}
}
const blocks = codeBlocks();
function collapseCodeBlock(block) {
const lines = elems('.ln', block);
const codeLines = lines.length;
if (codeLines > maxLines) {
const expandDot = createEl()
pushClass(expandDot, panelExpand);
pushClass(expandDot, panelFrom);
expandDot.title = "Toggle code block expand";
expandDot.textContent = "...";
const outerBlock = block.closest('.highlight');
window.setTimeout(function(){
const expandIcon = outerBlock.nextElementSibling.lastElementChild;
deleteClass(expandIcon, panelHide);
}, 150)
restrainCodeBlockHeight(lines);
const highlightElement = block.parentNode.parentNode;
highlightElement.appendChild(expandDot);
}
}
blocks.forEach(function(block){
collapseCodeBlock(block);
})
function actionPanel() {
const panel = createEl();
panel.className = panelBox;
codeActionButtons.forEach(function(button) {
// create button
const btn = createEl('a');
btn.href = '#';
btn.title = button.title;
btn.className = `icon panel_icon panel_${button.id}`;
button.show ? false : pushClass(btn, panelHide);
// load icon inside button
loadSvg(button.icon, btn);
// append button on panel
panel.appendChild(btn);
});
return panel;
}
function toggleLineNumbers(elems) {
elems.forEach(function (elem, index) {
// mark the code element when there are no lines
modifyClass(elem, 'pre_nolines')
});
restrainCodeBlockHeight(elems);
}
function toggleLineWrap(elem) {
modifyClass(elem, 'pre_wrap');
// retain max number of code lines on line wrap
const lines = elems('.ln', elem);
restrainCodeBlockHeight(lines);
}
function copyCode(codeElement) {
lineNumbers = elems('.ln', codeElement);
// remove line numbers before copying
if(lineNumbers.length) {
lineNumbers.forEach(function(line){
line.remove();
});
}
const codeToCopy = codeElement.textContent;
// copy code
copyToClipboard(codeToCopy);
}
function disableCodeLineNumbers(block){
const lines = elems('.ln', block)
toggleLineNumbers(lines);
}
(function codeActions(){
const blocks = codeBlocks();
const highlightWrapId = highlightWrap;
blocks.forEach(function(block){
// disable line numbers if disabled globally
const showLines = elem('body').dataset.lines;
parseBoolean(showLines) === false ? disableCodeLineNumbers(block) : false;
const highlightElement = block.parentNode.parentNode;
// wrap code block in a div
const highlightWrapper = createEl();
highlightWrapper.className = highlightWrapId;
wrapEl(highlightElement, highlightWrapper);
const panel = actionPanel();
// show wrap icon only if the code block needs wrapping
const wrapIcon = elem(`.${wrapId}`, panel);
codeBlockFits(block) ? false : deleteClass(wrapIcon, panelHide);
// append buttons
highlightWrapper.appendChild(panel);
});
function isItem(target, id) {
// if is item or within item
return target.matches(`.${id}`) || target.closest(`.${id}`);
}
function showActive(target, targetClass,activeClass = 'active') {
const active = activeClass;
const targetElement = target.matches(`.${targetClass}`) ? target : target.closest(`.${targetClass}`);
deleteClass(targetElement, active);
setTimeout(function() {
modifyClass(targetElement, active)
}, 50)
}
doc.addEventListener('click', function(event){
// copy code block
const target = event.target;
const isCopyIcon = isItem(target, copyId);
const isWrapIcon = isItem(target, wrapId);
const isLinesIcon = isItem(target, linesId);
const isExpandIcon = isItem(target, panelExpand);
const isActionable = isCopyIcon || isWrapIcon || isLinesIcon || isExpandIcon;
if(isActionable) {
event.preventDefault();
showActive(target, 'icon');
const codeElement = target.closest(`.${highlightWrapId}`).firstElementChild.firstElementChild;
let lineNumbers = elems('.ln', codeElement);
isWrapIcon ? toggleLineWrap(codeElement) : false;
isLinesIcon ? toggleLineNumbers(lineNumbers) : false;
if (isExpandIcon) {
let thisCodeBlock = codeElement.firstElementChild;
const outerBlock = thisCodeBlock.closest('.highlight');
if(maxHeightIsSet(thisCodeBlock)) {
thisCodeBlock.style.maxHeight = fullHeight;
// mark code block as expanded
pushClass(outerBlock, panelExpanded)
} else {
thisCodeBlock.style.maxHeight = thisCodeBlock.dataset.height;
// unmark code block as expanded
deleteClass(outerBlock, panelExpanded)
}
}
if(isCopyIcon) {
// clone code element
const codeElementClone = codeElement.cloneNode(true);
copyCode(codeElementClone);
}
}
});
(function addLangLabel() {
const blocks = codeBlocks();
blocks.forEach(function(block){
let label = block.dataset.lang;
label = label === 'sh' ? 'bash' : label;
if(label !== "fallback") {
const labelEl = createEl();
labelEl.textContent = label;
pushClass(labelEl, 'lang');
block.closest(`.${highlightWrap}`).appendChild(labelEl);
}
});
})();
})();
|