-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypechohight.js
57 lines (49 loc) · 1.54 KB
/
typechohight.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
hljs.initHighlightingOnLoad();
hljs.initLineNumbersOnLoad({
singleLine: true
});
//前补0函数
function PrefixInteger(num, length) {
return (Array(length).join('0') + num).slice(-length);
}
var i = 0;
//增加COPY按钮
$("pre code").each(function() {
this.id = "pre_code_" + (i++);
$(this).after('<div class="div-copy-code">'
+'<button class="btn-copy-code" data-clipboard-action="copy" data-clipboard-target="#' + this.id + '">Copy</button>'
+'<button class="btn-copy-code btn-down-code-2-img" down-pic-id="' + this.id + '">Down2Img</button></div>');
});
$("button.btn-down-code-2-img").click(function(){
var codeNode=document.getElementById($(this).attr("down-pic-id"));
$(codeNode).toggleClass("take-pic");
//$("#text").attr("data_obj");
domtoimage.toJpeg(codeNode, { quality: 0.95 }).then(function (dataUrl) {
var link = document.createElement('a');
link.download = 'code.jpg';
link.href = dataUrl;
link.click();
link=null;
$(codeNode).toggleClass("take-pic");
});
//domtoimage.toPng(());
});
//COPY 按钮跟随滚动条
$("pre").scroll(function() {
var offsetTop = $(this).scrollTop();
$(this).find('.btn-copy-code').css('top', offsetTop + 5);
});
var clipboard = new ClipboardJS('.btn-copy-code');
clipboard.on('success',function(e) {
e.clearSelection();
$(e.trigger).text('success');
var time = setInterval(function() {
$(e.trigger).text('Copy');
clearInterval(time);
},
3000);
});
clipboard.on('error',function(e) {
console.error('Action:', e.action);
console.error('Trigger:', e.trigger);
});