|
| 1 | +<template> |
| 2 | + <div class="column-display-editor"> |
| 3 | + <div class="field"> |
| 4 | + <div class="label">Field</div> |
| 5 | + <v-select |
| 6 | + :model-value="form.fieldKey" |
| 7 | + :items="availableFieldChoices" |
| 8 | + :disabled="mode === 'edit'" |
| 9 | + placeholder="Select a column" |
| 10 | + @update:model-value="onFieldChange" |
| 11 | + /> |
| 12 | + </div> |
| 13 | + |
| 14 | + <div class="field"> |
| 15 | + <div class="label">Display Template</div> |
| 16 | + <interface-system-display-template |
| 17 | + :collection-name="targetCollection" |
| 18 | + :value="form.template" |
| 19 | + placeholder="{{ field }}" |
| 20 | + :include-relations="true" |
| 21 | + @input="form.template = $event ?? ''" |
| 22 | + /> |
| 23 | + </div> |
| 24 | + |
| 25 | + <div class="actions"> |
| 26 | + <v-button secondary small @click="$emit('cancel')">Cancel</v-button> |
| 27 | + <v-button :disabled="!canSave" small @click="onSave">Save</v-button> |
| 28 | + </div> |
| 29 | + </div> |
| 30 | +</template> |
| 31 | + |
| 32 | +<script lang="ts" setup> |
| 33 | +import { computed, ref, watch } from 'vue'; |
| 34 | +import { useStores } from '@directus/extensions-sdk'; |
| 35 | +import type { ColumnDisplay } from '../composables/useColumnDisplays'; |
| 36 | +import { isRelational, resolveTargetCollection } from '../utils/displayHeuristics'; |
| 37 | +
|
| 38 | +const props = defineProps<{ |
| 39 | + mode: 'add' | 'edit'; |
| 40 | + initialFieldKey?: string; |
| 41 | + initialValue?: ColumnDisplay; |
| 42 | + collection: string; |
| 43 | + availableFieldChoices: Array<{ text: string; value: string }>; |
| 44 | +}>(); |
| 45 | +
|
| 46 | +const emit = defineEmits<{ |
| 47 | + (e: 'save', payload: { fieldKey: string; display: ColumnDisplay }): void; |
| 48 | + (e: 'cancel'): void; |
| 49 | +}>(); |
| 50 | +
|
| 51 | +const { useFieldsStore, useRelationsStore } = useStores(); |
| 52 | +const fieldsStore = useFieldsStore(); |
| 53 | +const relationsStore = useRelationsStore(); |
| 54 | +
|
| 55 | +const form = ref<{ fieldKey: string; template: string }>({ |
| 56 | + fieldKey: props.initialFieldKey ?? '', |
| 57 | + template: props.initialValue?.template ?? '', |
| 58 | +}); |
| 59 | +
|
| 60 | +const targetCollection = computed(() => { |
| 61 | + if (!form.value.fieldKey) return null; |
| 62 | + // Strip language suffix (translations.title:de-DE → translations.title) |
| 63 | + const rootKey = form.value.fieldKey.includes(':') |
| 64 | + ? form.value.fieldKey.split(':')[0] |
| 65 | + : form.value.fieldKey; |
| 66 | + // Use root field on the parent for relational lookup |
| 67 | + const rootField = rootKey.split('.')[0]; |
| 68 | + const fieldDef = fieldsStore.getField(props.collection, rootField); |
| 69 | + if (!fieldDef) return props.collection; |
| 70 | + if (!isRelational(fieldDef)) return props.collection; |
| 71 | + const target = resolveTargetCollection(fieldDef, relationsStore as any, fieldsStore as any); |
| 72 | + return target ?? props.collection; |
| 73 | +}); |
| 74 | +
|
| 75 | +const canSave = computed(() => { |
| 76 | + if (!form.value.fieldKey) return false; |
| 77 | + // Save is disabled in both modes when the template is empty. To delete an |
| 78 | + // existing override the user clicks the ⊘ icon on the item, which is the |
| 79 | + // explicit, discoverable path. (Avoids a "Save erases my override" surprise.) |
| 80 | + if (!form.value.template.trim()) return false; |
| 81 | + return true; |
| 82 | +}); |
| 83 | +
|
| 84 | +function onFieldChange(value: string) { |
| 85 | + form.value.fieldKey = value; |
| 86 | + // When the chosen field changes, clear the template to avoid stale tokens |
| 87 | + form.value.template = ''; |
| 88 | +} |
| 89 | +
|
| 90 | +function onSave() { |
| 91 | + emit('save', { |
| 92 | + fieldKey: form.value.fieldKey, |
| 93 | + display: { template: form.value.template }, |
| 94 | + }); |
| 95 | +} |
| 96 | +
|
| 97 | +watch( |
| 98 | + () => props.initialValue, |
| 99 | + (val) => { |
| 100 | + if (val) form.value.template = val.template; |
| 101 | + } |
| 102 | +); |
| 103 | +</script> |
| 104 | + |
| 105 | +<style scoped> |
| 106 | +.column-display-editor { |
| 107 | + width: 100%; |
| 108 | + margin-bottom: 12px; |
| 109 | +} |
| 110 | +.field { |
| 111 | + margin-bottom: 12px; |
| 112 | +} |
| 113 | +.label { |
| 114 | + display: block; |
| 115 | + margin-bottom: 4px; |
| 116 | + color: var(--foreground-normal); |
| 117 | + font-weight: 600; |
| 118 | + font-size: 13px; |
| 119 | +} |
| 120 | +.actions { |
| 121 | + display: flex; |
| 122 | + gap: 8px; |
| 123 | + justify-content: flex-end; |
| 124 | +} |
| 125 | +</style> |
0 commit comments