-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscript.js
226 lines (181 loc) · 5.38 KB
/
script.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
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
/**
* DokuWiki Plugin copycode (Action Component)
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Nicolas Prigent <[email protected]>
*
* Adds a click event on all code blocks that copy the content of the block to clipboard
*
*/
class SourceCodeProvider {
constructor(source) {
if (this.constructor === SourceCodeProvider) {
throw new TypeError("Abstract class must be subclassed.");
}
this._source = source;
}
get_source_code() {
throw new TypeError("Not implemented.");
}
}
class SelectionProvider extends SourceCodeProvider {
get_source_code() {
return this._source.getSelection().toString();
}
}
class BlockProvider extends SourceCodeProvider {
constructor(source, inline=false) {
super(source);
this._inline = inline;
}
get_source_code() {
let result = this._source.textContent.split("_||copycode||_").join("\n");
if (this._inline) {
result = result.split("\n").join(" ");
}
return result;
}
}
class CopyCodeStrategy {
constructor() {
if (this.constructor === CopyCodeStrategy) {
throw new TypeError("Abstract class must be subclassed.");
}
}
get_message() {
throw new TypeError("Not implemented.");
}
copy() {
throw new TypeError("Not implemented.");
}
}
class DummyCopyStrategy {
get_message() {
return "";
}
copy() {
// dummy doens't da anything.
}
}
class CopyCodeStrategyBase {
constructor(source) {
if (this.constructor === CopyCodeStrategyBase) {
throw new TypeError("Abstract class must be subclassed.");
}
this._source = source;
this._provider = null;
}
get_message() {
return '<div class="' + this._alert_class + ' alert-copycode">' + this._message + "</div>";
}
copy() {
if (this._provider === null) {
throw new TypeError("No source-code provider available.");
}
let inputValue = this._provider.get_source_code();
// replacing problematic white space character that appears for no obvious reason :/
inputValue = inputValue.split(/\u00A0/).join("");
if (inputValue !== "") {
// check if clipboard is available in navigator
if (navigator.clipboard != undefined) {
//Copy raw text to clipboard
navigator.clipboard.writeText(inputValue);
} else {
// if for any reason the clipboard is unavalaible, uses the fake textarea hack to copy the content
let textarea = document.createElement("textarea");
textarea.value = inputValue;
textarea.style = "height: 1px; width : 1px";
document.body.appendChild(textarea);
textarea.select();
document.execCommand("copy");
textarea.remove();
}
}
}
}
class CopyHighlighted extends CopyCodeStrategyBase {
constructor(source) {
super(source);
this._message = LANG.plugins.copycode["highlightedcopied"];
this._alert_class = "orange";
this._provider = new SelectionProvider(this._source)
}
}
class CopyBlock extends CopyCodeStrategyBase {
constructor(source) {
super(source);
this._message = LANG.plugins.copycode["copied"];
this._alert_class = "green";
this._provider = new BlockProvider(this._source)
}
}
class CopyBlockInline extends CopyBlock {
constructor(source) {
super(source);
this._message = LANG.plugins.copycode["copiedinline"];
this._alert_class = "blue";
this._provider = new BlockProvider(this._source, true)
}
}
jQuery(document).ready(function ($) {
//detects mouseup after scroll
var scrolling = false;
function preventClickOnScroll () {
$(window).mouseup(function(){
scrolling = false;
});
}
function alertMessage(message) {
$("body").append(message);
window.setTimeout(function () {
$(".alert-copycode")
.fadeTo(500, 0)
.slideUp(500, function () {
$(this).remove();
});
}, 1000);
}
//enabled <code> and <file> blocks on all wiki pages and hooks(sidebar,header,mainpage,dropdownpages etc.)
var blocs = $(".dokuwiki pre.code, .dokuwiki pre.file");
//enabled inlinecodes ''like this''
if (JSINFO.plugins.copycode.EnableForInline)
blocs = blocs.add(".dokuwiki code");
blocs.addClass("enabled-copycode");
for (i = 0; i < blocs.length; i++) {
if (JSINFO.plugins.copycode.EnableBlockInline) {
//deactivate context menu on right click
$(blocs[i]).on("contextmenu", function (evt) {
if (window.getSelection().toString() == "") {
evt.preventDefault();
}
});
}
// detects scrolling on element
$(blocs[i]).scroll(function() {
scrolling = true;
preventClickOnScroll();
});
$(blocs[i]).mouseup(function (event) {
if (!scrolling){
let strategy = new DummyCopyStrategy();
if (JSINFO.plugins.copycode.EnableForHighlighted) {
strategy = new CopyHighlighted(window);
}
if (window.getSelection().toString() === "") {
strategy = new CopyBlock(this);
if (event.which === 3) {
strategy = new DummyCopyStrategy();
if (JSINFO.plugins.copycode.EnableBlockInline) {
strategy = new CopyBlockInline(this);
}
}
}
strategy.copy();
alertMessage(strategy.get_message());
}
});
line = $(blocs[i])
.find("ol > li")
.append('<span class="copycode_line">_||copycode||_</span>');
}
});