Files
shumai/packages/webui/components/compare/compare-utils.ts
T
yiling 6c3d3ea2b3 feat(webui): add compare versions view for version stacks
Add a Compare Versions mode to the file detail page (authenticated and
public share) that splits the viewer into two 50/50 panes showing two
versions side by side.

- Breadcrumb file menu gains a Compare Versions entry (version stacks
  with >=2 versions); the file segment is hidden in compare mode.
- URL-driven state (compare/cmpLeft/cmpRight/cmpActive) on both file
  routes so the view is refresh/share-safe.
- Per-side topbar with filename, version, version-switch dropdown, an
  active-side highlight, and an X exit button.
- Shared control bar reflects the active pane; for same-type media it
  coordinates both panes (play/pause, seek, frame-step, zoom, pan sync),
  seekbar length matches the active side while the shorter side holds its
  last frame; only the active side plays audio; resolution/download apply
  to the active side; fullscreen covers the whole compare view.
- Right sidebar (comments/fields) binds to the active side; comment
  selection seeks the active pane and annotations/comments can be created.
- Reuses useFramePlayer + DrawingCanvas without modifying the existing
  single-file video player.
- Unit tests for compare selection/clamp logic.
2026-07-04 10:20:16 +08:00

38 lines
1.3 KiB
TypeScript

import type { PaneKind } from './types'
/** Classify an asset's media type into a compare pane kind. */
export function kindOf(mediaType: string | null | undefined): PaneKind {
if (!mediaType) return 'unsupported'
if (mediaType.startsWith('image/')) return 'image'
if (mediaType.startsWith('video/')) return 'video'
return 'unsupported'
}
interface VersionLike {
id: string
version: number
}
/**
* Given a version stack's versions, pick the default compare pair:
* left = latest (highest version number), right = latest - 1.
* Returns null if fewer than two versions exist.
*/
export function pickDefaultCompareVersions(
versions: ReadonlyArray<VersionLike> | undefined | null,
): { left: VersionLike; right: VersionLike } | null {
if (!versions || versions.length < 2) return null
const sorted = [...versions].sort((a, b) => b.version - a.version)
return { left: sorted[0], right: sorted[1] }
}
/** Clamp a frame index into the valid range [0, totalFrames - 1]. */
export function clampFrame(frame: number, totalFrames: number): number {
return Math.max(0, Math.min(frame, Math.max(0, totalFrames - 1)))
}
/** True when both panes hold the same (non-unsupported) media kind. */
export function isSameType(left: PaneKind, right: PaneKind): boolean {
return left === right && left !== 'unsupported'
}