This commit is contained in:
1 parent
efda65ae5f
commit
be0d4a87f5
2 files changed
+210
-59
No files matched your search
@@ -15,9 +15,11 @@
|
||||
autoplay = false,
|
||||
class: className = '',
|
||||
mediaClass = '',
|
||||
showSeekButtons = true,
|
||||
showSeekButtons: _showSeekButtons = true,
|
||||
showVolume = true,
|
||||
showFullscreen = true,
|
||||
showGraph = false,
|
||||
graphVisible = false,
|
||||
qualityOptions = [],
|
||||
selectedQuality = '',
|
||||
currentTime: externalCurrentTime = null,
|
||||
@@ -30,6 +32,8 @@
|
||||
onEnded,
|
||||
onSeek,
|
||||
onSkip,
|
||||
onQualityChange,
|
||||
onToggleGraph,
|
||||
endControls,
|
||||
}: {
|
||||
kind?: MediaPlayerKind
|
||||
@@ -39,9 +43,12 @@
|
||||
autoplay?: boolean
|
||||
class?: string
|
||||
mediaClass?: string
|
||||
/** @deprecated Seeking is available with the arrow keys and mobile double-tap. */
|
||||
showSeekButtons?: boolean
|
||||
showVolume?: boolean
|
||||
showFullscreen?: boolean
|
||||
showGraph?: boolean
|
||||
graphVisible?: boolean
|
||||
qualityOptions?: MediaPlayerQualityOption[]
|
||||
selectedQuality?: string
|
||||
currentTime?: number | null
|
||||
@@ -54,6 +61,8 @@
|
||||
onEnded?: (event: Event) => void
|
||||
onSeek?: (value: number) => void
|
||||
onSkip?: (seconds: number) => void
|
||||
onQualityChange?: (value: string) => void
|
||||
onToggleGraph?: () => void
|
||||
endControls?: Snippet
|
||||
} = $props()
|
||||
|
||||
@@ -66,7 +75,9 @@
|
||||
let muted = $state(false)
|
||||
let fullscreen = $state(false)
|
||||
let controlsVisible = $state(true)
|
||||
let orientationLocked = $state(false)
|
||||
let controlsHideTimeout: ReturnType<typeof setTimeout> | null = null
|
||||
let orientationLockRequest = 0
|
||||
|
||||
const currentTime = $derived(externalCurrentTime ?? internalTime)
|
||||
const duration = $derived(externalDuration ?? internalDuration)
|
||||
@@ -127,6 +138,60 @@
|
||||
}
|
||||
}
|
||||
|
||||
function isMobileDevice() {
|
||||
return (
|
||||
typeof window !== 'undefined' &&
|
||||
(window.matchMedia?.('(pointer: coarse)').matches === true || navigator.maxTouchPoints > 0)
|
||||
)
|
||||
}
|
||||
|
||||
function isWideVideo() {
|
||||
return Boolean(
|
||||
mediaElement &&
|
||||
!audio &&
|
||||
mediaElement instanceof HTMLVideoElement &&
|
||||
mediaElement.videoWidth > mediaElement.videoHeight,
|
||||
)
|
||||
}
|
||||
|
||||
async function syncScreenOrientation(nextFullscreen: boolean) {
|
||||
const request = ++orientationLockRequest
|
||||
const orientation =
|
||||
typeof window !== 'undefined'
|
||||
? (window.screen?.orientation as
|
||||
| (ScreenOrientation & {
|
||||
lock: (orientation: 'landscape') => Promise<void>
|
||||
})
|
||||
| undefined)
|
||||
: undefined
|
||||
const shouldLock = nextFullscreen && isMobileDevice() && isWideVideo()
|
||||
|
||||
if (!shouldLock) {
|
||||
if (orientationLocked) orientation?.unlock()
|
||||
orientationLocked = false
|
||||
return
|
||||
}
|
||||
|
||||
if (!orientation?.lock) return
|
||||
try {
|
||||
await orientation.lock('landscape')
|
||||
if (request !== orientationLockRequest) {
|
||||
orientation.unlock()
|
||||
return
|
||||
}
|
||||
orientationLocked = true
|
||||
} catch {
|
||||
// Orientation locking is not available in every mobile browser.
|
||||
}
|
||||
}
|
||||
|
||||
function handleVideoDoubleTap(event: MouseEvent) {
|
||||
if (audio || !isMobileDevice()) return
|
||||
const target = event.currentTarget as HTMLVideoElement
|
||||
const bounds = target.getBoundingClientRect()
|
||||
skip(event.clientX - bounds.left < bounds.width / 2 ? -10 : 10)
|
||||
}
|
||||
|
||||
function handlePlay(event: Event) {
|
||||
playing = true
|
||||
onPlay?.(event)
|
||||
@@ -148,6 +213,7 @@
|
||||
internalDuration = Number.isFinite(mediaElement.duration) ? mediaElement.duration : 0
|
||||
volume = mediaElement.volume
|
||||
muted = mediaElement.muted || mediaElement.volume === 0
|
||||
void syncScreenOrientation(fullscreen)
|
||||
onLoadedMetadata?.(event)
|
||||
}
|
||||
|
||||
@@ -219,6 +285,7 @@
|
||||
|
||||
function handleFullscreenChange() {
|
||||
fullscreen = Boolean(document.fullscreenElement)
|
||||
void syncScreenOrientation(fullscreen)
|
||||
}
|
||||
|
||||
$effect(() => {
|
||||
@@ -247,16 +314,18 @@
|
||||
|
||||
return () => {
|
||||
for (const eventName of activityEvents) playerFrame?.removeEventListener(eventName, handlePlayerActivity)
|
||||
orientationLockRequest += 1
|
||||
if (orientationLocked) {
|
||||
window.screen?.orientation?.unlock()
|
||||
orientationLocked = false
|
||||
}
|
||||
}
|
||||
})
|
||||
</script>
|
||||
|
||||
<svelte:window onfullscreenchange={handleFullscreenChange} />
|
||||
|
||||
<div
|
||||
bind:this={playerFrame}
|
||||
class={`media-player ${className}`}
|
||||
>
|
||||
<div bind:this={playerFrame} class={`media-player ${className}`}>
|
||||
{#if audio}
|
||||
<div
|
||||
class="media-player-audio-surface"
|
||||
@@ -294,9 +363,11 @@
|
||||
{autoplay}
|
||||
preload="auto"
|
||||
playsinline
|
||||
tabindex="0"
|
||||
class={`media-player-video ${mediaClass}`}
|
||||
aria-label={title ? `Video player for ${title}` : 'Video player'}
|
||||
onclick={handleSurfaceClick}
|
||||
ondblclick={handleVideoDoubleTap}
|
||||
onkeydown={handleKeydown}
|
||||
onplay={handlePlay}
|
||||
onpause={handlePause}
|
||||
@@ -320,15 +391,17 @@
|
||||
{controlsVisible}
|
||||
onTogglePlayback={togglePlayback}
|
||||
onSeek={seek}
|
||||
onSkip={skip}
|
||||
onToggleMute={toggleMute}
|
||||
onVolumeChange={changeVolume}
|
||||
onToggleFullscreen={toggleFullscreen}
|
||||
{showSeekButtons}
|
||||
{showVolume}
|
||||
{showFullscreen}
|
||||
{showGraph}
|
||||
{graphVisible}
|
||||
{qualityOptions}
|
||||
{selectedQuality}
|
||||
{onQualityChange}
|
||||
{onToggleGraph}
|
||||
{endControls}
|
||||
overlay
|
||||
/>
|
||||
@@ -357,6 +430,7 @@
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
object-fit: contain;
|
||||
touch-action: manipulation;
|
||||
}
|
||||
|
||||
.media-player-audio-surface {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
<script lang="ts">
|
||||
import { Activity, Maximize2, Minimize2, Pause, Play, RotateCcw, RotateCw, Volume2, VolumeX } from '@lucide/svelte'
|
||||
import { Maximize2, Minimize2, Pause, Play, Settings, Volume2, VolumeX } from '@lucide/svelte'
|
||||
import { onMount } from 'svelte'
|
||||
import type { Snippet } from 'svelte'
|
||||
|
||||
export type MediaPlayerKind = 'audio' | 'video'
|
||||
@@ -20,7 +21,7 @@
|
||||
disabled = false,
|
||||
overlay = false,
|
||||
controlsVisible = true,
|
||||
showSeekButtons = true,
|
||||
showSeekButtons: _showSeekButtons = true,
|
||||
showVolume = true,
|
||||
showFullscreen = true,
|
||||
showGraph = false,
|
||||
@@ -29,7 +30,7 @@
|
||||
selectedQuality = '',
|
||||
onTogglePlayback,
|
||||
onSeek,
|
||||
onSkip,
|
||||
onSkip: _onSkip,
|
||||
onToggleMute,
|
||||
onVolumeChange,
|
||||
onToggleFullscreen,
|
||||
@@ -47,6 +48,7 @@
|
||||
disabled?: boolean
|
||||
overlay?: boolean
|
||||
controlsVisible?: boolean
|
||||
/** @deprecated Seeking is available with the arrow keys and mobile double-tap. */
|
||||
showSeekButtons?: boolean
|
||||
showVolume?: boolean
|
||||
showFullscreen?: boolean
|
||||
@@ -56,6 +58,7 @@
|
||||
selectedQuality?: string
|
||||
onTogglePlayback?: () => void
|
||||
onSeek?: (value: number) => void
|
||||
/** @deprecated Seeking is handled by the player surface. */
|
||||
onSkip?: (seconds: number) => void
|
||||
onToggleMute?: () => void
|
||||
onVolumeChange?: (value: number) => void
|
||||
@@ -65,6 +68,9 @@
|
||||
endControls?: Snippet
|
||||
} = $props()
|
||||
|
||||
let settingsMenu = $state<HTMLDivElement | null>(null)
|
||||
let settingsOpen = $state(false)
|
||||
|
||||
const progress = $derived(duration > 0 ? `${Math.min(100, Math.max(0, (currentTime / duration) * 100))}%` : '0%')
|
||||
const displayVolume = $derived(muted ? 0 : Math.min(1, Math.max(0, volume)))
|
||||
const label = $derived(kind === 'audio' ? 'audio' : 'video')
|
||||
@@ -88,11 +94,31 @@
|
||||
|
||||
function handleQuality(event: Event) {
|
||||
onQualityChange?.((event.currentTarget as HTMLSelectElement).value)
|
||||
settingsOpen = false
|
||||
}
|
||||
|
||||
function handleDocumentPointerDown(event: PointerEvent) {
|
||||
if (settingsOpen && !settingsMenu?.contains(event.target as Node)) settingsOpen = false
|
||||
}
|
||||
|
||||
function handleDocumentKeydown(event: KeyboardEvent) {
|
||||
if (event.key === 'Escape') settingsOpen = false
|
||||
}
|
||||
|
||||
onMount(() => {
|
||||
document.addEventListener('pointerdown', handleDocumentPointerDown)
|
||||
document.addEventListener('keydown', handleDocumentKeydown)
|
||||
|
||||
return () => {
|
||||
document.removeEventListener('pointerdown', handleDocumentPointerDown)
|
||||
document.removeEventListener('keydown', handleDocumentKeydown)
|
||||
}
|
||||
})
|
||||
</script>
|
||||
|
||||
<div
|
||||
class:media-player-controls-overlay={overlay}
|
||||
class:media-player-controls-fullscreen={fullscreen}
|
||||
class:media-player-controls-hidden={!controlsVisible}
|
||||
class="media-player-controls"
|
||||
>
|
||||
@@ -120,26 +146,6 @@
|
||||
>
|
||||
{#if playing}<Pause class="media-player-icon-large" />{:else}<Play class="media-player-icon-large" />{/if}
|
||||
</button>
|
||||
{#if showSeekButtons}
|
||||
<button
|
||||
type="button"
|
||||
class="media-player-control media-player-seek-button"
|
||||
aria-label="Rewind 10 seconds"
|
||||
title="Rewind 10 seconds (Left Arrow)"
|
||||
{disabled}
|
||||
onclick={() => onSkip?.(-10)}><RotateCcw class="media-player-icon" /></button
|
||||
>
|
||||
<button
|
||||
type="button"
|
||||
class="media-player-control media-player-seek-button"
|
||||
aria-label="Skip forward 10 seconds"
|
||||
title="Skip forward 10 seconds (Right Arrow)"
|
||||
{disabled}
|
||||
onclick={() => onSkip?.(10)}><RotateCw class="media-player-icon" /></button
|
||||
>
|
||||
{/if}
|
||||
<span class="media-player-time">{formatTime(currentTime)} / {formatTime(duration)}</span>
|
||||
<div class="media-player-control-trailing">
|
||||
{#if showVolume}
|
||||
<button
|
||||
type="button"
|
||||
@@ -164,31 +170,51 @@
|
||||
oninput={handleVolume}
|
||||
/>
|
||||
{/if}
|
||||
{#if qualityOptions.length > 0}
|
||||
<select
|
||||
class="media-player-quality"
|
||||
value={selectedQuality}
|
||||
aria-label="Video quality"
|
||||
title="Video quality"
|
||||
<span class="media-player-time">{formatTime(currentTime)} / {formatTime(duration)}</span>
|
||||
<div class="media-player-control-trailing">
|
||||
{@render endControls?.()}
|
||||
{#if showGraph || qualityOptions.length > 0}
|
||||
<div bind:this={settingsMenu} class="media-player-settings">
|
||||
<button
|
||||
type="button"
|
||||
class="media-player-control"
|
||||
aria-label="Player settings"
|
||||
aria-expanded={settingsOpen}
|
||||
aria-haspopup="dialog"
|
||||
title="Player settings"
|
||||
{disabled}
|
||||
onchange={handleQuality}
|
||||
onclick={() => (settingsOpen = !settingsOpen)}
|
||||
>
|
||||
<Settings class="media-player-icon" />
|
||||
</button>
|
||||
{#if settingsOpen}
|
||||
<div class="media-player-settings-menu" role="dialog" aria-label="Player settings">
|
||||
{#if showGraph}
|
||||
<button
|
||||
type="button"
|
||||
class="media-player-settings-toggle"
|
||||
role="switch"
|
||||
aria-checked={graphVisible}
|
||||
{disabled}
|
||||
onclick={onToggleGraph}
|
||||
>
|
||||
<span>Funscript graph</span>
|
||||
<span class="media-player-settings-status">{graphVisible ? 'On' : 'Off'}</span>
|
||||
</button>
|
||||
{/if}
|
||||
{#if qualityOptions.length > 0}
|
||||
<label class="media-player-settings-quality">
|
||||
<span>Quality</span>
|
||||
<select value={selectedQuality} aria-label="Video quality" {disabled} onchange={handleQuality}>
|
||||
{#each qualityOptions as option (option.value)}
|
||||
<option value={option.value}>{option.label}</option>
|
||||
{/each}
|
||||
</select>
|
||||
</label>
|
||||
{/if}
|
||||
{@render endControls?.()}
|
||||
{#if showGraph}
|
||||
<button
|
||||
type="button"
|
||||
class="media-player-control"
|
||||
aria-label={graphVisible ? 'Hide funscript graph' : 'Show funscript graph'}
|
||||
aria-pressed={graphVisible}
|
||||
title={graphVisible ? 'Hide funscript graph' : 'Show funscript graph'}
|
||||
{disabled}
|
||||
onclick={onToggleGraph}><Activity class="media-player-icon" /></button
|
||||
>
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
{/if}
|
||||
{#if showFullscreen}
|
||||
<button
|
||||
@@ -375,20 +401,67 @@
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.media-player-quality {
|
||||
width: auto;
|
||||
max-width: 6.25rem;
|
||||
height: 2.25rem;
|
||||
.media-player-settings {
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.media-player-settings-menu {
|
||||
position: absolute;
|
||||
right: 0;
|
||||
bottom: calc(100% + 0.5rem);
|
||||
z-index: 30;
|
||||
display: grid;
|
||||
min-width: 13rem;
|
||||
gap: 0.25rem;
|
||||
padding: 0.4rem;
|
||||
border: 1px solid rgb(255 255 255 / 0.18);
|
||||
border-radius: 0.5rem;
|
||||
background: rgb(0 0 0 / 0.42);
|
||||
padding: 0 0.5rem;
|
||||
color: rgb(255 255 255 / 0.92);
|
||||
border-radius: 0.6rem;
|
||||
background: rgb(16 18 23 / 0.96);
|
||||
box-shadow: 0 0.5rem 1.5rem rgb(0 0 0 / 0.35);
|
||||
color: white;
|
||||
}
|
||||
|
||||
.media-player-settings-toggle,
|
||||
.media-player-settings-quality {
|
||||
display: flex;
|
||||
width: 100%;
|
||||
min-height: 2.25rem;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 1rem;
|
||||
border-radius: 0.4rem;
|
||||
padding: 0.35rem 0.5rem;
|
||||
color: rgb(255 255 255 / 0.9);
|
||||
font-size: 0.75rem;
|
||||
text-align: left;
|
||||
}
|
||||
|
||||
.media-player-settings-toggle {
|
||||
border: 0;
|
||||
background: transparent;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.media-player-quality option {
|
||||
.media-player-settings-toggle:hover:not(:disabled) {
|
||||
background: rgb(255 255 255 / 0.12);
|
||||
}
|
||||
|
||||
.media-player-settings-status {
|
||||
color: var(--sc-primary, #80b6f5);
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.media-player-settings-quality select {
|
||||
max-width: 7rem;
|
||||
border: 1px solid rgb(255 255 255 / 0.18);
|
||||
border-radius: 0.35rem;
|
||||
background: rgb(0 0 0 / 0.42);
|
||||
padding: 0.25rem 0.35rem;
|
||||
color: inherit;
|
||||
font-size: inherit;
|
||||
}
|
||||
|
||||
.media-player-settings-quality option {
|
||||
background: var(--sc-surface, #1b1f27);
|
||||
color: var(--sc-fg, white);
|
||||
}
|
||||
@@ -396,7 +469,8 @@
|
||||
.media-player-control:focus-visible,
|
||||
.media-player-progress:focus-visible,
|
||||
.media-player-volume:focus-visible,
|
||||
.media-player-quality:focus-visible {
|
||||
.media-player-settings-toggle:focus-visible,
|
||||
.media-player-settings-quality select:focus-visible {
|
||||
outline: 2px solid var(--sc-ring, var(--sc-primary, #80b6f5));
|
||||
outline-offset: 2px;
|
||||
}
|
||||
@@ -412,9 +486,12 @@
|
||||
}
|
||||
|
||||
@media (max-width: 639px) {
|
||||
.media-player-seek-button,
|
||||
.media-player-volume {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.media-player-controls-fullscreen .media-player-volume {
|
||||
display: block;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
Reference in new issue
Block a user