File size: 2,550 Bytes
e37337c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<script context="module" lang="ts">
	export { default as BaseCode } from "./shared/Code.svelte";
	export { default as BaseCopy } from "./shared/Copy.svelte";
	export { default as BaseDownload } from "./shared/Download.svelte";
	export { default as BaseWidget } from "./shared/Widgets.svelte";
	export { default as BaseExample } from "./Example.svelte";
</script>

<script lang="ts">
	import { Gradio } from "@gradio/utils";
	import type { CodeProps, CodeEvents } from "./types";
	import { StatusTracker } from "@gradio/statustracker";

	import Code from "./shared/Code.svelte";
	import Widget from "./shared/Widgets.svelte";
	import { Block, BlockLabel, Empty } from "@gradio/atoms";
	import { Code as CodeIcon } from "@gradio/icons";

	const props = $props();
	const gradio = new Gradio<CodeEvents, CodeProps>(props);

	let dark_mode = gradio.shared.theme === "dark";

	let label = $derived(gradio.shared.label || gradio.i18n("code.code"));
	let old_value = $state(gradio.props.value);
	let first_change = true;

	$effect(() => {
		if (first_change) {
			first_change = false;
			return;
		}
		if (old_value != gradio.props.value) {
			old_value = gradio.props.value;
			gradio.dispatch("change");
		}
	});
</script>

<Block
	height={gradio.props.max_lines && "fit-content"}
	variant={"solid"}
	padding={false}
	elem_id={gradio.shared.elem_id}
	elem_classes={gradio.shared.elem_classes}
	visible={gradio.shared.visible}
	scale={gradio.shared.scale}
	min_width={gradio.shared.min_width}
>
	<StatusTracker
		autoscroll={gradio.shared.autoscroll}
		i18n={gradio.i18n}
		{...gradio.shared.loading_status}
		on:clear_status={() =>
			gradio.dispatch("clear_status", gradio.shared.loading_status)}
	/>

	{#if gradio.shared.show_label}
		<BlockLabel
			Icon={CodeIcon}
			show_label={gradio.shared.show_label}
			{label}
			float={false}
		/>
	{/if}

	{#if !gradio.props.value && !gradio.shared.interactive}
		<Empty unpadded_box={true} size="large">
			<CodeIcon />
		</Empty>
	{:else}
		<Widget language={gradio.props.language} value={gradio.props.value} />

		<Code
			bind:value={gradio.props.value}
			language={gradio.props.language}
			lines={gradio.props.lines}
			max_lines={gradio.props.max_lines}
			{dark_mode}
			wrap_lines={gradio.props.wrap_lines}
			show_line_numbers={gradio.props.show_line_numbers}
			autocomplete={gradio.props.autocomplete}
			readonly={!gradio.shared.interactive}
			on:blur={() => gradio.dispatch("blur")}
			on:focus={() => gradio.dispatch("focus")}
			on:input={() => gradio.dispatch("input")}
		/>
	{/if}
</Block>