ff0f812e00
Pure Dart A1a artifact domain slice mirroring web/src/artifacts.js and artifactNavigation.js: record parsing, capture facts, stage/visit grouping, public attribution, kind vocabulary, summaries, viewer paging and safe artifact url resolution. Tests mirror the domain directory; nothing is wired into a screen or transport.
77 lines
2.6 KiB
Dart
77 lines
2.6 KiB
Dart
import 'artifact_groups.dart';
|
|
import 'artifact_kind.dart';
|
|
import 'artifact_record.dart';
|
|
|
|
/// One artifact in the viewer's filtered page list, with the stage visit it
|
|
/// belongs to. [identity] is the recorded url when there is one; otherwise it is
|
|
/// a visit-scoped key so two url-less records never collapse into one page.
|
|
class PagedArtifact {
|
|
const PagedArtifact({
|
|
required this.stageKey,
|
|
required this.visit,
|
|
required this.artifact,
|
|
required this.pageIndex,
|
|
});
|
|
|
|
final String stageKey;
|
|
final int visit;
|
|
final ArtifactRecord artifact;
|
|
|
|
/// Position in the filtered page list.
|
|
final int pageIndex;
|
|
|
|
String get identity => artifact.hasUrl
|
|
? artifact.url
|
|
: '$stageKey\u0000$visit\u0000$pageIndex\u0000${artifact.name}';
|
|
}
|
|
|
|
/// The artifacts the viewer can page through, in gallery order: group by group,
|
|
/// each group's images, videos and markdown/text documents in recorded order.
|
|
/// Other artifacts open externally and are not part of this list. Pure: it only
|
|
/// filters and orders records the gallery already holds.
|
|
List<PagedArtifact> pagedArtifacts(List<ArtifactGroup> groups) {
|
|
final pages = <PagedArtifact>[];
|
|
for (final group in groups) {
|
|
for (final artifact in group.artifacts) {
|
|
if (!isViewerArtifact(artifact.contentType, artifact.name)) continue;
|
|
pages.add(
|
|
PagedArtifact(
|
|
stageKey: group.stageKey,
|
|
visit: group.visit,
|
|
artifact: artifact,
|
|
pageIndex: pages.length,
|
|
),
|
|
);
|
|
}
|
|
}
|
|
return List<PagedArtifact>.unmodifiable(pages);
|
|
}
|
|
|
|
/// Each page's recorded url mapped to its page index, so a tile and the viewer
|
|
/// derive from the same ordered list. The first occurrence of a url wins. A
|
|
/// record with no recorded url has no fetchable identity and is omitted.
|
|
Map<String, int> viewerIndexByUrl(List<ArtifactGroup> groups) {
|
|
final indices = <String, int>{};
|
|
for (final page in pagedArtifacts(groups)) {
|
|
if (!page.artifact.hasUrl) continue;
|
|
indices.putIfAbsent(page.artifact.url, () => page.pageIndex);
|
|
}
|
|
return indices;
|
|
}
|
|
|
|
/// Paging from [current] by [delta] within `count` pages, clamped at both ends
|
|
/// so a page never wraps and an empty list is index 0.
|
|
int pagedIndex(int current, int delta, int count) {
|
|
if (count <= 0) return 0;
|
|
final wanted = current + delta;
|
|
if (wanted < 0) return 0;
|
|
if (wanted >= count) return count - 1;
|
|
return wanted;
|
|
}
|
|
|
|
/// The viewer's position label, e.g. `2 of 5 · image/png`.
|
|
String artifactViewerPositionLabel(int index, int count, String contentType) {
|
|
final type = contentType.split(';').first.trim();
|
|
return '${index + 1} of $count · ${type.isEmpty ? 'artifact' : type}';
|
|
}
|