Skip to content
Merged
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
61 changes: 25 additions & 36 deletions lib/screens/home_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -276,19 +276,11 @@ class HomeScreenState extends State<HomeScreen> with TickerProviderStateMixin {
});
}

Future<void> _handleRefresh() async {
final htmlService = Provider.of<HtmlService>(context, listen: false);
await htmlService.refresh(switchToTab: _tabController.index);
}

Widget _buildRefreshable(Widget child, String tag) {
Widget _buildTabPage(Widget child, String tag) {
final page = TabPageWrapper(
tag: tag,
focusNode: _tabFocusNode(tag),
child: RefreshIndicator(
onRefresh: _handleRefresh,
child: child,
),
child: child,
);
// Analysis tabs get the disclaimer; browser, reader, source and DOM tree
// show the page itself.
Expand All @@ -313,31 +305,28 @@ class HomeScreenState extends State<HomeScreen> with TickerProviderStateMixin {
);
}

Widget _buildScrollableRefreshable(Widget child, String tag,
Widget _buildScrollableTabPage(Widget child, String tag,
{bool hasScrollBody = true, bool useSliverToBoxAdapter = false}) {
return TabPageWrapper(
tag: tag,
focusNode: _tabFocusNode(tag),
child: RefreshIndicator(
onRefresh: _handleRefresh,
child: CustomScrollView(
shrinkWrap: false,
physics: const AlwaysScrollableScrollPhysics(),
slivers: [
if (useSliverToBoxAdapter)
SliverToBoxAdapter(child: child)
else if (hasScrollBody)
SliverFillRemaining(
hasScrollBody: true,
child: child,
)
else
SliverFillRemaining(
hasScrollBody: false,
child: child,
),
],
),
child: CustomScrollView(
shrinkWrap: false,
physics: const AlwaysScrollableScrollPhysics(),
slivers: [
if (useSliverToBoxAdapter)
SliverToBoxAdapter(child: child)
else if (hasScrollBody)
SliverFillRemaining(
hasScrollBody: true,
child: child,
)
else
SliverFillRemaining(
hasScrollBody: false,
child: child,
),
],
),
);
}
Expand All @@ -350,15 +339,15 @@ class HomeScreenState extends State<HomeScreen> with TickerProviderStateMixin {
final sourceView = currentFile != null
? KeepAliveWrapper(
key: const ValueKey('tab_source'),
child: _buildRefreshable(
child: _buildTabPage(
SourceViewer(
key: const ValueKey('file_viewer_source'),
file: currentFile,
),
'source',
),
)
: _buildScrollableRefreshable(
: _buildScrollableTabPage(
Center(
key: const ValueKey('tab_source_empty'),
child: htmlService.isLoading
Expand All @@ -373,7 +362,7 @@ class HomeScreenState extends State<HomeScreen> with TickerProviderStateMixin {
focusNode: _tabFocusNode('browser'),
isBrowserTab: true,
child: KeepAliveWrapper(
child: _buildRefreshable(
child: _buildTabPage(
(currentFile != null &&
(currentFile.isUrl ||
isHtmlOrXml ||
Expand All @@ -398,7 +387,7 @@ class HomeScreenState extends State<HomeScreen> with TickerProviderStateMixin {

final readerView = KeepAliveWrapper(
key: const ValueKey('tab_reader'),
child: _buildRefreshable(
child: _buildTabPage(
ReaderView(
gestureRecognizers: {
Factory<VerticalDragGestureRecognizer>(
Expand All @@ -412,7 +401,7 @@ class HomeScreenState extends State<HomeScreen> with TickerProviderStateMixin {

Widget page(AppTab tab, Widget view) => KeepAliveWrapper(
key: ValueKey('tab_${tab.tag}'),
child: _buildRefreshable(view, tab.tag));
child: _buildTabPage(view, tab.tag));

return [
for (final tab in _tabs)
Expand Down
67 changes: 67 additions & 0 deletions lib/services/derive/dom_tree.dart
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,70 @@ DomTree buildDomTreeRecord((String, DomTreeKind) args) =>

typedef DomTreeBuilder = Future<DomTree> Function(
String text, DomTreeKind kind);

/// What to reveal in the DOM tree: an element found by tag/id/classes plus
/// its occurrence among same-signature elements in document order (most
/// elements have no id, so the signature alone is not always unique).
class DomReveal {
final String tag, id, classes;
final int occurrence;
const DomReveal(
{required this.tag, required this.id, required this.classes, required this.occurrence});

@override
bool operator ==(Object other) =>
other is DomReveal &&
other.tag == tag &&
other.id == id &&
other.classes == classes &&
other.occurrence == occurrence;

@override
int get hashCode => Object.hash(tag, id, classes, occurrence);
}

bool _nodeMatches(DomTreeNode n, DomReveal r) {
if (!n.isElement) return false;
if (r.tag.isNotEmpty && n.label.toLowerCase() != r.tag.toLowerCase()) {
return false;
}
if (r.id.isNotEmpty && n.attributes['id'] != r.id) return false;
if (r.classes.isNotEmpty) {
final have = (n.attributes['class'] ?? '').split(RegExp(r'\s+'));
if (!r.classes.split(RegExp(r'\s+')).every(have.contains)) return false;
}
return true;
}

/// The [r.occurrence]-th (0-based) element in document order that matches
/// [r], searched in the body subtree when there is one (layout blocks come
/// from body); null when there is no such node.
DomTreeNode? findNodeBySignature(List<DomTreeNode> roots, DomReveal r) {
var start = roots;
for (final root in roots) {
for (final c in root.children) {
if (c.label.toLowerCase() == 'body') {
start = [c];
break;
}
}
}
var n = 0;
DomTreeNode? found;
void walk(List<DomTreeNode> nodes) {
for (final node in nodes) {
if (found != null) return;
if (_nodeMatches(node, r)) {
if (n == r.occurrence) {
found = node;
return;
}
n++;
}
walk(node.children);
}
}

walk(start);
return found;
}
58 changes: 53 additions & 5 deletions lib/services/derive/structure_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,14 @@ class StructureModel {
});
}

const double kMaxWireframeHeight = 6000;
/// The wireframe is never drawn wider than this, even when the page is.
const double kMaxWireframeWidth = 1000;

/// Scale from page pixels to logical pixels: fits [availableWidth], and
/// never lets the drawing exceed [kMaxWireframeHeight].
double wireframeScale(PageLayout l, double availableWidth) {
/// Scale from page pixels to logical pixels: 1:1, so the drawing is as wide
/// as the page itself, capped at [kMaxWireframeWidth].
double wireframeScale(PageLayout l) {
if (l.width <= 0 || l.height <= 0) return 1;
return math.min(availableWidth / l.width, kMaxWireframeHeight / l.height);
return math.min(1.0, kMaxWireframeWidth / l.width);
}

/// The smallest block that contains the page point; null when none does.
Expand All @@ -94,6 +95,53 @@ int? lineOfId(String source, String id) {
return '\n'.allMatches(source.substring(0, m.start)).length + 1;
}

/// Two blocks have the same signature when tag, id and classes match; the
/// signature plus the occurrence index identifies an element even without a
/// unique id.
bool sameBlockSignature(LayoutBlock a, LayoutBlock b) =>
a.tag == b.tag && a.id == b.id && a.classes == b.classes;

/// 0-based index of [target] among [blocks] (document order) that share its
/// signature.
int occurrenceOfBlock(List<LayoutBlock> blocks, LayoutBlock target) {
var n = 0;
for (final b in blocks) {
if (identical(b, target)) return n;
if (sameBlockSignature(b, target)) n++;
}
return 0;
}

/// 1-based line of the [occurrence]-th (0-based) opening tag in [source] that
/// matches [tag] and carries [id] (when non-empty) and all of [classes] (when
/// non-empty); null when there is no such tag.
int? lineOfSignature(String source,
{required String tag, String id = '', String classes = '', int occurrence = 0}) {
final tagRe = RegExp('<${RegExp.escape(tag)}\\b', caseSensitive: false);
final idRe = id.isEmpty
? null
: RegExp('\\bid\\s*=\\s*(?:"${RegExp.escape(id)}"|\'${RegExp.escape(id)}\')');
final classRe = RegExp('\\bclass\\s*=\\s*(?:"([^"]*)"|\'([^\']*)\')');
final wanted = classes.isEmpty ? const <String>[] : classes.split(RegExp(r'\s+'));
var n = 0;
for (final m in tagRe.allMatches(source)) {
final gt = source.indexOf('>', m.start);
if (gt < 0) continue;
final tagText = source.substring(m.start, gt);
if (idRe != null && !idRe.hasMatch(tagText)) continue;
if (wanted.isNotEmpty) {
final cm = classRe.firstMatch(tagText);
final have = (cm?.group(1) ?? cm?.group(2) ?? '').split(RegExp(r'\s+'));
if (!wanted.every(have.contains)) continue;
}
if (n == occurrence) {
return '\n'.allMatches(source.substring(0, m.start)).length + 1;
}
n++;
}
return null;
}

const _notInBrowser = Unavailable<StructureModel>(
UnavailableReason.notApplicable, 'only for pages loaded in the browser');

Expand Down
Loading
Loading