Browse Source

把路由配置合并到一起了

master
hejl 2 months ago
parent
commit
4b1298b92d
  1. 13
      win_text_editor/lib/framework/controllers/tab_items_controller.dart
  2. 13
      win_text_editor/lib/framework/widgets/tab_view.dart
  3. 32
      win_text_editor/lib/menus/menu_actions.dart
  4. 49
      win_text_editor/lib/modules/module_router.dart

13
win_text_editor/lib/framework/controllers/tab_items_controller.dart

@ -1,7 +1,6 @@ @@ -1,7 +1,6 @@
import 'package:flutter/material.dart';
import 'package:win_text_editor/framework/models/tab_model.dart';
import 'package:win_text_editor/modules/content_search/controllers/content_search_controller.dart';
import 'package:win_text_editor/modules/template_parser/controllers/template_parser_controller.dart';
import 'package:win_text_editor/modules/module_router.dart';
import 'package:win_text_editor/shared/base/base_content_controller.dart';
import 'package:win_text_editor/framework/controllers/logger.dart';
@ -54,14 +53,7 @@ class TabItemsController with ChangeNotifier { @@ -54,14 +53,7 @@ class TabItemsController with ChangeNotifier {
}
BaseContentController? createContentController(AppTab tab) {
switch (tab.type) {
case 'content_search':
return ContentSearchController();
case 'template_parser':
return TemplateParserController();
default:
return null;
}
return ModuleRouter.createControllerForTab(tab);
}
BaseContentController? get activeContentController {
@ -74,6 +66,7 @@ class TabItemsController with ChangeNotifier { @@ -74,6 +66,7 @@ class TabItemsController with ChangeNotifier {
Logger().error("没有活动的内容控制器,activeTabId:$_activeTabId", source: 'TabItemsController');
return null;
}
// ignore: unnecessary_type_check
if (controller is! BaseContentController) {
Logger().error("活动内容控制器不是BaseContentController类型", source: 'TabItemsController');
return null;

13
win_text_editor/lib/framework/widgets/tab_view.dart

@ -1,10 +1,8 @@ @@ -1,10 +1,8 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:win_text_editor/modules/template_parser/widgets/template_parser_view.dart';
import 'package:win_text_editor/shared/components/text_editor.dart';
import 'package:win_text_editor/modules/module_router.dart';
import 'package:win_text_editor/framework/controllers/tab_items_controller.dart';
import 'package:win_text_editor/framework/models/tab_model.dart';
import 'package:win_text_editor/modules/content_search/widgets/content_search_view.dart';
class TabView extends StatefulWidget {
final List<AppTab> tabs;
@ -57,14 +55,7 @@ class _TabViewState extends State<TabView> { @@ -57,14 +55,7 @@ class _TabViewState extends State<TabView> {
}
Widget _buildTabItem(AppTab tab, ChangeNotifier? controller) {
switch (tab.type) {
case 'content_search':
return ContentSearchView(tabId: tab.id);
case 'template_parser':
return TemplateParserView(tabId: tab.id);
default:
return TextEditor(tabId: tab.id, initialContent: tab.content);
}
return ModuleRouter.buildWidgetForTab(tab, controller);
}
}

32
win_text_editor/lib/menus/menu_actions.dart

@ -7,19 +7,9 @@ import 'package:win_text_editor/framework/controllers/file_provider.dart'; @@ -7,19 +7,9 @@ import 'package:win_text_editor/framework/controllers/file_provider.dart';
import 'package:collection/collection.dart';
import 'dart:io';
class MenuActions {
//
static const String templateParserTabType = "template_parser";
static const String templateParserTabTitle = "模板解析";
static const IconData templateParserTabIcon = Icons.auto_awesome_mosaic;
static const String templateParserDefaultContent = "";
//
static const String contentSearchTabType = "content_search";
static const String contentSearchTabTitle = "内容搜索";
static const IconData contentSearchTabIcon = Icons.search;
static const String contentSearchDefaultContent = "";
import 'package:win_text_editor/modules/module_router.dart';
class MenuActions {
static Future<void> handleMenuAction(String value, BuildContext context) async {
switch (value) {
case MenuConstants.openFolder:
@ -55,10 +45,10 @@ class MenuActions { @@ -55,10 +45,10 @@ class MenuActions {
await tabManager.addTab(
tabId,
title: contentSearchTabTitle,
type: contentSearchTabType,
icon: contentSearchTabIcon,
content: contentSearchDefaultContent,
title: "内容搜索",
type: RouterKey.contentSearch,
icon: Icons.search,
content: "",
);
}
@ -67,7 +57,7 @@ class MenuActions { @@ -67,7 +57,7 @@ class MenuActions {
// 使 firstWhereOrNull
final existingTab = tabManager.tabs.firstWhereOrNull(
(tab) => tab.type == templateParserTabType,
(tab) => tab.type == RouterKey.templateParser,
);
if (existingTab != null) {
@ -77,10 +67,10 @@ class MenuActions { @@ -77,10 +67,10 @@ class MenuActions {
final tabId = DateTime.now().millisecondsSinceEpoch.toString();
await tabManager.addTab(
tabId,
title: templateParserTabTitle,
type: templateParserTabType,
icon: templateParserTabIcon,
content: templateParserDefaultContent,
title: "模板解析",
type: RouterKey.templateParser,
icon: Icons.auto_awesome_mosaic,
content: "",
);
}
}

49
win_text_editor/lib/modules/module_router.dart

@ -0,0 +1,49 @@ @@ -0,0 +1,49 @@
// modules/tab_content_registry.dart
import 'package:flutter/material.dart';
import 'package:win_text_editor/framework/models/tab_model.dart';
import 'package:win_text_editor/shared/base/base_content_controller.dart';
import 'package:win_text_editor/modules/content_search/controllers/content_search_controller.dart';
import 'package:win_text_editor/modules/template_parser/controllers/template_parser_controller.dart';
import 'package:win_text_editor/modules/content_search/widgets/content_search_view.dart';
import 'package:win_text_editor/modules/template_parser/widgets/template_parser_view.dart';
class RouterKey {
static const String contentSearch = 'content_search';
static const String templateParser = 'template_parser';
static const String textEditor = 'text_editor';
}
class ModuleRouter {
//
static final Map<String, ContentControllerCreator> _controllerCreators = {
RouterKey.contentSearch: (tab) => ContentSearchController(),
RouterKey.templateParser: (tab) => TemplateParserController(),
};
// UI组件
static final Map<String, ContentWidgetBuilder> _widgetBuilders = {
RouterKey.contentSearch: (tab, controller) => ContentSearchView(tabId: tab.id),
RouterKey.templateParser: (tab, controller) => TemplateParserView(tabId: tab.id),
};
static BaseContentController? createControllerForTab(AppTab tab) {
final creator = _controllerCreators[tab.type];
return creator != null ? creator(tab) : null;
}
static Widget buildWidgetForTab(AppTab tab, ChangeNotifier? controller) {
final builder = _widgetBuilders[tab.type] ?? _widgetBuilders['default'];
return builder!(tab, controller);
}
static void registerControllerCreator(String type, ContentControllerCreator creator) {
_controllerCreators[type] = creator;
}
static void registerWidgetBuilder(String type, ContentWidgetBuilder builder) {
_widgetBuilders[type] = builder;
}
}
typedef ContentControllerCreator = BaseContentController Function(AppTab tab);
typedef ContentWidgetBuilder = Widget Function(AppTab tab, ChangeNotifier? controller);
Loading…
Cancel
Save