Page through a folder's files in the file viewer
Opening a file from the Files panel now passes the folder's previewable files in their shown order, so the arrows, swipes and Previous/Next step through them and Escape closes the viewer. Download follows the file on screen.
This commit is contained in:
@@ -63,6 +63,10 @@ class ArtifactViewerPage extends StatefulWidget {
|
|||||||
final bool includeExternalDocuments;
|
final bool includeExternalDocuments;
|
||||||
|
|
||||||
final String configuredEndpoint;
|
final String configuredEndpoint;
|
||||||
|
|
||||||
|
/// Where Download fetches the shown file. An artifact whose `raw` carries
|
||||||
|
/// `download_url` (the Files panel's per-file links) overrides it, so paging
|
||||||
|
/// through a folder downloads the file on screen.
|
||||||
final String? downloadUrl;
|
final String? downloadUrl;
|
||||||
|
|
||||||
/// The preview rule; the Files panel passes its own (every HTML previews).
|
/// The preview rule; the Files panel passes its own (every HTML previews).
|
||||||
@@ -235,9 +239,13 @@ class _ArtifactViewerPageState extends State<ArtifactViewerPage>
|
|||||||
Widget buildFrame(BuildContext context, {required bool fullscreen}) {
|
Widget buildFrame(BuildContext context, {required bool fullscreen}) {
|
||||||
final page = _page;
|
final page = _page;
|
||||||
final plan = _plan;
|
final plan = _plan;
|
||||||
final downloadResolved = widget.downloadUrl == null
|
final pageDownload = page?.artifact.raw['download_url'];
|
||||||
|
final download = pageDownload is String && pageDownload.isNotEmpty
|
||||||
|
? pageDownload
|
||||||
|
: widget.downloadUrl;
|
||||||
|
final downloadResolved = download == null
|
||||||
? null
|
? null
|
||||||
: resolveArtifactUrl(widget.configuredEndpoint, widget.downloadUrl!);
|
: resolveArtifactUrl(widget.configuredEndpoint, download);
|
||||||
final title = page == null
|
final title = page == null
|
||||||
? artifactViewerTitle
|
? artifactViewerTitle
|
||||||
: artifactDisplayName(page.artifact);
|
: artifactDisplayName(page.artifact);
|
||||||
|
|||||||
@@ -217,7 +217,12 @@ class _FilesPanelState extends State<FilesPanel> {
|
|||||||
if (widget.onOpen != null) {
|
if (widget.onOpen != null) {
|
||||||
widget.onOpen!(entry);
|
widget.onOpen!(entry);
|
||||||
} else {
|
} else {
|
||||||
widget.viewer.open(context, endpoint: _endpoint, entry: entry);
|
widget.viewer.open(
|
||||||
|
context,
|
||||||
|
endpoint: _endpoint,
|
||||||
|
entry: entry,
|
||||||
|
siblings: widget.controller.entries,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,10 +37,14 @@ class FilesViewer {
|
|||||||
final ArtifactExternalOpen openExternal;
|
final ArtifactExternalOpen openExternal;
|
||||||
final FileUrlLauncher launch;
|
final FileUrlLauncher launch;
|
||||||
|
|
||||||
|
/// Opens [entry]. [siblings] are the folder's entries in the order shown;
|
||||||
|
/// the viewer then steps through every previewable one with the arrows,
|
||||||
|
/// swipes and Previous/Next, and Escape closes it.
|
||||||
Future<void> open(
|
Future<void> open(
|
||||||
BuildContext context, {
|
BuildContext context, {
|
||||||
required String endpoint,
|
required String endpoint,
|
||||||
required DirectoryEntry entry,
|
required DirectoryEntry entry,
|
||||||
|
List<DirectoryEntry> siblings = const [],
|
||||||
}) async {
|
}) async {
|
||||||
final urls = resolveFileUrls(endpoint, entry.path);
|
final urls = resolveFileUrls(endpoint, entry.path);
|
||||||
if (urls == null) {
|
if (urls == null) {
|
||||||
@@ -62,18 +66,28 @@ class FilesViewer {
|
|||||||
_showSheet(context, entry, urls);
|
_showSheet(context, entry, urls);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
final artifact = ArtifactRecord(
|
ArtifactRecord? record(DirectoryEntry file) {
|
||||||
name: entry.name,
|
if (file.isDir || !isPreviewableFile(file.name)) return null;
|
||||||
url: urls.view.toString(),
|
final links = resolveFileUrls(endpoint, file.path);
|
||||||
contentType: type,
|
if (links == null) return null;
|
||||||
sizeBytes: entry.size ?? 0,
|
final kind = fileContentType(file.name);
|
||||||
raw: const {},
|
if (artifactKind(kind, file.name) == ArtifactKind.pdf) return null;
|
||||||
);
|
return ArtifactRecord(
|
||||||
final group = ArtifactGroup(
|
name: file.name,
|
||||||
stageKey: 'Files',
|
url: links.view.toString(),
|
||||||
visit: 1,
|
contentType: kind,
|
||||||
artifacts: [artifact],
|
sizeBytes: file.size ?? 0,
|
||||||
);
|
raw: {'download_url': links.download.toString()},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
final artifact = record(entry)!;
|
||||||
|
final pages = [
|
||||||
|
for (final file in siblings)
|
||||||
|
if (file.path == entry.path) artifact else ?record(file),
|
||||||
|
];
|
||||||
|
if (!pages.contains(artifact)) pages.add(artifact);
|
||||||
|
final group = ArtifactGroup(stageKey: 'Files', visit: 1, artifacts: pages);
|
||||||
final page = ArtifactViewerPage(
|
final page = ArtifactViewerPage(
|
||||||
groups: [group],
|
groups: [group],
|
||||||
selectedIdentity: artifactVisitIdentityOf(group, artifact),
|
selectedIdentity: artifactVisitIdentityOf(group, artifact),
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter/services.dart';
|
||||||
import 'package:flutter/cupertino.dart';
|
import 'package:flutter/cupertino.dart';
|
||||||
import 'package:pipelines_app/features/chat/files/files_preview.dart';
|
import 'package:pipelines_app/features/chat/files/files_preview.dart';
|
||||||
import 'package:pipelines_app/features/artifacts/domain/artifact_kind.dart';
|
import 'package:pipelines_app/features/artifacts/domain/artifact_kind.dart';
|
||||||
@@ -324,4 +325,45 @@ void main() {
|
|||||||
expect(find.text("Couldn't load this folder."), findsOneWidget);
|
expect(find.text("Couldn't load this folder."), findsOneWidget);
|
||||||
expect(find.textContaining('internal transport'), findsNothing);
|
expect(find.textContaining('internal transport'), findsNothing);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
testWidgets('arrows step through the folder\'s previewable files and '
|
||||||
|
'Escape closes the viewer', (tester) async {
|
||||||
|
final fetched = <String>[];
|
||||||
|
await pump(
|
||||||
|
tester,
|
||||||
|
viewer: FilesViewer(
|
||||||
|
fetch: (url) async {
|
||||||
|
fetched.add(url.queryParameters['path'] ?? '');
|
||||||
|
return ArtifactFetchResult(
|
||||||
|
status: 200,
|
||||||
|
bytes: url.queryParameters['path']!.endsWith('.png')
|
||||||
|
? base64Decode(
|
||||||
|
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==',
|
||||||
|
)
|
||||||
|
: utf8.encode('A text file preview'),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
);
|
||||||
|
Finder shown(String name) => find.descendant(
|
||||||
|
of: find.byType(ArtifactViewerPage),
|
||||||
|
matching: find.text(name),
|
||||||
|
);
|
||||||
|
await tester.tap(find.text('notes.txt'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(find.byType(ArtifactViewerPage), findsOneWidget);
|
||||||
|
expect(shown('notes.txt'), findsWidgets);
|
||||||
|
// The zip and PDF are not previewable, so the next file is the image.
|
||||||
|
await tester.sendKeyEvent(LogicalKeyboardKey.arrowRight);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(shown('picture.png'), findsWidgets);
|
||||||
|
expect(fetched, contains('/home/demo/picture.png'));
|
||||||
|
await tester.sendKeyEvent(LogicalKeyboardKey.arrowLeft);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(shown('notes.txt'), findsWidgets);
|
||||||
|
await tester.sendKeyEvent(LogicalKeyboardKey.escape);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(find.byType(ArtifactViewerPage), findsNothing);
|
||||||
|
expect(find.text('notes.txt'), findsOneWidget);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user