feat: Lyrics

This commit is contained in:
yhlhoh committed 2026-10-24 10:54:52 +08:00
1 parent 6ed3e169e4
commit 82b1fa0d47
17 files changed
+3863 -283

No files matched your search

+91
View File
@@ -0,0 +1,91 @@
'use strict';
/**
* 外观预设 / 内容预设 / 设置的校验与规整。
* 保持与旧版 API 字段兼容,同时对长度、枚举、类型做约束。
*/
const LRC_STYLE_DEFAULTS = require('./lrc').DEFAULT_LRC_STYLE;
const ALIGNS = ['left', 'center', 'right'];
function str(value, maxLength) {
if (typeof value !== 'string') return '';
return value.slice(0, maxLength);
}
function idOf(input) {
const id = String(input && input.id != null ? input.id : '').trim();
if (!id) throw new Error('缺少 id');
return id;
}
function normalizeFieldDefs(input) {
if (!Array.isArray(input)) return [];
const seen = new Set();
const result = [];
for (const raw of input) {
if (!raw || typeof raw !== 'object') continue;
const key = str(raw.key, 64).trim();
if (!key || !/^[\w-]+$/.test(key) || seen.has(key)) continue;
seen.add(key);
result.push({
key,
label: str(raw.label, 80).trim() || key,
defaultValue: str(raw.defaultValue, 2000),
});
if (result.length >= 40) break;
}
return result;
}
function normalizeAppearancePreset(input) {
const src = input && typeof input === 'object' ? input : {};
const align = ALIGNS.includes(src.align) ? src.align : 'right';
return {
id: idOf(src),
name: str(src.name, 120).trim() || '未命名外观',
kind: 'iframe',
align,
themeColor: str(src.themeColor, 40),
extraCss: str(src.extraCss, 200_000),
fieldDefs: normalizeFieldDefs(src.fieldDefs),
html: str(src.html, 1_000_000),
};
}
function normalizeContentPreset(input) {
const src = input && typeof input === 'object' ? input : {};
const fields = {};
if (src.fields && typeof src.fields === 'object' && !Array.isArray(src.fields)) {
let count = 0;
for (const [key, value] of Object.entries(src.fields)) {
if (!/^[\w-]+$/.test(key)) continue;
fields[key] = str(typeof value === 'string' ? value : String(value == null ? '' : value), 20_000);
count += 1;
if (count >= 80) break;
}
}
return {
id: idOf(src),
name: str(src.name, 120).trim() || '未命名内容',
appearancePresetId: str(src.appearancePresetId, 120),
fields,
};
}
function normalizeSettings(input) {
const src = input && typeof input === 'object' ? input : {};
const duration = Number(src.autoHideDuration);
return {
autoHideDuration: Number.isFinite(duration) ? Math.max(0, Math.min(600_000, Math.round(duration))) : 0,
};
}
module.exports = {
ALIGNS,
LRC_STYLE_DEFAULTS,
normalizeAppearancePreset,
normalizeContentPreset,
normalizeSettings,
};