Files
pipelines/app/lib/features/artifacts/ui/artifact_html_view_web.dart
T
alan 4e0481fd8d Artifact viewer: full screen view; HTML scrolls, pans and zooms; links open safely
Full screen button in the viewer's bottom bar opens a root full-screen route
with the same bar and top bar; exit by the bar, Close, Escape or back. It
renders the already fetched bytes, never the system browser. The external
Open action is now labelled Open in browser.

Native web views claim their gestures (vertical drags inline, every pointer
in full screen) so the page swipe no longer starves them. On the web, full
screen adds zoom buttons that scale the live sandboxed frame. Links: in-page
anchors scroll, http(s)/mailto open outside, everything else is inert.

Capture kit: --touch, swipe=, pinch=, tabs=; html=1 fixture artifact.
2026-11-03 18:50:41 +00:00

110 lines
3.3 KiB
Dart

import 'dart:js_interop';
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:web/web.dart' as web;
import 'artifact_html_links_web.dart';
/// The frame's document cannot be pinched: a pinch over it is a page zoom,
/// which the app's viewport (`user-scalable=no`) refuses, and the sandbox
/// keeps the document out of the app's reach. The full-screen view zooms
/// the live frame with buttons instead.
const bool inertHtmlZoomButtons = true;
/// A sandboxed iframe ([artifactHtmlSandbox]): the document cannot run
/// script, reach the app's origin, submit forms or navigate the app. Its
/// links are fixed first ([artifactHtmlWithInertLinks]): a `#place` scrolls
/// the frame, a web or mail address opens a new tab, anything else is inert.
///
/// The frame scrolls its document itself, by wheel, trackpad and touch.
/// [zoom] scales the live frame as a browser's page zoom would: the frame is
/// laid out 1/zoom as wide and tall and scaled back to fill its box, so the
/// document reflows and still scrolls inside it. In the app, Chrome returns
/// the frame's document to its top when a zoom step resizes it.
///
/// While another route covers this one (a menu, a sheet) the frame stops
/// taking pointers: a platform view takes every click over its area, even
/// where Flutter draws that menu on top.
class InertHtmlView extends StatefulWidget {
const InertHtmlView({
super.key,
required this.html,
this.interactive = false,
this.zoom,
});
final String html;
/// The full-screen view's frame. Pointers already reach the frame directly
/// on the web, inline as in full screen.
final bool interactive;
final ValueListenable<double>? zoom;
@override
State<InertHtmlView> createState() => _InertHtmlViewState();
}
class _InertHtmlViewState extends State<InertHtmlView> {
web.HTMLIFrameElement? _frame;
bool _covered = false;
@override
void initState() {
super.initState();
widget.zoom?.addListener(_style);
}
@override
void didUpdateWidget(covariant InertHtmlView oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.zoom != widget.zoom) {
oldWidget.zoom?.removeListener(_style);
widget.zoom?.addListener(_style);
_style();
}
}
@override
void dispose() {
widget.zoom?.removeListener(_style);
super.dispose();
}
void _style() {
final frame = _frame;
if (frame == null) return;
final zoom = widget.zoom?.value ?? 1;
final size = '${100 / zoom}%';
frame.style
..width = size
..height = size
..transformOrigin = '0 0'
..transform = zoom == 1 ? '' : 'scale($zoom)'
..pointerEvents = _covered ? 'none' : '';
}
@override
Widget build(BuildContext context) {
final covered = !(ModalRoute.of(context)?.isCurrent ?? true);
if (covered != _covered) {
_covered = covered;
_style();
}
return HtmlElementView.fromTagName(
key: ValueKey<int>(widget.html.hashCode),
tagName: 'iframe',
onElementCreated: (element) {
final frame = element as web.HTMLIFrameElement;
frame.setAttribute('sandbox', artifactHtmlSandbox);
frame.srcdoc = artifactHtmlWithInertLinks(widget.html).toJS;
frame.style
..border = 'none'
..backgroundColor = 'white';
_frame = frame;
_style();
},
);
}
}