From d6c20903c2a44bafc1b90d2c3c335ee285e94919 Mon Sep 17 00:00:00 2001 From: hejl Date: Sun, 1 Jun 2025 22:31:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BA=86checkbox=EF=BC=8C?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E8=B0=83=E7=94=A8=E5=AE=8C=E6=88=90=E9=83=A8?= =?UTF-8?q?=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controllers/call_function_controller.dart | 32 ++++--- .../services/call_function_service.dart | 90 ++++++++++--------- .../widgets/call_function_left_side.dart | 6 +- .../widgets/call_function_right_side.dart | 51 ----------- .../widgets/search_settings.dart | 3 +- .../services/memory_table_service.dart | 13 +-- .../widgets/memory_table_right_side.dart | 25 ++---- .../controllers/uft_component_controller.dart | 11 +-- .../services/uft_component_service.dart | 76 ++++++++++++---- .../widgets/uft_component_right_side.dart | 25 ++---- .../lib/shared/components/my_checkbox.dart | 13 ++- .../lib/shared/data/std_fields_cache.dart | 71 ++++++++++++++- .../uft_std_fields/field_data_service.dart | 67 -------------- .../uft_std_fields/fields_data_grid.dart | 8 -- 14 files changed, 233 insertions(+), 258 deletions(-) delete mode 100644 win_text_editor/lib/shared/uft_std_fields/field_data_service.dart diff --git a/win_text_editor/lib/modules/call_function/controllers/call_function_controller.dart b/win_text_editor/lib/modules/call_function/controllers/call_function_controller.dart index cad2637..97a5772 100644 --- a/win_text_editor/lib/modules/call_function/controllers/call_function_controller.dart +++ b/win_text_editor/lib/modules/call_function/controllers/call_function_controller.dart @@ -3,7 +3,6 @@ import 'package:win_text_editor/framework/controllers/logger.dart'; import 'package:win_text_editor/framework/services/macro_template_service.dart'; import 'package:win_text_editor/modules/call_function/models/call_function.dart'; import 'package:win_text_editor/modules/call_function/services/call_function_service.dart'; -import 'package:win_text_editor/shared/models/std_filed.dart'; import 'package:win_text_editor/shared/uft_std_fields/field_data_source.dart'; import 'package:win_text_editor/shared/base/base_content_controller.dart'; @@ -23,28 +22,23 @@ class CallFunctionController extends BaseContentController { CallFunctionController() : _service = CallFunctionService(Logger()) { // 初始化空数据 - final initialFields = [Field('1', '', '', '', false)]; inputSource = FieldsDataSource( - initialFields, + [], onSelectionChanged: (index, isSelected) { - updateFieldSelection(index, isSelected); + updateInputSelection(index, isSelected); }, ); outputSource = FieldsDataSource( - initialFields, + [], onSelectionChanged: (index, isSelected) { - updateFieldSelection(index, isSelected); + updateOutputSelection(index, isSelected); }, ); // 初始化CallFunction - _modle = CallFunction( - functionName: '', - inputParameters: initialFields, - outputParameters: initialFields, - ); + _modle = CallFunction(functionName: '', inputParameters: [], outputParameters: []); } String? get errorMessage => _errorMessage; @@ -81,7 +75,7 @@ class CallFunctionController extends BaseContentController { _modle = CallFunction( functionName: tableName, inputParameters: functionData.inputFields, - outputParameters: functionData.inputFields, + outputParameters: functionData.outputFields, ); // Clear any previous error @@ -97,7 +91,7 @@ class CallFunctionController extends BaseContentController { } // 新增:更新字段选择状态 - void updateFieldSelection(int index, bool isSelected) { + void updateInputSelection(int index, bool isSelected) { final fields = (inputSource as FieldsDataSource).data; if (index >= 0 && index < fields.length) { fields[index].isSelected = isSelected; @@ -109,6 +103,18 @@ class CallFunctionController extends BaseContentController { } } + void updateOutputSelection(int index, bool isSelected) { + final fields = (outputSource as FieldsDataSource).data; + if (index >= 0 && index < fields.length) { + fields[index].isSelected = isSelected; + outputSource.notifyListeners(); + + // 同步更新CallFunction + _modle.outputParameters[index].isSelected = isSelected; + notifyListeners(); + } + } + @override void onOpenFolder(String folderPath) { // 不支持打开文件夹 diff --git a/win_text_editor/lib/modules/call_function/services/call_function_service.dart b/win_text_editor/lib/modules/call_function/services/call_function_service.dart index dd513cd..40742d6 100644 --- a/win_text_editor/lib/modules/call_function/services/call_function_service.dart +++ b/win_text_editor/lib/modules/call_function/services/call_function_service.dart @@ -1,9 +1,10 @@ // memory_table_service.dart import 'dart:io'; +import 'package:win_text_editor/modules/uft_component/models/uft_component.dart'; +import 'package:win_text_editor/modules/uft_component/services/uft_component_service.dart'; import 'package:win_text_editor/shared/data/std_fields_cache.dart'; import 'package:win_text_editor/shared/models/std_filed.dart'; -import 'package:win_text_editor/shared/uft_std_fields/field_data_service.dart'; import 'package:xml/xml.dart' as xml; import 'package:win_text_editor/framework/controllers/logger.dart'; @@ -31,13 +32,7 @@ class CallFunctionService { } // 2. 查找 metadata 目录和 stdfield.stfield 文件 - if (await StdFieldsCache.getLength() == 0) { - _logger.info("加载标准字段缓存"); - final metadataFile = await FieldDataService.findMetadataFile(filePath); - if (metadataFile != null) { - await FieldDataService.processStdFieldFile(metadataFile); - } - } + await StdFieldsCache.loadForFile(filePath); // 3. Read and parse structure file content final file = File(filePath); @@ -54,49 +49,22 @@ class CallFunctionService { final chineseName = rootNode.getAttribute('chineseName') ?? ''; final objectId = rootNode.getAttribute('objectId') ?? ''; + List componentList = []; + // 5. Process inputParameters (fields) final inputParameters = document.findAllElements('inputParameters'); - final inputFields = []; - int index = 1; - - for (final parameter in inputParameters) { - final id = parameter.getAttribute('id') ?? ''; - // 尝试从缓存获取标准字段信息 - final stdField = StdFieldsCache.getData(id); - inputFields.add( - Field( - (index++).toString(), // 序号 - id, // 名称 - stdField?.chineseName ?? '', // 中文名 - stdField?.dateType ?? '', // 类型 - ), - ); - } + final inputFields = await parserFields(filePath, inputParameters, componentList); // 6. Process outputParameters final outputParameters = document.findAllElements('outputParameters'); - final outputFields = []; - index = 1; - - for (final parameter in outputParameters) { - final id = parameter.getAttribute('id') ?? ''; - // 尝试从缓存获取标准字段信息 - final stdField = StdFieldsCache.getData(id); - outputFields.add( - Field( - (index++).toString(), // 序号 - id, // 名称 - stdField?.chineseName ?? '', // 中文名 - stdField?.dateType ?? '', // 类型 - ), - ); - } + final outputFields = await parserFields(filePath, outputParameters, componentList); return FunctionData( chineseName: chineseName, objectId: objectId, - inputFields: inputFields.isNotEmpty ? inputFields : FieldDataService.getDefaultFields(), - outputFields: outputFields.isNotEmpty ? outputFields : FieldDataService.getDefaultFields(), + inputFields: inputFields, + outputFields: outputFields, + componentList: componentList, ); } on xml.XmlParserException catch (e) { _logger.error("XML解析错误: ${e.message}"); @@ -106,6 +74,42 @@ class CallFunctionService { rethrow; } } + + Future> parserFields( + String filePath, + Iterable parameters, + List componentList, + ) async { + final fields = []; + int index = 0; + + for (final parameter in parameters) { + index++; + final id = parameter.getAttribute('id') ?? ''; + final paramType = parameter.getAttribute('paramType') ?? 'FIELD'; + + if (paramType == 'COMPONENT') { + final component = await UftComponentService.getUftComponent(filePath, id); + // _logger.debug("value.id:${component?.name}, chineseName:${component?.chineseName}"); + if (component != null) componentList.add(component); + fields.add( + Field( + index.toString(), + id, + component?.chineseName ?? '', // 使用组件的中文名 + 'COMPONENT', + ), + ); + } else { + final stdField = StdFieldsCache.getData(id); + fields.add( + Field(index.toString(), id, stdField?.chineseName ?? '', stdField?.dateType ?? ''), + ); + } + } + + return fields; + } } class FunctionData { @@ -113,11 +117,13 @@ class FunctionData { final String objectId; final List inputFields; final List outputFields; + List? componentList; FunctionData({ required this.chineseName, required this.objectId, required this.inputFields, required this.outputFields, + this.componentList, }); } diff --git a/win_text_editor/lib/modules/call_function/widgets/call_function_left_side.dart b/win_text_editor/lib/modules/call_function/widgets/call_function_left_side.dart index 91a2b03..73185e9 100644 --- a/win_text_editor/lib/modules/call_function/widgets/call_function_left_side.dart +++ b/win_text_editor/lib/modules/call_function/widgets/call_function_left_side.dart @@ -55,7 +55,7 @@ class CallFunctionLeftSide extends StatelessWidget { child: FieldsDataGrid( fieldsSource: controller.inputSource as FieldsDataSource, onSelectionChanged: (index, isSelected) { - controller.updateFieldSelection(index, isSelected); + controller.updateInputSelection(index, isSelected); }, ), ), @@ -66,9 +66,9 @@ class CallFunctionLeftSide extends StatelessWidget { Expanded( flex: 4, child: FieldsDataGrid( - fieldsSource: controller.inputSource as FieldsDataSource, + fieldsSource: controller.outputSource as FieldsDataSource, onSelectionChanged: (index, isSelected) { - controller.updateFieldSelection(index, isSelected); + controller.updateOutputSelection(index, isSelected); }, ), ), diff --git a/win_text_editor/lib/modules/call_function/widgets/call_function_right_side.dart b/win_text_editor/lib/modules/call_function/widgets/call_function_right_side.dart index 6d74d06..7d7b450 100644 --- a/win_text_editor/lib/modules/call_function/widgets/call_function_right_side.dart +++ b/win_text_editor/lib/modules/call_function/widgets/call_function_right_side.dart @@ -32,22 +32,10 @@ class _CallFunctionRightSideState extends State { widget.codeController.text = widget.controller.genCodeString(_selectedOperations)!; } - void _toggleOperation(String operation, bool? value) { - setState(() { - if (value == true) { - _selectedOperations.add(operation); - } else { - _selectedOperations.remove(operation); - } - _updateDisplay(); - }); - } - @override Widget build(BuildContext context) { return Column( children: [ - _buildCheckboxSection(), Padding( padding: const EdgeInsets.only(left: 8.0, right: 8.0), child: Row( @@ -74,45 +62,6 @@ class _CallFunctionRightSideState extends State { ); } - Widget _buildCheckboxSection() { - final operations = ['获取记录', '获取记录数', '插入记录', '修改记录', '删除记录', '遍历记录']; - - return SizedBox( - width: double.infinity, - child: Card( - child: Padding( - padding: const EdgeInsets.all(8.0), - child: Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Wrap( - spacing: 16, - runSpacing: 8, - children: operations.map((op) => _buildCheckbox(op)).toList(), - ), - ], - ), - ), - ), - ); - } - - Widget _buildCheckbox(String label) { - return Row( - mainAxisSize: MainAxisSize.min, - children: [ - Transform.scale( - scale: 0.75, // 调整这个值来改变大小 - child: Checkbox( - value: _selectedOperations.contains(label), - onChanged: (bool? value) => _toggleOperation(label, value), - ), - ), - Text(label), - ], - ); - } - Widget _buildCodeEditor() { return Card( child: Padding( diff --git a/win_text_editor/lib/modules/content_search/widgets/search_settings.dart b/win_text_editor/lib/modules/content_search/widgets/search_settings.dart index c31e1e1..fc5a84c 100644 --- a/win_text_editor/lib/modules/content_search/widgets/search_settings.dart +++ b/win_text_editor/lib/modules/content_search/widgets/search_settings.dart @@ -232,7 +232,7 @@ class _SearchSettingsState extends State { ), ], ), - // const SizedBox(height: 8), + const SizedBox(height: 8), // 仅在自定义规则选中时显示的JS函数说明 Visibility( visible: controller.customRule, @@ -286,6 +286,7 @@ class _SearchSettingsState extends State { ), ], ), + const SizedBox(height: 8), Text( ' 搜索计时:[${_formatDuration(_elapsedTime)}]', style: const TextStyle(fontSize: 12), diff --git a/win_text_editor/lib/modules/memory_table/services/memory_table_service.dart b/win_text_editor/lib/modules/memory_table/services/memory_table_service.dart index 5ef3979..0a1e91e 100644 --- a/win_text_editor/lib/modules/memory_table/services/memory_table_service.dart +++ b/win_text_editor/lib/modules/memory_table/services/memory_table_service.dart @@ -4,7 +4,6 @@ import 'dart:io'; import 'package:win_text_editor/modules/memory_table/models/memory_table.dart'; import 'package:win_text_editor/shared/data/std_fields_cache.dart'; import 'package:win_text_editor/shared/models/std_filed.dart'; -import 'package:win_text_editor/shared/uft_std_fields/field_data_service.dart'; import 'package:xml/xml.dart' as xml; import 'package:path/path.dart' as path; import 'package:win_text_editor/framework/controllers/logger.dart'; @@ -22,13 +21,7 @@ class MemoryTableService { } // 2. 查找 metadata 目录和 stdfield.stfield 文件 - if (await StdFieldsCache.getLength() == 0) { - _logger.info("加载标准字段缓存"); - final metadataFile = await FieldDataService.findMetadataFile(filePath); - if (metadataFile != null) { - await FieldDataService.processStdFieldFile(metadataFile); - } - } + await StdFieldsCache.loadForFile(filePath); // 3. Read and parse structure file content final file = File(filePath); @@ -98,8 +91,8 @@ class MemoryTableService { tableName: fileNameWithoutExt, chineseName: chineseName, objectId: objectId, - fields: fields.isNotEmpty ? fields : FieldDataService.getDefaultFields(), - indexes: indexList.isNotEmpty ? indexList : FieldDataService.getDefaultIndexes(), + fields: fields.isNotEmpty ? fields : [], + indexes: indexList.isNotEmpty ? indexList : [], ); } on xml.XmlParserException catch (e) { _logger.error("XML解析错误: ${e.message}"); diff --git a/win_text_editor/lib/modules/memory_table/widgets/memory_table_right_side.dart b/win_text_editor/lib/modules/memory_table/widgets/memory_table_right_side.dart index c47622a..8a91318 100644 --- a/win_text_editor/lib/modules/memory_table/widgets/memory_table_right_side.dart +++ b/win_text_editor/lib/modules/memory_table/widgets/memory_table_right_side.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:win_text_editor/modules/memory_table/controllers/memory_table_controller.dart'; +import 'package:win_text_editor/shared/components/my_checkbox.dart'; class MemoryTableRightSide extends StatefulWidget { final MemoryTableController controller; @@ -81,12 +82,12 @@ class _MemoryTableRightSideState extends State { width: double.infinity, child: Card( child: Padding( - padding: const EdgeInsets.all(8.0), + padding: const EdgeInsets.only(left: 8, top: 4, bottom: 12), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Wrap( - spacing: 16, + spacing: 8, runSpacing: 8, children: operations.map((op) => _buildCheckbox(op)).toList(), ), @@ -97,21 +98,11 @@ class _MemoryTableRightSideState extends State { ); } - Widget _buildCheckbox(String label) { - return Row( - mainAxisSize: MainAxisSize.min, - children: [ - Transform.scale( - scale: 0.75, - child: Checkbox( - value: _selectedOperations.contains(label), - onChanged: (bool? value) => _toggleOperation(label, value), - ), - ), - Text(label), - ], - ); - } + Widget _buildCheckbox(String label) => MyCheckbox( + title: label, + value: _selectedOperations.contains(label), + onChanged: (bool? value) => _toggleOperation(label, value), + ); Widget _buildCodeEditor() { return Card( diff --git a/win_text_editor/lib/modules/uft_component/controllers/uft_component_controller.dart b/win_text_editor/lib/modules/uft_component/controllers/uft_component_controller.dart index 8f0bbc7..094b5f2 100644 --- a/win_text_editor/lib/modules/uft_component/controllers/uft_component_controller.dart +++ b/win_text_editor/lib/modules/uft_component/controllers/uft_component_controller.dart @@ -15,19 +15,16 @@ class UftComponentController extends BaseContentController { late DataGridSource fieldsSource; late DataGridSource componentsSource; - final UftComponentService _service; final MacroTemplateService templateService = MacroTemplateService(); // 新增:维护当前选中组件对象 late UftComponent _currentUftComponent; - UftComponentController() : _service = UftComponentService(Logger()) { + UftComponentController() { // 初始化空数据 - final initialFields = [Field('1', '', '', '', false), Field('2', '', '', '', false)]; + final List initialFields = []; - final initalComponents = [ - UftComponent(id: 1, name: '', chineseName: '', fields: initialFields), - ]; + final List initalComponents = []; componentsSource = ComponentSource( initalComponents, @@ -67,7 +64,7 @@ class UftComponentController extends BaseContentController { Future onOpenFile(String filePath) async { Logger().info("Opening file: $filePath"); try { - final components = await _service.parseComponentFile(filePath); + final components = await UftComponentService.parseComponentFile(filePath); // Update data sources (componentsSource as ComponentSource).updateData(components); diff --git a/win_text_editor/lib/modules/uft_component/services/uft_component_service.dart b/win_text_editor/lib/modules/uft_component/services/uft_component_service.dart index 35889e0..962b778 100644 --- a/win_text_editor/lib/modules/uft_component/services/uft_component_service.dart +++ b/win_text_editor/lib/modules/uft_component/services/uft_component_service.dart @@ -2,40 +2,50 @@ import 'dart:io'; +import 'package:collection/collection.dart'; import 'package:win_text_editor/modules/memory_table/models/memory_table.dart'; import 'package:win_text_editor/modules/uft_component/models/uft_component.dart'; import 'package:win_text_editor/shared/data/std_fields_cache.dart'; import 'package:win_text_editor/shared/models/std_filed.dart'; import 'package:win_text_editor/framework/controllers/logger.dart'; -import 'package:win_text_editor/shared/uft_std_fields/field_data_service.dart'; import 'package:xml/xml.dart' as xml; class UftComponentService { - final Logger _logger; + static final Logger _logger = Logger(); + static List _components = []; - UftComponentService(this._logger); + static Future getUftComponent(String filePath, String componentName) async { + if (_components.isNotEmpty) { + return _components.firstWhereOrNull((c) => c.name == componentName); + } + return parseComponentFile( + filePath, + ).then((l) => l.firstWhereOrNull((c) => c.name == componentName)); + } - Future> parseComponentFile(String filePath) async { + static Future> parseComponentFile(String filePath) async { try { + if (_components.isNotEmpty) { + _components.firstWhereOrNull((c) => c.isSelected)?.isSelected = false; + return _components; + } // 1. Check file extension - if (!filePath.toLowerCase().endsWith('component.xml')) { - throw const FormatException("文件名必须是component.xml"); + File? file = + filePath.toLowerCase().endsWith('component.xml') + ? File(filePath) + : await findMetadataFile(filePath); + + if (file == null) { + throw const FormatException("没有找到标准组件文件:component.xml"); } // 2. 查找 metadata 目录和 stdfield.stfield 文件 - if (await StdFieldsCache.getLength() == 0) { - _logger.info("加载标准字段缓存"); - final metadataFile = await FieldDataService.findMetadataFile(filePath); - if (metadataFile != null) { - await FieldDataService.processStdFieldFile(metadataFile); - } - } + await StdFieldsCache.loadForFile(filePath); // 3. Read and parse structure file content - final file = File(filePath); final content = await file.readAsString(); - + _logger.info('加载标准组件'); final document = xml.XmlDocument.parse(content); final componentNodes = document.findAllElements('items'); @@ -44,8 +54,8 @@ class UftComponentService { } // 4. 解析组件列表 - final components = []; - int id = 1; + _components = []; + int id = 0; for (var node in componentNodes) { if (node.findElements("items").isEmpty) continue; id++; @@ -99,7 +109,7 @@ class UftComponentService { ); } - components.add( + _components.add( UftComponent( id: id, name: name, @@ -110,7 +120,7 @@ class UftComponentService { ); } - return components; + return _components; } on xml.XmlParserException catch (e) { _logger.error("XML解析错误: ${e.message}"); rethrow; @@ -119,4 +129,32 @@ class UftComponentService { rethrow; } } + + static Future findMetadataFile(String filePath) async { + Directory currentDir = File(filePath).parent; + const targetDirName = 'metadata'; + const targetFileName = 'component.xml'; + + // 向上查找 metadata 目录 + while (true) { + final metadataDir = Directory('${currentDir.path}/$targetDirName'); + if (await metadataDir.exists()) { + final stdFieldFile = File('${metadataDir.path}/$targetFileName'); + if (await stdFieldFile.exists()) { + return stdFieldFile; + } else { + Logger().error("没找到标准组件文件 $targetFileName"); + return null; + } + } + + // 到达根目录时停止 + if (currentDir.path == currentDir.parent.path) { + Logger().error("没有找到元数据目录 $targetDirName"); + return null; + } + + currentDir = currentDir.parent; + } + } } diff --git a/win_text_editor/lib/modules/uft_component/widgets/uft_component_right_side.dart b/win_text_editor/lib/modules/uft_component/widgets/uft_component_right_side.dart index 8dd5806..352586b 100644 --- a/win_text_editor/lib/modules/uft_component/widgets/uft_component_right_side.dart +++ b/win_text_editor/lib/modules/uft_component/widgets/uft_component_right_side.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:win_text_editor/modules/uft_component/controllers/uft_component_controller.dart'; +import 'package:win_text_editor/shared/components/my_checkbox.dart'; class UftComponentRightSide extends StatefulWidget { final UftComponentController controller; @@ -81,12 +82,12 @@ class _UftComponentRightSideState extends State { width: double.infinity, child: Card( child: Padding( - padding: const EdgeInsets.all(8.0), + padding: const EdgeInsets.only(left: 8, top: 4, bottom: 12), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Wrap( - spacing: 16, + spacing: 8, runSpacing: 8, children: operations.map((op) => _buildCheckbox(op)).toList(), ), @@ -97,21 +98,11 @@ class _UftComponentRightSideState extends State { ); } - Widget _buildCheckbox(String label) { - return Row( - mainAxisSize: MainAxisSize.min, - children: [ - Transform.scale( - scale: 0.75, - child: Checkbox( - value: _selectedOperations.contains(label), - onChanged: (bool? value) => _toggleOperation(label, value), - ), - ), - Text(label), - ], - ); - } + Widget _buildCheckbox(String label) => MyCheckbox( + title: label, + value: _selectedOperations.contains(label), + onChanged: (bool? value) => _toggleOperation(label, value), + ); Widget _buildCodeEditor() { return Card( diff --git a/win_text_editor/lib/shared/components/my_checkbox.dart b/win_text_editor/lib/shared/components/my_checkbox.dart index 2122147..5c637b3 100644 --- a/win_text_editor/lib/shared/components/my_checkbox.dart +++ b/win_text_editor/lib/shared/components/my_checkbox.dart @@ -1,21 +1,30 @@ import 'package:flutter/material.dart'; +@immutable class MyCheckbox extends StatelessWidget { final String title; final bool value; final ValueChanged? onChanged; final bool enabled = true; + final double? maxWidth; - const MyCheckbox({super.key, required this.title, required this.value, this.onChanged}); + const MyCheckbox({ + super.key, + required this.title, + required this.value, + this.onChanged, + this.maxWidth, + }); @override Widget build(BuildContext context) { return ConstrainedBox( - constraints: const BoxConstraints(minHeight: 32), + constraints: BoxConstraints(maxHeight: 28, maxWidth: maxWidth ?? title.length * 12.0 + 38), child: ListTile( dense: true, title: Text(title, style: const TextStyle(fontSize: 12)), contentPadding: EdgeInsets.zero, + horizontalTitleGap: 4, leading: Transform.scale( scale: 0.75, child: Checkbox( diff --git a/win_text_editor/lib/shared/data/std_fields_cache.dart b/win_text_editor/lib/shared/data/std_fields_cache.dart index 1060a6b..a826164 100644 --- a/win_text_editor/lib/shared/data/std_fields_cache.dart +++ b/win_text_editor/lib/shared/data/std_fields_cache.dart @@ -1,8 +1,13 @@ +import 'dart:io'; + import 'package:hive_flutter/hive_flutter.dart'; +import 'package:win_text_editor/framework/controllers/logger.dart'; import 'package:win_text_editor/shared/models/std_filed.dart'; +import 'package:xml/xml.dart' as xml; class StdFieldsCache { static late final Box _box; // 指定泛型类型 + static bool _loaded = false; // 初始化 static Future init() async { @@ -11,18 +16,82 @@ class StdFieldsCache { _box = await Hive.openBox('uft_stdFieldsCache'); // 指定泛型类型 } + static Future loadForFile(String currFilePath) async { + if (_loaded == false) { + Logger().info("加载标准字段缓存"); + final metadataFile = await findMetadataFile(currFilePath); + if (metadataFile != null) { + await processStdFieldFile(metadataFile); + } + _loaded = true; + } + } + static Future getLength() async { return _box.length; } // 通用数据存取 - static StdField? getData(String key) => _box.get(key); + static getData(String key) { + return _box.get(key); + } + static Future setData(String key, StdField value) => _box.put(key, value); // 清空缓存 static Future clear() async { await _box.clear(); } + + static Future findMetadataFile(String filePath) async { + Directory currentDir = File(filePath).parent; + const targetDirName = 'metadata'; + const targetFileName = 'stdfield.stdfield'; + + // 向上查找 metadata 目录 + while (true) { + final metadataDir = Directory('${currentDir.path}/$targetDirName'); + if (await metadataDir.exists()) { + final stdFieldFile = File('${metadataDir.path}/$targetFileName'); + if (await stdFieldFile.exists()) { + return stdFieldFile; + } else { + Logger().error("没找到标准字段文件 $targetFileName"); + return null; + } + } + + // 到达根目录时停止 + if (currentDir.path == currentDir.parent.path) { + Logger().error("没有找到元数据目录 $targetDirName"); + return null; + } + + currentDir = currentDir.parent; + } + } + + // 处理标准字段文件并缓存 + static Future processStdFieldFile(File stdFieldFile) async { + try { + final content = await stdFieldFile.readAsString(); + final document = xml.XmlDocument.parse(content); + final items = document.findAllElements('items'); + + for (final item in items) { + final name = item.getAttribute('name') ?? ''; + final chineseName = item.getAttribute('chineseName') ?? ''; + final dataType = item.getAttribute('dataType') ?? ''; + + if (name.isNotEmpty) { + final stdField = StdField(name: name, chineseName: chineseName, dateType: dataType); + await StdFieldsCache.setData(name, stdField); + } + } + } catch (e) { + Logger().error("处理标准字段文件时出错: $e"); + } + } } // 确保typeId在整个应用中唯一 diff --git a/win_text_editor/lib/shared/uft_std_fields/field_data_service.dart b/win_text_editor/lib/shared/uft_std_fields/field_data_service.dart deleted file mode 100644 index f6d246f..0000000 --- a/win_text_editor/lib/shared/uft_std_fields/field_data_service.dart +++ /dev/null @@ -1,67 +0,0 @@ -import 'dart:io'; - -import 'package:win_text_editor/framework/controllers/logger.dart'; -import 'package:win_text_editor/modules/memory_table/models/memory_table.dart'; -import 'package:win_text_editor/shared/data/std_fields_cache.dart'; -import 'package:win_text_editor/shared/models/std_filed.dart'; -import 'package:xml/xml.dart' as xml; - -class FieldDataService { - static Future findMetadataFile(String filePath) async { - Directory currentDir = File(filePath).parent; - const targetDirName = 'metadata'; - const targetFileName = 'stdfield.stdfield'; - - // 向上查找 metadata 目录 - while (true) { - final metadataDir = Directory('${currentDir.path}/$targetDirName'); - if (await metadataDir.exists()) { - final stdFieldFile = File('${metadataDir.path}/$targetFileName'); - if (await stdFieldFile.exists()) { - return stdFieldFile; - } else { - Logger().error("没找到标准字段文件 $targetFileName"); - return null; - } - } - - // 到达根目录时停止 - if (currentDir.path == currentDir.parent.path) { - Logger().error("没有找到元数据目录 $targetDirName"); - return null; - } - - currentDir = currentDir.parent; - } - } - - // 处理标准字段文件并缓存 - static Future processStdFieldFile(File stdFieldFile) async { - try { - final content = await stdFieldFile.readAsString(); - final document = xml.XmlDocument.parse(content); - final items = document.findAllElements('items'); - - for (final item in items) { - final name = item.getAttribute('name') ?? ''; - final chineseName = item.getAttribute('chineseName') ?? ''; - final dataType = item.getAttribute('dataType') ?? ''; - - if (name.isNotEmpty) { - final stdField = StdField(name: name, chineseName: chineseName, dateType: dataType); - await StdFieldsCache.setData(name, stdField); - } - } - } catch (e) { - Logger().error("处理标准字段文件时出错: $e"); - } - } - - static List getDefaultFields() { - return [Field('1', '', '', ''), Field('2', '', '', ''), Field('3', '', '', '')]; - } - - static List getDefaultIndexes() { - return [Index('', false, '', ''), Index('', false, '', '')]; - } -} diff --git a/win_text_editor/lib/shared/uft_std_fields/fields_data_grid.dart b/win_text_editor/lib/shared/uft_std_fields/fields_data_grid.dart index c629dc3..2c4943e 100644 --- a/win_text_editor/lib/shared/uft_std_fields/fields_data_grid.dart +++ b/win_text_editor/lib/shared/uft_std_fields/fields_data_grid.dart @@ -11,14 +11,6 @@ class FieldsDataGrid extends StatelessWidget { const FieldsDataGrid({super.key, required this.fieldsSource, this.onSelectionChanged}); - Container _buildGridHeader(String text) { - return Container( - alignment: Alignment.center, - color: Colors.grey[200], - child: Text(text, style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 16)), - ); - } - Widget _buildCheckboxHeader( BuildContext context, SelectableDataSource dataSource,