|
| 1 | +<script lang="ts"> |
| 2 | + import Button from '$lib/components/common/button/Button.svelte' |
| 3 | + import type { Preview } from '$lib/gen' |
| 4 | + import { createEventDispatcher, onMount } from 'svelte' |
| 5 | + import { Maximize2, Trash2 } from 'lucide-svelte' |
| 6 | + import { inferArgs } from '$lib/infer' |
| 7 | + import type { Schema } from '$lib/common' |
| 8 | + import Editor from '$lib/components/Editor.svelte' |
| 9 | + import { emptySchema } from '$lib/utils' |
| 10 | +
|
| 11 | + import { scriptLangToEditorLang } from '$lib/scripts' |
| 12 | + import ScriptGen from '$lib/components/copilot/ScriptGen.svelte' |
| 13 | + import DiffEditor from '$lib/components/DiffEditor.svelte' |
| 14 | + import EditorSettings from '$lib/components/EditorSettings.svelte' |
| 15 | + import InlineScriptEditorDrawer from '../apps/editor/inlineScriptsPanel/InlineScriptEditorDrawer.svelte' |
| 16 | + import type { InlineScript } from '../apps/types' |
| 17 | + import type { AppInput } from '../apps/inputType' |
| 18 | + import CacheTtlPopup from '../apps/editor/inlineScriptsPanel/CacheTtlPopup.svelte' |
| 19 | + import RunButtonInner from '../apps/editor/inlineScriptsPanel/RunButtonInner.svelte' |
| 20 | +
|
| 21 | + let inlineScriptEditorDrawer: InlineScriptEditorDrawer |
| 22 | +
|
| 23 | + export let inlineScript: InlineScript | undefined |
| 24 | + export let name: string | undefined = undefined |
| 25 | + export let id: string |
| 26 | + export let fields: Record<string, AppInput> = {} |
| 27 | + export let path: string |
| 28 | +
|
| 29 | + export let editor: Editor | undefined = undefined |
| 30 | + let diffEditor: DiffEditor |
| 31 | + let validCode = true |
| 32 | +
|
| 33 | + async function inferInlineScriptSchema( |
| 34 | + language: Preview['language'], |
| 35 | + content: string, |
| 36 | + schema: Schema |
| 37 | + ): Promise<Schema> { |
| 38 | + try { |
| 39 | + await inferArgs(language, content, schema) |
| 40 | + validCode = true |
| 41 | + } catch (e) { |
| 42 | + console.error("Couldn't infer args", e) |
| 43 | + validCode = false |
| 44 | + } |
| 45 | +
|
| 46 | + return schema |
| 47 | + } |
| 48 | +
|
| 49 | + onMount(async () => { |
| 50 | + if (inlineScript && !inlineScript.schema) { |
| 51 | + if (inlineScript.language != 'frontend') { |
| 52 | + inlineScript.schema = await inferInlineScriptSchema( |
| 53 | + inlineScript?.language, |
| 54 | + inlineScript?.content, |
| 55 | + emptySchema() |
| 56 | + ) |
| 57 | + } |
| 58 | + } |
| 59 | + }) |
| 60 | +
|
| 61 | + const dispatch = createEventDispatcher() |
| 62 | + let runLoading = false |
| 63 | +
|
| 64 | + let drawerIsOpen: boolean | undefined = undefined |
| 65 | +
|
| 66 | + async function onRun() { |
| 67 | + console.log('onRun') |
| 68 | + } |
| 69 | +
|
| 70 | + async function onCancel() { |
| 71 | + console.log('onCancel') |
| 72 | + } |
| 73 | +</script> |
| 74 | + |
| 75 | +{#if inlineScript} |
| 76 | + {#if inlineScript.language != 'frontend'} |
| 77 | + <InlineScriptEditorDrawer |
| 78 | + {id} |
| 79 | + appPath={path} |
| 80 | + bind:isOpen={drawerIsOpen} |
| 81 | + {editor} |
| 82 | + bind:this={inlineScriptEditorDrawer} |
| 83 | + bind:inlineScript |
| 84 | + on:createScriptFromInlineScript={() => { |
| 85 | + dispatch('createScriptFromInlineScript') |
| 86 | + drawerIsOpen = false |
| 87 | + }} |
| 88 | + /> |
| 89 | + {/if} |
| 90 | + <div class="h-full flex flex-col gap-1"> |
| 91 | + <div class="flex justify-between w-full gap-2 px-2 pt-1 flex-row items-center"> |
| 92 | + {#if name !== undefined} |
| 93 | + <div class="flex flex-row gap-2 w-full items-center"> |
| 94 | + <input |
| 95 | + on:keydown|stopPropagation |
| 96 | + bind:value={name} |
| 97 | + placeholder="Inline script name" |
| 98 | + class="!text-xs !rounded-sm !shadow-none" |
| 99 | + on:keyup={() => { |
| 100 | + // $app = $app |
| 101 | + // if (stateId) { |
| 102 | + // $stateId++ |
| 103 | + // } |
| 104 | + }} |
| 105 | + /> |
| 106 | + <div |
| 107 | + title={validCode ? 'Main function parsable' : 'Main function not parsable'} |
| 108 | + class="rounded-full !w-2 !h-2 {validCode ? 'bg-green-300' : 'bg-red-300'}" |
| 109 | + /> |
| 110 | + </div> |
| 111 | + {/if} |
| 112 | + <div class="flex w-full flex-row gap-1 items-center justify-end"> |
| 113 | + {#if inlineScript} |
| 114 | + <CacheTtlPopup bind:cache_ttl={inlineScript.cache_ttl} /> |
| 115 | + {/if} |
| 116 | + <ScriptGen |
| 117 | + lang={inlineScript?.language} |
| 118 | + {editor} |
| 119 | + {diffEditor} |
| 120 | + inlineScript |
| 121 | + args={Object.entries(fields).reduce((acc, [key, obj]) => { |
| 122 | + acc[key] = obj.type === 'static' ? obj.value : undefined |
| 123 | + return acc |
| 124 | + }, {})} |
| 125 | + /> |
| 126 | + <EditorSettings /> |
| 127 | + |
| 128 | + <Button |
| 129 | + title="Delete" |
| 130 | + size="xs2" |
| 131 | + color="light" |
| 132 | + variant="contained" |
| 133 | + aria-label="Delete" |
| 134 | + on:click={() => dispatch('delete')} |
| 135 | + endIcon={{ icon: Trash2 }} |
| 136 | + iconOnly |
| 137 | + /> |
| 138 | + {#if inlineScript.language != 'frontend'} |
| 139 | + <Button |
| 140 | + size="xs2" |
| 141 | + color="light" |
| 142 | + title="Full Editor" |
| 143 | + variant="contained" |
| 144 | + on:click={() => { |
| 145 | + inlineScriptEditorDrawer?.openDrawer() |
| 146 | + }} |
| 147 | + endIcon={{ icon: Maximize2 }} |
| 148 | + iconOnly |
| 149 | + /> |
| 150 | + {/if} |
| 151 | + |
| 152 | + <Button |
| 153 | + variant="border" |
| 154 | + size="xs2" |
| 155 | + color="light" |
| 156 | + on:click={async () => { |
| 157 | + editor?.format() |
| 158 | + }} |
| 159 | + shortCut={{ |
| 160 | + key: 'S' |
| 161 | + }} |
| 162 | + > |
| 163 | + Format |
| 164 | + </Button> |
| 165 | + <RunButtonInner {onRun} {onCancel} /> |
| 166 | + </div> |
| 167 | + </div> |
| 168 | + |
| 169 | + <!-- {inlineScript.content} --> |
| 170 | + |
| 171 | + <div class="border-y h-full w-full"> |
| 172 | + {#if !drawerIsOpen && inlineScript.language != 'frontend'} |
| 173 | + <Editor |
| 174 | + path={path + '/' + id} |
| 175 | + bind:this={editor} |
| 176 | + small |
| 177 | + class="flex flex-1 grow h-full" |
| 178 | + scriptLang={inlineScript.language} |
| 179 | + bind:code={inlineScript.content} |
| 180 | + fixedOverflowWidgets={true} |
| 181 | + cmdEnterAction={async () => { |
| 182 | + if (inlineScript) { |
| 183 | + inlineScript.content = editor?.getCode() ?? '' |
| 184 | + } |
| 185 | + try { |
| 186 | + runLoading = true |
| 187 | + // await Promise.all( |
| 188 | + // $runnableComponents[id]?.cb?.map((f) => f?.(inlineScript, true)) ?? [] |
| 189 | + // ) |
| 190 | + } catch {} |
| 191 | + runLoading = false |
| 192 | + }} |
| 193 | + on:change={async (e) => { |
| 194 | + if (inlineScript && inlineScript.language != 'frontend') { |
| 195 | + if (inlineScript.lock != undefined) { |
| 196 | + inlineScript.lock = undefined |
| 197 | + } |
| 198 | + const oldSchema = JSON.stringify(inlineScript.schema) |
| 199 | + if (inlineScript.schema == undefined) { |
| 200 | + inlineScript.schema = emptySchema() |
| 201 | + } |
| 202 | + await inferInlineScriptSchema(inlineScript?.language, e.detail, inlineScript.schema) |
| 203 | + if (JSON.stringify(inlineScript.schema) != oldSchema) { |
| 204 | + inlineScript = inlineScript |
| 205 | + } |
| 206 | + } |
| 207 | + // $app = $app |
| 208 | + }} |
| 209 | + args={Object.entries(fields).reduce((acc, [key, obj]) => { |
| 210 | + acc[key] = obj.type === 'static' ? obj.value : undefined |
| 211 | + return acc |
| 212 | + }, {})} |
| 213 | + /> |
| 214 | + |
| 215 | + <DiffEditor |
| 216 | + open={false} |
| 217 | + bind:this={diffEditor} |
| 218 | + class="h-full" |
| 219 | + automaticLayout |
| 220 | + fixedOverflowWidgets |
| 221 | + defaultLang={scriptLangToEditorLang(inlineScript?.language)} |
| 222 | + /> |
| 223 | + {/if} |
| 224 | + </div> |
| 225 | + </div> |
| 226 | +{/if} |
0 commit comments