49 lines
2.7 KiB
Vue
49 lines
2.7 KiB
Vue
<script setup lang="ts">
|
|
import { computed } from "vue";
|
|
import { Plus, Trash2 } from "@lucide/vue";
|
|
|
|
import { Button } from "@/components/ui/button";
|
|
import { Input } from "@/components/ui/input";
|
|
import { Switch } from "@/components/ui/switch";
|
|
import OptionSelect from "./OptionSelect.vue";
|
|
import { setWorkflowField, workflowField } from "./workflow-edit";
|
|
|
|
// A workflow's declared inputs or outputs: name, type and whether required.
|
|
const props = defineProps<{ yaml: string; field: "inputs" | "outputs" }>();
|
|
const emit = defineEmits<{ "update:yaml": [string] }>();
|
|
|
|
type Spec = { type?: string; required?: boolean };
|
|
const TYPES = ["string", "number", "bool", "object", "array", "any"].map((value) => ({ value, label: value }));
|
|
|
|
const list = computed(() => Object.entries((workflowField(props.yaml, props.field) as Record<string, Spec> | undefined) ?? {}));
|
|
function write(entries: [string, Spec][]): void {
|
|
emit("update:yaml", setWorkflowField(props.yaml, props.field, Object.fromEntries(entries.filter(([name]) => name))));
|
|
}
|
|
function edit(index: number, name: string, spec: Spec): void {
|
|
write(list.value.map((entry, i) => (i === index ? [name, spec] : entry)));
|
|
}
|
|
function add(): void {
|
|
const stem = props.field.slice(0, -1);
|
|
let n = list.value.length + 1;
|
|
while (list.value.some(([name]) => name === `${stem}_${n}`)) n++;
|
|
write([...list.value, [`${stem}_${n}`, { type: "string" }]]);
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<section class="space-y-1.5">
|
|
<div class="flex items-center text-xs font-medium text-muted-foreground">
|
|
{{ field === "inputs" ? "Inputs" : "Outputs" }}
|
|
<Button type="button" variant="ghost" size="xs" class="ml-auto" @click="add"><Plus data-icon="inline-start" />Add</Button>
|
|
</div>
|
|
<div v-if="list.length" class="divide-y divide-border rounded-md border border-border">
|
|
<div v-for="([name, spec], i) in list" :key="i" class="flex items-center gap-2 px-2 py-1.5">
|
|
<Input :model-value="name" class="h-7 min-w-0 flex-1 font-mono text-xs" :aria-label="`${field} name`" @change="edit(i, ($event.target as HTMLInputElement).value.trim(), spec)" />
|
|
<OptionSelect :model-value="spec.type ?? 'string'" :options="TYPES" class="w-24" :aria-label="`${name} type`" @update:model-value="edit(i, name, { ...spec, type: $event })" />
|
|
<Switch :model-value="spec.required === true" :title="spec.required ? 'Required' : 'Optional'" :aria-label="`${name} required`" @update:model-value="edit(i, name, { ...spec, required: $event || undefined })" />
|
|
<Button type="button" variant="ghost" size="icon-xs" :aria-label="`Remove ${name}`" @click="write(list.filter((_, j) => j !== i))"><Trash2 /></Button>
|
|
</div>
|
|
</div>
|
|
</section>
|
|
</template>
|