刘光辉
9 小时以前 cf11e63b659afcc9792ab3e395f1b45d844431a8
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
<script setup lang="ts">
import type { DraftField } from '../domain/draft';
import type { FieldConflict } from '../domain/conflict';
import type { FillFieldValue } from '../domain/types';
 
import { AimOutlined } from '@ant-design/icons-vue';
import dayjs from 'dayjs';
import { computed, onBeforeUnmount, ref, watch } from 'vue';
 
import { isReferenceTag } from '../domain/draft';
 
const props = withDefaults(
  defineProps<{
    compact?: boolean;
    conflicts?: Record<string, FieldConflict>;
    disabled: boolean;
    errors: Record<string, string>;
    fields: DraftField[];
    filterInput?: string;
    focusedTag?: null | string;
  }>(),
  {
    compact: false,
    conflicts: () => ({}),
    filterInput: '',
    focusedTag: null,
  },
);
const emit = defineEmits<{
  locate: [tag: string];
  resolve: [tag: string, resolution: 'document' | 'draft'];
  update: [tag: string, value: FillFieldValue];
}>();
const filterQuery = ref('');
let filterTimer: ReturnType<typeof setTimeout> | undefined;
 
const filteredFields = computed(() => {
  const query = filterQuery.value.trim().toLocaleLowerCase();
  if (!query) return props.fields;
  return props.fields.filter((field) => (field.alias || '').toLocaleLowerCase().includes(query) || field.tag.toLocaleLowerCase().includes(query));
});
 
watch(
  () => props.filterInput,
  (value) => {
    if (filterTimer) clearTimeout(filterTimer);
    filterTimer = setTimeout(() => {
      filterQuery.value = value;
    }, 300);
  },
  { immediate: true },
);
 
onBeforeUnmount(() => {
  if (filterTimer) clearTimeout(filterTimer);
});
 
function updateDate(tag: string, value: null | string) {
  emit('update', tag, value || '');
}
 
function isDateTime(field: DraftField): boolean {
  return /h/iu.test(field.dateFormat || '');
}
 
function pickerFormat(field: DraftField): string {
  return isDateTime(field) ? 'YYYY-MM-DD HH:mm:ss' : 'YYYY-MM-DD';
}
 
function displayValue(value: FillFieldValue): string {
  if (typeof value === 'boolean') return value ? '已勾选' : '未勾选';
  return value || '(空)';
}
 
function isReadOnlyField(field: DraftField): boolean {
  return field.readOnly === true || isReferenceTag(field.tag);
}
</script>
<template>
  <div class="field-list" :class="{ 'field-list-compact': compact }">
    <a-empty v-if="!fields.length" description="文档中没有已标注字段" />
    <a-empty v-else-if="!filteredFields.length" :image="false" description="没有匹配的字段" />
    <div
      v-for="field in filteredFields"
      :key="field.tag"
      class="field-row"
      :class="{ 'field-row-focused': field.tag === focusedTag }"
      :data-field-tag="field.tag">
      <div class="field-label">
        <label>
          {{ field.alias || field.tag }}<span v-if="field.required" class="required-mark" aria-label="必填"> *</span>
          <a-tag v-if="field.valueSource === 'CROSS_TEMPLATE'" color="blue" data-test="cross-template-value-source">跨模板字段值</a-tag>
        </label>
        <a-tooltip title="在文档中定位" placement="topRight">
          <a-button type="text" size="small" :aria-label="`在文档中定位 ${field.alias || field.tag}`" @click="emit('locate', field.tag)">
            <template #icon><AimOutlined /></template>
          </a-button>
        </a-tooltip>
      </div>
      <a-input
        v-if="field.type === 'text'"
        :disabled="disabled || isReadOnlyField(field)"
        placeholder="请输入"
        :value="field.draftValue"
        @update:value="emit('update', field.tag, $event)" />
      <a-textarea
        v-else-if="field.type === 'multiline'"
        :disabled="disabled || isReadOnlyField(field)"
        placeholder="请输入"
        :value="field.draftValue"
        @update:value="emit('update', field.tag, $event)" />
      <a-select
        v-else-if="field.type === 'select'"
        :disabled="disabled || isReadOnlyField(field)"
        :options="field.options.map((item) => ({ label: item.display, value: item.value }))"
        placeholder="请选择"
        :value="field.draftValue === '' ? undefined : field.draftValue"
        @update:value="emit('update', field.tag, $event)" />
      <a-checkbox
        v-else-if="field.type === 'checkbox'"
        :checked="Boolean(field.draftValue)"
        :disabled="disabled || isReadOnlyField(field)"
        @update:checked="emit('update', field.tag, $event)" />
      <a-date-picker
        v-else
        :disabled="disabled || isReadOnlyField(field)"
        :format="pickerFormat(field)"
        :placeholder="isDateTime(field) ? '请选择日期时间' : '请选择日期'"
        :show-time="isDateTime(field) ? { format: 'HH:mm:ss' } : false"
        :value="field.draftValue ? dayjs(String(field.draftValue)) : null"
        :value-format="pickerFormat(field)"
        @change="updateDate(field.tag, $event)" />
      <div v-if="conflicts[field.tag]" class="field-conflict" role="alert">
        <div class="field-conflict-value">
          <span>文档</span><strong>{{ displayValue(conflicts[field.tag]!.documentValue) }}</strong>
        </div>
        <div class="field-conflict-value">
          <span>草稿</span><strong>{{ displayValue(conflicts[field.tag]!.draftValue) }}</strong>
        </div>
        <a-space>
          <a-button size="small" data-resolution="document" @click="emit('resolve', field.tag, 'document')">使用文档值</a-button>
          <a-button size="small" type="primary" data-resolution="draft" @click="emit('resolve', field.tag, 'draft')">保留草稿</a-button>
        </a-space>
      </div>
      <div v-if="errors[field.tag] || field.formulaStatus === 'ERROR'" class="field-error">
        {{ errors[field.tag] || field.formulaMessage || '公式计算失败' }}
      </div>
    </div>
  </div>
</template>