Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 | 2x 2x 2x 2x 924x 2x 2x 93x 24x 2x 2x 2x 2x 2x 2x 162x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 8x 2x 2x 5x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 2x 2x 2x 2x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 77x 77x 77x 77x 77x 77x 77x 77x 2002x 77x 2x 77x 77x 77x 77x 77x 11x 11x 9x 9x 9x 77x 14x 14x 5x 5x 5x 2x 3x 77x 12x 12x 12x 121x 103x 103x 103x 18x 121x 12x 12x 12x 6x 4x 4x 4x 2x 2x 8x 8x 1x 1x 1x 7x 6x 6x 6x 6x 1x 1x 5x 5x 5x 3x 3x 77x 2x 2x 2x 1x 1x 3x 3x 3x 1x 3x 1x 3x 1x 3x 1079x 187x 187x 93x 93x 40x 1x 39x 2x 1x 1x 1x 2x 1x 1x 1x 1x 2x 1x 6x 6x 77x 134x 57x 11x 11x 10x 1x 1x 77x 123x 44x 121x 77x 77x 77x 77x 77x 3x 2x 2x 1x 1x 3x 2x 2x 1x 1x 1x 3x 2x 3x 2x 2x 3x 2x 3x 2x 2x 2x | <template>
<div class="schema-builder">
<div class="row g-4">
<!-- Left Panel: Field Palette -->
<div class="col-md-2">
<div class="field-palette">
<draggable
:model-value="sortedFieldTypes"
:group="{ name: 'fields', pull: 'clone', put: false }"
:clone="cloneFieldType"
:sort="false"
item-key="type"
class="palette-list"
>
<template #item="{ element }">
<div class="palette-item">
<font-awesome-icon :icon="element.icon" class="me-2" />
{{ t(`field.${element.type}`) }}
</div>
</template>
</draggable>
</div>
</div>
<!-- Center Panel: Schema Fields -->
<div class="col-md-3">
<draggable
:model-value="modelValue"
:group="{ name: 'fields', put: true }"
item-key="fieldId"
class="fields-list"
:class="{ empty: modelValue.length === 0 }"
@update:model-value="onFieldsUpdate"
>
<template #item="{ element }">
<div
class="field-item"
:class="{ selected: selectedField?.fieldId === element.fieldId }"
:style="{ order: getFieldSortOrder(element) }"
@click="selectField(element)"
>
<div class="field-icon">
<font-awesome-icon :icon="getFieldIcon(element.type)" />
</div>
<div class="field-content flex-grow-1">
<div class="field-name">
{{ getFieldName(element) }}
</div>
<div class="field-meta">
<span class="badge bg-light text-dark">{{ t(`field.${element.type}`) }}</span>
<span v-if="element.length" class="text-muted ms-2">
<small>max: {{ element.length }}</small>
</span>
</div>
</div>
<button
type="button"
class="btn btn-sm btn-outline-danger field-delete"
@click.stop="removeField(element)"
>
<font-awesome-icon icon="fa-solid fa-trash" />
</button>
</div>
</template>
<template #footer>
<div v-if="modelValue.length === 0" class="empty-state text-center py-5">
<font-awesome-icon
icon="fa-solid fa-hand-pointer"
size="3x"
class="text-muted mb-3"
/>
<p class="text-muted">{{ t('schema.dragFieldsHere') }}</p>
</div>
</template>
</draggable>
</div>
<!-- Right Panel: Field Configuration -->
<div class="col-md-7">
<div v-if="!selectedField" class="text-center text-muted py-4">
<font-awesome-icon icon="fa-solid fa-mouse-pointer" size="2x" class="mb-2" />
<p class="small">{{ t('schema.selectFieldToConfig') }}</p>
</div>
<div v-else class="field-config">
<!-- Field Name (Multilingual) -->
<div class="mb-3">
<label class="form-label small fw-bold">{{ t('schema.fieldName') }}</label>
<MultilingualInput
v-model="selectedField.name"
:placeholder="t('field.placeholderFieldName')"
:default-language="defaultLanguage"
:enabled-languages="enabledLanguages"
:current-language="currentLanguage"
:show-language-selector="false"
:show-language-hint="true"
required
/>
</div>
<!-- Field Type (Read-only) -->
<div class="mb-3">
<label class="form-label small fw-bold">{{ t('schema.fieldType') }}</label>
<div class="form-control-plaintext">
<font-awesome-icon :icon="getFieldIcon(selectedField.type || 'text')" class="me-2" />
{{ selectedField.type ? t(`field.${selectedField.type}`) : '' }}
</div>
</div>
<!-- Type-specific configurations -->
<template
v-if="selectedField.type && ['text', 'textarea', 'email'].includes(selectedField.type)"
>
<div class="mb-3">
<label class="form-label small fw-bold">{{ t('field.maxLength') }}</label>
<input
v-model.number="selectedField.length"
type="number"
class="form-control form-control-sm"
min="1"
/>
</div>
<div class="mb-3">
<label class="form-label small fw-bold">{{ t('field.placeholder') }}</label>
<input
v-model="selectedField.placeholder"
type="text"
class="form-control form-control-sm"
/>
</div>
</template>
<template v-if="selectedField.type === 'number'">
<div class="mb-3">
<label class="form-label small fw-bold">{{ t('schema.minValue') }}</label>
<input
v-model.number="selectedField.min"
type="number"
class="form-control form-control-sm"
/>
</div>
<div class="mb-3">
<label class="form-label small fw-bold">{{ t('schema.maxValue') }}</label>
<input
v-model.number="selectedField.max"
type="number"
class="form-control form-control-sm"
/>
</div>
<div class="mb-3">
<label class="form-label small fw-bold">{{ t('schema.step') }}</label>
<input
v-model.number="selectedField.step"
type="number"
class="form-control form-control-sm"
step="0.01"
/>
</div>
</template>
<template v-if="selectedField.type === 'select'">
<div class="mb-3">
<label class="form-label small fw-bold">{{ t('field.optionsCommaSeparated') }}</label>
<textarea
v-model="selectedField.optionsStr"
class="form-control form-control-sm"
rows="4"
:placeholder="t('schema.oneOptionPerLine')"
@blur="parseOptions(selectedField)"
/>
<small class="text-muted">{{ t('schema.oneOptionPerLine') }}</small>
</div>
</template>
<template v-if="selectedField.type === 'image' || selectedField.type === 'image_set'">
<div class="row g-2 mb-2">
<div class="col-6">
<label class="form-label small fw-bold">{{ t('schema.imageMinWidth') }}</label>
<input
v-model.number="selectedField.minWidth"
type="number"
min="1"
class="form-control form-control-sm"
/>
</div>
<div class="col-6">
<label class="form-label small fw-bold">{{ t('schema.imageMaxWidth') }}</label>
<input
v-model.number="selectedField.maxWidth"
type="number"
min="1"
class="form-control form-control-sm"
/>
</div>
<div class="col-6">
<label class="form-label small fw-bold">{{ t('schema.imageMinHeight') }}</label>
<input
v-model.number="selectedField.minHeight"
type="number"
min="1"
class="form-control form-control-sm"
/>
</div>
<div class="col-6">
<label class="form-label small fw-bold">{{ t('schema.imageMaxHeight') }}</label>
<input
v-model.number="selectedField.maxHeight"
type="number"
min="1"
class="form-control form-control-sm"
/>
</div>
</div>
<div class="row g-2 mb-2">
<div class="col-12">
<label class="form-label small fw-bold">{{ t('schema.imageAspectRatio') }}</label>
<select
v-model="selectedAspectRatioPreset"
class="form-select form-select-sm"
@change="onAspectRatioPresetChange"
>
<option value="">{{ t('schema.imageAspectRatioFree') }}</option>
<option value="1:1">1:1</option>
<option value="4:3">4:3</option>
<option value="3:2">3:2</option>
<option value="16:9">16:9</option>
<option value="9:16">9:16</option>
<option :value="CUSTOM_ASPECT_RATIO">
{{ t('schema.imageAspectRatioCustom') }}
</option>
</select>
<small class="text-muted">{{ t('schema.imageAspectRatioHint') }}</small>
</div>
<div v-if="selectedAspectRatioPreset === CUSTOM_ASPECT_RATIO" class="col-12">
<input
v-model="selectedAspectRatioCustom"
type="text"
class="form-control form-control-sm"
:class="{ 'is-invalid': isCustomAspectRatioInvalid }"
:placeholder="t('schema.imageAspectRatioCustomPlaceholder')"
@blur="onAspectRatioCustomBlur"
@keyup.enter="onAspectRatioCustomBlur"
/>
<small class="text-muted">{{ t('schema.imageAspectRatioCustomHint') }}</small>
</div>
<div class="col-6">
<label class="form-label small fw-bold">
{{ t('schema.imageResizeMaxWidth') }}
</label>
<input
v-model.number="selectedField.resizeMaxWidth"
type="number"
min="1"
class="form-control form-control-sm"
/>
</div>
<div class="col-6">
<label class="form-label small fw-bold">
{{ t('schema.imageResizeMaxHeight') }}
</label>
<input
v-model.number="selectedField.resizeMaxHeight"
type="number"
min="1"
class="form-control form-control-sm"
/>
</div>
</div>
<div class="mb-3">
<div class="form-check">
<input
id="field-image-crop-enabled"
v-model="selectedField.cropEnabled"
type="checkbox"
class="form-check-input"
/>
<label class="form-check-label small fw-bold" for="field-image-crop-enabled">
{{ t('schema.imageEnableCrop') }}
</label>
</div>
<small class="text-muted">{{ t('schema.imageCropHint') }}</small>
</div>
</template>
<template v-if="selectedField.type === 'image_set'">
<div class="mb-3">
<label class="form-label small fw-bold">{{ t('schema.maxImages') }}</label>
<input
v-model.number="selectedField.maxImages"
type="number"
class="form-control form-control-sm"
min="1"
max="20"
/>
</div>
</template>
<!-- entity_ref: target entity selector -->
<template v-if="selectedField.type === 'entity_ref'">
<div class="mb-3">
<label class="form-label small fw-bold">{{ t('field.referenceTargetEntity') }}</label>
<select
v-model="selectedField.reference!.entityId"
class="form-select form-select-sm"
>
<option value="">{{ t('common.select') }}</option>
<option
v-for="entity in entitiesStore.entities"
:key="entity.id"
:value="entity.id"
>
{{
typeof entity.name === 'string'
? entity.name
: (entity.name?.['en'] ?? entity.id)
}}
</option>
</select>
<small class="text-muted">{{ t('field.referenceTargetEntityHelp') }}</small>
</div>
<div class="mb-3">
<label class="form-label small fw-bold">{{ t('field.referenceDisplayForm') }}</label>
<select
v-model="selectedField.reference!.displayFormId"
class="form-select form-select-sm"
:disabled="!selectedTargetEntityForms.length"
>
<option value="">{{ t('field.referenceDisplayFormDefault') }}</option>
<option v-for="form in selectedTargetEntityForms" :key="form.id" :value="form.id">
{{
form.name?.[currentLanguage] ||
form.name?.[defaultLanguage] ||
form.name?.en ||
form.id
}}
</option>
</select>
<small class="text-muted">{{ t('field.referenceDisplayFormHelp') }}</small>
</div>
</template>
<!-- multi-value config (user and entity_ref) -->
<template v-if="['user', 'entity_ref'].includes(selectedField.type ?? '')">
<div class="mb-3">
<div class="form-check">
<input
id="field-reference-multiple"
v-model="selectedField.reference!.multiple"
type="checkbox"
class="form-check-input"
/>
<label class="form-check-label small fw-bold" for="field-reference-multiple">
{{ t('field.referenceMultiple') }}
</label>
</div>
</div>
<template v-if="selectedField.reference?.multiple">
<div class="mb-3">
<label class="form-label small fw-bold">{{ t('field.referenceMinValues') }}</label>
<input
v-model.number="selectedField.reference!.minValues"
type="number"
min="0"
class="form-control form-control-sm"
/>
</div>
<div class="mb-3">
<label class="form-label small fw-bold">{{ t('field.referenceMaxValues') }}</label>
<input
v-model.number="selectedField.reference!.maxValues"
type="number"
min="1"
class="form-control form-control-sm"
/>
</div>
</template>
</template>
<template v-if="selectedField.type === 'workflow'">
<div class="mb-3">
<label class="form-label small fw-bold">{{ t('schema.workflowDefinition') }}</label>
<WorkflowEditor
:key="selectedField.fieldId"
v-model="selectedField.workflowDefinition"
:default-language="defaultLanguage"
:enabled-languages="enabledLanguages"
:current-language="currentLanguage"
@open-state-panel="openStatePanel"
@open-transition-panel="openTransitionPanel"
/>
</div>
</template>
<!-- Mandatory toggle (not for boolean/workflow types) -->
<div v-if="!['boolean', 'workflow'].includes(selectedField.type ?? '')" class="mb-3">
<div class="form-check">
<input
id="field-mandatory"
v-model="selectedField.mandatory"
type="checkbox"
class="form-check-input"
/>
<label class="form-check-label small fw-bold" for="field-mandatory">
{{ t('field.mandatory') }}
</label>
</div>
</div>
<!-- Unique toggle (text/email/number/select/date/textarea types) -->
<div
v-if="
['text', 'email', 'number', 'select', 'date', 'textarea'].includes(
selectedField.type ?? ''
)
"
class="mb-3"
>
<div class="form-check">
<input
id="field-unique"
v-model="selectedField.unique"
type="checkbox"
class="form-check-input"
/>
<label class="form-check-label small fw-bold" for="field-unique">
{{ t('field.unique') }}
</label>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Workflow state editor panel -->
<WorkflowStatePanel
:is-open="statePanelOpen"
:state="editingState"
:enabled-languages="enabledLanguages"
:default-language="defaultLanguage"
:current-language="currentLanguage"
:forms="forms"
@close="statePanelOpen = false"
@save="onStateSaved"
/>
<!-- Workflow transition editor panel -->
<WorkflowTransitionPanel
:is-open="transitionPanelOpen"
:transition="editingTransition"
:transition-index="editingTransitionIndex"
:all-states="selectedField?.workflowDefinition?.states ?? []"
:enabled-languages="enabledLanguages"
:default-language="defaultLanguage"
:current-language="currentLanguage"
@close="transitionPanelOpen = false"
@save="onTransitionSaved"
/>
</template>
<script setup lang="ts">
// @implements UC-WORKFLOW-001.1, UC-I18N-005.1, UC-I18N-010.1
import { computed, ref, watch } from 'vue'
import draggable from 'vuedraggable'
import MultilingualInput from './MultilingualInput.vue'
import WorkflowEditor from './WorkflowEditor.vue'
import WorkflowStatePanel from './WorkflowStatePanel.vue'
import WorkflowTransitionPanel from './WorkflowTransitionPanel.vue'
import { useUILanguage } from '@/composables/useUILanguage'
import { useEntitiesStore } from '@/stores/entities'
import type { EntityField } from '@/stores/entities'
import type {
WorkflowDefinition,
WorkflowState,
WorkflowTransition,
FormDefinition
} from '@/schemas/entity-schema'
const { t, uiLanguage } = useUILanguage()
const entitiesStore = useEntitiesStore()
interface FieldForm extends EntityField {
optionsStr?: string
workflowDefinition?: WorkflowDefinition
min?: number
max?: number
step?: number
}
interface FieldType {
type: string
icon: string
}
interface Props {
modelValue: FieldForm[]
defaultLanguage?: string
enabledLanguages?: string[]
currentLanguage?: string
hasItems?: boolean // If true, disable schema modifications
forms?: FormDefinition[]
}
interface Emits {
(e: 'update:modelValue', value: FieldForm[]): void
}
const props = withDefaults(defineProps<Props>(), {
defaultLanguage: 'en',
enabledLanguages: () => ['en', 'fr'],
currentLanguage: 'en',
hasItems: false,
forms: () => []
})
const emit = defineEmits<Emits>()
// Field types palette
const fieldTypes: FieldType[] = [
{ type: 'boolean', icon: 'fa-solid fa-toggle-on' },
{ type: 'date', icon: 'fa-solid fa-calendar' },
{ type: 'email', icon: 'fa-solid fa-envelope' },
{ type: 'entity_ref', icon: 'fa-solid fa-link' },
{ type: 'image', icon: 'fa-solid fa-image' },
{ type: 'image_set', icon: 'fa-solid fa-images' },
{ type: 'number', icon: 'fa-solid fa-hashtag' },
{ type: 'select', icon: 'fa-solid fa-list' },
{ type: 'text', icon: 'fa-solid fa-font' },
{ type: 'textarea', icon: 'fa-solid fa-align-left' },
{ type: 'user', icon: 'fa-solid fa-user' },
{ type: 'workflow', icon: 'fa-solid fa-diagram-project' }
]
const sortedFieldTypes = computed(() => {
const collator = new Intl.Collator(uiLanguage.value, { sensitivity: 'base' })
return [...fieldTypes].sort((a, b) => {
return collator.compare(t(`field.${a.type}`), t(`field.${b.type}`))
})
})
const selectedField = ref<FieldForm | null>(null)
const CUSTOM_ASPECT_RATIO = '__custom__'
const PRESET_ASPECT_RATIOS = ['', '1:1', '4:3', '3:2', '16:9', '9:16']
const selectedAspectRatioPreset = ref('')
const selectedAspectRatioCustom = ref('')
const ASPECT_RATIO_PATTERN = /^(\d+(?:\.\d+)?)\s*[:xX/]\s*(\d+(?:\.\d+)?)$/
const isAspectRatioFormat = (value: string): boolean => {
const match = value.trim().match(ASPECT_RATIO_PATTERN)
if (!match) return false
const width = Number.parseFloat(match[1])
const height = Number.parseFloat(match[2])
return Number.isFinite(width) && Number.isFinite(height) && width > 0 && height > 0
}
const normalizeAspectRatio = (value: string): string | null => {
const match = value.trim().match(ASPECT_RATIO_PATTERN)
if (!match) return null
const width = Number.parseFloat(match[1])
const height = Number.parseFloat(match[2])
if (!Number.isFinite(width) || !Number.isFinite(height) || width <= 0 || height <= 0) {
return null
}
return `${width}:${height}`
}
const isCustomAspectRatioInvalid = computed(() => {
Iif (selectedAspectRatioPreset.value !== CUSTOM_ASPECT_RATIO) {
return false
}
const current = selectedAspectRatioCustom.value.trim()
return current !== '' && !isAspectRatioFormat(current)
})
function syncAspectRatioControls() {
if (!selectedField.value || !['image', 'image_set'].includes(selectedField.value.type ?? '')) {
selectedAspectRatioPreset.value = ''
selectedAspectRatioCustom.value = ''
return
}
const ratio = (selectedField.value.aspectRatio || '').trim()
if (!ratio) {
selectedAspectRatioPreset.value = ''
selectedAspectRatioCustom.value = ''
return
}
if (PRESET_ASPECT_RATIOS.includes(ratio)) {
selectedAspectRatioPreset.value = ratio
selectedAspectRatioCustom.value = ''
return
}
selectedAspectRatioPreset.value = CUSTOM_ASPECT_RATIO
selectedAspectRatioCustom.value = ratio
}
function onAspectRatioPresetChange() {
Iif (!selectedField.value) return
if (selectedAspectRatioPreset.value === '') {
delete selectedField.value.aspectRatio
selectedAspectRatioCustom.value = ''
return
}
if (selectedAspectRatioPreset.value === CUSTOM_ASPECT_RATIO) {
const normalized = normalizeAspectRatio(selectedAspectRatioCustom.value)
Iif (normalized) {
selectedAspectRatioCustom.value = normalized
selectedField.value.aspectRatio = normalized
} else {
delete selectedField.value.aspectRatio
}
return
}
selectedField.value.aspectRatio = selectedAspectRatioPreset.value
selectedAspectRatioCustom.value = ''
}
function onAspectRatioCustomBlur() {
Iif (!selectedField.value || selectedAspectRatioPreset.value !== CUSTOM_ASPECT_RATIO) return
const normalized = normalizeAspectRatio(selectedAspectRatioCustom.value)
if (!normalized) return
selectedAspectRatioCustom.value = normalized
selectedField.value.aspectRatio = normalized
}
const selectedTargetEntityForms = computed<FormDefinition[]>(() => {
Iif (selectedField.value?.type !== 'entity_ref') return []
const targetEntityId = selectedField.value.reference?.entityId
if (!targetEntityId) return []
const targetEntity = entitiesStore.entities.find((entity) => entity.id === targetEntityId)
return targetEntity?.forms ?? []
})
// Clone a field type from palette to create a new field
function cloneFieldType(fieldType: FieldType): FieldForm {
const fieldId = `field_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`
const newField: FieldForm = {
fieldId,
name: { en: '', fr: '' },
type: fieldType.type as any,
mandatory: false,
unique: false
}
// Set defaults based on type
if (fieldType.type === 'image_set') {
newField.maxImages = 10
}
if (fieldType.type === 'user') {
newField.reference = { kind: 'user' }
}
if (fieldType.type === 'entity_ref') {
newField.reference = { kind: 'entity' }
}
return newField
}
function getFieldIcon(type: string): string {
return fieldTypes.find((ft) => ft.type === type)?.icon || 'fa-solid fa-question'
}
function getFieldName(field: FieldForm): string {
Iif (typeof field.name === 'string') {
return field.name || t('schema.unnamedField')
}
return field.name?.[props.currentLanguage] || field.name?.['en'] || t('schema.unnamedField')
}
function getFieldSortOrder(field: FieldForm): number {
// Calculate alphabetical order for CSS flexbox ordering
const fieldName = getFieldName(field).toLowerCase()
// Simple hash to generate consistent order
return fieldName.charCodeAt(0) * 1000 + (fieldName.charCodeAt(1) || 0)
}
function selectField(field: FieldForm) {
if (props.hasItems) {
// Don't allow editing if entity has items
return
}
selectedField.value = field
}
function removeField(field: FieldForm) {
if (props.hasItems) {
return
}
const index = props.modelValue.findIndex((f) => f.fieldId === field.fieldId)
Eif (index > -1) {
const updatedFields = props.modelValue.filter((_, i) => i !== index)
emit('update:modelValue', updatedFields)
// Deselect if removed field was selected
Eif (selectedField.value?.fieldId === field.fieldId) {
selectedField.value = null
}
}
}
function onFieldsUpdate(updatedFields: FieldForm[]) {
// When draggable updates the fields list (add, remove, reorder)
emit('update:modelValue', updatedFields)
}
function parseOptions(field: FieldForm) {
if (field.optionsStr) {
// Split by newlines or commas
field.options = field.optionsStr
.split(/[\n,]/)
.map((opt) => opt.trim())
.filter((opt) => opt)
}
}
// Watch modelValue for external changes (e.g., auto-save)
watch(
() => props.modelValue,
(newVal) => {
// Update selectedField if it still exists in the list
if (selectedField.value) {
const updated = newVal.find((f) => f.fieldId === selectedField.value?.fieldId)
if (updated) {
// Only reassign when the object reference changed (e.g. after a drag emits
// a new array that flows back from the parent). Skip reassignment for
// in-place mutations (same ref, e.g. typing a field name): reassigning the
// same object causes Vue to re-render all field-item :class bindings, which
// patches the DOM and confuses SortableJS mid-drag.
if (updated !== selectedField.value) {
selectedField.value = updated
}
} else {
selectedField.value = null
}
}
},
{ deep: true }
)
watch(
() => {
if (!selectedField.value) return ''
return `${selectedField.value.fieldId ?? ''}:${selectedField.value.type ?? ''}:${selectedField.value.aspectRatio ?? ''}`
},
() => {
syncAspectRatioControls()
},
{ immediate: true }
)
// ---- Workflow panel state ----
const statePanelOpen = ref(false)
const editingState = ref<WorkflowState | null>(null)
const transitionPanelOpen = ref(false)
const editingTransitionIndex = ref(-1)
const editingTransition = ref<WorkflowTransition | null>(null)
function openStatePanel(stateId: string) {
if (!selectedField.value?.workflowDefinition) return
const state = selectedField.value.workflowDefinition.states.find((s) => s.id === stateId)
if (!state) return
editingState.value = state
statePanelOpen.value = true
}
function openTransitionPanel(transitionIndex: number) {
if (!selectedField.value?.workflowDefinition) return
const transition = selectedField.value.workflowDefinition.transitions[transitionIndex]
if (!transition) return
editingTransition.value = transition
editingTransitionIndex.value = transitionIndex
transitionPanelOpen.value = true
}
function onStateSaved(updatedState: WorkflowState) {
if (!selectedField.value?.workflowDefinition) return
selectedField.value.workflowDefinition = {
...selectedField.value.workflowDefinition,
states: selectedField.value.workflowDefinition.states.map((s) =>
s.id === updatedState.id ? updatedState : s
)
}
statePanelOpen.value = false
editingState.value = null
}
function onTransitionSaved(index: number, updatedTransition: WorkflowTransition) {
if (!selectedField.value?.workflowDefinition) return
selectedField.value.workflowDefinition = {
...selectedField.value.workflowDefinition,
transitions: selectedField.value.workflowDefinition.transitions.map((t, i) =>
i === index ? updatedTransition : t
)
}
transitionPanelOpen.value = false
editingTransition.value = null
editingTransitionIndex.value = -1
}
</script>
<style scoped>
.schema-builder {
min-height: 500px;
}
.field-palette {
min-height: 400px;
}
.palette-list {
display: flex;
flex-direction: column;
gap: 0.5rem;
}
.palette-item {
padding: 0.75rem;
background: var(--bw-surface-muted);
border: 2px dashed var(--bw-border);
border-radius: 0.375rem;
cursor: move;
transition:
background-color 0.2s,
border-color 0.2s,
border-style 0.2s;
user-select: none;
}
.palette-item:hover {
background: color-mix(in srgb, var(--bw-surface-muted) 80%, var(--bw-text) 20%);
border-color: #615581;
border-style: solid;
}
.fields-list {
min-height: 200px;
height: 100%;
display: flex;
flex-direction: column;
gap: 0.5rem;
}
.fields-list.empty {
min-height: 400px;
height: 100%;
}
.field-item {
display: flex;
align-items: center;
gap: 0.75rem;
padding: 0.75rem;
background: var(--bw-surface);
border: 1px solid var(--bw-border);
border-radius: 0.375rem;
cursor: pointer;
/* Exclude 'order' from transition: flexbox reorder must be instant so SortableJS
reads stable getBoundingClientRect() during drags (no mid-animation positions). */
transition:
background-color 0.2s,
border-color 0.2s,
border-width 0.1s,
box-shadow 0.2s;
}
.field-item:hover {
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
border-color: #615581;
}
.field-item.selected {
background: color-mix(in srgb, var(--bw-surface) 78%, #615581 22%);
border-color: #615581;
border-width: 2px;
}
.field-icon {
width: 2rem;
height: 2rem;
display: flex;
align-items: center;
justify-content: center;
background: var(--bw-surface-muted);
border-radius: 0.25rem;
color: var(--bw-text-muted);
}
.field-content {
display: flex;
flex-direction: column;
gap: 0.25rem;
}
.field-name {
font-weight: 500;
color: var(--bw-text);
}
.field-meta {
display: flex;
align-items: center;
gap: 0.5rem;
}
.field-delete {
opacity: 0;
transition: opacity 0.2s;
}
.field-item:hover .field-delete {
opacity: 1;
}
.empty-state {
height: 100%;
border: 2px dashed var(--bw-border);
border-radius: 0.375rem;
background: var(--bw-surface-muted);
}
.field-config {
padding: 0.5rem;
}
.field-config .form-label {
margin-bottom: 0.5rem;
}
.field-config .form-control,
.field-config .form-control-sm {
font-size: 0.95rem;
}
</style>
|