-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathuseSkipSettings.js
More file actions
226 lines (197 loc) · 5.7 KB
/
useSkipSettings.js
File metadata and controls
226 lines (197 loc) · 5.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import { ref, computed, onUnmounted } from 'vue'
/**
* 片头片尾跳过功能的组合式函数
* @param {Object} options - 配置选项
* @param {Function} options.onSkipToNext - 跳转到下一集的回调函数
* @param {Function} options.getCurrentTime - 获取当前播放时间的函数
* @param {Function} options.setCurrentTime - 设置播放时间的函数
* @param {Function} options.getDuration - 获取视频总时长的函数
* @returns {Object} 返回片头片尾跳过相关的响应式数据和方法
*/
export function useSkipSettings(options = {}) {
const {
onSkipToNext = () => {},
getCurrentTime = () => 0,
setCurrentTime = () => {},
getDuration = () => 0
} = options
// 响应式数据
const showSkipSettingsDialog = ref(false)
const skipIntroEnabled = ref(false)
const skipOutroEnabled = ref(false)
const skipIntroSeconds = ref(90)
const skipOutroSeconds = ref(90)
const skipIntroApplied = ref(false)
const skipOutroTimer = ref(null)
// 计算属性
const skipEnabled = computed(() => {
return skipIntroEnabled.value || skipOutroEnabled.value
})
// 本地存储键名
const STORAGE_KEY = 'drplayer_skip_settings'
/**
* 从本地存储加载设置
*/
const loadSkipSettings = () => {
try {
const saved = localStorage.getItem(STORAGE_KEY)
if (saved) {
const settings = JSON.parse(saved)
skipIntroEnabled.value = settings.skipIntroEnabled || false
skipOutroEnabled.value = settings.skipOutroEnabled || false
skipIntroSeconds.value = settings.skipIntroSeconds || 90
skipOutroSeconds.value = settings.skipOutroSeconds || 90
}
} catch (error) {
console.warn('加载片头片尾设置失败:', error)
}
}
/**
* 保存设置到本地存储
*/
const saveSkipSettings = (settings) => {
try {
// 更新响应式数据
skipIntroEnabled.value = settings.skipIntroEnabled
skipOutroEnabled.value = settings.skipOutroEnabled
skipIntroSeconds.value = settings.skipIntroSeconds
skipOutroSeconds.value = settings.skipOutroSeconds
// 保存到本地存储
const settingsToSave = {
skipIntroEnabled: settings.skipIntroEnabled,
skipOutroEnabled: settings.skipOutroEnabled,
skipIntroSeconds: settings.skipIntroSeconds,
skipOutroSeconds: settings.skipOutroSeconds
}
localStorage.setItem(STORAGE_KEY, JSON.stringify(settingsToSave))
// 应用新设置
applySkipSettings()
} catch (error) {
console.warn('保存片头片尾设置失败:', error)
}
}
/**
* 应用片头跳过设置
*/
const applyIntroSkip = () => {
if (!skipIntroEnabled.value || skipIntroApplied.value) return
const currentTime = getCurrentTime()
if (currentTime < skipIntroSeconds.value) {
setCurrentTime(skipIntroSeconds.value)
skipIntroApplied.value = true
console.log(`已跳过片头 ${skipIntroSeconds.value} 秒`)
}
}
/**
* 设置片尾跳过逻辑
*/
const setupOutroSkip = () => {
if (!skipOutroEnabled.value) return
const duration = getDuration()
if (duration <= 0) return
const currentTime = getCurrentTime()
const timeToSkip = duration - skipOutroSeconds.value
if (currentTime >= timeToSkip && !skipOutroTimer.value) {
skipOutroTimer.value = setTimeout(() => {
console.log(`已跳过片尾 ${skipOutroSeconds.value} 秒,跳转到下一集`)
onSkipToNext()
}, 1000) // 延迟1秒执行,避免频繁触发
}
}
/**
* 应用片头片尾设置
*/
const applySkipSettings = () => {
// 应用片头跳过
applyIntroSkip()
// 设置片尾跳过
setupOutroSkip()
}
/**
* 处理时间更新事件
*/
const handleTimeUpdate = () => {
// 应用片头跳过(仅在未应用时)
if (skipIntroEnabled.value && !skipIntroApplied.value) {
applyIntroSkip()
}
// 处理片尾跳过
if (skipOutroEnabled.value) {
const duration = getDuration()
const currentTime = getCurrentTime()
if (duration > 0) {
const timeToSkip = duration - skipOutroSeconds.value
if (currentTime >= timeToSkip && !skipOutroTimer.value) {
skipOutroTimer.value = setTimeout(() => {
console.log(`已跳过片尾 ${skipOutroSeconds.value} 秒,跳转到下一集`)
onSkipToNext()
}, 1000)
}
}
}
}
/**
* 重置跳过状态
*/
const resetSkipState = () => {
skipIntroApplied.value = false
if (skipOutroTimer.value) {
clearTimeout(skipOutroTimer.value)
skipOutroTimer.value = null
}
}
/**
* 初始化片头片尾设置
*/
const initSkipSettings = () => {
resetSkipState()
loadSkipSettings()
}
/**
* 打开设置弹窗
*/
const openSkipSettingsDialog = () => {
showSkipSettingsDialog.value = true
}
/**
* 关闭设置弹窗
*/
const closeSkipSettingsDialog = () => {
showSkipSettingsDialog.value = false
}
/**
* 清理资源
*/
const cleanup = () => {
if (skipOutroTimer.value) {
clearTimeout(skipOutroTimer.value)
skipOutroTimer.value = null
}
}
// 组件卸载时清理资源
onUnmounted(() => {
cleanup()
})
return {
// 响应式数据
showSkipSettingsDialog,
skipIntroEnabled,
skipOutroEnabled,
skipIntroSeconds,
skipOutroSeconds,
skipIntroApplied,
skipOutroTimer,
// 计算属性
skipEnabled,
// 方法
loadSkipSettings,
saveSkipSettings,
applySkipSettings,
handleTimeUpdate,
resetSkipState,
initSkipSettings,
openSkipSettingsDialog,
closeSkipSettingsDialog,
cleanup
}
}