feat: Implement core course management, enrollment, and classroom learning functionalities with new composables and components.
This commit is contained in:
parent
05755992a7
commit
754f211a08
4 changed files with 221 additions and 66 deletions
|
|
@ -12,7 +12,7 @@ const props = defineProps<{
|
|||
const emit = defineEmits<{
|
||||
(e: 'timeupdate', currentTime: number, duration: number): void;
|
||||
(e: 'ended'): void;
|
||||
(e: 'loadedmetadata'): void;
|
||||
(e: 'loadedmetadata', duration: number): void;
|
||||
}>();
|
||||
|
||||
const videoRef = ref<HTMLVideoElement | null>(null);
|
||||
|
|
@ -39,7 +39,115 @@ const formatTime = (time: number) => {
|
|||
const currentTimeDisplay = computed(() => formatTime(currentTime.value));
|
||||
const durationDisplay = computed(() => formatTime(duration.value || 0));
|
||||
|
||||
// YouTube Helper Logic
|
||||
const isYoutube = computed(() => {
|
||||
const s = props.src.toLowerCase();
|
||||
return s.includes('youtube.com') || s.includes('youtu.be');
|
||||
});
|
||||
|
||||
const youtubeEmbedUrl = computed(() => {
|
||||
if (!isYoutube.value) return '';
|
||||
let videoId = '';
|
||||
|
||||
// Extract Video ID
|
||||
if (props.src.includes('youtu.be')) {
|
||||
videoId = props.src.split('youtu.be/')[1]?.split('?')[0];
|
||||
} else {
|
||||
const urlParams = new URLSearchParams(props.src.split('?')[1]);
|
||||
videoId = urlParams.get('v') || '';
|
||||
}
|
||||
|
||||
// Return Embed URL with enablejsapi=1
|
||||
return `https://www.youtube.com/embed/${videoId}?enablejsapi=1&rel=0`;
|
||||
});
|
||||
|
||||
// YouTube API Tracking
|
||||
let ytPlayer: any = null;
|
||||
let ytInterval: any = null;
|
||||
|
||||
const initYoutubeAPI = () => {
|
||||
if (!isYoutube.value || typeof window === 'undefined') return;
|
||||
|
||||
// Load API Script if not exists
|
||||
if (!(window as any).YT) {
|
||||
const tag = document.createElement('script');
|
||||
tag.src = "https://www.youtube.com/iframe_api";
|
||||
const firstScriptTag = document.getElementsByTagName('script')[0];
|
||||
firstScriptTag.parentNode?.insertBefore(tag, firstScriptTag);
|
||||
}
|
||||
|
||||
const setupPlayer = () => {
|
||||
ytPlayer = new (window as any).YT.Player('youtube-iframe', {
|
||||
events: {
|
||||
'onReady': (event: any) => {
|
||||
duration.value = event.target.getDuration();
|
||||
|
||||
// Resume Logic for YouTube
|
||||
if (props.initialSeekTime && props.initialSeekTime > 0) {
|
||||
event.target.seekTo(props.initialSeekTime, true);
|
||||
}
|
||||
|
||||
emit('loadedmetadata', event.target.getDuration());
|
||||
},
|
||||
'onStateChange': (event: any) => {
|
||||
if (event.data === (window as any).YT.PlayerState.PLAYING) {
|
||||
startYTTracking();
|
||||
} else {
|
||||
stopYTTracking();
|
||||
}
|
||||
if (event.data === (window as any).YT.PlayerState.ENDED) {
|
||||
emit('ended');
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
if ((window as any).YT && (window as any).YT.Player) {
|
||||
setupPlayer();
|
||||
} else {
|
||||
(window as any).onYouTubeIframeAPIReady = setupPlayer;
|
||||
}
|
||||
};
|
||||
|
||||
const startYTTracking = () => {
|
||||
stopYTTracking();
|
||||
ytInterval = setInterval(() => {
|
||||
if (ytPlayer && ytPlayer.getCurrentTime) {
|
||||
currentTime.value = ytPlayer.getCurrentTime();
|
||||
emit('timeupdate', currentTime.value, duration.value);
|
||||
}
|
||||
}, 1000); // Check every second
|
||||
};
|
||||
|
||||
const stopYTTracking = () => {
|
||||
if (ytInterval) clearInterval(ytInterval);
|
||||
};
|
||||
|
||||
onMounted(() => {
|
||||
if (isYoutube.value) initYoutubeAPI();
|
||||
});
|
||||
|
||||
onUnmounted(() => {
|
||||
stopYTTracking();
|
||||
});
|
||||
|
||||
// Watch for src change to re-init
|
||||
watch(() => props.src, () => {
|
||||
if (isYoutube.value) {
|
||||
setTimeout(initYoutubeAPI, 500);
|
||||
}
|
||||
});
|
||||
|
||||
const togglePlay = () => {
|
||||
if (isYoutube.value) {
|
||||
if (ytPlayer && ytPlayer.getPlayerState) {
|
||||
const state = ytPlayer.getPlayerState();
|
||||
if (state === 1) ytPlayer.pauseVideo();
|
||||
else ytPlayer.playVideo();
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (!videoRef.value) return;
|
||||
if (isPlaying.value) videoRef.value.pause();
|
||||
else videoRef.value.play();
|
||||
|
|
@ -63,7 +171,7 @@ const handleLoadedMetadata = () => {
|
|||
videoRef.value.currentTime = seekTo;
|
||||
}
|
||||
}
|
||||
emit('loadedmetadata');
|
||||
emit('loadedmetadata', videoRef.value?.duration || 0);
|
||||
};
|
||||
|
||||
const handleEnded = () => {
|
||||
|
|
@ -72,7 +180,7 @@ const handleEnded = () => {
|
|||
};
|
||||
|
||||
const seek = (e: MouseEvent) => {
|
||||
if (!videoRef.value) return;
|
||||
if (!videoRef.value || !isFinite(videoRef.value.duration)) return;
|
||||
const rect = (e.currentTarget as HTMLElement).getBoundingClientRect();
|
||||
const percent = (e.clientX - rect.left) / rect.width;
|
||||
videoRef.value.currentTime = percent * videoRef.value.duration;
|
||||
|
|
@ -102,38 +210,52 @@ watch([volume, isMuted], () => {
|
|||
|
||||
<template>
|
||||
<div class="bg-black rounded-xl overflow-hidden shadow-2xl mb-6 aspect-video relative group ring-1 ring-white/10">
|
||||
<video
|
||||
ref="videoRef"
|
||||
:src="src"
|
||||
class="w-full h-full object-contain"
|
||||
@click="togglePlay"
|
||||
@timeupdate="handleTimeUpdate"
|
||||
@loadedmetadata="handleLoadedMetadata"
|
||||
@ended="handleEnded"
|
||||
/>
|
||||
|
||||
<!-- Custom Controls Overlay -->
|
||||
<div class="absolute bottom-0 left-0 right-0 p-4 bg-gradient-to-t from-black/80 via-black/40 to-transparent transition-opacity opacity-0 group-hover:opacity-100">
|
||||
<div class="flex items-center gap-4 text-white">
|
||||
<q-btn flat round dense :icon="isPlaying ? 'pause' : 'play_arrow'" @click.stop="togglePlay" />
|
||||
<div class="relative flex-grow h-1.5 bg-white/20 rounded-full cursor-pointer group/progress overflow-hidden" @click="seek">
|
||||
<div class="absolute top-0 left-0 h-full bg-blue-500 rounded-full group-hover/progress:bg-blue-400 transition-all shadow-[0_0_10px_rgba(59,130,246,0.5)]" :style="{ width: videoProgress + '%' }"></div>
|
||||
</div>
|
||||
<span class="text-xs font-mono font-medium opacity-90">{{ currentTimeDisplay }} / {{ durationDisplay }}</span>
|
||||
|
||||
<!-- Volume Control -->
|
||||
<div class="flex items-center gap-2 group/volume">
|
||||
<q-btn flat round dense :icon="volumeIcon" @click.stop="handleToggleMute" color="white" />
|
||||
<div class="w-0 group-hover/volume:w-20 overflow-hidden transition-all duration-300 flex items-center">
|
||||
<input
|
||||
type="range"
|
||||
min="0"
|
||||
max="1"
|
||||
step="0.1"
|
||||
:value="volume"
|
||||
@input="handleVolumeChange"
|
||||
class="w-20 h-1 bg-white/30 rounded-lg appearance-none cursor-pointer accent-blue-500"
|
||||
/>
|
||||
<!-- 1. YouTube Player -->
|
||||
<iframe
|
||||
v-if="isYoutube"
|
||||
id="youtube-iframe"
|
||||
:src="youtubeEmbedUrl"
|
||||
class="w-full h-full"
|
||||
frameborder="0"
|
||||
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
|
||||
allowfullscreen
|
||||
></iframe>
|
||||
|
||||
<!-- 2. Standard HTML5 Video Player -->
|
||||
<div v-else class="w-full h-full relative">
|
||||
<video
|
||||
ref="videoRef"
|
||||
:src="src"
|
||||
class="w-full h-full object-contain"
|
||||
@click="togglePlay"
|
||||
@timeupdate="handleTimeUpdate"
|
||||
@loadedmetadata="handleLoadedMetadata"
|
||||
@ended="handleEnded"
|
||||
/>
|
||||
|
||||
<!-- Custom Controls Overlay (Only for HTML5 Video) -->
|
||||
<div class="absolute bottom-0 left-0 right-0 p-4 bg-gradient-to-t from-black/80 via-black/40 to-transparent transition-opacity opacity-0 group-hover:opacity-100">
|
||||
<div class="flex items-center gap-4 text-white">
|
||||
<q-btn flat round dense :icon="isPlaying ? 'pause' : 'play_arrow'" @click.stop="togglePlay" />
|
||||
<div class="relative flex-grow h-1.5 bg-white/20 rounded-full cursor-pointer group/progress overflow-hidden" @click="seek">
|
||||
<div class="absolute top-0 left-0 h-full bg-blue-500 rounded-full group-hover/progress:bg-blue-400 transition-all shadow-[0_0_10px_rgba(59,130,246,0.5)]" :style="{ width: videoProgress + '%' }"></div>
|
||||
</div>
|
||||
<span class="text-xs font-mono font-medium opacity-90">{{ currentTimeDisplay }} / {{ durationDisplay }}</span>
|
||||
|
||||
<!-- Volume Control -->
|
||||
<div class="flex items-center gap-2 group/volume">
|
||||
<q-btn flat round dense :icon="volumeIcon" @click.stop="handleToggleMute" color="white" />
|
||||
<div class="w-0 group-hover/volume:w-20 overflow-hidden transition-all duration-300 flex items-center">
|
||||
<input
|
||||
type="range"
|
||||
min="0"
|
||||
max="1"
|
||||
step="0.1"
|
||||
:value="volume"
|
||||
@input="handleVolumeChange"
|
||||
class="w-20 h-1 bg-white/30 rounded-lg appearance-none cursor-pointer accent-blue-500"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue