diff --git a/notes/flutter_web/lib/routes.g.dart b/notes/flutter_web/lib/routes.g.dart index da12a1f7..59670cfc 100644 --- a/notes/flutter_web/lib/routes.g.dart +++ b/notes/flutter_web/lib/routes.g.dart @@ -6,6 +6,8 @@ // ignore_for_file: library_prefixes // ignore_for_file: non_constant_identifier_names +import 'package:you_note_dart/note.dart'; +import 'package:you_flutter/router.dart'; import 'package:flutter_web/routes/notes/page.dart' as notes_; import 'package:flutter_web/routes/notes/research/parameterized/page.dart' as notes_research_parameterized_; import 'package:flutter_web/routes/notes/research/remote_view/page.dart' as notes_research_remote_view_; @@ -60,111 +62,110 @@ import 'package:flutter_web/routes/notes/Improve_app/web_optimization/page.dart' import 'package:flutter_web/routes/notes/Improve_app/RepaintBoundary/page.dart' as notes_Improve_app_RepaintBoundary_; import 'package:flutter_web/routes/notes/Improve_app/event&listener&lifeycle/page.dart' as notes_Improve_app_event_listener_lifeycle_; import 'package:flutter_web/routes/notes/layout.dart' as notes__; -import 'package:you_flutter/router.dart'; var root = To("routes", children: [ - To("notes", builder: notes__.layout(notes_.build), children: [ - To("research", children: [ - To("bash_note"), - To("parameterized", builder: notes__.layout(notes_research_parameterized_.build)), - To("remote_view", builder: notes__.layout(notes_research_remote_view_.build)), + ToNote("notes", layout: notes__.layout, builder: notes_.build, children: [ + ToNote("research", children: [ + ToNote("bash_note"), + ToNote("parameterized", builder: notes_research_parameterized_.build), + ToNote("remote_view", builder: notes_research_remote_view_.build), ]), - To("env_info", builder: notes__.layout(notes_env_info_.build)), - To("layout", builder: notes__.layout(notes_layout_.build), children: [ - To("old", children: [ - To("layout", builder: notes__.layout(notes_layout_old_layout_.build)), + ToNote("env_info", builder: notes_env_info_.build), + ToNote("layout", builder: notes_layout_.build, children: [ + ToNote("old", children: [ + ToNote("layout", builder: notes_layout_old_layout_.build), ]), - To("box_layout", children: [ - To("constraints", builder: notes__.layout(notes_layout_box_layout_constraints_.build)), + ToNote("box_layout", children: [ + ToNote("constraints", builder: notes_layout_box_layout_constraints_.build), ]), - To("responsive_design", builder: notes__.layout(notes_layout_responsive_design_.build)), - To("1.welcome"), + ToNote("responsive_design", builder: notes_layout_responsive_design_.build), + ToNote("1.welcome"), ]), - To("navigation", builder: notes__.layout(notes_navigation_.build), children: [ - To("navigator_v1"), - To("navigator_v2"), + ToNote("navigation", builder: notes_navigation_.build, children: [ + ToNote("navigator_v1"), + ToNote("navigator_v2"), ]), - To("state", builder: notes__.layout(notes_state_.build), children: [ - To("1.vanilla_state", builder: notes__.layout(notes_state_vanilla_state_.build)), - To("StatefulBuilder", builder: notes__.layout(notes_state_StatefulBuilder_.build)), + ToNote("state", builder: notes_state_.build, children: [ + ToNote("1.vanilla_state", builder: notes_state_vanilla_state_.build), + ToNote("StatefulBuilder", builder: notes_state_StatefulBuilder_.build), ]), - To("media&assets&file", children: [ - To("file_desktop", builder: notes__.layout(notes_media_assets_file_file_desktop_.build)), - To("file_web", builder: notes__.layout(notes_media_assets_file_file_web_.build)), - To("assets", builder: notes__.layout(notes_media_assets_file_assets_.build)), + ToNote("media&assets&file", children: [ + ToNote("file_desktop", builder: notes_media_assets_file_file_desktop_.build), + ToNote("file_web", builder: notes_media_assets_file_file_web_.build), + ToNote("assets", builder: notes_media_assets_file_assets_.build), ]), - To("style&theming", children: [ - To("material3", builder: notes__.layout(notes_style_theming_material3_.build)), - To("Material"), + ToNote("style&theming", children: [ + ToNote("material3", builder: notes_style_theming_material3_.build), + ToNote("Material"), ]), - To("pure_dart", children: [ - To("dart3", builder: notes__.layout(notes_pure_dart_dart3_.build)), - To("async", builder: notes__.layout(notes_pure_dart_async_.build)), - To("safe_interface", builder: notes__.layout(notes_pure_dart_safe_interface_.build)), - To("execption", builder: notes__.layout(notes_pure_dart_execption_.build)), - To("serialization_json", builder: notes__.layout(notes_pure_dart_serialization_json_.build)), - To("analyzer", builder: notes__.layout(notes_pure_dart_analyzer_.build)), + ToNote("pure_dart", children: [ + ToNote("dart3", builder: notes_pure_dart_dart3_.build), + ToNote("async", builder: notes_pure_dart_async_.build), + ToNote("safe_interface", builder: notes_pure_dart_safe_interface_.build), + ToNote("execption", builder: notes_pure_dart_execption_.build), + ToNote("serialization_json", builder: notes_pure_dart_serialization_json_.build), + ToNote("analyzer", builder: notes_pure_dart_analyzer_.build), ]), - To("dev", children: [ - To("host_mirror", builder: notes__.layout(notes_dev_host_mirror_.build)), - To("macos_config", builder: notes__.layout(notes_dev_macos_config_.build)), - To("devtool", builder: notes__.layout(notes_dev_devtool_.build), children: [ - To("vmservice", builder: notes__.layout(notes_dev_devtool_vmservice_.build)), + ToNote("dev", children: [ + ToNote("host_mirror", builder: notes_dev_host_mirror_.build), + ToNote("macos_config", builder: notes_dev_macos_config_.build), + ToNote("devtool", builder: notes_dev_devtool_.build, children: [ + ToNote("vmservice", builder: notes_dev_devtool_vmservice_.build), ]), - To("debug", builder: notes__.layout(notes_dev_debug_.build)), + ToNote("debug", builder: notes_dev_debug_.build), ]), - To("i18n", builder: notes__.layout(notes_i18n_.build)), - To("thinking_in_ui", children: [ - To("readable_html_vs_dart", builder: notes__.layout(notes_thinking_in_ui_readable_html_vs_dart_.build)), + ToNote("i18n", builder: notes_i18n_.build), + ToNote("thinking_in_ui", children: [ + ToNote("readable_html_vs_dart", builder: notes_thinking_in_ui_readable_html_vs_dart_.build), ]), - To("x.note_inside", children: [ - To("how_note_work", builder: notes__.layout(notes_x_note_inside_how_note_work_.build)), - To("note_api_thinking"), - To("mockup_content", builder: notes__.layout(notes_x_note_inside_mockup_content_.build)), - To("1.welcome", builder: notes__.layout(notes_x_note_inside_welcome_.build)), - To("note_test_self", builder: notes__.layout(notes_x_note_inside_note_test_self_.build), children: [ - To("nobody", builder: notes__.layout(notes_x_note_inside_note_test_self_nobody_.build)), + ToNote("x.note_inside", children: [ + ToNote("how_note_work", builder: notes_x_note_inside_how_note_work_.build), + ToNote("note_api_thinking"), + ToNote("mockup_content", builder: notes_x_note_inside_mockup_content_.build), + ToNote("1.welcome", builder: notes_x_note_inside_welcome_.build), + ToNote("note_test_self", builder: notes_x_note_inside_note_test_self_.build, children: [ + ToNote("nobody", builder: notes_x_note_inside_note_test_self_nobody_.build), ]), ]), - To("widgets", children: [ - To("specific_widgets", children: [ - To("input", builder: notes__.layout(notes_widgets_specific_widgets_input_.build)), - To("webview", builder: notes__.layout(notes_widgets_specific_widgets_webview_.build)), - To("slider", builder: notes__.layout(notes_widgets_specific_widgets_slider_.build)), - To("selectable", builder: notes__.layout(notes_widgets_specific_widgets_selectable_.build)), - To("button", children: [ - To("ButtonStyleButton", builder: notes__.layout(notes_widgets_specific_widgets_button_ButtonStyleButton_.build)), - To("FloatingActionButton", builder: notes__.layout(notes_widgets_specific_widgets_button_FloatingActionButton_.build)), - To("IconButton", builder: notes__.layout(notes_widgets_specific_widgets_button_IconButton_.build)), - To("overview", builder: notes__.layout(notes_widgets_specific_widgets_button_overview_.build)), + ToNote("widgets", children: [ + ToNote("specific_widgets", children: [ + ToNote("input", builder: notes_widgets_specific_widgets_input_.build), + ToNote("webview", builder: notes_widgets_specific_widgets_webview_.build), + ToNote("slider", builder: notes_widgets_specific_widgets_slider_.build), + ToNote("selectable", builder: notes_widgets_specific_widgets_selectable_.build), + ToNote("button", children: [ + ToNote("ButtonStyleButton", builder: notes_widgets_specific_widgets_button_ButtonStyleButton_.build), + ToNote("FloatingActionButton", builder: notes_widgets_specific_widgets_button_FloatingActionButton_.build), + ToNote("IconButton", builder: notes_widgets_specific_widgets_button_IconButton_.build), + ToNote("overview", builder: notes_widgets_specific_widgets_button_overview_.build), ]), - To("Divider"), - To("text", children: [ - To("RichText", builder: notes__.layout(notes_widgets_specific_widgets_text_RichText_.build)), - To("Text", builder: notes__.layout(notes_widgets_specific_widgets_text_Text_.build)), + ToNote("Divider"), + ToNote("text", children: [ + ToNote("RichText", builder: notes_widgets_specific_widgets_text_RichText_.build), + ToNote("Text", builder: notes_widgets_specific_widgets_text_Text_.build), ]), - To("icon", builder: notes__.layout(notes_widgets_specific_widgets_icon_.build)), + ToNote("icon", builder: notes_widgets_specific_widgets_icon_.build), ]), - To("container_widgets", children: [ - To("drawer"), - To("Card"), - To("app"), - To("dialog"), - To("menu", builder: notes__.layout(notes_widgets_container_widgets_menu_.build)), - To("scrollable", children: [ - To("Dismissible", builder: notes__.layout(notes_widgets_container_widgets_scrollable_Dismissible_.build)), - To("DraggableScrollableSheet"), + ToNote("container_widgets", children: [ + ToNote("drawer"), + ToNote("Card"), + ToNote("app"), + ToNote("dialog"), + ToNote("menu", builder: notes_widgets_container_widgets_menu_.build), + ToNote("scrollable", children: [ + ToNote("Dismissible", builder: notes_widgets_container_widgets_scrollable_Dismissible_.build), + ToNote("DraggableScrollableSheet"), ]), - To("page_view"), - To("bar", builder: notes__.layout(notes_widgets_container_widgets_bar_.build)), + ToNote("page_view"), + ToNote("bar", builder: notes_widgets_container_widgets_bar_.build), ]), ]), - To("Improve_app", builder: notes__.layout(notes_Improve_app_.build), children: [ - To("1.overview", builder: notes__.layout(notes_Improve_app_overview_.build)), - To("error_handle"), - To("web_optimization", builder: notes__.layout(notes_Improve_app_web_optimization_.build)), - To("RepaintBoundary", builder: notes__.layout(notes_Improve_app_RepaintBoundary_.build)), - To("event&listener&lifeycle", builder: notes__.layout(notes_Improve_app_event_listener_lifeycle_.build)), + ToNote("Improve_app", builder: notes_Improve_app_.build, children: [ + ToNote("1.overview", builder: notes_Improve_app_overview_.build), + ToNote("error_handle"), + ToNote("web_optimization", builder: notes_Improve_app_web_optimization_.build), + ToNote("RepaintBoundary", builder: notes_Improve_app_RepaintBoundary_.build), + ToNote("event&listener&lifeycle", builder: notes_Improve_app_event_listener_lifeycle_.build), ]), ]), ]); diff --git a/notes/flutter_web/lib/routes/notes/layout.dart b/notes/flutter_web/lib/routes/notes/layout.dart index a7c3eac7..596762fd 100644 --- a/notes/flutter_web/lib/routes/notes/layout.dart +++ b/notes/flutter_web/lib/routes/notes/layout.dart @@ -2,13 +2,8 @@ import 'package:flutter/widgets.dart'; import 'package:you_flutter/router.dart'; import 'package:you_note_dart/note.dart'; import 'package:you_note_dart/note_layouts.dart'; -/// [PageLayoutBuilder] -PageBuilder layout(NoteBuilder builder) { - return (context, uri) => NoteLayoutStyle1(uri: uri, builder: builder); -} - /// [PageLayoutBuilder] @ToType(type: ToNote) -Widget layout2(BuildContext context, ToUri uri, NoteBuilder builder) { - return NoteLayoutStyle1(uri: uri, builder: builder); +Widget layout(BuildContext context, ToUri uri, Cell cell) { + return NoteLayoutStyle1(uri: uri, cell: cell); } diff --git a/packages/you_cli/bin/cli.dart b/packages/you_cli/bin/cli.dart index 749a6319..e3184bae 100644 --- a/packages/you_cli/bin/cli.dart +++ b/packages/you_cli/bin/cli.dart @@ -91,6 +91,7 @@ class Cmd_gen_routes_g_dart extends Command { // (context, print) async => await notes_i18n_.loadLibrary().then((_) => notes_i18n_.build(context, print)) // - async layout + page : // notes_layout.layout((context, print) async => await notes_i18n_.loadLibrary().then((_) => notes_i18n_.build(context, print))) + @Deprecated("已废弃,待完成重构后删除") code.Expression? builderExpression(RouteNode node) { if (!node.file_page_dart.existsSync()) { return null; @@ -98,7 +99,7 @@ class Cmd_gen_routes_g_dart extends Command { code.Expression builder = code.refer("${node.flatName}_").property("build"); RouteNode? layout = node.findLayoutSync(); if (layout != null) { - builder = code.refer("${layout.flatName}__").property("layout").call([builder]); + builder = refer("${layout.flatName}__").property("layout2").call([builder]); } if (async) { @@ -118,16 +119,17 @@ class Cmd_gen_routes_g_dart extends Command { // 没用code_builder是因为它会格式化换行,很多换行,很乱。 Future _genRouteTreeCode(RouteNode node) async { - code.Expression? builder = builderExpression(node); - String builderStr = builder == null ? "" : builder.accept(code.DartEmitter()).toString().split("\n").join(); - - String buildArg = !node.file_page_dart.existsSync() ? "" : ",builder:$builderStr"; + String buildArg = !node.file_page_dart.existsSync() ? "" : ",builder:${node.flatName}_.build"; + String layoutArg = !node.file_layout_dart.existsSync() ? "" : ",layout:${node.flatName}__.layout"; String padding = "".padLeft(node.level, ' '); + var toType = node.findToType(); if (node.children.isEmpty) { - return '''${padding}To${async ? ".lazy" : ""}("${node.dir.basename}" $buildArg) '''; + return '''$padding${_allocator.allocate(toType)}${async ? ".lazy" : ""}("${node.dir.basename}" $layoutArg $buildArg) '''; } - return '''${padding}To${async ? ".lazy" : ""}("${node.dir.basename}" $buildArg, children:[ -${node.children.map((child) async => await _genRouteTreeCode(child)).map((e) => "$e,").join("\n")} + List children=await Future.wait(node.children.map((child) async => await _genRouteTreeCode(child))); + + return '''$padding${_allocator.allocate(toType)}${async ? ".lazy" : ""}("${node.dir.basename}" $layoutArg $buildArg, children:[ +${children.map((e) => "$e,").join("\n")} $padding])'''; } @@ -142,7 +144,7 @@ $padding])'''; throw AssertionError("【--dir $dir】 not exists"); } - var rootRoute = cli.rootRoute; + var rootRoute = await cli.rootRoute; Iterable routes = rootRoute.toList(); var nameMaxLen = routes.map((e) => e.flatName.length).reduce((value, element) => value > element ? value : element); @@ -183,7 +185,6 @@ $padding])'''; } }), ) - ..directives.add(Directive.import("package:you_flutter/router.dart")) ..body.add( Code(""" @@ -200,8 +201,9 @@ $newRoutes var fmt = DartFormatter(pageWidth: 200); var dartEmitter = DartEmitter(allocator: _allocator, orderDirectives: false, useNullSafetySyntax: true); - var allCode = fmt.format('${all.accept(dartEmitter)}'); - _log("gen: ${cli.file_routes_g_dart.path}"); + var allCode ='${all.accept(dartEmitter)}'; + _log("gen: ${cli.file_routes_g_dart.path} : $allCode"); + allCode = fmt.format(allCode); await cli.file_routes_g_dart.writeAsString(allCode); // 暂时不格式化,因为要保持变量名后的padding,对齐变量更好看 // file.writeAsString(_fmt.format(toCode)); diff --git a/packages/you_cli/lib/src/cli_core.dart b/packages/you_cli/lib/src/cli_core.dart index 8983d382..84eed7ba 100644 --- a/packages/you_cli/lib/src/cli_core.dart +++ b/packages/you_cli/lib/src/cli_core.dart @@ -8,7 +8,6 @@ import 'package:analyzer/file_system/physical_file_system.dart'; import 'package:code_builder/code_builder.dart'; import 'package:file/file.dart'; import 'package:path/path.dart' as path; -import 'package:you_cli/src/urils.dart'; import 'package:you_cli/src/yaml.dart'; // final Glob _PAGE_GLOB = Glob("{**/page.dart,page.dart}"); @@ -17,9 +16,12 @@ class YouCli { : dir_project = projectDir.fileSystem.directory(path.normalize(path.absolute(projectDir.path))), fs = projectDir.fileSystem; - static const ToType toTypeDefault = ToType(type: Reference("To", "package:you_flutter/router.dart")); + static const Reference toTypeDefault = Reference("To", "package:you_flutter/router.dart"); static const String toTypeName = "ToType"; - static const String layoutFunctionName = "layout2"; + static const String pageDart = "page.dart"; + static const String layoutDart = "layout.dart"; + static const String layoutFunctionName = "layout"; + static const String pageBuildFunctionName = "build"; final Directory dir_project; final FileSystem fs; Pubspec? _pubspec; @@ -38,17 +40,24 @@ class YouCli { Pubspec get pubspec => _pubspec ??= Pubspec.parseFileSync(file_pubspec_yaml); - RouteNode get rootRoute { - RouteNode from(Directory dir) { + Future get rootRoute async { + Future from(Directory dir) async { if (!dir.existsSync()) { return RouteNode(dir: dir, children: []); } - var children = dir.listSync(recursive: false).whereType().map((e) => from(e)); - return RouteNode(dir: dir, children: children.toList()); + var children = await Future.wait(dir.listSync(recursive: false).whereType().map((e) async => await from(e))); + var (layout: layoutFunction, toType: toType) = await analysisLayout(dir.childFile(layoutDart)); + return RouteNode( + dir: dir, + pageBuild: await analysisPage(dir.childFile(pageDart)), + layoutFunction: layoutFunction, + toType: toType, + children: children, + ); } - return _rootRoute ??= from(dir_routes); + return _rootRoute ??= await from(dir_routes); } AnalysisSession get analysisSession { @@ -58,10 +67,12 @@ class YouCli { ).contexts[0].currentSession; } - LibraryElement? findRealExportLib(T toFind,LibraryElement useAt){ - for(var import in useAt.importedLibraries){ - for(var c in analyzerUtils.findChildrenByType(import)){ - if(toFind==c){ + /// given a internal lib: package:you_flutter/src/router.dart + /// => find it's public export : package:you_flutter/router.dart + LibraryElement? findPublicExportLib(TypeDefiningElement toFind, LibraryElement useAt) { + for (var import in useAt.importedLibraries) { + for (var MapEntry(key: _, value: value) in import.exportNamespace.definedNames.entries) { + if (toFind == value) { return import; } } @@ -69,7 +80,11 @@ class YouCli { return toFind.library!; } - Future<({FunctionElement? layout, ToType? toType})> analysisLayout(File file) async { + Future<({FunctionElement? layout, Reference? toType})> analysisLayout(File file) async { + if (!await file.exists()) { + return (layout: null, toType: null); + } + var layoutLib = (await analysisSession.getResolvedLibrary(path.normalize(path.absolute(file.path))) as ResolvedLibraryResult).element; FunctionElement? layoutFunction = layoutLib.definingCompilationUnit.functions.where((e) => e.name == layoutFunctionName).firstOrNull; if (layoutFunction == null) { @@ -77,18 +92,18 @@ class YouCli { } var findToTypeAnno = layoutFunction.metadata.map((e) => e.computeConstantValue()).where((e) { var t = e?.type; - if(t == null){ + if (t == null) { return false; } if (t.getDisplayString(withNullability: false) != toTypeName) { return false; } - var element=t.element; - if(element is! ClassElement){ + var element = t.element; + if (element is! ClassElement) { return false; } // result?.type?.element?.library?.children - var publicExportFrom = findRealExportLib(element, layoutLib); + var publicExportFrom = findPublicExportLib(element, layoutLib); return publicExportFrom?.identifier == toTypeDefault.type.url; }).firstOrNull; if (findToTypeAnno == null) { @@ -105,32 +120,30 @@ class YouCli { return (layout: layoutFunction, toType: toTypeDefault); } - var publicExportFrom = findRealExportLib(findToTypeAnno.type!.element!, layoutLib); + var publicExportFrom = findPublicExportLib(type.element! as TypeDefiningElement, layoutLib); var url = publicExportFrom?.identifier; - return (layout: layoutFunction, toType: ToType(type: refer(symbol, url))); + return (layout: layoutFunction, toType: refer(symbol, url)); } -} - -/// ref: you_flutter: ExtendTo -class ToType { - final Reference type; - const ToType({required this.type}); - - @override - String toString() { - return " type:$type"; + Future analysisPage(File file) async { + if (!await file.exists()) { + return null; + } + var lib = (await analysisSession.getResolvedLibrary(path.normalize(path.absolute(file.path))) as ResolvedLibraryResult).element; + return lib.definingCompilationUnit.functions.where((e) => e.name == pageBuildFunctionName).firstOrNull; } } class RouteNode { final List children; final Directory dir; + final Reference? toType; + final FunctionElement? layoutFunction; + final FunctionElement? pageBuild; late RouteNode _parent = this; - ToType? toType; - RouteNode({required this.dir, this.toType, required this.children}) { + RouteNode({required this.dir, this.toType, required this.children, this.layoutFunction, this.pageBuild}) { for (var child in children) { child._parent = this; } @@ -218,7 +231,7 @@ class RouteNode { return _parent.findLayoutSync(); } - ToType findToType() { + Reference findToType() { if (toType != null) { return toType!; } diff --git a/packages/you_cli/test/bin/analyzer_test.dart b/packages/you_cli/test/bin/analyzer_test.dart index 77c5e707..a640c519 100644 --- a/packages/you_cli/test/bin/analyzer_test.dart +++ b/packages/you_cli/test/bin/analyzer_test.dart @@ -1,4 +1,5 @@ import 'package:checks/checks.dart'; +import 'package:code_builder/code_builder.dart'; import 'package:file/file.dart'; import 'package:file/local.dart'; import 'package:test/test.dart'; @@ -10,10 +11,7 @@ void main() { FileSystem fs=LocalFileSystem(); YouCli cli = YouCli(projectDir: fs.directory("../../notes/flutter_web/lib")); var result =await cli.analysisLayout(fs.file("../../notes/flutter_web/lib/routes/notes/layout.dart")); - print("sssss $result"); - check(result.toType!.type.symbol).equals("ToNote"); - + check(result.toType!.type).equals(refer("ToNote","package:you_note_dart/note.dart")); }); - }); } diff --git a/packages/you_cli/test/bin/cli_commands_test.dart b/packages/you_cli/test/bin/cli_commands_test.dart index 4c4a9a9d..9bc1a268 100644 --- a/packages/you_cli/test/bin/cli_commands_test.dart +++ b/packages/you_cli/test/bin/cli_commands_test.dart @@ -20,13 +20,13 @@ void main() { ..writeAsString("""name: app"""); }); group("Cmd_gen_routes_g_dart", () { - void checkBuilder(String? expected, {required bool async}) { + Future checkBuilder(String? expected, {required bool async}) async { Cmd_gen_routes_g_dart cmd = Cmd_gen_routes_g_dart.libMode(fs: fs, async: async, dir: fs.directory("/app/lib/routes")); - var result = cmd.builderExpression(cli.rootRoute); + var result = cmd.builderExpression(await cli.rootRoute); if (expected == null) { check(result).isNull(); } else { - check(cmd.builderExpression(cli.rootRoute)!.accept(DartEmitter()).toString().split("\n").join()).equals(expected); + check(cmd.builderExpression(await cli.rootRoute)!.accept(DartEmitter()).toString().split("\n").join()).equals(expected); } } diff --git a/packages/you_cli/test/bin/cli_core_test.dart b/packages/you_cli/test/bin/cli_core_test.dart index 1753e475..f5e12699 100644 --- a/packages/you_cli/test/bin/cli_core_test.dart +++ b/packages/you_cli/test/bin/cli_core_test.dart @@ -5,14 +5,15 @@ import 'package:you_cli/src/cli_core.dart'; void main() { group("RouteNode", () { - test('create from directory', () { + test('create from directory', () async { late MemoryFileSystem fs = MemoryFileSystem(); fs.directory("/note/lib/routes/notes/page_1").createSync(recursive: true); fs.directory("/note/lib/routes/notes/page_1/page_1_1").createSync(recursive: true); fs.directory("/note/lib/routes/notes/page_1/page_1_2").createSync(recursive: true); YouCli cli = YouCli(projectDir: fs.directory("/note")); - check(cli.rootRoute.toList().map((e) => e.routePath)).deepEquals([ + var rootRoute = await cli.rootRoute; + check(rootRoute.toList().map((e) => e.routePath)).deepEquals([ "/", "/notes", "/notes/page_1", diff --git a/packages/you_note_dart/lib/src/layouts/note_layout_default.dart b/packages/you_note_dart/lib/src/layouts/note_layout_default.dart index 5dec2e23..1ca89e0a 100644 --- a/packages/you_note_dart/lib/src/layouts/note_layout_default.dart +++ b/packages/you_note_dart/lib/src/layouts/note_layout_default.dart @@ -1,21 +1,16 @@ - import 'package:flutter/material.dart'; import 'package:you_flutter/router.dart'; import 'package:you_flutter/state.dart'; import 'package:you_note_dart/note.dart'; final class NoteLayoutDefault extends StatelessWidget { - final NoteBuilder builder; + final Cell rootCell; final ToUri uri; - const NoteLayoutDefault({super.key, required this.uri, required this.builder}); + const NoteLayoutDefault({super.key, required this.uri, required this.rootCell}); @override Widget build(BuildContext context) { - // collect note page contents(from print(xxx)) - Cell rootCell = Cell.empty(); - builder(context, rootCell); - return Scaffold( body: SafeArea( child: SelectionArea( diff --git a/packages/you_note_dart/lib/src/layouts/note_layout_style_1.dart b/packages/you_note_dart/lib/src/layouts/note_layout_style_1.dart index bd254c5b..dc1b08ff 100644 --- a/packages/you_note_dart/lib/src/layouts/note_layout_style_1.dart +++ b/packages/you_note_dart/lib/src/layouts/note_layout_style_1.dart @@ -7,24 +7,20 @@ import 'package:you_note_dart/src/note.dart'; /// 一个极简的笔记布局范例 /// 左边routes树,右边页面内容 final class NoteLayoutStyle1 extends StatelessWidget { - final NoteBuilder builder; + final Cell cell; final ToUri uri; - NoteLayoutStyle1({super.key, required this.uri, required this.builder}); + NoteLayoutStyle1({super.key, required this.uri, required this.cell}); @override Widget build(BuildContext context) { - // 收集笔记页面内容 - Cell rootCell = Cell.empty(); - builder(context, rootCell); - return Scaffold( body: SafeArea( child: SelectionArea( /// Watch是you_flutter的state管理组件, 被其包裹的状态可以被观测刷新(ref: Cell._contents = [].signal()) child: Watch((context) { // contents是收集到调用print(xx)的所有结果 - var pageContents = rootCell.toList().expand((cell) sync* { + var pageContents = cell.toList().expand((cell) sync* { for (var content in cell.contents) { yield Align( alignment: Alignment.centerLeft, diff --git a/packages/you_note_dart/lib/src/note.dart b/packages/you_note_dart/lib/src/note.dart index 276be507..b252f0dd 100644 --- a/packages/you_note_dart/lib/src/note.dart +++ b/packages/you_note_dart/lib/src/note.dart @@ -13,7 +13,7 @@ import 'package:you_note_dart/src/layouts/note_layout_default.dart'; typedef NoteBuilder = void Function(BuildContext context, Cell print); typedef LazyNoteBuilder = Future Function(BuildContext context, Cell print); -typedef NoteLayoutBuilder = Widget Function(BuildContext context, ToUri uri, NoteBuilder builder); +typedef NoteLayoutBuilder = Widget Function(BuildContext context, ToUri uri, Cell cell); base class ToNote extends To { final NoteBuilder? _builder; @@ -28,15 +28,15 @@ base class ToNote extends To { if (_builder == null) { return null; } + Cell rootCell = Cell.empty(); + _builder(context, rootCell); To? find = findLayoutNode(); if (find == null) { - return NoteLayoutDefault(uri: uri, builder: _builder); + return NoteLayoutDefault(uri: uri, rootCell: rootCell); } - return (find as ToNote)._layout!(context, uri, _builder); + return (find as ToNote)._layout!(context, uri, rootCell); } - -// ToNote.lazy(super.part) : super.lazy(); } class NoteRoute {