Skip to content

Commit 1b1a609

Browse files
committed
Use clippie module to copy to clipboard
1 parent 3e8db31 commit 1b1a609

File tree

5 files changed

+13
-45
lines changed

5 files changed

+13
-45
lines changed

package-lock.json

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"add-asset-webpack-plugin": "2.0.1",
2020
"ansi-to-html": "0.7.2",
2121
"asciinema-player": "3.2.0",
22+
"clippie": "3.1.4",
2223
"css-loader": "6.7.3",
2324
"dropzone": "6.0.0-beta.2",
2425
"easymde": "2.18.0",

web_src/js/features/clipboard.js

+2-41
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,9 @@
11
import {showTemporaryTooltip} from '../modules/tippy.js';
22
import {toAbsoluteUrl} from '../utils.js';
3+
import {clippie} from 'clippie';
34

45
const {copy_success, copy_error} = window.config.i18n;
56

6-
export async function copyToClipboard(content) {
7-
if (content instanceof Blob) {
8-
const item = new ClipboardItem({[content.type]: content});
9-
await navigator.clipboard.write([item]);
10-
} else { // text
11-
try {
12-
await navigator.clipboard.writeText(content);
13-
} catch {
14-
return fallbackCopyToClipboard(content);
15-
}
16-
}
17-
return true;
18-
}
19-
20-
// Fallback to use if navigator.clipboard doesn't exist. Achieved via creating
21-
// a temporary textarea element, selecting the text, and using document.execCommand
22-
function fallbackCopyToClipboard(text) {
23-
if (!document.execCommand) return false;
24-
25-
const tempTextArea = document.createElement('textarea');
26-
tempTextArea.value = text;
27-
28-
// avoid scrolling
29-
tempTextArea.style.top = 0;
30-
tempTextArea.style.left = 0;
31-
tempTextArea.style.position = 'fixed';
32-
33-
document.body.appendChild(tempTextArea);
34-
35-
tempTextArea.select();
36-
37-
// if unsecure (not https), there is no navigator.clipboard, but we can still
38-
// use document.execCommand to copy to clipboard
39-
const success = document.execCommand('copy');
40-
41-
document.body.removeChild(tempTextArea);
42-
43-
return success;
44-
}
45-
467
// For all DOM elements with [data-clipboard-target] or [data-clipboard-text],
478
// this copy-to-clipboard will work for them
489
export function initGlobalCopyToClipboardListener() {
@@ -61,7 +22,7 @@ export function initGlobalCopyToClipboardListener() {
6122
e.preventDefault();
6223

6324
(async() => {
64-
const success = await copyToClipboard(text);
25+
const success = await clippie(text);
6526
showTemporaryTooltip(target, success ? copy_success : copy_error);
6627
})();
6728

web_src/js/features/copycontent.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import {copyToClipboard} from './clipboard.js';
1+
import {clippie} from 'clippie';
22
import {showTemporaryTooltip} from '../modules/tippy.js';
33
import {convertImage} from '../utils.js';
44

55
const {i18n} = window.config;
66

77
async function doCopy(content, btn) {
8-
const success = await copyToClipboard(content);
8+
const success = await clippie(content);
99
showTemporaryTooltip(btn, success ? i18n.copy_success : i18n.copy_error);
1010
}
1111

web_src/js/features/repo-code.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import $ from 'jquery';
22
import {svg} from '../svg.js';
33
import {invertFileFolding} from './file-fold.js';
44
import {createTippy} from '../modules/tippy.js';
5-
import {copyToClipboard} from './clipboard.js';
5+
import {clippie} from 'clippie';
66
import {toAbsoluteUrl} from '../utils.js';
77

88
export const singleAnchorRegex = /^#(L|n)([1-9][0-9]*)$/;
@@ -190,7 +190,7 @@ export function initRepoCodeView() {
190190
currentTarget.closest('tr').outerHTML = blob;
191191
});
192192
$(document).on('click', '.copy-line-permalink', async (e) => {
193-
const success = await copyToClipboard(toAbsoluteUrl(e.currentTarget.getAttribute('data-url')));
193+
const success = await clippie(toAbsoluteUrl(e.currentTarget.getAttribute('data-url')));
194194
if (!success) return;
195195
document.querySelector('.code-line-button')?._tippy?.hide();
196196
});

0 commit comments

Comments
 (0)