Skip to content

Commit 9ee0da8

Browse files
committed
Fix the copy button
1 parent e2df641 commit 9ee0da8

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
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: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { assert } from '@ember/debug';
2+
13
import { toBlob, toPng } from 'html-to-image';
24

35
export function getSnippetElement(event: Event) {
@@ -39,22 +41,21 @@ export function getSnippetElement(event: Event) {
3941
throw new Error('Could not find snippet element');
4042
}
4143

42-
export async function withExtraStyles(target: HTMLElement, next: () => Promise<void>) {
43-
let pre = target.querySelector('pre');
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();
4452

45-
if (!pre) {
46-
return await next();
47-
}
53+
assert(`Element to copy must be an HTMLElement`, toCopy instanceof HTMLElement);
4854

49-
pre.classList.add('drop-shadow-lg');
50-
pre.style.margin = '0';
55+
toCopy.classList.add('drop-shadow-lg');
56+
toCopy.style.margin = '0';
5157

52-
try {
53-
await next();
54-
} finally {
55-
pre.classList.remove('drop-shadow-lg');
56-
pre.setAttribute('style', '');
57-
}
58+
await toClipboard(toCopy);
5859
}
5960

6061
export async function toClipboard(target: HTMLElement) {
@@ -67,6 +68,10 @@ export async function toClipboard(target: HTMLElement) {
6768
return false;
6869
}
6970

71+
if (node.classList.contains('limber__menu__content')) {
72+
return false;
73+
}
74+
7075
return true;
7176
};
7277

0 commit comments

Comments
 (0)