PluginProbe
Copy Anything to Clipboard for WordPress – Copy Button, Copy Text & Copy Code / 2.2.2
Copy Anything to Clipboard for WordPress – Copy Button, Copy Text & Copy Code v2.2.2
5.5.3 3.1.0 3.2.0 3.2.1 3.3.0 3.4.0 3.4.1 3.4.2 3.4.3 3.5.0 3.5.1 3.5.2 3.6.0 3.7.0 3.8.0 3.8.1 3.8.2 3.8.3 4.0.0 4.0.2 4.0.3 4.0.4 4.0.5 4.1.0 4.1.1 All 78 releases
copy-the-code / assets / js / copy-the-code.js

copy-the-code.js in Copy Anything to Clipboard for WordPress – Copy Button, Copy Text & Copy Code 2.2.2, at assets/js/copy-the-code.js

372 lines 12.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 window.CopyTheCodeToClipboard = (function (window, document, navigator) {
2 var textArea,
3 copy;
4
5 function isOS() {
6 return navigator.userAgent.match(/ipad|iphone/i);
7 }
8
9 function createTextArea(text) {
10 textArea = document.createElement('textArea');
11 textArea.value = text;
12 document.body.appendChild(textArea);
13 }
14
15 function selectText() {
16 var range,
17 selection;
18
19 if (isOS()) {
20 range = document.createRange();
21 range.selectNodeContents(textArea);
22 selection = window.getSelection();
23 selection.removeAllRanges();
24 selection.addRange(range);
25 textArea.setSelectionRange(0, 999999);
26 } else {
27 textArea.select();
28 }
29 }
30
31 function copyToClipboard() {
32 document.execCommand('copy');
33 document.body.removeChild(textArea);
34 }
35
36 copy = function (text) {
37 createTextArea(text);
38 selectText();
39 copyToClipboard();
40 };
41
42 return {
43 copy: copy
44 };
45 })(window, document, navigator);
46
47 (function ($) {
48
49 CopyTheCode = {
50
51 selector: copyTheCode.settings.selector || copyTheCode.selector || 'pre',
52 button_position: copyTheCode.settings['button-position'] || 'inside',
53
54 /**
55 * Init
56 */
57 init: function () {
58 this._bind();
59 this._initialize();
60 },
61
62 /**
63 * Binds events
64 */
65 _bind: function () {
66 $(document).on('click', '.copy-the-code-button', CopyTheCode.copyCode);
67 $(document).on('click', '.copy-the-code-shortcode', CopyTheCode.copyShortcode);
68 },
69
70 /**
71 * Initialize the Button
72 */
73 _initialize: function () {
74 if (!$(copyTheCode.selectors).length) {
75 return;
76 }
77
78 $(copyTheCode.selectors).each(function (index, el) {
79 var button_copy_text = el['button_copy_text'] || '';
80 var button_position = el['button_position'] || '';
81 var button_text = el['button_text'] || '';
82 var button_title = el['button_title'] || '';
83 var selector = el['selector'] || '';
84 var style = el['style'] || '';
85
86 $(selector).each(function (index, single_selector) {
87
88 var buttonMarkup = CopyTheCode._getButtonMarkup(button_title, button_text, style);
89
90 $(single_selector).addClass('copy-the-code-target');
91
92 if ('cover' !== style && 'outside' === button_position) {
93 $(single_selector).wrap('<span data-button-text="' + button_text + '" data-button-position="' + button_position + '" data-button-copy-text="' + button_copy_text + '" data-style="' + style + '" data-button-title="' + button_title + '" data-selector="' + selector + '" class="copy-the-code-wrap copy-the-code-style-' + style + ' copy-the-code-outside-wrap"></span>');
94 $(single_selector).parent().prepend('<div class="copy-the-code-outside">' + buttonMarkup + '</div>');
95 } else {
96 $(single_selector).wrap('<span data-button-text="' + button_text + '" data-button-position="' + button_position + '" data-button-copy-text="' + button_copy_text + '" data-style="' + style + '" data-button-title="' + button_title + '" data-selector="' + selector + '" class="copy-the-code-wrap copy-the-code-style-' + style + ' copy-the-code-inside-wrap"></span>');
97 $(single_selector).append(buttonMarkup);
98 }
99
100 switch (style) {
101 case 'svg-icon':
102 $(single_selector).find('.copy-the-code-button').html(copyTheCode.buttonSvg);
103 break;
104 case 'cover':
105 case 'button':
106 default:
107 $(single_selector).find('.copy-the-code-button').html(button_text);
108 break;
109 }
110
111
112 });
113 });
114
115 },
116
117 /**
118 * Get Copy Button Markup
119 */
120 _getButtonMarkup: function (button_title, button_text, style) {
121 if ('svg-icon' === style) {
122 button_text = copyTheCode.buttonSvg;
123 }
124
125 return '<button class="copy-the-code-button" data-style="' + style + '" title="' + button_title + '">' + button_text + '</button>';
126 },
127
128 format: function (html) {
129 var tab = '\t';
130 var result = '';
131 var indent = '';
132
133 html.split(/>\s*</).forEach(function (element) {
134 if (element.match(/^\/\w/)) {
135 indent = indent.substring(tab.length);
136 }
137
138 result += indent + '<' + element + '>\r\n';
139
140 if (element.match(/^<?\w[^>]*[^\/]$/) && !element.startsWith("input")) {
141 indent += tab;
142 }
143 });
144
145 return result.substring(1, result.length - 3);
146 },
147
148 /**
149 * Copy to Clipboard
150 */
151 copyShortcode: function (event) {
152 event.preventDefault();
153
154 var btn = $(this),
155 oldText = btn.text(),
156 target = btn.attr('data-target') || '',
157 copy_content_as = btn.attr('data-copy-as') || copyTheCode.copy_content_as,
158 button_copy_text = btn.attr('data-button-copy-text') || '',
159 content = btn.attr('data-content') || '';
160
161 // Copy the secrate content.
162 if (content) {
163 CopyTheCodeToClipboard.copy(content);
164 // Copied!
165 btn.text(button_copy_text);
166 setTimeout(function () {
167 btn.text(oldText);
168 }, 1000);
169 return;
170 }
171
172 var source = $(target);
173
174 if (!source.length) {
175 btn.text('Not found!');
176 setTimeout(function () {
177 btn.text(oldText);
178 }, 1000);
179 return;
180 }
181
182 var html = source.html();
183
184 html = CopyTheCode.format(html);
185
186 if ('html' !== copy_content_as) {
187
188 // Convert the <br/> tags into new line.
189 var brRegex = /<br\s*[\/]?>/gi;
190 html = html.replace(brRegex, "\n");
191
192 // Convert the <div> tags into new line.
193 var divRegex = /<div\s*[\/]?>/gi;
194 html = html.replace(divRegex, "\n");
195
196 // Convert the <p> tags into new line.
197 var pRegex = /<p\s*[\/]?>/gi;
198 html = html.replace(pRegex, "\n");
199
200 // Convert the <li> tags into new line.
201 var pRegex = /<li\s*[\/]?>/gi;
202 html = html.replace(pRegex, "\n");
203
204 // Remove all tags.
205 html = html.replace(/(<([^>]+)>)/ig, '');
206 }
207
208 if ('html' !== copy_content_as) {
209 html = html.replace(/[\t\n]+/gm, ' ').trim();
210 } else {
211 var reWhiteSpace = new RegExp("/^\s+$/");
212 html = html.replace(reWhiteSpace, "");
213 }
214
215 var tempElement = $("<div id='temp-element'></div>");
216 $("body").append(tempElement);
217 html = $.trim(html);
218 $('#temp-element').html(html);
219 var html = $('#temp-element').html();
220 $('#temp-element').remove();
221
222 var tempHTML = html;
223
224 // Copy the Code.
225 var tempPre = $("<textarea id='temp-pre'>"),
226 temp = $("<textarea>");
227
228 // Append temporary elements to DOM.
229 $("body").append(temp);
230 $("body").append(tempPre);
231
232 // Set temporary HTML markup.
233 tempPre.html(tempHTML);
234
235 var content = tempPre.text();
236
237 content = $.trim(content);
238
239 // Format the HTML markup.
240 temp.val(content).select();
241
242 // Support for IOS devices too.
243 CopyTheCodeToClipboard.copy(content);
244
245 // Remove temporary elements.
246 temp.remove();
247 tempPre.remove();
248
249 // Copied!
250 btn.text(button_copy_text);
251 setTimeout(function () {
252 btn.text(oldText);
253 }, 1000);
254 },
255
256 /**
257 * Copy to Clipboard
258 */
259 copyCode: function (event) {
260 event.preventDefault();
261
262 var btn = $(this),
263 oldText = btn.text(),
264 parent = btn.parents('.copy-the-code-wrap'),
265 selector = parent.attr('data-selector') || '',
266 button_text = parent.attr('data-button-text') || '',
267 button_position = parent.attr('data-button-position') || '',
268 button_copy_text = parent.attr('data-button-copy-text') || '',
269 button_title = parent.attr('data-button-title') || '',
270 style = parent.attr('data-style') || '';
271
272 // Fix: nested selectors e.g. `.entry-content pre`
273 if (selector.indexOf(' ') >= 0) {
274 var source = btn.parents('.copy-the-code-wrap');
275 } else {
276 var source = btn.parents('.copy-the-code-wrap').find(selector);
277 }
278
279 var html = source.html();
280
281 // Remove all duplicate empty lines.
282 html = html.replace(/^(?=\n)$|^\s*|\s*$|\n\n+/gm,"");
283
284 if ('html' !== copyTheCode.copy_content_as) {
285 // Convert the <br/> tags into new line.
286 var brRegex = /<br\s*[\/]?>/gi;
287 html = html.replace(brRegex, "\n");
288
289 // Convert the <div> tags into new line.
290 var divRegex = /<div\s*[\/]?>/gi;
291 html = html.replace(divRegex, "\n");
292
293 // Convert the <p> tags into new line.
294 var pRegex = /<p\s*[\/]?>/gi;
295 html = html.replace(pRegex, "\n");
296
297 // Convert the <li> tags into new line.
298 var pRegex = /<li\s*[\/]?>/gi;
299 html = html.replace(pRegex, "\n");
300
301 // Remove all tags.
302 html = html.replace(/(<([^>]+)>)/ig, '');
303 }
304
305 // Remove white spaces.
306 var reWhiteSpace = new RegExp("/^\s+$/");
307 html = html.replace(reWhiteSpace, "");
308
309 var tempElement = $("<div id='temp-element'></div>");
310 $("body").append(tempElement);
311 html = $.trim(html);
312 $('#temp-element').html(html);
313 var html = $('#temp-element').html();
314 $('#temp-element').remove();
315
316 var tempHTML = html;
317 var reWhiteSpace = new RegExp("/^\s+$/");
318 tempHTML = tempHTML.replace(reWhiteSpace, "");
319
320 var buttonMarkup = CopyTheCode._getButtonMarkup(button_title, button_text, style);
321
322 // Remove the <copy> button.
323 tempHTML = tempHTML.replace(buttonMarkup, '');
324
325 // Remove button text.
326 tempHTML = tempHTML.replace(button_text, '');
327
328 // Copy the Code.
329 var tempPre = $("<textarea id='temp-pre'>"),
330 temp = $("<textarea>");
331
332 // Append temporary elements to DOM.
333 $("body").append(temp);
334 $("body").append(tempPre);
335
336 // Set temporary HTML markup.
337 tempPre.html(tempHTML);
338
339 var content = tempPre.text();
340
341 content = $.trim(content);
342
343 // Format the HTML markup.
344 temp.val(content).select();
345
346 // Support for IOS devices too.
347 CopyTheCodeToClipboard.copy(content);
348
349 // Remove temporary elements.
350 temp.remove();
351 tempPre.remove();
352
353 // Copied!
354 btn.text(button_copy_text);
355 setTimeout(function () {
356 if ('svg-icon' === style) {
357 btn.html(copyTheCode.buttonSvg);
358 } else {
359 btn.text(oldText);
360 }
361 }, 1000);
362 }
363 };
364
365 /**
366 * Initialization
367 */
368 $(function () {
369 CopyTheCode.init();
370 });
371
372 })(jQuery);