Skip to content

Commit 5d3ecce

Browse files
svelte5: code snippet components and optimize markup
1 parent 6eb9df5 commit 5d3ecce

File tree

2 files changed

+29
-43
lines changed

2 files changed

+29
-43
lines changed

src/lib/components/MultiCodeContextless.svelte

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { getCodeHtml, type Language } from '$lib/utils/code';
44
import { copy } from '$lib/utils/copy';
55
import { platformMap } from '$lib/utils/references';
6-
import { writable } from 'svelte/store';
6+
import { SvelteSet } from 'svelte/reactivity';
77
88
interface Props {
99
selected?: Language;
@@ -14,18 +14,26 @@
1414
1515
let { selected = $bindable('js'), data = [], width = null, height = null }: Props = $props();
1616
17-
let snippets = $derived(writable(new Set(data.map((d) => d.language))));
18-
const getSnippets = () => {
19-
return snippets;
20-
};
21-
22-
let content = $derived(data.find((d) => d.language === selected)?.content ?? '');
23-
24-
let platform = $derived(data.find((d) => d.language === selected)?.platform ?? '');
17+
const snippets = $derived(new SvelteSet(data.map((d) => d.language)));
18+
const content = $derived(data.find((d) => d.language === selected)?.content ?? '');
19+
const platform = $derived(data.find((d) => d.language === selected)?.platform ?? '');
20+
const result = $derived(
21+
getCodeHtml({
22+
content,
23+
language: selected ?? 'sh',
24+
withLineNumbers: true
25+
})
26+
);
27+
const options = $derived(
28+
Array.from(snippets).map((language) => ({
29+
value: language,
30+
label: platformMap[language]
31+
}))
32+
);
2533
26-
getSnippets().subscribe((n) => {
27-
if (selected === null && n.size > 0) {
28-
selected = Array.from(n)[0] as Language;
34+
$effect(() => {
35+
if (selected === null && snippets.size > 0) {
36+
selected = Array.from(snippets)[0] as Language;
2937
}
3038
});
3139
@@ -35,9 +43,7 @@
3543
} as const;
3644
type CopyStatusType = keyof typeof CopyStatus;
3745
type CopyStatusValue = (typeof CopyStatus)[CopyStatusType];
38-
39-
let copyText = $state<CopyStatusValue>(CopyStatus.Copy);
40-
46+
let copyText: CopyStatusValue = $state(CopyStatus.Copy);
4147
async function handleCopy() {
4248
await copy(content);
4349
@@ -46,28 +52,13 @@
4652
copyText = CopyStatus.Copy;
4753
}, 1000);
4854
}
49-
50-
let result = $derived(
51-
getCodeHtml({
52-
content,
53-
language: selected ?? 'sh',
54-
withLineNumbers: true
55-
})
56-
);
57-
let options = $derived(
58-
Array.from($snippets).map((language) => ({
59-
value: language,
60-
label: platformMap[language]
61-
}))
62-
);
6355
</script>
6456

6557
<section
66-
class="dark web-code-snippet mx-auto w-full lg:!max-w-[90vw]"
58+
class="dark web-code-snippet mx-auto lg:!max-w-[90vw]"
6759
aria-label="code-snippet panel"
68-
style={`width: ${width ? width / 16 + 'rem' : 'inherit'}; height: ${
69-
height ? height / 16 + 'rem' : 'inherit'
70-
}`}
60+
style:width={width ? width / 16 + 'rem' : 'inherit'}
61+
style:height={height ? height / 16 + 'rem' : 'inherit'}
7162
>
7263
<header class="web-code-snippet-header">
7364
<div class="web-code-snippet-header-start">
@@ -79,9 +70,9 @@
7970
</div>
8071
<div class="web-code-snippet-header-end">
8172
<ul class="buttons-list flex gap-3">
82-
{#if $snippets.entries.length}
73+
{#if snippets.size}
8374
<li class="buttons-list-item flex self-center">
84-
<Select bind:value={selected} bind:options />
75+
<Select bind:value={selected} {options} />
8576
</li>
8677
{/if}
8778
<li class="buttons-list-item" style="padding-inline-start: 13px">
@@ -104,7 +95,7 @@
10495
</header>
10596
<div
10697
class="web-code-snippet-content overflow-auto"
107-
style={`height: ${height ? height / 16 + 'rem' : 'inherit'}`}
98+
style:height={height ? height / 16 + 'rem' : 'inherit'}
10899
>
109100
<!-- eslint-disable-next-line svelte/no-at-html-tags -->
110101
{@html result}

src/lib/components/shared/dialog.svelte

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,9 @@
2727
{@render children()}
2828
</div>
2929
{:else}
30-
<button
31-
class="contents cursor-pointer"
32-
onclick={() => {
33-
if (browser && window) window.open(url, '_blank');
34-
}}
35-
>
30+
<a href={url} target="_blank" rel="noopener noreferrer" class="contents cursor-pointer">
3631
{@render children()}
37-
</button>
32+
</a>
3833
{/if}
3934

4035
{#if $open}

0 commit comments

Comments
 (0)