You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
4.4 KiB

7 months ago
<script lang="ts">
7 months ago
import type { MetadataField } from '../../lib';
import FieldValueEditor from './FieldValueEditor.svelte';
7 months ago
let {
7 months ago
fieldKey, field, locales, defaultLocale,
attributes = [],
onKeyChange, onFieldChange,
7 months ago
}: {
fieldKey: string;
field: MetadataField;
locales: string[];
defaultLocale: string;
7 months ago
attributes?: string[];
7 months ago
onKeyChange: (newKey: string) => void;
onFieldChange: (field: MetadataField) => void;
} = $props();
7 months ago
let editKey = $state(fieldKey);
let keyError = $state('');
7 months ago
7 months ago
// Sincronizar con prop si cambia externamente (p.ej. tras rename en otro sitio)
$effect(() => { editKey = fieldKey; });
let asBase = $derived(field as MetadataField & { hidden?: boolean; attrs?: string[] });
let activeAttrs = $derived(asBase.attrs ?? []);
function handleKeyCommit(): void {
7 months ago
const trimmed = editKey.trim();
7 months ago
if (!trimmed) {
keyError = 'La clave no puede estar vacía';
editKey = fieldKey; // revertir
return;
}
keyError = '';
if (trimmed !== fieldKey) onKeyChange(trimmed);
7 months ago
}
7 months ago
function handleKeydown(e: KeyboardEvent): void {
e.stopPropagation(); // evitar que suba al header de la fila
if (e.key === 'Enter') (e.currentTarget as HTMLInputElement).blur();
if (e.key === 'Escape') { editKey = fieldKey; keyError = ''; (e.currentTarget as HTMLInputElement).blur(); }
7 months ago
}
7 months ago
function update(partial: Partial<MetadataField & { hidden?: boolean; attrs?: string[] }>): void {
onFieldChange({ ...field, ...partial } as MetadataField);
7 months ago
}
7 months ago
function toggleAttr(attr: string): void {
const current = asBase.attrs ?? [];
const next = current.includes(attr)
? current.filter(a => a !== attr)
: [...current, attr];
update({ attrs: next });
}
7 months ago
</script>
7 months ago
<div class="flex flex-col gap-4 p-4">
7 months ago
<!-- Key -->
7 months ago
<fieldset class="fieldset w-full">
<legend class="fieldset-legend">
Clave del campo
{#if keyError}
<span class="text-error ml-2 font-normal text-xs">{keyError}</span>
{/if}
</legend>
7 months ago
<input
7 months ago
value={editKey}
oninput={(e) => { editKey = (e.currentTarget as HTMLInputElement).value; keyError = ''; }}
onblur={handleKeyCommit}
onkeydown={handleKeydown}
class={['input input-bordered w-full font-mono', keyError ? 'input-error' : ''].join(' ')}
7 months ago
placeholder="nombre_campo"
/>
7 months ago
</fieldset>
<!-- Value editor by type -->
<FieldValueEditor {field} {locales} {defaultLocale} {onFieldChange} />
<!-- Attributes -->
{#if attributes.length > 0}
<div class="divider my-0"></div>
<div class="flex flex-col gap-2">
<span class="text-base-content/50">Atributos</span>
<div class="flex flex-wrap gap-2">
{#each attributes as attr (attr)}
{@const active = activeAttrs.includes(attr)}
<button
type="button"
onclick={() => toggleAttr(attr)}
class={['badge badge-md cursor-pointer select-none transition-colors',
active ? 'badge-primary' : 'badge-ghost border border-base-300'
].join(' ')}
>
{#if active}
<svg class="mr-1 h-3 w-3" viewBox="0 0 12 12" xmlns="http://www.w3.org/2000/svg">
<path fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" d="M2 6l3 3 5-5"/>
</svg>
{/if}
{attr}
</button>
{/each}
7 months ago
</div>
</div>
{/if}
<!-- Hidden toggle -->
7 months ago
<div class="divider my-0"></div>
<div class="flex items-center gap-4">
<span>Ocultar campo</span>
<input type="checkbox" checked={asBase.hidden ?? false}
onchange={(e) => update({ hidden: (e.currentTarget as HTMLInputElement).checked })}
class="toggle toggle-sm" />
<span class="text-base-content/50">{asBase.hidden ? 'No se muestra en la UI' : 'Visible'}</span>
7 months ago
</div>
7 months ago
</div>

Powered by TurnKey Linux.