diff --git a/frontend/app_flowy/packages/flowy_editor/example/lib/main.dart b/frontend/app_flowy/packages/flowy_editor/example/lib/main.dart index 68a10783a6..d697fe1a9c 100644 --- a/frontend/app_flowy/packages/flowy_editor/example/lib/main.dart +++ b/frontend/app_flowy/packages/flowy_editor/example/lib/main.dart @@ -88,8 +88,11 @@ class _MyHomePageState extends State { } else { final data = Map.from(json.decode(snapshot.data!)); final stateTree = StateTree.fromJson(data); - return renderPlugins.buildWidgetWithNode( - stateTree.root, + return renderPlugins.buildWidget( + NodeWidgetContext( + buildContext: context, + node: stateTree.root, + ), ); } }, diff --git a/frontend/app_flowy/packages/flowy_editor/example/lib/plugin/image_node_widget.dart b/frontend/app_flowy/packages/flowy_editor/example/lib/plugin/image_node_widget.dart index 02d6d8fc85..cb9d44da60 100644 --- a/frontend/app_flowy/packages/flowy_editor/example/lib/plugin/image_node_widget.dart +++ b/frontend/app_flowy/packages/flowy_editor/example/lib/plugin/image_node_widget.dart @@ -8,14 +8,26 @@ class ImageNodeBuilder extends NodeWidgetBuilder { String get src => node.attributes['image_src'] as String; @override - Widget build() { - final childrenWidget = buildChildren(); + Widget build(BuildContext buildContext) { final image = Image.network(src); - if (childrenWidget != null) { + Widget? children; + if (node.children.isNotEmpty) { + children = Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: node.children + .map( + (e) => renderPlugins.buildWidget( + NodeWidgetContext(buildContext: buildContext, node: e), + ), + ) + .toList(), + ); + } + if (children != null) { return Column( children: [ image, - childrenWidget, + children, ], ); } else { diff --git a/frontend/app_flowy/packages/flowy_editor/example/lib/plugin/text_node_widget.dart b/frontend/app_flowy/packages/flowy_editor/example/lib/plugin/text_node_widget.dart index c73878c269..7acf35cec9 100644 --- a/frontend/app_flowy/packages/flowy_editor/example/lib/plugin/text_node_widget.dart +++ b/frontend/app_flowy/packages/flowy_editor/example/lib/plugin/text_node_widget.dart @@ -8,19 +8,33 @@ class TextNodeBuilder extends NodeWidgetBuilder { String get content => node.attributes['content'] as String; @override - Widget build() { - final childrenWidget = buildChildren(); + Widget build(BuildContext buildContext) { final richText = SelectableText.rich( TextSpan( text: node.attributes['content'] as String, style: node.attributes.toTextStyle(), ), ); - if (childrenWidget != null) { + + Widget? children; + if (node.children.isNotEmpty) { + children = Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: node.children + .map( + (e) => renderPlugins.buildWidget( + NodeWidgetContext(buildContext: buildContext, node: e), + ), + ) + .toList(), + ); + } + + if (children != null) { return Column( children: [ richText, - childrenWidget, + children, ], ); } else { diff --git a/frontend/app_flowy/packages/flowy_editor/lib/render/node_widget_builder.dart b/frontend/app_flowy/packages/flowy_editor/lib/render/node_widget_builder.dart index 3d32c52bab..484b38ceb4 100644 --- a/frontend/app_flowy/packages/flowy_editor/lib/render/node_widget_builder.dart +++ b/frontend/app_flowy/packages/flowy_editor/lib/render/node_widget_builder.dart @@ -9,21 +9,9 @@ class NodeWidgetBuilder { NodeWidgetBuilder.create({required this.node, required this.renderPlugins}); - Widget call() => build(); - Widget build() => throw UnimplementedError(); - Widget? buildChildren() { - if (node.children.isEmpty) { - return null; - } + Widget call(BuildContext buildContext) => build(buildContext); - // default layout - return Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: node.children - .map( - (e) => renderPlugins.buildWidgetWithNode(e), - ) - .toList(), - ); - } + /// Render the current [Node] + /// and the layout style of [Node.Children]. + Widget build(BuildContext buildContext) => throw UnimplementedError(); } diff --git a/frontend/app_flowy/packages/flowy_editor/lib/render/render_plugins.dart b/frontend/app_flowy/packages/flowy_editor/lib/render/render_plugins.dart index 49929f17e1..49d5dc9e1f 100644 --- a/frontend/app_flowy/packages/flowy_editor/lib/render/render_plugins.dart +++ b/frontend/app_flowy/packages/flowy_editor/lib/render/render_plugins.dart @@ -2,6 +2,12 @@ import 'package:flutter/material.dart'; import '../document/node.dart'; import 'node_widget_builder.dart'; +class NodeWidgetContext { + BuildContext buildContext; + Node node; + NodeWidgetContext({required this.buildContext, required this.node}); +} + typedef NodeWidgetBuilderF = A Function({ required T node, @@ -28,13 +34,15 @@ class RenderPlugins { nodeWidgetBuilders.removeWhere((key, _) => key == name); } - Widget buildWidgetWithNode(Node node) { - final nodeWidgetBuilder = _nodeWidgetBuilder(node.type); - return nodeWidgetBuilder(node: node, renderPlugins: this)(); + Widget buildWidget(NodeWidgetContext context) { + final nodeWidgetBuilder = _nodeWidgetBuilder(context.node.type); + return nodeWidgetBuilder(node: context.node, renderPlugins: this)( + context.buildContext); } NodeWidgetBuilderF _nodeWidgetBuilder(String name) { - assert(nodeWidgetBuilders.containsKey(name)); + assert(nodeWidgetBuilders.containsKey(name), + 'Could not query the builder with this $name'); return nodeWidgetBuilders[name]!; } }