|
3 | 3 | import { getCodeHtml, type Language } from '$lib/utils/code';
|
4 | 4 | import { copy } from '$lib/utils/copy';
|
5 | 5 | import { platformMap } from '$lib/utils/references';
|
6 |
| - import { writable } from 'svelte/store'; |
| 6 | + import { SvelteSet } from 'svelte/reactivity'; |
7 | 7 |
|
8 | 8 | interface Props {
|
9 | 9 | selected?: Language;
|
|
14 | 14 |
|
15 | 15 | let { selected = $bindable('js'), data = [], width = null, height = null }: Props = $props();
|
16 | 16 |
|
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 | + ); |
25 | 33 |
|
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; |
29 | 37 | }
|
30 | 38 | });
|
31 | 39 |
|
|
35 | 43 | } as const;
|
36 | 44 | type CopyStatusType = keyof typeof CopyStatus;
|
37 | 45 | type CopyStatusValue = (typeof CopyStatus)[CopyStatusType];
|
38 |
| -
|
39 |
| - let copyText = $state<CopyStatusValue>(CopyStatus.Copy); |
40 |
| -
|
| 46 | + let copyText: CopyStatusValue = $state(CopyStatus.Copy); |
41 | 47 | async function handleCopy() {
|
42 | 48 | await copy(content);
|
43 | 49 |
|
|
46 | 52 | copyText = CopyStatus.Copy;
|
47 | 53 | }, 1000);
|
48 | 54 | }
|
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 |
| - ); |
63 | 55 | </script>
|
64 | 56 |
|
65 | 57 | <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]" |
67 | 59 | 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'} |
71 | 62 | >
|
72 | 63 | <header class="web-code-snippet-header">
|
73 | 64 | <div class="web-code-snippet-header-start">
|
|
79 | 70 | </div>
|
80 | 71 | <div class="web-code-snippet-header-end">
|
81 | 72 | <ul class="buttons-list flex gap-3">
|
82 |
| - {#if $snippets.entries.length} |
| 73 | + {#if snippets.size} |
83 | 74 | <li class="buttons-list-item flex self-center">
|
84 |
| - <Select bind:value={selected} bind:options /> |
| 75 | + <Select bind:value={selected} {options} /> |
85 | 76 | </li>
|
86 | 77 | {/if}
|
87 | 78 | <li class="buttons-list-item" style="padding-inline-start: 13px">
|
|
104 | 95 | </header>
|
105 | 96 | <div
|
106 | 97 | class="web-code-snippet-content overflow-auto"
|
107 |
| - style={`height: ${height ? height / 16 + 'rem' : 'inherit'}`} |
| 98 | + style:height={height ? height / 16 + 'rem' : 'inherit'} |
108 | 99 | >
|
109 | 100 | <!-- eslint-disable-next-line svelte/no-at-html-tags -->
|
110 | 101 | {@html result}
|
|
0 commit comments