Skip to content

Commit 34ad99a

Browse files
committed
Factorized clipboardCopy in SuperDebug
1 parent 144c2a5 commit 34ad99a

File tree

2 files changed

+70
-72
lines changed

2 files changed

+70
-72
lines changed

src/lib/client/SuperDebug.svelte

Lines changed: 1 addition & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,8 @@
1-
<script context="module">
2-
/*! clipboard-copy. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */
3-
4-
function makeError() {
5-
return new DOMException('The request is not allowed', 'NotAllowedError');
6-
}
7-
8-
/**
9-
* @param {string} text
10-
*/
11-
async function copyClipboardApi(text) {
12-
// Use the Async Clipboard API when available. Requires a secure browsing
13-
// context (i.e. HTTPS)
14-
if (!navigator.clipboard) {
15-
throw makeError();
16-
}
17-
return navigator.clipboard.writeText(text);
18-
}
19-
20-
/**
21-
* @param {string} text
22-
*/
23-
async function copyExecCommand(text) {
24-
// Put the text to copy into a <span>
25-
const span = document.createElement('span');
26-
span.textContent = text;
27-
28-
// Preserve consecutive spaces and newlines
29-
span.style.whiteSpace = 'pre';
30-
span.style.webkitUserSelect = 'auto';
31-
span.style.userSelect = 'all';
32-
33-
// Add the <span> to the page
34-
document.body.appendChild(span);
35-
36-
// Make a selection object representing the range of text selected by the user
37-
const selection = window.getSelection();
38-
const range = window.document.createRange();
39-
selection?.removeAllRanges();
40-
range.selectNode(span);
41-
selection?.addRange(range);
42-
43-
// Copy text to the clipboard
44-
let success = false;
45-
try {
46-
success = window.document.execCommand('copy');
47-
} finally {
48-
// Cleanup
49-
selection?.removeAllRanges();
50-
window.document.body.removeChild(span);
51-
}
52-
53-
if (!success) throw makeError();
54-
}
55-
56-
/**
57-
* @param {string} text
58-
*/
59-
async function clipboardCopy(text) {
60-
try {
61-
await copyClipboardApi(text);
62-
} catch (err) {
63-
// ...Otherwise, use document.execCommand() fallback
64-
try {
65-
await copyExecCommand(text);
66-
} catch (err2) {
67-
throw err2 || err || makeError();
68-
}
69-
}
70-
}
71-
</script>
72-
731
<script>
742
import { browser } from '$app/environment';
753
import { page } from '$app/stores';
764
import { readable, get } from 'svelte/store';
5+
import { clipboardCopy } from './clipboardCopy.js';
776
787
let styleInit = false;
798

src/lib/client/clipboardCopy.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*! clipboard-copy. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */
2+
3+
function makeError() {
4+
return new DOMException('The request is not allowed', 'NotAllowedError');
5+
}
6+
7+
/**
8+
* @param {string} text
9+
*/
10+
async function copyClipboardApi(text) {
11+
// Use the Async Clipboard API when available. Requires a secure browsing
12+
// context (i.e. HTTPS)
13+
if (!navigator.clipboard) {
14+
throw makeError();
15+
}
16+
return navigator.clipboard.writeText(text);
17+
}
18+
19+
/**
20+
* @param {string} text
21+
*/
22+
async function copyExecCommand(text) {
23+
// Put the text to copy into a <span>
24+
const span = document.createElement('span');
25+
span.textContent = text;
26+
27+
// Preserve consecutive spaces and newlines
28+
span.style.whiteSpace = 'pre';
29+
span.style.webkitUserSelect = 'auto';
30+
span.style.userSelect = 'all';
31+
32+
// Add the <span> to the page
33+
document.body.appendChild(span);
34+
35+
// Make a selection object representing the range of text selected by the user
36+
const selection = window.getSelection();
37+
const range = window.document.createRange();
38+
selection?.removeAllRanges();
39+
range.selectNode(span);
40+
selection?.addRange(range);
41+
42+
// Copy text to the clipboard
43+
let success = false;
44+
try {
45+
success = window.document.execCommand('copy');
46+
} finally {
47+
// Cleanup
48+
selection?.removeAllRanges();
49+
window.document.body.removeChild(span);
50+
}
51+
52+
if (!success) throw makeError();
53+
}
54+
55+
/**
56+
* @param {string} text
57+
*/
58+
export async function clipboardCopy(text) {
59+
try {
60+
await copyClipboardApi(text);
61+
} catch (err) {
62+
// ...Otherwise, use document.execCommand() fallback
63+
try {
64+
await copyExecCommand(text);
65+
} catch (err2) {
66+
throw err2 || err || makeError();
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)