Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Oct 1, 2024
1 parent cc38f11 commit 56031ad
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion packages/site-kit/src/lib/components/Text.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,31 @@
update();
}
}
async function copy(e: Event) {
if ((e.target as HTMLButtonElement).classList.contains('copy-to-clipboard')) {
const parent = e
.composedPath()
.find((node) => (node as HTMLElement).classList.contains('code-block')) as HTMLElement;
const ts = !!parent.querySelector('.ts-toggle:checked');
const code = parent.querySelector(`pre:${ts ? 'last' : 'first'}-of-type code`) as HTMLElement;
let result = '';
for (const node of code.childNodes ?? []) {
if (!(node as HTMLElement).classList.contains('deleted')) {
result += node.textContent!.trimEnd() + '\n';
}
}
navigator.clipboard.writeText(result.trim());
}
}
</script>

<div onchangecapture={toggle} bind:this={container} class="text">{@render children()}</div>
<div onclickcapture={copy} onchangecapture={toggle} bind:this={container} class="text">
{@render children()}
</div>

<style>
.text :global {
Expand Down

0 comments on commit 56031ad

Please sign in to comment.