-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhide-code.js
33 lines (31 loc) · 1.32 KB
/
hide-code.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
function hideCode(hook, vm) {
var hideCodeConfig = vm.config.hideCode;
hook.doneEach(function () {
if (hideCodeConfig) {
var targetElms = Array.apply(null, document.querySelectorAll("pre[data-lang]"));
var template = ['<div class="hide-code-mask">', '<div class="hide-code-mask-btn">', '</div>'].join("");
targetElms.forEach(function (elm) {
if (!hideCodeConfig.scroll && hideCodeConfig.height < elm.offsetHeight) {
elm.classList.add('hide-code');
}
elm.style.maxHeight = hideCodeConfig.height + 'px';
elm.insertAdjacentHTML("beforeend", template);
});
}
});
hook.mounted(function () {
if (hideCodeConfig) {
var listenerHost = document.querySelector(".content");
listenerHost.addEventListener("click", function (evt) {
var isCopyCodeButton = evt.target.classList.contains("hide-code-mask-btn");
if (isCopyCodeButton) {
var buttonElm = evt.target;
var preElm = buttonElm.parentNode.parentNode;
preElm.classList.remove('hide-code');
preElm.style.maxHeight = "";
}
});
}
});
}
export default hideCode;