forked from nicolasprigent/Dokuwiki-Copycode-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
83 lines (60 loc) · 2.26 KB
/
script.js
File metadata and controls
83 lines (60 loc) · 2.26 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
/**
* DokuWiki Plugin copycode (Action Component)
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Nicolas Prigent <mail.nicolasprigent@gmail.com>
*
* Adds a click event on all code blocks that copy the content of the block to clipboard
*
*/
document.addEventListener('DOMContentLoaded', function () {
var bloc_code = jQuery("pre.code, pre.file, code");
for(i=0;i<bloc_code.length;i++){
bloc_code[i].addEventListener('click', function(){
selected_text = window.getSelection().toString();
if (selected_text != "") {
writeToClipboard(selected_text);
}else{
writeToClipboard(this);
}
});
line = jQuery(bloc_code[i]).find("ol > li").append('<span class="copycode_line">_||copycode||_</span>');
}
});
function writeToClipboard(elem) {
inputValue = '';
if (typeof elem == 'string'){
inputValue = elem;
alertText = 'selectioncopied';
alertClass = 'orange';
}else{
inputValue = elem.textContent;
if (inputValue) {
inputValue = inputValue.split("_||copycode||_").join("\n");
alertText = 'copied';
alertClass = 'green';
}
}
if (inputValue != '') {
if (navigator.clipboard != undefined) {
navigator.clipboard.writeText(inputValue);
} else {
/*if for any reason the clipboard is unavalaible, uses the fake textarea hack to copy the content*/
var $textarea = jQuery('<textarea />');
$textarea.val(inputValue).css({ width: "1px", height: "1px" }).appendTo('body');
$textarea.select();
document.execCommand('copy');
$textarea.remove();
}
alertMessage(LANG.plugins.copycode[alertText], alertClass);
}
};
function alertMessage(message, alertclass){
var alertMsg = '<div class="' + alertclass + ' alert-copycode">' + message + '</div>';
jQuery( "body" ).append( alertMsg );
window.setTimeout(function() {
jQuery(".alert-copycode").fadeTo(500, 0).slideUp(500, function(){
jQuery(this).remove();
});
}, 1000);
};