generated from songquanpeng/blog-theme-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.ejs
58 lines (51 loc) · 2.08 KB
/
code.ejs
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
<%- include('./partials/header') %>
<div class="index posts-expand">
<article itemscope="" itemtype="http://schema.org/Article" class="post-block" lang="en" style="opacity: 1; display: block;">
<%- include('./partials/page-info') %>
<div class="post-body" itemprop="articleBody" style="opacity: 1; display: block; transform: translateY(0px);">
<div class="control-panel" style="margin-top: 16px">
<button class="btn btn-light" id="darkBtn" onclick="changeBackground()">Focus</button>
<button class="btn btn-light" onclick="copyCode()">Copy</button>
</div>
<pre>
<code id="code-display"><%=page.content%></code>
</pre>
<%- config.copyright %>
<%- config.ad %>
<label>
<textarea id="hiddenTextArea" style="position: absolute;left: -100%;"></textarea>
</label>
</div>
<footer class="post-footer">
<%- include('./partials/prev-next') %>
</footer>
</article>
</div>
<script>
const body = document.getElementsByTagName("body")[0];
const darkBtn = document.getElementById('darkBtn');
let dark = false;
function copyCode() {
const copyText = document.getElementById("code-display").textContent;
const textArea = document.getElementById('hiddenTextArea');
textArea.textContent = copyText;
document.body.append(textArea);
textArea.select();
document.execCommand("copy");
}
let e = document.querySelector('#code-display');
function changeBackground() {
const comment = document.getElementsByClassName("comment")[0];
let backgroundColor = getComputedStyle(e).backgroundColor;
if(dark){
body.setAttribute('style', '');
if(comment) comment.style.cssText = "";
}else{
body.setAttribute('style', 'background:'+backgroundColor);
if(comment) comment.style.cssText = "visibility:hidden";
}
dark = !dark;
}
</script>
<%- include('./partials/comment') %>
<%- include('./partials/footer') %>