You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
119 lines
3.2 KiB
119 lines
3.2 KiB
import 'package:flutter/material.dart'; |
|
import 'package:win_text_editor/app/models/tab_model.dart'; |
|
import 'package:win_text_editor/app/providers/logger.dart'; |
|
import 'package:win_text_editor/app/widgets/template_parser_tab.dart'; |
|
|
|
class TabProvider with ChangeNotifier { |
|
final List<ContentTab> _tabs = []; |
|
String? _activeTabId; |
|
|
|
List<ContentTab> get tabs => _tabs; |
|
String? get activeTabId => _activeTabId; |
|
|
|
final Map<String, TemplateParserTabState> _tabControllers = {}; |
|
|
|
void registerTextTabController(String tabId, TemplateParserTabState controller) { |
|
_tabControllers[tabId] = controller; |
|
} |
|
|
|
void unregisterTextTabController(String tabId) { |
|
_tabControllers.remove(tabId); |
|
} |
|
|
|
ContentTab? get activeTab { |
|
if (_activeTabId == null) return null; |
|
try { |
|
return _tabs.firstWhere((tab) => tab.id == _activeTabId); |
|
} catch (e) { |
|
Logger().error("找不到活动选项卡: $_activeTabId", source: 'EditorProvider'); |
|
return null; |
|
} |
|
} |
|
|
|
Future<void> addTab({ |
|
String title = '未命名', |
|
String? type, |
|
IconData? icon, |
|
String content = '', |
|
}) async { |
|
final newTab = ContentTab( |
|
id: DateTime.now().millisecondsSinceEpoch.toString(), |
|
title: title, |
|
type: type, |
|
icon: icon, |
|
content: content, |
|
); |
|
|
|
_tabs.add(newTab); |
|
_activeTabId = newTab.id; |
|
notifyListeners(); |
|
} |
|
|
|
ContentTab? getTabById(String tabId) { |
|
try { |
|
return _tabs.firstWhere((tab) => tab.id == tabId); |
|
} catch (e) { |
|
Logger().error("找不到选项卡: ${tabId}", source: 'EditorProvider'); |
|
return null; |
|
} |
|
} |
|
|
|
void closeTab(String tabId) { |
|
Logger().info('关闭选项卡: $tabId'); |
|
_tabs.removeWhere((tab) => tab.id == tabId); |
|
if (_activeTabId == tabId) { |
|
_activeTabId = _tabs.isNotEmpty ? _tabs.last.id : null; |
|
} |
|
notifyListeners(); |
|
} |
|
|
|
void setActiveTab(String tabId) { |
|
Logger().info('设置活动选项卡: $tabId'); |
|
_activeTabId = tabId; |
|
notifyListeners(); |
|
} |
|
|
|
void updateContent(String tabId, String content, String? name) { |
|
try { |
|
final tab = _tabs.firstWhere((t) => t.id == tabId); |
|
tab.content = content; |
|
|
|
if (name != null) { |
|
tab.fileName = name; |
|
} |
|
// Logger().debug("内容更新成功,文件:${tab.fileName}, ${tab.content.length}"); |
|
notifyListeners(); |
|
} catch (e) { |
|
Logger().error("更新内容失败: ${e.toString()}", source: 'EditorProvider'); |
|
} |
|
} |
|
|
|
Future<void> requestLoadFile(BuildContext context, String filePath) async { |
|
if (_activeTabId == null) { |
|
Logger().warning("没有活动选项卡,无法加载文件"); |
|
return; |
|
} |
|
|
|
final parserTabState = _tabControllers[_activeTabId]; |
|
if (parserTabState == null) { |
|
Logger().warning("找不到 TextTab 状态"); |
|
return; |
|
} |
|
|
|
if (!parserTabState.mounted) { |
|
Logger().warning("TextTab 状态组件未挂载"); |
|
return; |
|
} |
|
|
|
try { |
|
await parserTabState.loadFile(context, filePath); |
|
} catch (e) { |
|
Logger().error("加载文件失败: ${e.toString()}"); |
|
if (context.mounted) { |
|
ScaffoldMessenger.of( |
|
context, |
|
).showSnackBar(SnackBar(content: Text('加载文件失败: ${e.toString()}'))); |
|
} |
|
} |
|
} |
|
}
|
|
|