All files / src/composables useEntityFieldLayout.ts

93.15% Statements 68/73
76.31% Branches 58/76
81.81% Functions 9/11
98.33% Lines 59/60

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                                                    29x 16x 16x 16x   12x 16x 16x   4x 3x     1x         40x 21x 21x 21x 20x       40x 23x 23x 23x 22x                       130x 145x 145x   145x   120x 130x           21x 21x 26x 26x       21x                   21x 21x   21x 26x 26x   25x 26x 26x 26x   26x 15x 15x     10x 26x 26x     21x 21x 21x 6x 4x 10x             6x 6x       21x 15x           21x 21x                 130x    
import { computed, type ComputedRef } from 'vue'
import type { EntityField } from '@/stores/entities'
 
export interface FormFieldPlacement {
  fieldId: string
  order: number
  x?: number
  y?: number
  w?: number
  h?: number
}
 
export interface FieldLayoutCell {
  key: string
  field: EntityField
  span: number
  start: number
  h?: number
}
 
export interface FieldLayoutRow {
  key: string
  cells: FieldLayoutCell[]
}
 
export function sortFormFieldsByPosition(fields: FormFieldPlacement[]): FormFieldPlacement[] {
  return [...fields].sort((a, b) => {
    const aY = a.y ?? Number.MAX_SAFE_INTEGER
    const bY = b.y ?? Number.MAX_SAFE_INTEGER
    if (aY !== bY) return aY - bY
 
    const aX = a.x ?? Number.MAX_SAFE_INTEGER
    const bX = b.x ?? Number.MAX_SAFE_INTEGER
    if (aX !== bX) return aX - bX
 
    if ((a.order ?? 0) !== (b.order ?? 0)) {
      return (a.order ?? 0) - (b.order ?? 0)
    }
 
    return a.fieldId.localeCompare(b.fieldId)
  })
}
 
export function toGridSpan(width: number | undefined): number {
  if (!width || !Number.isFinite(width)) return 12
  const normalized = Math.floor(width)
  Iif (normalized < 1) return 1
  if (normalized > 12) return 12
  return normalized
}
 
export function toGridStart(column: number | undefined): number {
  if (column === undefined || !Number.isFinite(column)) return 1
  const normalized = Math.floor(column) + 1
  Iif (normalized < 1) return 1
  if (normalized > 12) return 12
  return normalized
}
 
/**
 * Lays out entity fields on a 12-column grid, following the same row/order
 * rules a form editor would use. Shared by EntityItemCard and EntityItemHeader
 * so the value rows and the header row always line up.
 */
export function useEntityFieldLayout(
  fields: ComputedRef<EntityField[]> | (() => EntityField[]),
  layout: ComputedRef<FormFieldPlacement[]> | (() => FormFieldPlacement[])
) {
  const visibleFieldRows = computed<FieldLayoutRow[]>(() => {
    const fieldList = typeof fields === 'function' ? fields() : fields.value
    const layoutList = typeof layout === 'function' ? layout() : layout.value
 
    if (!fieldList.length) return []
 
    if (!layoutList?.length) {
      return fieldList.map((field, index) => ({
        key: `default-${field.fieldId ?? index}`,
        cells: [{ key: `default-cell-${field.fieldId ?? index}`, field, span: 12, start: 1 }]
      }))
    }
 
    const fieldsById = new Map<string, EntityField>()
    for (const field of fieldList) {
      Eif (field.fieldId) {
        fieldsById.set(field.fieldId, field)
      }
    }
 
    const rowsByY = new Map<
      number,
      Array<{ key: string; field: EntityField; x: number; span: number; start: number; h?: number }>
    >()
    const fallbackRows: Array<{
      key: string
      field: EntityField
      span: number
      start: number
      h?: number
    }> = []
    const usedFieldIds = new Set<string>()
 
    for (const placement of sortFormFieldsByPosition(layoutList)) {
      const field = fieldsById.get(placement.fieldId)
      if (!field || usedFieldIds.has(placement.fieldId)) continue
 
      const key = `${placement.fieldId}-${placement.order}-${placement.x ?? 'na'}-${placement.y ?? 'na'}`
      const span = toGridSpan(placement.w)
      const start = toGridStart(placement.x)
      usedFieldIds.add(placement.fieldId)
 
      if (placement.y === undefined || placement.x === undefined) {
        fallbackRows.push({ key, field, span, start: 1, h: placement.h })
        continue
      }
 
      const row = rowsByY.get(placement.y) ?? []
      row.push({ key, field, x: placement.x, span, start, h: placement.h })
      rowsByY.set(placement.y, row)
    }
 
    const rows: FieldLayoutRow[] = []
    const sortedY = [...rowsByY.keys()].sort((a, b) => a - b)
    for (const y of sortedY) {
      const cells = (rowsByY.get(y) ?? [])
        .sort((a, b) => a.x - b.x)
        .map((cell) => ({
          key: cell.key,
          field: cell.field,
          span: cell.span,
          start: cell.start,
          h: cell.h
        }))
      Eif (cells.length > 0) {
        rows.push({ key: `row-${y}`, cells })
      }
    }
 
    for (const cell of fallbackRows) {
      rows.push({
        key: `fallback-${cell.key}`,
        cells: [{ key: cell.key, field: cell.field, span: cell.span, start: cell.start, h: cell.h }]
      })
    }
 
    Eif (rows.length > 0) {
      return rows
    }
 
    return fieldList.map((field, index) => ({
      key: `default-${field.fieldId ?? index}`,
      cells: [{ key: `default-cell-${field.fieldId ?? index}`, field, span: 12, start: 1 }]
    }))
  })
 
  return { visibleFieldRows, toGridSpan, toGridStart }
}