Skip to content

Commit b77aa26

Browse files
committed
womp
1 parent 9ee0da8 commit b77aa26

File tree

1 file changed

+31
-20
lines changed

1 file changed

+31
-20
lines changed

apps/repl/app/components/limber/copy-utils.ts

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { assert } from '@ember/debug';
2-
31
import { toBlob, toPng } from 'html-to-image';
42

53
export function getSnippetElement(event: Event) {
@@ -41,24 +39,35 @@ export function getSnippetElement(event: Event) {
4139
throw new Error('Could not find snippet element');
4240
}
4341

44-
export async function copyToClipboard(target: HTMLElement) {
45-
/**
46-
* 1. clone
47-
* 2. remove extra styles
48-
* 3. image
49-
* 3. clipboard
50-
*/
51-
let toCopy = (target.querySelector('pre') || target).cloneNode();
52-
53-
assert(`Element to copy must be an HTMLElement`, toCopy instanceof HTMLElement);
54-
55-
toCopy.classList.add('drop-shadow-lg');
56-
toCopy.style.margin = '0';
57-
58-
await toClipboard(toCopy);
42+
/**
43+
* Cloning elements for screenshotting is hard.
44+
* Look at all this:
45+
* https://github.com/bubkoo/html-to-image/blob/128dc3edfde95d6ac636f2756630f5cbd6f7c8df/src/clone-node.ts#L231
46+
*
47+
* So instead, we jitter a bit
48+
*/
49+
export async function copyToClipboard(toCopy: HTMLElement) {
50+
let pre = toCopy.querySelector('pre');
51+
52+
try {
53+
pre?.classList.add('drop-shadow-lg');
54+
Object.assign(toCopy.style, {
55+
margin: 0,
56+
display: 'inline-block',
57+
width: 'fit-content',
58+
});
59+
await toClipboard(toCopy);
60+
} finally {
61+
pre?.classList.remove('drop-shadow-lg');
62+
Object.assign(toCopy.style, {
63+
margin: 'unset',
64+
display: 'unset',
65+
width: 'unset',
66+
});
67+
}
5968
}
6069

61-
export async function toClipboard(target: HTMLElement) {
70+
async function toClipboard(target: HTMLElement) {
6271
let backgroundColor = '#ffffff';
6372
let canCopyToImage = 'ClipboardItem' in window;
6473
let filter = (node: HTMLElement | Text) => {
@@ -84,10 +93,12 @@ export async function toClipboard(target: HTMLElement) {
8493
// html-to-image does not make adjustments if margins exist anyway
8594
width: box.width + 32,
8695
height: box.height + 32,
96+
cacheBust: true,
97+
/**
98+
* Good for pasting on social medias
99+
*/
87100
pixelRatio: 3,
88101
style: {
89-
// m-0
90-
// make margin uniform all the way around
91102
margin: '1rem',
92103
},
93104
};

0 commit comments

Comments
 (0)