2026-06-24 17:25:54 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* Shortcodes JavaScript - 实况照片、语音、音乐卡片
|
|
|
|
|
|
* 移植自 Hugo-Theme-Amigo
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2026-06-24 21:02:53 +08:00
|
|
|
|
// ============================================================
|
|
|
|
|
|
// Global Audio Manager - 全局音频/视频管理器
|
|
|
|
|
|
// 确保所有音频控件互斥播放,PJAX 切换时保存进度
|
|
|
|
|
|
// ============================================================
|
|
|
|
|
|
|
|
|
|
|
|
var AudioManager = {
|
|
|
|
|
|
currentPlaying: null,
|
|
|
|
|
|
currentType: null,
|
|
|
|
|
|
savedStates: {},
|
2026-06-25 15:24:04 +08:00
|
|
|
|
// 全局音乐播放器 - PJAX 持久化
|
|
|
|
|
|
globalAudio: null,
|
|
|
|
|
|
globalMusicSrc: null,
|
|
|
|
|
|
globalMusicCard: null,
|
|
|
|
|
|
// Session storage key
|
|
|
|
|
|
STORAGE_KEY: 'amigo_music_state',
|
|
|
|
|
|
|
|
|
|
|
|
// 获取当前页面的存储 key(基于 URL path)
|
|
|
|
|
|
_getPageKey: function() {
|
|
|
|
|
|
return window.location.pathname;
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
init: function() {
|
|
|
|
|
|
// 创建全局音频元素
|
|
|
|
|
|
if (!this.globalAudio) {
|
|
|
|
|
|
this.globalAudio = new Audio();
|
|
|
|
|
|
this.globalAudio.preload = 'metadata';
|
|
|
|
|
|
|
|
|
|
|
|
// 监听全局音频事件
|
|
|
|
|
|
this.globalAudio.addEventListener('timeupdate', this._onTimeUpdate.bind(this));
|
|
|
|
|
|
this.globalAudio.addEventListener('ended', this._onEnded.bind(this));
|
|
|
|
|
|
this.globalAudio.addEventListener('loadedmetadata', this._onLoadedMetadata.bind(this));
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// 保存当前播放状态到 sessionStorage(按页面保存)
|
|
|
|
|
|
saveToSession: function() {
|
|
|
|
|
|
if (!this.globalAudio || !this.globalMusicSrc) return;
|
|
|
|
|
|
|
|
|
|
|
|
var pageKey = this._getPageKey();
|
|
|
|
|
|
var allStates = this._loadAllStates();
|
|
|
|
|
|
|
|
|
|
|
|
allStates[pageKey] = {
|
|
|
|
|
|
src: this.globalMusicSrc,
|
|
|
|
|
|
currentTime: this.globalAudio.currentTime,
|
|
|
|
|
|
isPlaying: !this.globalAudio.paused,
|
|
|
|
|
|
name: this.globalMusicCard ? this.globalMusicCard.dataset.name : '',
|
|
|
|
|
|
artist: this.globalMusicCard ? this.globalMusicCard.dataset.artist : ''
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
sessionStorage.setItem(this.STORAGE_KEY, JSON.stringify(allStates));
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
console.warn('Failed to save music state:', e);
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// 加载所有页面的状态
|
|
|
|
|
|
_loadAllStates: function() {
|
|
|
|
|
|
try {
|
|
|
|
|
|
var stored = sessionStorage.getItem(this.STORAGE_KEY);
|
|
|
|
|
|
if (!stored) return {};
|
|
|
|
|
|
return JSON.parse(stored);
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
return {};
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// 从 sessionStorage 恢复当前页面的播放状态
|
|
|
|
|
|
restoreFromSession: function() {
|
|
|
|
|
|
var pageKey = this._getPageKey();
|
|
|
|
|
|
var allStates = this._loadAllStates();
|
|
|
|
|
|
|
|
|
|
|
|
var state = allStates[pageKey];
|
|
|
|
|
|
if (!state || !state.src) return null;
|
|
|
|
|
|
|
|
|
|
|
|
return state;
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// 清除当前页面的播放状态
|
|
|
|
|
|
clearSession: function() {
|
|
|
|
|
|
var pageKey = this._getPageKey();
|
|
|
|
|
|
var allStates = this._loadAllStates();
|
|
|
|
|
|
|
|
|
|
|
|
delete allStates[pageKey];
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
sessionStorage.setItem(this.STORAGE_KEY, JSON.stringify(allStates));
|
|
|
|
|
|
} catch (e) {}
|
|
|
|
|
|
},
|
|
|
|
|
|
|
2026-06-25 15:47:04 +08:00
|
|
|
|
// 重置播放器到初始状态(刷新页面/非 PJAX 导航时调用)
|
|
|
|
|
|
reset: function() {
|
|
|
|
|
|
if (this.globalAudio) {
|
|
|
|
|
|
this.globalAudio.pause();
|
|
|
|
|
|
this.globalAudio.currentTime = 0;
|
|
|
|
|
|
this.globalAudio.src = '';
|
|
|
|
|
|
}
|
|
|
|
|
|
this.globalMusicSrc = null;
|
|
|
|
|
|
this.globalMusicCard = null;
|
|
|
|
|
|
sessionStorage.removeItem(this.STORAGE_KEY);
|
|
|
|
|
|
},
|
|
|
|
|
|
|
2026-06-25 15:24:04 +08:00
|
|
|
|
// 恢复播放状态 - 在 PJAX 切换后调用
|
|
|
|
|
|
restorePlayingState: function() {
|
|
|
|
|
|
var state = this.restoreFromSession();
|
|
|
|
|
|
if (!state || !state.src) {
|
2026-06-25 15:47:04 +08:00
|
|
|
|
// 当前页面没有保存的状态,保持当前播放状态(继续播放)
|
2026-06-25 15:24:04 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
|
|
|
|
// 找到对应的音乐卡片
|
|
|
|
|
|
var cards = document.querySelectorAll('.amigo-music-card');
|
|
|
|
|
|
var targetCard = null;
|
|
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < cards.length; i++) {
|
|
|
|
|
|
var card = cards[i];
|
|
|
|
|
|
if (card.dataset.src === state.src) {
|
|
|
|
|
|
targetCard = card;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!targetCard) {
|
2026-06-25 15:47:04 +08:00
|
|
|
|
// 当前页面没有这个音乐卡片,保持当前播放状态(继续播放)
|
2026-06-25 15:24:04 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 设置全局音频
|
|
|
|
|
|
this.globalMusicSrc = state.src;
|
|
|
|
|
|
this.globalMusicCard = targetCard;
|
|
|
|
|
|
this.globalAudio.src = state.src;
|
|
|
|
|
|
|
2026-06-25 15:47:04 +08:00
|
|
|
|
// 更新 UI(恢复原来的播放状态)
|
2026-06-25 15:24:04 +08:00
|
|
|
|
this._updateUI(targetCard, state.isPlaying);
|
|
|
|
|
|
|
|
|
|
|
|
// 等待元数据加载完成
|
|
|
|
|
|
this.globalAudio.addEventListener('loadedmetadata', function onLoaded() {
|
|
|
|
|
|
self.globalAudio.removeEventListener('loadedmetadata', onLoaded);
|
|
|
|
|
|
|
|
|
|
|
|
if (state.currentTime > 0 && state.currentTime < self.globalAudio.duration) {
|
|
|
|
|
|
self.globalAudio.currentTime = state.currentTime;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
self._onTimeUpdate();
|
|
|
|
|
|
|
2026-06-25 15:47:04 +08:00
|
|
|
|
// 如果之前在播放,继续播放
|
|
|
|
|
|
if (state.isPlaying && self.globalAudio.paused) {
|
2026-06-25 15:24:04 +08:00
|
|
|
|
self.globalAudio.play().catch(function() {});
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (this.globalAudio.readyState >= 1) {
|
|
|
|
|
|
if (state.currentTime > 0 && state.currentTime < this.globalAudio.duration) {
|
|
|
|
|
|
this.globalAudio.currentTime = state.currentTime;
|
|
|
|
|
|
}
|
|
|
|
|
|
this._onTimeUpdate();
|
2026-06-25 15:47:04 +08:00
|
|
|
|
// 如果之前在播放,继续播放
|
|
|
|
|
|
if (state.isPlaying && this.globalAudio.paused) {
|
2026-06-25 15:24:04 +08:00
|
|
|
|
this.globalAudio.play().catch(function() {});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// 播放指定音乐卡片
|
|
|
|
|
|
playMusic: function(card) {
|
|
|
|
|
|
if (!card) return;
|
|
|
|
|
|
|
|
|
|
|
|
var audioSrc = card.dataset.src;
|
|
|
|
|
|
if (!audioSrc) return;
|
|
|
|
|
|
|
|
|
|
|
|
// 如果是同一首歌,切换播放/暂停
|
|
|
|
|
|
if (this.globalMusicSrc === audioSrc && this.globalAudio) {
|
|
|
|
|
|
if (this.globalAudio.paused) {
|
|
|
|
|
|
this.globalAudio.play().catch(function() {});
|
|
|
|
|
|
this.globalMusicCard = card;
|
|
|
|
|
|
this._updateUI(card, true);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
this.globalAudio.pause();
|
|
|
|
|
|
this._updateUI(card, false);
|
|
|
|
|
|
}
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 暂停其他所有音频
|
|
|
|
|
|
this.pauseAll();
|
|
|
|
|
|
|
|
|
|
|
|
// 设置新的音频源
|
|
|
|
|
|
this.globalMusicSrc = audioSrc;
|
|
|
|
|
|
this.globalMusicCard = card;
|
|
|
|
|
|
this.globalAudio.src = audioSrc;
|
|
|
|
|
|
|
|
|
|
|
|
// 更新 UI
|
|
|
|
|
|
this._updateUI(card, true);
|
|
|
|
|
|
|
|
|
|
|
|
// 尝试播放
|
|
|
|
|
|
var self = this;
|
|
|
|
|
|
this.globalAudio.play().then(function() {
|
|
|
|
|
|
// 保存状态
|
|
|
|
|
|
self.saveToSession();
|
|
|
|
|
|
}).catch(function(err) {
|
|
|
|
|
|
console.warn('Play failed:', err);
|
|
|
|
|
|
});
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// 跳转到指定时间
|
|
|
|
|
|
seekTo: function(time) {
|
|
|
|
|
|
if (this.globalAudio) {
|
|
|
|
|
|
this.globalAudio.currentTime = time;
|
|
|
|
|
|
this.saveToSession();
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// 更新时间回调
|
|
|
|
|
|
_onTimeUpdate: function() {
|
|
|
|
|
|
if (!this.globalAudio || !this.globalMusicCard) return;
|
|
|
|
|
|
|
|
|
|
|
|
// 如果正在拖动进度条,跳过 UI 更新
|
|
|
|
|
|
if (this.globalAudio.__seeking) return;
|
|
|
|
|
|
|
|
|
|
|
|
var currentTime = this.globalAudio.currentTime;
|
|
|
|
|
|
var duration = this.globalAudio.duration;
|
|
|
|
|
|
|
|
|
|
|
|
if (isNaN(duration) || duration === 0) return;
|
|
|
|
|
|
|
|
|
|
|
|
// 更新进度条
|
|
|
|
|
|
var progress = (currentTime / duration) * 100;
|
|
|
|
|
|
this.globalMusicCard.style.setProperty('--music-progress', progress + '%');
|
|
|
|
|
|
|
|
|
|
|
|
// 更新时间显示
|
|
|
|
|
|
var currentTimeEl = this.globalMusicCard.querySelector('.amigo-music-card__time--current');
|
|
|
|
|
|
if (currentTimeEl) {
|
|
|
|
|
|
currentTimeEl.textContent = formatTime(currentTime);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 更新总时长(如果还没更新过)
|
|
|
|
|
|
var durationTimeEl = this.globalMusicCard.querySelector('.amigo-music-card__time--duration');
|
|
|
|
|
|
if (durationTimeEl && durationTimeEl.textContent === '--:--') {
|
|
|
|
|
|
durationTimeEl.textContent = formatTime(duration);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 定期保存状态(每5秒)
|
|
|
|
|
|
if (Math.floor(currentTime) % 5 === 0) {
|
|
|
|
|
|
this.saveToSession();
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// 加载完成回调
|
|
|
|
|
|
_onLoadedMetadata: function() {
|
|
|
|
|
|
if (!this.globalAudio || !this.globalMusicCard) return;
|
|
|
|
|
|
|
|
|
|
|
|
var duration = this.globalAudio.duration;
|
|
|
|
|
|
|
|
|
|
|
|
// 更新总时长显示
|
|
|
|
|
|
var durationTimeEl = this.globalMusicCard.querySelector('.amigo-music-card__time--duration');
|
|
|
|
|
|
if (durationTimeEl) {
|
|
|
|
|
|
durationTimeEl.textContent = formatTime(duration);
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// 播放结束回调
|
|
|
|
|
|
_onEnded: function() {
|
|
|
|
|
|
if (this.globalMusicCard) {
|
|
|
|
|
|
this._updateUI(this.globalMusicCard, false);
|
|
|
|
|
|
}
|
|
|
|
|
|
this.globalMusicCard = null;
|
|
|
|
|
|
this.clearSession();
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// 更新 UI 状态
|
|
|
|
|
|
_updateUI: function(card, isPlaying) {
|
|
|
|
|
|
if (!card) return;
|
|
|
|
|
|
|
|
|
|
|
|
card.setAttribute('data-playing', isPlaying ? 'true' : 'false');
|
|
|
|
|
|
|
|
|
|
|
|
var playBtn = card.querySelector('.amigo-music-card__play');
|
|
|
|
|
|
if (playBtn) {
|
|
|
|
|
|
playBtn.setAttribute('aria-pressed', isPlaying ? 'true' : 'false');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (isPlaying) {
|
|
|
|
|
|
this.currentPlaying = card;
|
|
|
|
|
|
this.currentType = 'music';
|
|
|
|
|
|
} else if (this.globalMusicCard === card) {
|
|
|
|
|
|
this.currentPlaying = null;
|
|
|
|
|
|
this.currentType = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
2026-06-24 21:02:53 +08:00
|
|
|
|
|
|
|
|
|
|
pauseAll: function() {
|
|
|
|
|
|
// Pause all voice bubbles
|
|
|
|
|
|
document.querySelectorAll('.amigo-voice-bubble.is-playing').forEach(function(bubble) {
|
|
|
|
|
|
if (bubble.__audio) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
bubble.__audio.pause();
|
|
|
|
|
|
bubble.__audio.currentTime = 0;
|
|
|
|
|
|
} catch (e) {}
|
|
|
|
|
|
}
|
|
|
|
|
|
bubble.classList.remove('is-playing');
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-06-25 15:24:04 +08:00
|
|
|
|
// Pause global music
|
|
|
|
|
|
if (this.globalAudio && !this.globalAudio.paused) {
|
|
|
|
|
|
this.globalAudio.pause();
|
|
|
|
|
|
if (this.globalMusicCard) {
|
|
|
|
|
|
this._updateUI(this.globalMusicCard, false);
|
2026-06-24 21:02:53 +08:00
|
|
|
|
}
|
2026-06-25 15:24:04 +08:00
|
|
|
|
}
|
2026-06-24 21:02:53 +08:00
|
|
|
|
|
|
|
|
|
|
// Pause all live photos
|
|
|
|
|
|
document.querySelectorAll('.amigo-livephoto.is-playing').forEach(function(livePhoto) {
|
|
|
|
|
|
var video = livePhoto.querySelector('.amigo-livephoto-video');
|
|
|
|
|
|
if (video) {
|
|
|
|
|
|
try { video.pause(); video.currentTime = 0; } catch (e) {}
|
|
|
|
|
|
}
|
|
|
|
|
|
livePhoto.classList.remove('is-playing');
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
this.currentPlaying = null;
|
|
|
|
|
|
this.currentType = null;
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
registerPlay: function(element, type) {
|
|
|
|
|
|
if (this.currentPlaying && this.currentPlaying !== element) {
|
|
|
|
|
|
this.pauseAll();
|
|
|
|
|
|
}
|
|
|
|
|
|
this.currentPlaying = element;
|
|
|
|
|
|
this.currentType = type;
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
saveState: function() {
|
|
|
|
|
|
var states = [];
|
|
|
|
|
|
|
|
|
|
|
|
// Save music card states
|
|
|
|
|
|
document.querySelectorAll('.amigo-music-card').forEach(function(card, index) {
|
|
|
|
|
|
var audio = card.__audio;
|
|
|
|
|
|
if (audio && audio.src) {
|
|
|
|
|
|
states.push({
|
|
|
|
|
|
type: 'music',
|
|
|
|
|
|
src: card.dataset.src,
|
|
|
|
|
|
currentTime: audio.currentTime,
|
|
|
|
|
|
isPlaying: card.getAttribute('data-playing') === 'true',
|
|
|
|
|
|
index: index
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Save live photo states
|
|
|
|
|
|
document.querySelectorAll('.amigo-livephoto').forEach(function(livePhoto, index) {
|
|
|
|
|
|
var video = livePhoto.querySelector('.amigo-livephoto-video');
|
|
|
|
|
|
if (video && video.src) {
|
|
|
|
|
|
states.push({
|
|
|
|
|
|
type: 'livephoto',
|
|
|
|
|
|
src: livePhoto.dataset.src,
|
|
|
|
|
|
currentTime: video.currentTime,
|
|
|
|
|
|
isPlaying: livePhoto.classList.contains('is-playing'),
|
|
|
|
|
|
index: index
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
this.savedStates[window.location.pathname] = states;
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
restoreState: function() {
|
|
|
|
|
|
var path = window.location.pathname;
|
|
|
|
|
|
var states = this.savedStates[path];
|
|
|
|
|
|
if (!states || states.length === 0) return;
|
|
|
|
|
|
|
|
|
|
|
|
states.forEach(function(state) {
|
|
|
|
|
|
if (state.type === 'music') {
|
|
|
|
|
|
var cards = document.querySelectorAll('.amigo-music-card');
|
|
|
|
|
|
var card = cards[state.index];
|
|
|
|
|
|
if (card && card.__audio && card.dataset.src === state.src) {
|
|
|
|
|
|
var audio = card.__audio;
|
|
|
|
|
|
var playBtn = card.querySelector('.amigo-music-card__play');
|
|
|
|
|
|
var seekInput = card.querySelector('.amigo-music-card__seek');
|
|
|
|
|
|
var currentTimeEl = card.querySelector('.amigo-music-card__time--current');
|
|
|
|
|
|
|
|
|
|
|
|
audio.addEventListener('loadedmetadata', function onLoaded() {
|
|
|
|
|
|
audio.currentTime = state.currentTime;
|
|
|
|
|
|
audio.removeEventListener('loadedmetadata', onLoaded);
|
|
|
|
|
|
|
|
|
|
|
|
if (currentTimeEl) {
|
|
|
|
|
|
currentTimeEl.textContent = formatTime(state.currentTime);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (seekInput && audio.duration) {
|
|
|
|
|
|
var progress = (state.currentTime / audio.duration) * 100;
|
|
|
|
|
|
seekInput.value = (state.currentTime / audio.duration * 1000).toString();
|
|
|
|
|
|
card.style.setProperty('--music-progress', progress + '%');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (state.isPlaying) {
|
|
|
|
|
|
audio.play().catch(function() {});
|
|
|
|
|
|
card.setAttribute('data-playing', 'true');
|
|
|
|
|
|
if (playBtn) playBtn.setAttribute('aria-pressed', 'true');
|
|
|
|
|
|
AudioManager.currentPlaying = card;
|
|
|
|
|
|
AudioManager.currentType = 'music';
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (audio.readyState >= 1) {
|
|
|
|
|
|
audio.currentTime = state.currentTime;
|
|
|
|
|
|
if (currentTimeEl) {
|
|
|
|
|
|
currentTimeEl.textContent = formatTime(state.currentTime);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (seekInput && audio.duration) {
|
|
|
|
|
|
var progress = (state.currentTime / audio.duration) * 100;
|
|
|
|
|
|
seekInput.value = (state.currentTime / audio.duration * 1000).toString();
|
|
|
|
|
|
card.style.setProperty('--music-progress', progress + '%');
|
|
|
|
|
|
}
|
|
|
|
|
|
if (state.isPlaying) {
|
|
|
|
|
|
audio.play().catch(function() {});
|
|
|
|
|
|
card.setAttribute('data-playing', 'true');
|
|
|
|
|
|
if (playBtn) playBtn.setAttribute('aria-pressed', 'true');
|
|
|
|
|
|
AudioManager.currentPlaying = card;
|
|
|
|
|
|
AudioManager.currentType = 'music';
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
} else if (state.type === 'livephoto') {
|
|
|
|
|
|
var photos = document.querySelectorAll('.amigo-livephoto');
|
|
|
|
|
|
var livePhoto = photos[state.index];
|
|
|
|
|
|
if (livePhoto && livePhoto.dataset.src === state.src) {
|
|
|
|
|
|
var video = livePhoto.querySelector('.amigo-livephoto-video');
|
|
|
|
|
|
if (video) {
|
|
|
|
|
|
var src = livePhoto.dataset.src || '';
|
|
|
|
|
|
if (src && !video.getAttribute('src')) {
|
|
|
|
|
|
video.setAttribute('src', src);
|
|
|
|
|
|
video.src = src;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
video.addEventListener('loadedmetadata', function onLoaded() {
|
|
|
|
|
|
video.currentTime = state.currentTime;
|
|
|
|
|
|
video.removeEventListener('loadedmetadata', onLoaded);
|
|
|
|
|
|
|
|
|
|
|
|
if (state.isPlaying) {
|
|
|
|
|
|
video.play().catch(function() {});
|
|
|
|
|
|
livePhoto.classList.add('is-playing');
|
|
|
|
|
|
AudioManager.currentPlaying = livePhoto;
|
|
|
|
|
|
AudioManager.currentType = 'livephoto';
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (video.readyState >= 1) {
|
|
|
|
|
|
video.currentTime = state.currentTime;
|
|
|
|
|
|
if (state.isPlaying) {
|
|
|
|
|
|
video.play().catch(function() {});
|
|
|
|
|
|
livePhoto.classList.add('is-playing');
|
|
|
|
|
|
AudioManager.currentPlaying = livePhoto;
|
|
|
|
|
|
AudioManager.currentType = 'livephoto';
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-06-24 17:25:54 +08:00
|
|
|
|
// ============================================================
|
|
|
|
|
|
// Live Photo
|
|
|
|
|
|
// ============================================================
|
|
|
|
|
|
|
|
|
|
|
|
function initLivePhotoShortcodes() {
|
2026-06-24 20:22:40 +08:00
|
|
|
|
document.querySelectorAll('.amigo-livephoto').forEach(function(livePhoto) {
|
2026-06-24 17:25:54 +08:00
|
|
|
|
if (livePhoto.__liveBound) return;
|
|
|
|
|
|
livePhoto.__liveBound = true;
|
|
|
|
|
|
|
2026-06-24 20:22:40 +08:00
|
|
|
|
var video = livePhoto.querySelector('.amigo-livephoto-video');
|
|
|
|
|
|
var posterImg = livePhoto.querySelector('.amigo-livephoto-img');
|
|
|
|
|
|
var soundBtn = livePhoto.querySelector('.amigo-livephoto-sound');
|
2026-06-24 17:25:54 +08:00
|
|
|
|
|
2026-06-24 20:22:40 +08:00
|
|
|
|
if (!video) return;
|
2026-06-24 17:25:54 +08:00
|
|
|
|
|
|
|
|
|
|
var HOVER_DELAY = 500;
|
|
|
|
|
|
var hoverTimer = null;
|
|
|
|
|
|
var isLoaded = false;
|
2026-06-24 20:22:40 +08:00
|
|
|
|
var isUserToggled = false;
|
2026-06-24 17:25:54 +08:00
|
|
|
|
|
|
|
|
|
|
function ensureLoaded() {
|
|
|
|
|
|
if (isLoaded) return;
|
|
|
|
|
|
isLoaded = true;
|
|
|
|
|
|
var src = video.dataset.src || '';
|
|
|
|
|
|
if (src && !video.getAttribute('src')) {
|
|
|
|
|
|
video.setAttribute('src', src);
|
|
|
|
|
|
video.src = src;
|
|
|
|
|
|
}
|
|
|
|
|
|
try { video.load(); } catch (e) {}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function setMuted(isMuted) {
|
|
|
|
|
|
video.muted = !!isMuted;
|
2026-06-24 20:22:40 +08:00
|
|
|
|
if (isMuted) {
|
|
|
|
|
|
video.setAttribute('muted', '');
|
|
|
|
|
|
livePhoto.classList.add('is-muted');
|
|
|
|
|
|
} else {
|
|
|
|
|
|
video.removeAttribute('muted');
|
|
|
|
|
|
livePhoto.classList.remove('is-muted');
|
|
|
|
|
|
}
|
2026-06-24 17:25:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function playVideo() {
|
|
|
|
|
|
ensureLoaded();
|
2026-06-24 21:02:53 +08:00
|
|
|
|
AudioManager.registerPlay(livePhoto, 'livephoto');
|
2026-06-24 17:25:54 +08:00
|
|
|
|
livePhoto.classList.add('is-playing');
|
|
|
|
|
|
try { video.play(); } catch (e) {}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function stopVideo() {
|
|
|
|
|
|
livePhoto.classList.remove('is-playing');
|
|
|
|
|
|
try { video.pause(); video.currentTime = 0; } catch (e) {}
|
2026-06-24 21:02:53 +08:00
|
|
|
|
if (AudioManager.currentPlaying === livePhoto) {
|
|
|
|
|
|
AudioManager.currentPlaying = null;
|
|
|
|
|
|
AudioManager.currentType = null;
|
|
|
|
|
|
}
|
2026-06-24 17:25:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-24 20:22:40 +08:00
|
|
|
|
function togglePlay() {
|
2026-06-24 17:25:54 +08:00
|
|
|
|
if (livePhoto.classList.contains('is-playing')) {
|
2026-06-24 20:22:40 +08:00
|
|
|
|
isUserToggled = false;
|
2026-06-24 17:25:54 +08:00
|
|
|
|
stopVideo();
|
|
|
|
|
|
} else {
|
2026-06-24 20:22:40 +08:00
|
|
|
|
isUserToggled = true;
|
2026-06-24 17:25:54 +08:00
|
|
|
|
playVideo();
|
|
|
|
|
|
}
|
2026-06-24 20:22:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Click to toggle
|
|
|
|
|
|
livePhoto.addEventListener('click', function() {
|
|
|
|
|
|
togglePlay();
|
2026-06-24 17:25:54 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
2026-06-24 20:22:40 +08:00
|
|
|
|
// Sound button click
|
|
|
|
|
|
if (soundBtn) {
|
|
|
|
|
|
soundBtn.addEventListener('click', function(e) {
|
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
setMuted(!video.muted);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2026-06-24 17:25:54 +08:00
|
|
|
|
|
2026-06-24 20:22:40 +08:00
|
|
|
|
// Hover to play (desktop)
|
|
|
|
|
|
if ('ontouchstart' in window === false) {
|
|
|
|
|
|
livePhoto.addEventListener('mouseenter', function() {
|
|
|
|
|
|
if (isUserToggled) return;
|
|
|
|
|
|
hoverTimer = setTimeout(playVideo, HOVER_DELAY);
|
|
|
|
|
|
});
|
2026-06-24 17:25:54 +08:00
|
|
|
|
|
2026-06-24 20:22:40 +08:00
|
|
|
|
livePhoto.addEventListener('mouseleave', function() {
|
|
|
|
|
|
clearTimeout(hoverTimer);
|
|
|
|
|
|
if (!isUserToggled) {
|
|
|
|
|
|
stopVideo();
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2026-06-24 17:25:54 +08:00
|
|
|
|
|
2026-06-24 21:02:53 +08:00
|
|
|
|
// Video ended
|
|
|
|
|
|
video.addEventListener('ended', function() {
|
|
|
|
|
|
livePhoto.classList.remove('is-playing');
|
|
|
|
|
|
isUserToggled = false;
|
|
|
|
|
|
if (AudioManager.currentPlaying === livePhoto) {
|
|
|
|
|
|
AudioManager.currentPlaying = null;
|
|
|
|
|
|
AudioManager.currentType = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-06-24 20:22:40 +08:00
|
|
|
|
// Initialize muted state
|
|
|
|
|
|
setMuted(livePhoto.classList.contains('is-muted'));
|
2026-06-24 17:25:54 +08:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ============================================================
|
|
|
|
|
|
// Voice Bubble
|
|
|
|
|
|
// ============================================================
|
|
|
|
|
|
|
|
|
|
|
|
function initVoiceBubbles() {
|
|
|
|
|
|
document.querySelectorAll('.amigo-voice-bubble').forEach(function(bubble) {
|
|
|
|
|
|
if (bubble.__voiceBound) return;
|
|
|
|
|
|
bubble.__voiceBound = true;
|
|
|
|
|
|
|
|
|
|
|
|
var audioSrc = bubble.dataset.src;
|
|
|
|
|
|
var audio = null;
|
|
|
|
|
|
var isPlaying = false;
|
|
|
|
|
|
var durationSpan = bubble.querySelector('.amigo-voice-duration');
|
|
|
|
|
|
|
|
|
|
|
|
// Load audio duration
|
|
|
|
|
|
if (audioSrc) {
|
|
|
|
|
|
audio = new Audio(audioSrc);
|
2026-06-24 21:02:53 +08:00
|
|
|
|
bubble.__audio = audio;
|
2026-06-24 17:25:54 +08:00
|
|
|
|
audio.addEventListener('loadedmetadata', function() {
|
|
|
|
|
|
var duration = Math.round(audio.duration);
|
|
|
|
|
|
if (durationSpan) {
|
|
|
|
|
|
durationSpan.textContent = duration + '"';
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bubble.addEventListener('click', function() {
|
|
|
|
|
|
if (!audio) return;
|
|
|
|
|
|
|
|
|
|
|
|
if (isPlaying) {
|
|
|
|
|
|
audio.pause();
|
|
|
|
|
|
audio.currentTime = 0;
|
2026-06-24 20:22:40 +08:00
|
|
|
|
bubble.classList.remove('is-playing');
|
2026-06-24 17:25:54 +08:00
|
|
|
|
isPlaying = false;
|
2026-06-24 21:02:53 +08:00
|
|
|
|
if (AudioManager.currentPlaying === bubble) {
|
|
|
|
|
|
AudioManager.currentPlaying = null;
|
|
|
|
|
|
AudioManager.currentType = null;
|
|
|
|
|
|
}
|
2026-06-24 17:25:54 +08:00
|
|
|
|
} else {
|
2026-06-24 21:02:53 +08:00
|
|
|
|
AudioManager.registerPlay(bubble, 'voice');
|
2026-06-24 17:25:54 +08:00
|
|
|
|
audio.play();
|
2026-06-24 20:22:40 +08:00
|
|
|
|
bubble.classList.add('is-playing');
|
2026-06-24 17:25:54 +08:00
|
|
|
|
isPlaying = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
audio.addEventListener('ended', function() {
|
2026-06-24 20:22:40 +08:00
|
|
|
|
bubble.classList.remove('is-playing');
|
2026-06-24 17:25:54 +08:00
|
|
|
|
isPlaying = false;
|
2026-06-24 21:02:53 +08:00
|
|
|
|
if (AudioManager.currentPlaying === bubble) {
|
|
|
|
|
|
AudioManager.currentPlaying = null;
|
|
|
|
|
|
AudioManager.currentType = null;
|
|
|
|
|
|
}
|
2026-06-24 17:25:54 +08:00
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ============================================================
|
2026-06-25 15:24:04 +08:00
|
|
|
|
// Music Card - PJAX 持久化版本
|
|
|
|
|
|
// 使用全局 AudioManager 管理音乐播放
|
2026-06-24 17:25:54 +08:00
|
|
|
|
// ============================================================
|
|
|
|
|
|
|
|
|
|
|
|
function initMusicCards() {
|
|
|
|
|
|
document.querySelectorAll('.amigo-music-card').forEach(function(card) {
|
|
|
|
|
|
if (card.__musicBound) return;
|
|
|
|
|
|
card.__musicBound = true;
|
|
|
|
|
|
|
|
|
|
|
|
var audioSrc = card.dataset.src;
|
|
|
|
|
|
var playBtn = card.querySelector('.amigo-music-card__play');
|
|
|
|
|
|
var seekInput = card.querySelector('.amigo-music-card__seek');
|
|
|
|
|
|
var currentTime = card.querySelector('.amigo-music-card__time--current');
|
|
|
|
|
|
var durationTime = card.querySelector('.amigo-music-card__time--duration');
|
|
|
|
|
|
|
|
|
|
|
|
if (!audioSrc) return;
|
|
|
|
|
|
|
2026-06-25 15:24:04 +08:00
|
|
|
|
// 不再创建单独的 Audio 对象,使用全局 AudioManager
|
|
|
|
|
|
// card.__audio = null;
|
2026-06-24 17:25:54 +08:00
|
|
|
|
|
2026-06-25 15:24:04 +08:00
|
|
|
|
// 检查是否应该显示播放状态
|
|
|
|
|
|
// (由 AudioManager 在 restorePlayingState 中处理)
|
2026-06-24 17:25:54 +08:00
|
|
|
|
|
2026-06-25 15:24:04 +08:00
|
|
|
|
// Play/Pause - 使用 AudioManager
|
|
|
|
|
|
function togglePlay(e) {
|
|
|
|
|
|
if (e) e.stopPropagation();
|
|
|
|
|
|
AudioManager.playMusic(card);
|
2026-06-24 17:25:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (playBtn) {
|
2026-06-25 15:24:04 +08:00
|
|
|
|
playBtn.addEventListener('click', togglePlay);
|
2026-06-24 17:25:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
card.addEventListener('click', togglePlay);
|
|
|
|
|
|
|
2026-06-25 15:24:04 +08:00
|
|
|
|
// Seek - 使用 AudioManager
|
2026-06-24 17:25:54 +08:00
|
|
|
|
if (seekInput) {
|
|
|
|
|
|
seekInput.addEventListener('input', function() {
|
2026-06-25 15:24:04 +08:00
|
|
|
|
if (AudioManager.globalAudio && AudioManager.globalAudio.duration) {
|
|
|
|
|
|
var time = (parseFloat(seekInput.value) / 1000) * AudioManager.globalAudio.duration;
|
|
|
|
|
|
AudioManager.seekTo(time);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 拖动时暂停更新
|
|
|
|
|
|
seekInput.addEventListener('mousedown', function() {
|
|
|
|
|
|
if (AudioManager.globalAudio) {
|
|
|
|
|
|
AudioManager.globalAudio.__seeking = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
seekInput.addEventListener('mouseup', function() {
|
|
|
|
|
|
if (AudioManager.globalAudio) {
|
|
|
|
|
|
AudioManager.globalAudio.__seeking = false;
|
2026-06-24 17:25:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function formatTime(seconds) {
|
|
|
|
|
|
var m = Math.floor(seconds / 60);
|
|
|
|
|
|
var s = Math.floor(seconds % 60);
|
|
|
|
|
|
return m + ':' + (s < 10 ? '0' : '') + s;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-24 20:22:40 +08:00
|
|
|
|
// ============================================================
|
|
|
|
|
|
// Cleanup functions for PJAX
|
|
|
|
|
|
// ============================================================
|
|
|
|
|
|
|
|
|
|
|
|
function cleanupVoiceBubbles() {
|
|
|
|
|
|
document.querySelectorAll('.amigo-voice-bubble').forEach(function(bubble) {
|
|
|
|
|
|
if (bubble.__audio) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
bubble.__audio.pause();
|
|
|
|
|
|
bubble.__audio.src = '';
|
|
|
|
|
|
bubble.__audio = null;
|
|
|
|
|
|
} catch (e) {}
|
|
|
|
|
|
}
|
|
|
|
|
|
bubble.__voiceBound = false;
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function cleanupMusicCards() {
|
|
|
|
|
|
document.querySelectorAll('.amigo-music-card').forEach(function(card) {
|
|
|
|
|
|
if (card.__audio) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
card.__audio.pause();
|
|
|
|
|
|
card.__audio.src = '';
|
|
|
|
|
|
card.__audio = null;
|
|
|
|
|
|
} catch (e) {}
|
|
|
|
|
|
}
|
|
|
|
|
|
card.__musicBound = false;
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function cleanupLivePhotos() {
|
|
|
|
|
|
document.querySelectorAll('.amigo-livephoto').forEach(function(livePhoto) {
|
|
|
|
|
|
var video = livePhoto.querySelector('video');
|
|
|
|
|
|
if (video) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
video.pause();
|
|
|
|
|
|
video.src = '';
|
|
|
|
|
|
} catch (e) {}
|
|
|
|
|
|
}
|
|
|
|
|
|
livePhoto.__liveBound = false;
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function cleanupAllShortcodes() {
|
2026-06-25 15:24:04 +08:00
|
|
|
|
// 保存当前播放状态到 sessionStorage(音乐不中断)
|
|
|
|
|
|
AudioManager.saveToSession();
|
|
|
|
|
|
|
|
|
|
|
|
// 只清理语音气泡和实况照片(音乐由全局 AudioManager 管理)
|
2026-06-24 20:22:40 +08:00
|
|
|
|
cleanupVoiceBubbles();
|
|
|
|
|
|
cleanupLivePhotos();
|
2026-06-25 15:24:04 +08:00
|
|
|
|
|
|
|
|
|
|
// 不再清理音乐卡片,因为音乐由全局 AudioManager 管理
|
|
|
|
|
|
// cleanupMusicCards(); // 已移除,音乐在 PJAX 切换时继续播放
|
2026-06-24 20:22:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-24 17:25:54 +08:00
|
|
|
|
// ============================================================
|
|
|
|
|
|
// Initialize all shortcodes
|
|
|
|
|
|
// ============================================================
|
|
|
|
|
|
|
|
|
|
|
|
function initAllShortcodes() {
|
2026-06-25 15:47:04 +08:00
|
|
|
|
// 初始化 AudioManager(创建全局音频元素)
|
2026-06-25 15:24:04 +08:00
|
|
|
|
AudioManager.init();
|
|
|
|
|
|
|
2026-06-24 17:25:54 +08:00
|
|
|
|
initLivePhotoShortcodes();
|
|
|
|
|
|
initVoiceBubbles();
|
|
|
|
|
|
initMusicCards();
|
2026-06-25 15:24:04 +08:00
|
|
|
|
|
2026-06-25 15:47:04 +08:00
|
|
|
|
// 检查是否是 PJAX 导航
|
|
|
|
|
|
if (window.__isPjaxNavigation) {
|
|
|
|
|
|
// PJAX 导航:恢复播放状态(进度和播放状态)
|
|
|
|
|
|
setTimeout(function() {
|
|
|
|
|
|
AudioManager.restorePlayingState();
|
|
|
|
|
|
}, 100);
|
|
|
|
|
|
// 重置标志
|
|
|
|
|
|
window.__isPjaxNavigation = false;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 非 PJAX 导航(刷新页面/跳转):重置播放器到初始状态
|
|
|
|
|
|
AudioManager.reset();
|
|
|
|
|
|
}
|
2026-06-24 17:25:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// DOMContentLoaded
|
|
|
|
|
|
document.addEventListener('DOMContentLoaded', initAllShortcodes);
|
|
|
|
|
|
|
|
|
|
|
|
// Page load
|
|
|
|
|
|
window.addEventListener('load', initAllShortcodes);
|
|
|
|
|
|
|
2026-06-24 20:22:40 +08:00
|
|
|
|
// If DOM is already ready (script loaded late), initialize immediately
|
|
|
|
|
|
if (document.readyState === 'interactive' || document.readyState === 'complete') {
|
|
|
|
|
|
initAllShortcodes();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// PJAX support - cleanup before and re-initialize after page transition
|
2026-06-25 15:47:04 +08:00
|
|
|
|
document.addEventListener('pjax:beforeSend', function() {
|
|
|
|
|
|
window.__isPjaxNavigation = true;
|
|
|
|
|
|
cleanupAllShortcodes();
|
|
|
|
|
|
});
|
2026-06-24 17:25:54 +08:00
|
|
|
|
document.addEventListener('pjax:complete', initAllShortcodes);
|
|
|
|
|
|
document.addEventListener('pjax:success', initAllShortcodes);
|
|
|
|
|
|
|
|
|
|
|
|
// Also listen for custom events that some PJAX libraries fire
|
|
|
|
|
|
document.addEventListener('page:loaded', initAllShortcodes);
|
|
|
|
|
|
document.addEventListener('page:change', initAllShortcodes);
|
2026-06-24 20:22:40 +08:00
|
|
|
|
document.addEventListener('turbo:load', initAllShortcodes);
|
|
|
|
|
|
document.addEventListener('turbo:before-render', cleanupAllShortcodes);
|
|
|
|
|
|
|
|
|
|
|
|
// History API based navigation
|
|
|
|
|
|
document.addEventListener('popstate', function() {
|
|
|
|
|
|
cleanupAllShortcodes();
|
|
|
|
|
|
setTimeout(initAllShortcodes, 10);
|
|
|
|
|
|
});
|