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