ToolkitEditor.svelte 3.33 KB
Newer Older
Timothy J. Baek's avatar
Timothy J. Baek committed
1
<script>
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
2
	import { getContext, createEventDispatcher } from 'svelte';
Timothy J. Baek's avatar
Timothy J. Baek committed
3
4
5
6
7
8

	const i18n = getContext('i18n');

	import CodeEditor from './CodeEditor.svelte';
	import { goto } from '$app/navigation';

Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
9
10
	const dispatch = createEventDispatcher();

Timothy J. Baek's avatar
Timothy J. Baek committed
11
	let formElement = null;
Timothy J. Baek's avatar
Timothy J. Baek committed
12
13
	let loading = false;

Timothy J. Baek's avatar
Timothy J. Baek committed
14
15
16
17
18
	export let edit = false;

	export let id = '';
	export let name = '';
	export let meta = {
Timothy J. Baek's avatar
Timothy J. Baek committed
19
20
		description: ''
	};
Timothy J. Baek's avatar
Timothy J. Baek committed
21
	export let content = '';
Timothy J. Baek's avatar
Timothy J. Baek committed
22

Timothy J. Baek's avatar
Timothy J. Baek committed
23
	$: if (name && !edit) {
Timothy J. Baek's avatar
Timothy J. Baek committed
24
25
26
27
28
29
30
		id = name.replace(/\s+/g, '_').toLowerCase();
	}

	let codeEditor;

	const saveHandler = async () => {
		loading = true;
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
31
32
33
34
35
36
		dispatch('save', {
			id,
			name,
			meta,
			content
		});
Timothy J. Baek's avatar
Timothy J. Baek committed
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
	};

	const submitHandler = async () => {
		if (codeEditor) {
			const res = await codeEditor.formatHandler();

			if (res) {
				console.log('Code formatted successfully');
				saveHandler();
			}
		}
	};
</script>

<div class=" flex flex-col justify-between w-full overflow-y-auto h-full">
	<div class="mx-auto w-full md:px-0 h-full">
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
53
		<form
Timothy J. Baek's avatar
Timothy J. Baek committed
54
			bind:this={formElement}
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
55
56
57
58
59
			class=" flex flex-col max-h-[100dvh] h-full"
			on:submit|preventDefault={() => {
				submitHandler();
			}}
		>
Timothy J. Baek's avatar
Timothy J. Baek committed
60
61
62
63
64
65
			<div class="mb-2.5">
				<button
					class="flex space-x-1"
					on:click={() => {
						goto('/workspace/tools');
					}}
Timothy J. Baek's avatar
Timothy J. Baek committed
66
					type="button"
Timothy J. Baek's avatar
Timothy J. Baek committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
				>
					<div class=" self-center">
						<svg
							xmlns="http://www.w3.org/2000/svg"
							viewBox="0 0 20 20"
							fill="currentColor"
							class="w-4 h-4"
						>
							<path
								fill-rule="evenodd"
								d="M17 10a.75.75 0 01-.75.75H5.612l4.158 3.96a.75.75 0 11-1.04 1.08l-5.5-5.25a.75.75 0 010-1.08l5.5-5.25a.75.75 0 111.04 1.08L5.612 9.25H16.25A.75.75 0 0117 10z"
								clip-rule="evenodd"
							/>
						</svg>
					</div>
					<div class=" self-center font-medium text-sm">{$i18n.t('Back')}</div>
				</button>
			</div>

			<div class="flex flex-col flex-1 overflow-auto h-0 rounded-lg">
				<div class="w-full mb-2 flex flex-col gap-1.5">
					<div class="flex gap-2 w-full">
						<input
							class="w-full px-3 py-2 text-sm font-medium bg-gray-50 dark:bg-gray-850 dark:text-gray-200 rounded-lg outline-none"
							type="text"
							placeholder="Toolkit Name (e.g. My ToolKit)"
							bind:value={name}
							required
						/>

						<input
Timothy J. Baek's avatar
Timothy J. Baek committed
98
							class="w-full px-3 py-2 text-sm font-medium disabled:text-gray-300 dark:disabled:text-gray-700 bg-gray-50 dark:bg-gray-850 dark:text-gray-200 rounded-lg outline-none"
Timothy J. Baek's avatar
Timothy J. Baek committed
99
100
101
102
							type="text"
							placeholder="Toolkit ID (e.g. my_toolkit)"
							bind:value={id}
							required
Timothy J. Baek's avatar
Timothy J. Baek committed
103
							disabled={edit}
Timothy J. Baek's avatar
Timothy J. Baek committed
104
105
106
107
108
109
110
111
112
113
114
115
						/>
					</div>
					<input
						class="w-full px-3 py-2 text-sm font-medium bg-gray-50 dark:bg-gray-850 dark:text-gray-200 rounded-lg outline-none"
						type="text"
						placeholder="Toolkit Description (e.g. A toolkit for performing various operations)"
						bind:value={meta.description}
						required
					/>
				</div>

				<div class="mb-2 flex-1 overflow-auto h-0 rounded-lg">
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
116
117
118
119
					<CodeEditor
						bind:value={content}
						bind:this={codeEditor}
						on:save={() => {
Timothy J. Baek's avatar
Timothy J. Baek committed
120
121
122
							if (formElement) {
								formElement.requestSubmit();
							}
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
123
124
						}}
					/>
Timothy J. Baek's avatar
Timothy J. Baek committed
125
126
127
128
129
130
131
132
133
134
				</div>

				<div class="pb-3 flex justify-end">
					<button
						class="px-3 py-1.5 text-sm font-medium bg-emerald-600 hover:bg-emerald-700 text-gray-50 transition rounded-lg"
					>
						{$i18n.t('Save')}
					</button>
				</div>
			</div>
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
135
		</form>
Timothy J. Baek's avatar
Timothy J. Baek committed
136
137
	</div>
</div>