Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the copy button (when copying snippets) #1817

Merged
merged 3 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading