-
-
Notifications
You must be signed in to change notification settings - Fork 887
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add WrapperExtension helper (#1264)
- Loading branch information
1 parent
8ac444b
commit 2ffa1dd
Showing
22 changed files
with
296 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import 'package:flutter/widgets.dart'; | ||
import 'package:flutter_html/src/css_box_widget.dart'; | ||
import 'package:flutter_html/src/extension/html_extension.dart'; | ||
import 'package:flutter_html/src/style.dart'; | ||
import 'package:flutter_html/src/tree/styled_element.dart'; | ||
import 'package:html/dom.dart' as html; | ||
|
||
class TagWrapExtension extends HtmlExtension { | ||
final Set<String> tagsToWrap; | ||
final Widget Function(Widget child) builder; | ||
|
||
/// [TagWrapExtension] allows you to easily wrap a specific tag (or tags) | ||
/// in another element. For example, you could wrap `<table>` in a | ||
/// `SingleChildScrollView`: | ||
/// | ||
/// ```dart | ||
/// extensions: [ | ||
/// WrapperExtension( | ||
/// tagsToWrap: {"table"}, | ||
/// builder: (child) { | ||
/// return SingleChildScrollView( | ||
/// scrollDirection: Axis.horizontal, | ||
/// child: child, | ||
/// ); | ||
/// }, | ||
/// ), | ||
/// ], | ||
/// ``` | ||
TagWrapExtension({ | ||
required this.tagsToWrap, | ||
required this.builder, | ||
}); | ||
|
||
@override | ||
Set<String> get supportedTags => tagsToWrap; | ||
|
||
@override | ||
bool matches(ExtensionContext context) { | ||
switch (context.currentStep) { | ||
case CurrentStep.preparing: | ||
return super.matches(context); | ||
case CurrentStep.preStyling: | ||
case CurrentStep.preProcessing: | ||
return false; | ||
case CurrentStep.building: | ||
return context.styledElement is WrapperElement; | ||
} | ||
} | ||
|
||
@override | ||
StyledElement prepare( | ||
ExtensionContext context, List<StyledElement> children) { | ||
return WrapperElement( | ||
child: context.parser.prepareFromExtension( | ||
context, | ||
children, | ||
extensionsToIgnore: {this}, | ||
), | ||
); | ||
} | ||
|
||
@override | ||
InlineSpan build(ExtensionContext context, buildChildren) { | ||
final children = buildChildren(); | ||
final child = CssBoxWidget.withInlineSpanChildren( | ||
children: children.values.toList(), | ||
style: context.styledElement!.style, | ||
); | ||
|
||
return WidgetSpan( | ||
child: builder.call(child), | ||
); | ||
} | ||
} | ||
|
||
class WrapperElement extends StyledElement { | ||
WrapperElement({ | ||
required StyledElement child, | ||
}) : super( | ||
node: html.Element.tag("wrapper-element"), | ||
style: Style(), | ||
children: [child], | ||
name: "[wrapper-element]", | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.