Bilibili-Evolved/registry/lib/components/video/player/common/speed/utils.ts
JLoeve 8c8bc9f45a feat: 从「倍速记忆」/「倍速增强」中分离出「扩展倍速」组件和「视频倍速 - 快捷键支持」插件
* 【新增】支持固定全局倍速值和设置倍速菜单最大高度(后者不显示选项)
* 【新增】支持隐藏倍速菜单滚动条
* 【优化】改变选项实时更新
* 【优化】使用了更稳妥的方式获取菜单项元素
* 【优化】使用自实现的 mini-rxjs 处理事件流,避免重复操作,实现更改选项值实时响应
* 【优化】为切换倍速等操作加上了防抖处理
* 【修复】添加空倍速会报倍速值太小的错误信息
* 【迁移】添加了迁移提示和自动迁移配置的功能
* 【更名】「倍速记忆」/「倍速增强」->「记忆倍速」
* 【更名】「扩展倍速菜单」->「扩展倍速」
2022-03-16 23:23:37 +08:00

47 lines
1.1 KiB
TypeScript

import { getData, registerAndGetData } from '@/plugins/data'
export const trimLeadingDot = (selector: string) => selector.replace(/^\./, '')
export const useShare = <T>(
key: string,
defaultFactory?: () => T,
): [T | undefined, (value: T) => any] => {
const setShareValue = shared => {
registerAndGetData(key, shared)[0] = shared
}
const data = getData(key)
if (data.length) {
return [data[0], setShareValue]
}
if (defaultFactory) {
const defaultValue = defaultFactory()
setShareValue(defaultValue)
return [defaultValue, setShareValue]
}
return [
undefined,
setShareValue,
]
}
export const formatSpeedText = (speed: number, nameBtnStyle = false) => {
if (nameBtnStyle && speed === 1) {
return '倍速'
}
return Math.trunc(speed) === speed ? `${speed}.0x` : `${speed}x`
}
export const parseSpeedText = (text: string) => {
if (text === '倍速') {
return 1
}
const matchResult = /([0-9]*[.]?[0-9]+)x/.exec(text)
if (matchResult) {
return parseFloat(matchResult[1])
}
throw new Error(`unknown speed text: ${text}`)
}