merge(app): land HTML preview
This commit is contained in:
@@ -198,11 +198,12 @@ bool isMarkdownArtifact(String contentType, String name) {
|
||||
|
||||
/// How the viewer presents an artifact in-app, or [none] when it only offers
|
||||
/// Open/Download.
|
||||
enum ArtifactPreviewFormat { image, video, markdown, text, table, none }
|
||||
enum ArtifactPreviewFormat { image, video, markdown, text, table, html, none }
|
||||
|
||||
/// The one rule for in-app previews, shared by the gallery's paging and the
|
||||
/// viewer's fetch plan. Images and videos come from the content type; markdown
|
||||
/// from `text/markdown` or an md/markdown name; CSV/TSV as a table; plain
|
||||
/// from `text/markdown` or an md/markdown name; HTML rendered inertly;
|
||||
/// CSV/TSV as a table; plain
|
||||
/// text, logs, JSON and source/config files as selectable monospace text.
|
||||
ArtifactPreviewFormat artifactPreviewFormat(String contentType, String name) {
|
||||
if (isImageContentType(contentType)) return ArtifactPreviewFormat.image;
|
||||
@@ -212,6 +213,15 @@ ArtifactPreviewFormat artifactPreviewFormat(String contentType, String name) {
|
||||
if (mime == 'text/markdown' || ext == 'md' || ext == 'markdown') {
|
||||
return ArtifactPreviewFormat.markdown;
|
||||
}
|
||||
if (mime == 'text/html' || ext == 'html' || ext == 'htm') {
|
||||
// A turn's session export (and its repairs) stays external-only, opened
|
||||
// in its own tab as on the web; other HTML documents preview inertly.
|
||||
final lower = name.toLowerCase();
|
||||
if (lower == 'session.html' || lower.startsWith('session-repair-')) {
|
||||
return ArtifactPreviewFormat.none;
|
||||
}
|
||||
return ArtifactPreviewFormat.html;
|
||||
}
|
||||
if (mime == 'text/csv' ||
|
||||
mime == 'text/tab-separated-values' ||
|
||||
ext == 'csv' ||
|
||||
|
||||
@@ -15,6 +15,9 @@ enum ArtifactPreviewKind {
|
||||
/// A CSV/TSV drawn as a table from its fetched text.
|
||||
table,
|
||||
|
||||
/// An HTML document rendered inertly (no scripts, no navigation).
|
||||
html,
|
||||
|
||||
/// A recorded file the viewer cannot draw (archives, PDFs, ...).
|
||||
unsupported,
|
||||
|
||||
@@ -53,7 +56,8 @@ class ArtifactPreviewPlan {
|
||||
(kind == ArtifactPreviewKind.image ||
|
||||
kind == ArtifactPreviewKind.text ||
|
||||
kind == ArtifactPreviewKind.markdown ||
|
||||
kind == ArtifactPreviewKind.table);
|
||||
kind == ArtifactPreviewKind.table ||
|
||||
kind == ArtifactPreviewKind.html);
|
||||
}
|
||||
|
||||
const String artifactPreviewNoUrl = 'No artifact URL recorded.';
|
||||
@@ -103,6 +107,7 @@ ArtifactPreviewPlan planArtifactPreview({
|
||||
ArtifactPreviewFormat.markdown => ArtifactPreviewKind.markdown,
|
||||
ArtifactPreviewFormat.text => ArtifactPreviewKind.text,
|
||||
ArtifactPreviewFormat.table => ArtifactPreviewKind.table,
|
||||
ArtifactPreviewFormat.html => ArtifactPreviewKind.html,
|
||||
ArtifactPreviewFormat.none => null,
|
||||
};
|
||||
if (kind != null) {
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
import 'artifact_html_view_native.dart'
|
||||
if (dart.library.js_interop) 'artifact_html_view_web.dart'
|
||||
as impl;
|
||||
|
||||
/// Builds the rendered HTML preview; tests inject a plain stand-in so no
|
||||
/// platform view is created.
|
||||
typedef ArtifactHtmlBuilder =
|
||||
Widget Function(BuildContext context, String html);
|
||||
|
||||
/// Renders a recorded HTML document inertly: on the web in an iframe whose
|
||||
/// empty `sandbox` blocks scripts, forms, popups and navigation; on Android and
|
||||
/// iOS in a web view with JavaScript disabled and navigation refused. The
|
||||
/// document never runs code with the app's origin or credentials.
|
||||
Widget defaultArtifactHtml(BuildContext context, String html) => SizedBox(
|
||||
key: const Key('artifact-html-preview'),
|
||||
height: MediaQuery.sizeOf(context).height * 0.7,
|
||||
child: impl.InertHtmlView(html: html),
|
||||
);
|
||||
@@ -0,0 +1,36 @@
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:webview_flutter/webview_flutter.dart';
|
||||
|
||||
/// A web view with JavaScript disabled that refuses every navigation after the
|
||||
/// recorded document itself loads.
|
||||
class InertHtmlView extends StatefulWidget {
|
||||
const InertHtmlView({super.key, required this.html});
|
||||
|
||||
final String html;
|
||||
|
||||
@override
|
||||
State<InertHtmlView> createState() => _InertHtmlViewState();
|
||||
}
|
||||
|
||||
class _InertHtmlViewState extends State<InertHtmlView> {
|
||||
late final WebViewController _controller = WebViewController()
|
||||
..setJavaScriptMode(JavaScriptMode.disabled)
|
||||
..setNavigationDelegate(
|
||||
NavigationDelegate(
|
||||
onNavigationRequest: (request) =>
|
||||
request.url.startsWith('about:') || request.url.startsWith('data:')
|
||||
? NavigationDecision.navigate
|
||||
: NavigationDecision.prevent,
|
||||
),
|
||||
)
|
||||
..loadHtmlString(widget.html);
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant InertHtmlView oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (oldWidget.html != widget.html) _controller.loadHtmlString(widget.html);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => WebViewWidget(controller: _controller);
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import 'dart:js_interop';
|
||||
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:web/web.dart' as web;
|
||||
|
||||
/// A sandboxed iframe: the empty `sandbox` attribute applies every
|
||||
/// restriction, so the document cannot run script or leave the frame.
|
||||
class InertHtmlView extends StatelessWidget {
|
||||
const InertHtmlView({super.key, required this.html});
|
||||
|
||||
final String html;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => HtmlElementView.fromTagName(
|
||||
key: ValueKey<int>(html.hashCode),
|
||||
tagName: 'iframe',
|
||||
onElementCreated: (element) {
|
||||
final frame = element as web.HTMLIFrameElement;
|
||||
frame.setAttribute('sandbox', '');
|
||||
frame.srcdoc = html.toJS;
|
||||
frame.style
|
||||
..border = 'none'
|
||||
..width = '100%'
|
||||
..height = '100%'
|
||||
..backgroundColor = 'white';
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -3,6 +3,7 @@ import 'dart:typed_data';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../domain/artifact_preview.dart';
|
||||
import 'artifact_html_view.dart';
|
||||
import 'artifact_table_view.dart';
|
||||
import 'artifact_video_player.dart';
|
||||
import '../../../ui/text/monospace.dart';
|
||||
@@ -33,6 +34,7 @@ class ArtifactPreviewBody extends StatelessWidget {
|
||||
this.bytes,
|
||||
this.error,
|
||||
this.videoControllerFactory = defaultArtifactVideoController,
|
||||
this.htmlBuilder = defaultArtifactHtml,
|
||||
});
|
||||
|
||||
final ArtifactPreviewPlan plan;
|
||||
@@ -45,6 +47,9 @@ class ArtifactPreviewBody extends StatelessWidget {
|
||||
/// The video player's controller source; tests inject a fake.
|
||||
final ArtifactVideoControllerFactory videoControllerFactory;
|
||||
|
||||
/// The inert HTML renderer; tests inject a plain stand-in.
|
||||
final ArtifactHtmlBuilder htmlBuilder;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (plan.kind == ArtifactPreviewKind.unavailable ||
|
||||
@@ -80,6 +85,8 @@ class ArtifactPreviewBody extends StatelessWidget {
|
||||
);
|
||||
case ArtifactPreviewKind.markdown:
|
||||
return _PreviewPadding(child: markdownBuilder(context, text ?? ''));
|
||||
case ArtifactPreviewKind.html:
|
||||
return _PreviewPadding(child: htmlBuilder(context, text ?? ''));
|
||||
case ArtifactPreviewKind.table:
|
||||
return ArtifactTableView(text: text ?? '', delimiter: plan.delimiter);
|
||||
case ArtifactPreviewKind.video:
|
||||
|
||||
@@ -453,6 +453,38 @@ packages:
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "3.0.3"
|
||||
webview_flutter:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: webview_flutter
|
||||
sha256: d53e1ccf5516f25017e3c9d44c39034db352d20fa34fe200674270242c2c5111
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "4.14.1"
|
||||
webview_flutter_android:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: webview_flutter_android
|
||||
sha256: "4de8b3d1ff4ebe1bdb42e68a5e4f809194a3cb0117a8f495f590004f00da3964"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "4.14.1"
|
||||
webview_flutter_platform_interface:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: webview_flutter_platform_interface
|
||||
sha256: "1221c1b12f5278791042f2ec2841743784cf25c5a644e23d6680e5d718824f04"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "2.15.1"
|
||||
webview_flutter_wkwebview:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: webview_flutter_wkwebview
|
||||
sha256: "0f784be1b080e5b918ef7c2c070c1f4616899fd23f1d001feb74b4978f92bdbc"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "3.26.2"
|
||||
xdg_directories:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
||||
@@ -40,6 +40,7 @@ dependencies:
|
||||
sdk: flutter
|
||||
web: ^1.1.1
|
||||
video_player: ^2.14.1
|
||||
webview_flutter: ^4.14.1
|
||||
|
||||
dev_dependencies:
|
||||
flutter_test:
|
||||
|
||||
@@ -109,6 +109,14 @@ void main() {
|
||||
artifactPreviewFormat('text/markdown', 'plan'),
|
||||
ArtifactPreviewFormat.markdown,
|
||||
);
|
||||
expect(
|
||||
artifactPreviewFormat('text/html', 'report.html'),
|
||||
ArtifactPreviewFormat.html,
|
||||
);
|
||||
expect(
|
||||
artifactPreviewFormat('', 'session-repair-3-ab12.html'),
|
||||
ArtifactPreviewFormat.none,
|
||||
);
|
||||
for (final (contentType, name) in <(String, String)>[
|
||||
('text/html', 'session.html'),
|
||||
('application/zip', 'bundle.zip'),
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/artifacts/domain/artifact_preview.dart';
|
||||
import 'package:pipelines_app/features/artifacts/domain/artifact_url.dart';
|
||||
import 'package:pipelines_app/features/artifacts/ui/artifact_viewer_content.dart';
|
||||
|
||||
void main() {
|
||||
testWidgets('an HTML preview hands the fetched document to the renderer', (
|
||||
tester,
|
||||
) async {
|
||||
final rendered = <String>[];
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Scaffold(
|
||||
body: ArtifactPreviewBody(
|
||||
plan: ArtifactPreviewPlan(
|
||||
kind: ArtifactPreviewKind.html,
|
||||
resolved: ResolvedArtifactUrl(
|
||||
uri: Uri.parse('http://127.0.0.1:8080/artifacts/1/report.html'),
|
||||
external: false,
|
||||
),
|
||||
message: '',
|
||||
),
|
||||
loading: false,
|
||||
markdownBuilder: defaultArtifactMarkdown,
|
||||
text: '<h1>Report</h1>',
|
||||
htmlBuilder: (context, html) {
|
||||
rendered.add(html);
|
||||
return const Text('rendered');
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
expect(rendered, ['<h1>Report</h1>']);
|
||||
expect(find.text('rendered'), findsOneWidget);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user