第一版完成

This commit is contained in:
2025-03-28 15:15:05 +08:00
parent 0079786b25
commit aae2bc10b2
3 changed files with 410 additions and 243 deletions
+65 -65
View File
@@ -50,6 +50,23 @@ export function ExtractionSettings({ onChange }: ExtractionSettingsProps) {
llm: ''
});
// 获取所有可用字段(合并大模型、多模态和正则抽取的字段)
const getAllFields = (): string[] => {
const llm_ocr_fields = fields.llm_ocr || [];
// 从多模态字段中提取基本字段名(去除类型后缀)
const llm_fields = (fields.llm || []).map(field => {
const [fieldName] = field.split('_');
return fieldName;
});
// 获取正则字段名
const regex_fields = regexFields
.map(field => field.fieldName)
.filter(name => name.trim() !== '');
// 合并并去重
return [...new Set([...llm_ocr_fields, ...llm_fields, ...regex_fields])];
};
// 在组件初始化时,如果Context中已有字段数据,则使用Context数据初始化
useEffect(() => {
if (ruleContext && ruleContext.extractionFields.length > 0) {
@@ -61,14 +78,47 @@ export function ExtractionSettings({ onChange }: ExtractionSettingsProps) {
}
}, []);
// 更新所有抽取字段到Context
const updateAllFields = () => {
const allFields = getAllFields();
// 更新全局Context中的字段
if (ruleContext) {
ruleContext.updateExtractionFields(allFields);
}
// 触发自定义事件,通知字段已更新(兼容非Context的实现)
const event = new CustomEvent('extraction-fields-updated', {
detail: {
fields: allFields,
tab: currentTab,
fieldsData: {
llm_ocr: fields.llm_ocr || [],
llm: fields.llm || [],
regex: regexFields.map(f => f.fieldName).filter(name => name.trim() !== '')
}
}
});
document.dispatchEvent(event);
if (onChange) {
onChange({
extractionMethod: currentTab,
fields,
regexFields,
allFields // 添加合并后的所有字段
});
}
};
// 在所有字段集合变化时自动更新
useEffect(() => {
updateAllFields();
}, [fields, regexFields]);
const handleTabChange = (tab: string) => {
setCurrentTab(tab);
// 当切换抽取方法时,更新全局Context中的字段
if (ruleContext && fields[tab]) {
ruleContext.updateExtractionFields(fields[tab]);
}
if (onChange) {
onChange({ extractionMethod: tab });
}
@@ -114,26 +164,8 @@ export function ExtractionSettings({ onChange }: ExtractionSettingsProps) {
setSelectedFieldType('default');
}
// 更新全局Context中的字段
if (ruleContext) {
ruleContext.updateExtractionFields(newFields);
}
if (onChange) {
onChange({
extractionMethod: currentTab,
fields: {
...fields,
[type]: newFields
}
});
}
// 触发自定义事件,通知字段已更新(兼容非Context的实现)
const event = new CustomEvent('extraction-fields-updated', {
detail: { fields: newFields }
});
document.dispatchEvent(event);
// 立即触发字段更新事件,通知评查设置组件
setTimeout(() => updateAllFields(), 0);
}
};
@@ -153,26 +185,8 @@ export function ExtractionSettings({ onChange }: ExtractionSettingsProps) {
[type]: newFields
});
// 更新全局Context中的字段
if (ruleContext) {
ruleContext.updateExtractionFields(newFields);
}
if (onChange) {
onChange({
extractionMethod: currentTab,
fields: {
...fields,
[type]: newFields
}
});
}
// 触发自定义事件,通知字段已更新(兼容非Context的实现)
const event = new CustomEvent('extraction-fields-updated', {
detail: { fields: newFields }
});
document.dispatchEvent(event);
// 立即触发字段更新事件,通知评查设置组件
setTimeout(() => updateAllFields(), 0);
};
// 添加正则表达式字段行
@@ -180,14 +194,15 @@ export function ExtractionSettings({ onChange }: ExtractionSettingsProps) {
const newId = `${regexFields.length + 1}`;
setRegexFields([...regexFields, { id: newId, fieldName: '', regex: '' }]);
// 如果是新增了regex字段,也要更新字段列表通知评查设置组件
setTimeout(() => updateAllFields(), 0);
if (onChange) {
onChange({
extractionMethod: currentTab,
regexFields: [...regexFields, { id: newId, fieldName: '', regex: '' }]
});
}
// 添加字段时不触发事件,因为此时字段名称尚未填写
};
// 删除正则表达式字段行
@@ -216,13 +231,9 @@ export function ExtractionSettings({ onChange }: ExtractionSettingsProps) {
setRegexFields(newRegexFields);
// 如果是字段名更新且当前抽取方法是正则抽取,则更新Context
if (key === 'fieldName' && currentTab === 'ocr_regex' && ruleContext) {
const fieldNames = newRegexFields
.map(field => field.fieldName)
.filter(name => name.trim() !== '');
ruleContext.updateExtractionFields(fieldNames);
// 如果更新的是字段名,则触发字段更新事件
if (key === 'fieldName') {
setTimeout(() => updateAllFields(), 0);
}
if (onChange) {
@@ -231,17 +242,6 @@ export function ExtractionSettings({ onChange }: ExtractionSettingsProps) {
regexFields: newRegexFields
});
}
// 如果更新的是字段名,触发自定义事件通知字段已更新
if (key === 'fieldName' && value.trim()) {
const allFieldNames = [...fields[currentTab], ...newRegexFields.map(f => f.fieldName).filter(f => f)];
// 触发自定义事件,通知字段已更新(兼容非Context的实现)
const event = new CustomEvent('extraction-fields-updated', {
detail: { fields: allFieldNames }
});
document.dispatchEvent(event);
}
};
// 应用正则模板