Skip to content

Commit

Permalink
Merge pull request #1817 from NullVoxPopuli/fix-copy-button
Browse files Browse the repository at this point in the history
Fix the copy button (when copying snippets)
  • Loading branch information
NullVoxPopuli authored Sep 4, 2024
2 parents e2df641 + 72b7d37 commit 2ba59fe
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
4 changes: 2 additions & 2 deletions apps/repl/app/components/limber/copy-menu.gts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Component from '@glimmer/component';
import { on } from '@ember/modifier';

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

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

await withExtraStyles(code, () => toClipboard(code));
await copyToClipboard(code);
};

<template>
Expand Down
46 changes: 31 additions & 15 deletions apps/repl/app/components/limber/copy-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,35 @@ export function getSnippetElement(event: Event) {
throw new Error('Could not find snippet element');
}

export async function withExtraStyles(target: HTMLElement, next: () => Promise<void>) {
let pre = target.querySelector('pre');

if (!pre) {
return await next();
}

pre.classList.add('drop-shadow-lg');
pre.style.margin = '0';
/**
* Cloning elements for screenshotting is hard.
* Look at all this:
* https://github.com/bubkoo/html-to-image/blob/128dc3edfde95d6ac636f2756630f5cbd6f7c8df/src/clone-node.ts#L231
*
* So instead, we jitter a bit
*/
export async function copyToClipboard(toCopy: HTMLElement) {
let pre = toCopy.querySelector('pre');

try {
await next();
pre?.classList.add('drop-shadow-lg');
Object.assign(toCopy.style, {
margin: 0,
display: 'inline-block',
width: 'fit-content',
});
await toClipboard(toCopy);
} finally {
pre.classList.remove('drop-shadow-lg');
pre.setAttribute('style', '');
pre?.classList.remove('drop-shadow-lg');
Object.assign(toCopy.style, {
margin: 'unset',
display: 'unset',
width: 'unset',
});
}
}

export async function toClipboard(target: HTMLElement) {
async function toClipboard(target: HTMLElement) {
let backgroundColor = '#ffffff';
let canCopyToImage = 'ClipboardItem' in window;
let filter = (node: HTMLElement | Text) => {
Expand All @@ -67,6 +77,10 @@ export async function toClipboard(target: HTMLElement) {
return false;
}

if ('classList' in node && node.classList.contains('limber__menu__content')) {
return false;
}

return true;
};

Expand All @@ -79,10 +93,12 @@ export async function toClipboard(target: HTMLElement) {
// html-to-image does not make adjustments if margins exist anyway
width: box.width + 32,
height: box.height + 32,
cacheBust: true,
/**
* Good for pasting on social medias
*/
pixelRatio: 3,
style: {
// m-0
// make margin uniform all the way around
margin: '1rem',
},
};
Expand Down

0 comments on commit 2ba59fe

Please sign in to comment.