Skip to content

Commit 2ba59fe

Browse files
Merge pull request #1817 from NullVoxPopuli/fix-copy-button
Fix the copy button (when copying snippets)
2 parents e2df641 + 72b7d37 commit 2ba59fe

File tree

2 files changed

+33
-17
lines changed

2 files changed

+33
-17
lines changed

apps/repl/app/components/limber/copy-menu.gts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Component from '@glimmer/component';
22
import { on } from '@ember/modifier';
33

4-
import { getSnippetElement, toClipboard, withExtraStyles } from './copy-utils';
4+
import { copyToClipboard, getSnippetElement } from './copy-utils';
55
import Menu from './menu';
66

77
/**
@@ -17,7 +17,7 @@ export default class CopyMenu extends Component {
1717
copyAsImage = async (event: Event) => {
1818
let code = getSnippetElement(event);
1919

20-
await withExtraStyles(code, () => toClipboard(code));
20+
await copyToClipboard(code);
2121
};
2222

2323
<template>

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

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,35 @@ export function getSnippetElement(event: Event) {
3939
throw new Error('Could not find snippet element');
4040
}
4141

42-
export async function withExtraStyles(target: HTMLElement, next: () => Promise<void>) {
43-
let pre = target.querySelector('pre');
44-
45-
if (!pre) {
46-
return await next();
47-
}
48-
49-
pre.classList.add('drop-shadow-lg');
50-
pre.style.margin = '0';
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');
5151

5252
try {
53-
await next();
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);
5460
} finally {
55-
pre.classList.remove('drop-shadow-lg');
56-
pre.setAttribute('style', '');
61+
pre?.classList.remove('drop-shadow-lg');
62+
Object.assign(toCopy.style, {
63+
margin: 'unset',
64+
display: 'unset',
65+
width: 'unset',
66+
});
5767
}
5868
}
5969

60-
export async function toClipboard(target: HTMLElement) {
70+
async function toClipboard(target: HTMLElement) {
6171
let backgroundColor = '#ffffff';
6272
let canCopyToImage = 'ClipboardItem' in window;
6373
let filter = (node: HTMLElement | Text) => {
@@ -67,6 +77,10 @@ export async function toClipboard(target: HTMLElement) {
6777
return false;
6878
}
6979

80+
if ('classList' in node && node.classList.contains('limber__menu__content')) {
81+
return false;
82+
}
83+
7084
return true;
7185
};
7286

@@ -79,10 +93,12 @@ export async function toClipboard(target: HTMLElement) {
7993
// html-to-image does not make adjustments if margins exist anyway
8094
width: box.width + 32,
8195
height: box.height + 32,
96+
cacheBust: true,
97+
/**
98+
* Good for pasting on social medias
99+
*/
82100
pixelRatio: 3,
83101
style: {
84-
// m-0
85-
// make margin uniform all the way around
86102
margin: '1rem',
87103
},
88104
};

0 commit comments

Comments
 (0)