Skip to content

Commit 6cdecfa

Browse files
committed
add copy
1 parent d159410 commit 6cdecfa

2 files changed

Lines changed: 38 additions & 5 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<script lang="ts">
2+
import { onMount } from 'svelte';
3+
4+
onMount(() => {
5+
// will add a children to any <pre> element with class language-*
6+
let pres: HTMLCollection = document.getElementsByTagName('pre');
7+
for (let _ of pres) {
8+
const pre = _ as HTMLPreElement;
9+
if (![...pre.classList].some((el) => el.startsWith('language-'))) {
10+
continue;
11+
}
12+
const text = pre.innerText;
13+
pre.classList.add('relative');
14+
let copyButton = document.createElement('button');
15+
copyButton.addEventListener('click', () => {
16+
navigator.clipboard.writeText(text);
17+
copyButton.innerHTML = 'Copied 👍';
18+
setTimeout(() => {
19+
copyButton.innerHTML = 'Copy';
20+
}, 1500);
21+
});
22+
copyButton.className =
23+
'copy absolute top-0 right-0 mt-2 mr-2 bcu-button variant-ringed-primary';
24+
copyButton.innerText = 'Copy';
25+
pre.appendChild(copyButton);
26+
}
27+
});
28+
</script>
29+
30+
<slot />

‎apps/codingcatdev/src/routes/(content-single)/(non-course)/Content.svelte‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import type { Content, ContentType } from '$lib/types';
66
import { pluralize } from '$lib/utils';
77
import CloudinaryImage from '$lib/components/content/CloudinaryImage.svelte';
8+
import CopyCodeInjector from '$lib/components/content/CopyCodeInjector.svelte';
89
export let data: {
910
content: Content;
1011
contentType: ContentType;
@@ -14,10 +15,10 @@
1415

1516
{#if data?.content}
1617
<div class="flex justify-center">
17-
<section class="flex flex-col xl:flex-row gap-8 justify-center p-1 xl:p-8 w-full">
18-
<div class="flex flex-col gap-2 md:gap-8 max-w-7xl w-full">
18+
<section class="flex flex-col justify-center w-full gap-8 p-1 xl:flex-row xl:p-8">
19+
<div class="flex flex-col w-full gap-2 md:gap-8 max-w-7xl">
1920
<ol class="bcu-breadcrumb">
20-
<li class="bcu-crumb capitalize"><a href={`/${title}`}>{title}</a></li>
21+
<li class="capitalize bcu-crumb"><a href={`/${title}`}>{title}</a></li>
2122
<li class="bcu-crumb-separator" aria-hidden>&rsaquo;</li>
2223
<li>{data.content.title}</li>
2324
</ol>
@@ -34,8 +35,10 @@
3435
<CloudinaryImage src={data.content.cover} alt={data.content.title} />
3536
{/if}
3637

37-
<section class="flex-grow w-full markdown flex flex-col gap-2 md:gap-8">
38-
{@html data.content.html}
38+
<section class="flex flex-col flex-grow w-full gap-2 markdown md:gap-8">
39+
<CopyCodeInjector>
40+
{@html data.content.html}
41+
</CopyCodeInjector>
3942
</section>
4043
</div>
4144
</section>

0 commit comments

Comments
 (0)