Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update [email protected] #224

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/flutter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
token: ${{ secrets.CODECOV_TOKEN }}

build_apk:
if: ${{ github.ref == 'refs/heads/master' || contains(github.ref, 'android') || startsWith(github.ref, 'refs/heads/renovate/flutter-') }}
if: ${{ github.ref == 'refs/heads/master' || contains(github.ref, 'android') || startsWith(github.ref, 'refs/heads/renovate/flutter-') || contains(github.ref, 'fwfh') }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand Down Expand Up @@ -111,7 +111,7 @@ jobs:
fingerprint: <!-- ${{ github.sha }} -->

build_ipa:
if: ${{ github.ref == 'refs/heads/master' || contains(github.ref, 'ios') }}
if: ${{ github.ref == 'refs/heads/master' || contains(github.ref, 'ios') || contains(github.ref, 'fwfh') }}
runs-on: macos-latest
steps:
- uses: actions/checkout@v4
Expand Down Expand Up @@ -243,7 +243,7 @@ jobs:
fingerprint: <!-- ${{ github.sha }} -->

build_macos:
if: ${{ github.ref == 'refs/heads/master' || contains(github.ref, 'macos') || startsWith(github.ref, 'refs/heads/renovate/flutter-') }}
if: ${{ github.ref == 'refs/heads/master' || contains(github.ref, 'macos') || startsWith(github.ref, 'refs/heads/renovate/flutter-') || contains(github.ref, 'fwfh') }}
runs-on: macos-latest
steps:
- uses: actions/checkout@v4
Expand Down
20 changes: 10 additions & 10 deletions lib/src/widgets/html.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
// ignore_for_file: avoid_renaming_method_parameters, deprecated_member_use

import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:flutter_widget_from_html/flutter_widget_from_html.dart';
// ignore: implementation_imports
import 'package:flutter_widget_from_html_core/src/internal/core_ops.dart'
show Priority;
import 'package:fwfh_webview/fwfh_webview.dart';
import 'package:html_unescape/html_unescape.dart';
import 'package:http/http.dart';
Expand Down Expand Up @@ -141,18 +146,13 @@ class TinhteWidgetFactory extends WidgetFactory {

BuildOp get smilieOp {
return _smilieOp ??= BuildOp(
onTree: (meta, tree) {
final a = meta.element.attributes;
onParsed: (tree) {
final a = tree.element.attributes;
final title = a['data-title'];
if (title == null) return;
if (title == null) return tree;
final smilie = _kSmilies[title];
if (smilie == null) return;
final parentTree = tree.parent;
if (parentTree == null) return;

// TODO: use `replaceWith` when it comes back in v0.9
TextBit(parentTree, smilie).insertBefore(tree);
tree.detach();
if (smilie == null) return tree;
return tree.parent.sub()..addText(smilie);
},
);
}
Expand Down
2 changes: 2 additions & 0 deletions lib/src/widgets/html/chr.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// ignore_for_file: deprecated_member_use

part of '../html.dart';

class Chr {
Expand Down
96 changes: 49 additions & 47 deletions lib/src/widgets/html/galleria.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,108 +4,110 @@
const kSpacing = 3.0;

class Galleria {
final BuildMetadata galleryMeta;
final BuildTree galleryTree;
final TinhteWidgetFactory wf;

final _items = <WidgetPlaceholder>[];
final _lb = LbTrigger();

Galleria(this.wf, this.galleryMeta);
Galleria(this.wf, this.galleryTree);

BuildOp? _galleriaOp;
BuildOp get op {
return _galleriaOp ??= BuildOp(
onChild: onChild,
onWidgets: onWidgets,
onVisitChild: onVisitChild,
onRenderBlock: onRenderBlock,
);
}

void onChild(BuildMetadata childMeta) {
final e = childMeta.element;
if (e.parent != galleryMeta.element) return;
void onVisitChild(BuildTree _, BuildTree subTree) {
final e = subTree.element;
if (e.parent != galleryTree.element) return;
if (e.localName != 'li') return;

childMeta.register(_GalleriaItem(wf, this, childMeta).op);
subTree.register(_GalleriaItem(wf, this, subTree).op);
}

Iterable<Widget> onWidgets(
BuildMetadata _, Iterable<WidgetPlaceholder> widgets) =>
[
WidgetPlaceholder<Galleria>(this,
child: _GalleriaGrid(widgets.toList(growable: false)))
];
Widget onRenderBlock(BuildTree _, WidgetPlaceholder placeholder) =>
_items.isNotEmpty ? _GalleriaGrid(_items) : placeholder;
}

class _GalleriaItem {
final BuildMetadata itemMeta;
final BuildTree itemTree;
final Galleria galleria;
final WidgetFactory wf;

Widget? _description;
BuildOp? _descriptionOp;
String? _source;
WidgetPlaceholder? _trigger;
Widget? _trigger;
BuildOp? _triggerOp;

_GalleriaItem(this.wf, this.galleria, this.itemMeta);
_GalleriaItem(this.wf, this.galleria, this.itemTree);

BuildOp? _itemOp;
BuildOp get op {
return _itemOp ??= BuildOp(
onChild: onChild,
onWidgets: onWidgets,
onVisitChild: onVisitChild,
onRenderBlock: onRenderBlock,
);
}

void onChild(BuildMetadata childMeta) {
final e = childMeta.element;
if (e.parent != itemMeta.element) return;
void onVisitChild(BuildTree _, BuildTree subSubTree) {
final e = subSubTree.element;
if (e.parent != itemTree.element) return;

switch (e.className) {
case 'LbTrigger':
_source ??= wf.urlFull(e.attributes['href'] ?? '');
final triggerOp = _triggerOp ??= BuildOp(
onWidgets: (meta, widgets) {
// bypass built-in A tag handling with 0 priority
// and NOT returning anything in `onWidgets`
_trigger = wf.buildColumnPlaceholder(meta, widgets);
return [];
},
priority: 0,
alwaysRenderBlock: true,
onRenderedBlock: (_, block) => _trigger = block,
);
childMeta.register(triggerOp);
subSubTree.register(triggerOp);
break;
case 'Tinhte_Gallery_Description':
final descriptionOp = _descriptionOp ??= BuildOp(
onWidgets: (meta, widgets) {
meta.tsb.enqueue((p, dynamic _) => p.copyWith(
style: p
.getDependency<ThemeData>()
.textTheme
.bodySmall
?.copyWith(color: kCaptionColor)));
_description = wf.buildColumnPlaceholder(meta, widgets);
return [];
alwaysRenderBlock: true,
onParsed: (descriptionTree) {

Check warning on line 71 in lib/src/widgets/html/galleria.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/widgets/html/galleria.dart#L71

Added line #L71 was not covered by tests
return descriptionTree
..inheritanceResolvers.enqueue<BuildContext?>(
(style, context) => style.copyWith(
style: Theme.of(context!)
.textTheme
.bodySmall
?.copyWith(color: kCaptionColor),

Check warning on line 78 in lib/src/widgets/html/galleria.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/widgets/html/galleria.dart#L73-L78

Added lines #L73 - L78 were not covered by tests
),
null,
);
},
onRenderedBlock: (_, block) => _description = block,

Check warning on line 83 in lib/src/widgets/html/galleria.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/widgets/html/galleria.dart#L83

Added line #L83 was not covered by tests
);
childMeta.register(descriptionOp);
subSubTree.register(descriptionOp);

Check warning on line 85 in lib/src/widgets/html/galleria.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/widgets/html/galleria.dart#L85

Added line #L85 was not covered by tests
break;
}
}

Iterable<Widget> onWidgets(
BuildMetadata _, Iterable<WidgetPlaceholder> widgets) {
Widget onRenderBlock(BuildTree _, WidgetPlaceholder placeholder) {
final scopedSource = _source;
final scopedTrigger = _trigger;
if (scopedSource == null || scopedTrigger == null) return widgets;
if (scopedSource == null || scopedTrigger == null) return placeholder;

final index = galleria._lb.addSource(
LbTriggerSource.image(scopedSource),
caption: _description,
);
scopedTrigger.wrapWith((context, child) =>
galleria._lb.buildGestureDetector(context, child, index));

return [scopedTrigger];
galleria._items.add(
WidgetPlaceholder.lazy(
scopedTrigger,
debugLabel: '${itemTree.element.localName}--galleriaItem',
).wrapWith(
(context, child) =>
galleria._lb.buildGestureDetector(context, child, index),
),
);

return widget0;
}
}

Expand Down
2 changes: 2 additions & 0 deletions lib/src/widgets/html/lb_trigger.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// ignore_for_file: deprecated_member_use

part of '../html.dart';

const kCaptionColor = Colors.white70;
Expand Down
24 changes: 15 additions & 9 deletions lib/src/widgets/html/link_expander.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// ignore_for_file: deprecated_member_use

part of '../html.dart';

const kLinkExpanderSquareThumbnailSize = 120.0;
Expand Down Expand Up @@ -45,8 +47,11 @@ class LinkExpander {
break;
case 'span':
if (e.classes.contains('host')) {
childMeta.tsb.enqueue((p, dynamic _) =>
p.copyWith(style: p.style.copyWith(color: Colors.grey)));
childMeta.tsb.enqueue(
(p, dynamic _) =>
p.copyWith(style: p.style.copyWith(color: Colors.grey)),
null,
);
}
break;
}
Expand All @@ -68,9 +73,10 @@ class LinkExpander {
final a = meta.element.attributes;
final fullUrl = wf.urlFull(a['href'] ?? '');
final onTap = fullUrl != null ? wf.gestureTapCallback(fullUrl) : null;
final recognizer = wf.buildGestureRecognizer(meta, onTap: onTap);

return WidgetPlaceholder<LinkExpander>(this)
..wrapWith((context, previous) {
return WidgetPlaceholder(
builder: (context, previous) {
final decoBox =
wf.buildDecoration(meta, child, color: Theme.of(context).cardColor);
if (decoBox == null) return previous;
Expand All @@ -84,12 +90,13 @@ class LinkExpander {
);
}

if (onTap != null) {
built = wf.buildGestureDetector(meta, built, onTap) ?? built;
if (recognizer != null) {
built = wf.buildGestureDetector(meta, built, recognizer) ?? built;
}

return built;
});
},
);
}

Widget _buildSquare(Widget left, Widget right) => _buildBox(
Expand Down Expand Up @@ -196,8 +203,7 @@ class _LinkExpanderInfo {
Iterable<Widget> onWidgets(
BuildMetadata _, Iterable<WidgetPlaceholder> widgets) {
final scopedDescription = _description;
final widget = le._info = WidgetPlaceholder<_LinkExpanderInfo>(
this,
final widget = le._info = WidgetPlaceholder(
child: Padding(
padding: const EdgeInsets.symmetric(vertical: kPostBodyPadding),
child: LayoutBuilder(
Expand Down
63 changes: 40 additions & 23 deletions lib/src/widgets/html/photo_compare.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,58 @@ class PhotoCompare {

BuildOp get buildOp => BuildOp(
defaultStyles: (_) => {'margin': '0.5em 0'},
onChild: (childMeta) {
if (childMeta.element.localName == 'img') {
childMeta['display'] = 'block';
onVisitChild: (tree, subTree) {
if (subTree.element.localName == 'img') {
subTree.register(
BuildOp(
onRenderBlock: (_, placeholder) {
final value = tree.getNonInherited<_Images>();
if (value == null) {
tree.setNonInherited(_Images([placeholder]));
} else {
value.widgets.add(placeholder);
}
return widget0;
},
priority: Priority.tagImg + 1,
),
);
}
},
onWidgets: (meta, widgets) {
final images = <Widget>[];
for (final widget in widgets) {
if (widget is WidgetPlaceholder<ImageMetadata>) {
images.add(widget);
}
}
onParsed: (tree) {
final replacement = tree.parent.sub();
final images = tree.getNonInherited<_Images>()?.widgets;
if (images == null || images.length != 2) return tree;

if (images.length != 2) return widgets;

final a = meta.element.attributes;
final a = tree.element.attributes;
final configJson = a['data-config'] ?? '';
if (configJson.isEmpty) return widgets;
if (configJson.isEmpty) return tree;

final Map config = json.decode(configJson);
final width = (config['width'] as num?)?.toDouble();
final height = (config['height'] as num?)?.toDouble();
if (width == null || height == null) return widgets;

return [
_PhotoCompareWidget(
aspectRatio: width / height,
image0: images[0],
image1: images[1],
),
];
if (width == null || height == null) return tree;

return replacement
..append(
WidgetBit.block(
tree.parent,
_PhotoCompareWidget(
aspectRatio: width / height,
image0: images[0],
image1: images[1],
),
),
);
},
);
}

class _Images {
final List<Widget> widgets;
const _Images(this.widgets);
}

class _PhotoCompareWidget extends StatefulWidget {
final double aspectRatio;
final Widget image0;
Expand Down
Loading
Loading