883 lines
36 KiB
HTML
883 lines
36 KiB
HTML
<!DOCTYPE html>
|
||
<html lang="zh-CN">
|
||
<head>
|
||
<meta charset="UTF-8" />
|
||
<title>OBS Lower Third · 管理</title>
|
||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||
<!-- Ant Design & React via CDN -->
|
||
<script src="/libs/react.production.min.js"></script>
|
||
<script src="/libs/react-dom.production.min.js"></script>
|
||
<script src="/libs/dayjs.min.js"></script>
|
||
<script src="/libs/antd.min.js"></script>
|
||
<link rel="stylesheet" href="/libs/antd.reset.css" />
|
||
<!-- Babel standalone for JSX transpilation -->
|
||
<script src="/libs/babel.min.js"></script>
|
||
<style>
|
||
body { margin: 0; background: #0a0c14; min-height: 100vh; }
|
||
#root { min-height: 100vh; }
|
||
.preset-list-item { cursor: pointer; transition: all 0.15s; }
|
||
.preset-list-item:hover { background: rgba(255,255,255,0.04) !important; }
|
||
.preset-list-item.selected { background: rgba(24,144,255,0.12) !important; border-color: #1890ff !important; }
|
||
.preset-list-item.active-live { background: rgba(82,196,26,0.12) !important; border-color: #52c41a !important; }
|
||
.preset-list-item.selected.active-live { background: rgba(82,196,26,0.2) !important; border-color: #52c41a !important; }
|
||
.preview-frame-wrap {
|
||
width: 100%;
|
||
aspect-ratio: 16/9;
|
||
background: #000;
|
||
border-radius: 8px;
|
||
overflow: hidden;
|
||
position: relative;
|
||
}
|
||
.preview-frame-wrap iframe {
|
||
width: 100%;
|
||
height: 100%;
|
||
border: none;
|
||
}
|
||
.kbd-hint {
|
||
display: inline-block;
|
||
background: rgba(255,255,255,0.1);
|
||
border: 1px solid rgba(255,255,255,0.2);
|
||
border-radius: 4px;
|
||
padding: 1px 6px;
|
||
font-size: 12px;
|
||
font-family: monospace;
|
||
}
|
||
.ant-layout { background: transparent !important; }
|
||
.ant-layout-header { padding: 0 24px !important; }
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<div id="root"></div>
|
||
|
||
<script type="text/babel" data-presets="react">
|
||
const {
|
||
Layout, Menu, Tabs, Button, Input, Select, Form, Space, Tag,
|
||
Typography, Tooltip, Modal, InputNumber, Switch, Divider,
|
||
Card, Row, Col, List, Badge, message, Popconfirm, Empty,
|
||
Drawer, Alert, Spin, ConfigProvider, theme
|
||
} = antd;
|
||
const { Header, Content, Sider } = Layout;
|
||
const { Title, Text, Paragraph } = Typography;
|
||
const { TextArea } = Input;
|
||
|
||
// ── Placeholder replacement ─────────────────────────────────────────────
|
||
function fillPlaceholders(html, contentData) {
|
||
if (!html || !contentData) return html || '';
|
||
return html.replace(/\{\{(\w+)\}\}/g, (match, key) => {
|
||
return Object.prototype.hasOwnProperty.call(contentData, key)
|
||
? String(contentData[key])
|
||
: match;
|
||
});
|
||
}
|
||
|
||
// Build preview srcdoc wrapping template html in a container
|
||
function buildPreviewDoc(preset, contentData) {
|
||
if (!preset || !preset.html) return '<html><body style="background:#111;color:#666;display:flex;align-items:center;justify-content:center;height:100vh;margin:0;font-family:sans-serif;font-size:14px;">无预览内容</body></html>';
|
||
const processed = fillPlaceholders(preset.html, contentData || {});
|
||
if (preset.kind === 'iframe') {
|
||
return processed;
|
||
}
|
||
return processed;
|
||
}
|
||
|
||
// ── Preview Component ───────────────────────────────────────────────────
|
||
function PreviewPanel({ preset, contentData }) {
|
||
const doc = buildPreviewDoc(preset, contentData);
|
||
return (
|
||
<div className="preview-frame-wrap">
|
||
<iframe
|
||
key={doc}
|
||
sandbox="allow-scripts"
|
||
srcDoc={doc}
|
||
title="预览"
|
||
/>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
// ── Field Defs Editor ───────────────────────────────────────────────────
|
||
function FieldDefsEditor({ value = [], onChange }) {
|
||
const add = () => {
|
||
onChange([...value, { key: '', label: '', defaultValue: '' }]);
|
||
};
|
||
const remove = (idx) => {
|
||
const next = value.filter((_, i) => i !== idx);
|
||
onChange(next);
|
||
};
|
||
const update = (idx, field, val) => {
|
||
const next = value.map((item, i) => i === idx ? { ...item, [field]: val } : item);
|
||
onChange(next);
|
||
};
|
||
return (
|
||
<div>
|
||
{value.map((fd, idx) => (
|
||
<Space key={idx} style={{ display: 'flex', marginBottom: 8 }} align="baseline">
|
||
<Input
|
||
placeholder="字段Key (如: mainTitle)"
|
||
value={fd.key}
|
||
onChange={e => update(idx, 'key', e.target.value)}
|
||
style={{ width: 160 }}
|
||
/>
|
||
<Input
|
||
placeholder="字段标签 (如: 主标题)"
|
||
value={fd.label}
|
||
onChange={e => update(idx, 'label', e.target.value)}
|
||
style={{ width: 140 }}
|
||
/>
|
||
<Input
|
||
placeholder="默认值"
|
||
value={fd.defaultValue}
|
||
onChange={e => update(idx, 'defaultValue', e.target.value)}
|
||
style={{ width: 180 }}
|
||
/>
|
||
<Button danger size="small" onClick={() => remove(idx)}>删除</Button>
|
||
</Space>
|
||
))}
|
||
<Button type="dashed" onClick={add} size="small">+ 添加字段</Button>
|
||
<div style={{ color: '#888', fontSize: 12, marginTop: 6 }}>
|
||
在 HTML 中用 <code style={{background:'rgba(255,255,255,0.1)',padding:'1px 4px',borderRadius:3}}>{'{{key}}'}</code> 引用字段值
|
||
</div>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
// ── Appearance Presets Tab ──────────────────────────────────────────────
|
||
function AppearancePresetsTab({ presets, onRefresh }) {
|
||
const [selected, setSelected] = React.useState(null);
|
||
const [form] = Form.useForm();
|
||
const [fieldDefs, setFieldDefs] = React.useState([]);
|
||
const [previewData, setPreviewData] = React.useState({});
|
||
const [saving, setSaving] = React.useState(false);
|
||
|
||
const ap = selected ? presets.find(p => p.id === selected) : null;
|
||
|
||
// Build preview content data from fieldDefs defaults
|
||
const buildPreviewContentData = (fds) => {
|
||
const data = {};
|
||
(fds || []).forEach(fd => { data[fd.key] = fd.defaultValue || ''; });
|
||
return data;
|
||
};
|
||
|
||
const selectPreset = (p) => {
|
||
setSelected(p.id);
|
||
form.setFieldsValue({
|
||
name: p.name,
|
||
align: p.align || 'right',
|
||
html: p.html || '',
|
||
extraCss: p.extraCss || '',
|
||
});
|
||
const fds = p.fieldDefs || [];
|
||
setFieldDefs(fds);
|
||
setPreviewData(buildPreviewContentData(fds));
|
||
};
|
||
|
||
const clearForm = () => {
|
||
setSelected(null);
|
||
form.resetFields();
|
||
setFieldDefs([]);
|
||
setPreviewData({});
|
||
};
|
||
|
||
const handleSave = async () => {
|
||
try {
|
||
const values = await form.validateFields();
|
||
setSaving(true);
|
||
const body = { ...values, kind: 'iframe', fieldDefs };
|
||
let res;
|
||
if (selected) {
|
||
res = await fetch('/api/presets/' + selected, {
|
||
method: 'PUT',
|
||
headers: { 'Content-Type': 'application/json' },
|
||
body: JSON.stringify(body)
|
||
});
|
||
} else {
|
||
res = await fetch('/api/presets', {
|
||
method: 'POST',
|
||
headers: { 'Content-Type': 'application/json' },
|
||
body: JSON.stringify(body)
|
||
});
|
||
}
|
||
if (!res.ok) throw new Error('保存失败');
|
||
message.success('外观预设已保存');
|
||
clearForm();
|
||
onRefresh();
|
||
} catch(e) {
|
||
if (e.errorFields) return; // validation error
|
||
message.error('保存失败: ' + e.message);
|
||
} finally {
|
||
setSaving(false);
|
||
}
|
||
};
|
||
|
||
const handleDelete = async (id) => {
|
||
await fetch('/api/presets/' + id, { method: 'DELETE' });
|
||
if (selected === id) clearForm();
|
||
message.success('已删除');
|
||
onRefresh();
|
||
};
|
||
|
||
// Update preview when form html changes
|
||
const htmlValue = Form.useWatch('html', form);
|
||
const currentPreviewPreset = React.useMemo(() => {
|
||
return { kind: 'iframe', html: htmlValue || (ap ? ap.html : '') };
|
||
}, [htmlValue, ap]);
|
||
|
||
return (
|
||
<Row gutter={16} style={{ height: '100%' }}>
|
||
<Col xs={24} md={8}>
|
||
<div style={{ marginBottom: 12, display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
|
||
<Text strong style={{ color: '#fff' }}>外观预设列表</Text>
|
||
<Button size="small" type="primary" onClick={clearForm}>+ 新建</Button>
|
||
</div>
|
||
{presets.length === 0
|
||
? <Empty description="暂无外观预设" />
|
||
: presets.map(p => (
|
||
<Card
|
||
key={p.id}
|
||
size="small"
|
||
className={`preset-list-item${selected === p.id ? ' selected' : ''}`}
|
||
style={{ marginBottom: 8, background: 'rgba(255,255,255,0.04)', borderColor: 'rgba(255,255,255,0.1)' }}
|
||
onClick={() => selectPreset(p)}
|
||
>
|
||
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
|
||
<div>
|
||
<Text style={{ color: '#fff', fontSize: 13 }}>{p.name}</Text>
|
||
<br/>
|
||
<Text style={{ color: '#888', fontSize: 11 }}>
|
||
{(p.fieldDefs || []).length} 个字段 · 对齐: {p.align || 'right'}
|
||
</Text>
|
||
</div>
|
||
<Popconfirm
|
||
title="确认删除?"
|
||
onConfirm={e => { e.stopPropagation(); handleDelete(p.id); }}
|
||
onCancel={e => e.stopPropagation()}
|
||
>
|
||
<Button
|
||
danger size="small" type="text"
|
||
onClick={e => e.stopPropagation()}
|
||
>删除</Button>
|
||
</Popconfirm>
|
||
</div>
|
||
</Card>
|
||
))
|
||
}
|
||
</Col>
|
||
<Col xs={24} md={16}>
|
||
<Card
|
||
title={<Text style={{ color: '#fff' }}>{selected ? '编辑外观预设' : '新建外观预设'}</Text>}
|
||
style={{ background: 'rgba(255,255,255,0.04)', borderColor: 'rgba(255,255,255,0.1)' }}
|
||
>
|
||
<Form form={form} layout="vertical">
|
||
<Form.Item name="name" label="预设名称" rules={[{ required: true, message: '请输入名称' }]}>
|
||
<Input placeholder="例如:晚会右侧信息区" />
|
||
</Form.Item>
|
||
<Form.Item name="align" label="对齐方式" initialValue="right">
|
||
<Select>
|
||
<Select.Option value="right">右侧</Select.Option>
|
||
<Select.Option value="center">居中</Select.Option>
|
||
<Select.Option value="left">左侧</Select.Option>
|
||
</Select>
|
||
</Form.Item>
|
||
<Form.Item label="字段定义(用于内容预设)">
|
||
<FieldDefsEditor
|
||
value={fieldDefs}
|
||
onChange={(fds) => {
|
||
setFieldDefs(fds);
|
||
setPreviewData(buildPreviewContentData(fds));
|
||
}}
|
||
/>
|
||
</Form.Item>
|
||
<Form.Item name="html" label={
|
||
<span>HTML 模板 <Text style={{ color: '#888', fontSize: 11 }}>(可用 {'{{字段Key}}'} 占位)</Text></span>
|
||
}>
|
||
<TextArea
|
||
rows={10}
|
||
placeholder={'<!DOCTYPE html>\n<html>...\n <div class="title">{{mainTitle}}</div>\n...</html>'}
|
||
style={{ fontFamily: 'monospace', fontSize: 12 }}
|
||
/>
|
||
</Form.Item>
|
||
<Form.Item name="extraCss" label="额外 CSS(可选)">
|
||
<TextArea rows={3} placeholder=".lt-container { border-radius: 24px; }" style={{ fontFamily: 'monospace', fontSize: 12 }} />
|
||
</Form.Item>
|
||
<Form.Item>
|
||
<Space>
|
||
<Button type="primary" onClick={handleSave} loading={saving}>
|
||
{selected ? '更新预设' : '保存为新预设'}
|
||
</Button>
|
||
<Button onClick={clearForm}>清空</Button>
|
||
</Space>
|
||
</Form.Item>
|
||
</Form>
|
||
<Divider style={{ borderColor: 'rgba(255,255,255,0.1)' }} />
|
||
<div>
|
||
<Text strong style={{ color: '#aaa', fontSize: 12 }}>预设预览(使用字段默认值)</Text>
|
||
<div style={{ marginTop: 8 }}>
|
||
<PreviewPanel preset={currentPreviewPreset} contentData={previewData} />
|
||
</div>
|
||
</div>
|
||
</Card>
|
||
</Col>
|
||
</Row>
|
||
);
|
||
}
|
||
|
||
// ── Content Presets Tab ─────────────────────────────────────────────────
|
||
function ContentPresetsTab({ contentPresets, appearancePresets, onRefresh }) {
|
||
const [selected, setSelected] = React.useState(null);
|
||
const [form] = Form.useForm();
|
||
const [fields, setFields] = React.useState({});
|
||
const [saving, setSaving] = React.useState(false);
|
||
|
||
const cp = selected ? contentPresets.find(c => c.id === selected) : null;
|
||
|
||
const selectedApId = Form.useWatch('appearancePresetId', form);
|
||
const ap = appearancePresets.find(p => p.id === selectedApId);
|
||
|
||
const selectPreset = (c) => {
|
||
setSelected(c.id);
|
||
form.setFieldsValue({
|
||
name: c.name,
|
||
appearancePresetId: c.appearancePresetId,
|
||
});
|
||
setFields(c.fields || {});
|
||
};
|
||
|
||
const clearForm = () => {
|
||
setSelected(null);
|
||
form.resetFields();
|
||
setFields({});
|
||
};
|
||
|
||
const handleSave = async () => {
|
||
try {
|
||
const values = await form.validateFields();
|
||
setSaving(true);
|
||
const body = { ...values, fields };
|
||
let res;
|
||
if (selected) {
|
||
res = await fetch('/api/content-presets/' + selected, {
|
||
method: 'PUT',
|
||
headers: { 'Content-Type': 'application/json' },
|
||
body: JSON.stringify(body)
|
||
});
|
||
} else {
|
||
res = await fetch('/api/content-presets', {
|
||
method: 'POST',
|
||
headers: { 'Content-Type': 'application/json' },
|
||
body: JSON.stringify(body)
|
||
});
|
||
}
|
||
if (!res.ok) throw new Error('保存失败');
|
||
message.success('内容预设已保存');
|
||
clearForm();
|
||
onRefresh();
|
||
} catch(e) {
|
||
if (e.errorFields) return;
|
||
message.error('保存失败: ' + e.message);
|
||
} finally {
|
||
setSaving(false);
|
||
}
|
||
};
|
||
|
||
const handleDelete = async (id) => {
|
||
await fetch('/api/content-presets/' + id, { method: 'DELETE' });
|
||
if (selected === id) clearForm();
|
||
message.success('已删除');
|
||
onRefresh();
|
||
};
|
||
|
||
// Build preview with filled fields
|
||
const previewContentData = React.useMemo(() => {
|
||
const data = {};
|
||
if (ap && ap.fieldDefs) {
|
||
ap.fieldDefs.forEach(fd => { data[fd.key] = fd.defaultValue || ''; });
|
||
}
|
||
return { ...data, ...fields };
|
||
}, [ap, fields]);
|
||
|
||
return (
|
||
<Row gutter={16}>
|
||
<Col xs={24} md={8}>
|
||
<div style={{ marginBottom: 12, display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
|
||
<Text strong style={{ color: '#fff' }}>内容预设列表</Text>
|
||
<Button size="small" type="primary" onClick={clearForm}>+ 新建</Button>
|
||
</div>
|
||
{contentPresets.length === 0
|
||
? <Empty description="暂无内容预设" />
|
||
: contentPresets.map(c => {
|
||
const linkAp = appearancePresets.find(p => p.id === c.appearancePresetId);
|
||
return (
|
||
<Card
|
||
key={c.id}
|
||
size="small"
|
||
className={`preset-list-item${selected === c.id ? ' selected' : ''}`}
|
||
style={{ marginBottom: 8, background: 'rgba(255,255,255,0.04)', borderColor: 'rgba(255,255,255,0.1)' }}
|
||
onClick={() => selectPreset(c)}
|
||
>
|
||
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
|
||
<div>
|
||
<Text style={{ color: '#fff', fontSize: 13 }}>{c.name}</Text>
|
||
<br/>
|
||
<Text style={{ color: '#888', fontSize: 11 }}>
|
||
外观: {linkAp ? linkAp.name : <span style={{color:'#ff4d4f'}}>未链接</span>}
|
||
</Text>
|
||
</div>
|
||
<Popconfirm title="确认删除?" onConfirm={e => { e.stopPropagation(); handleDelete(c.id); }} onCancel={e => e.stopPropagation()}>
|
||
<Button danger size="small" type="text" onClick={e => e.stopPropagation()}>删除</Button>
|
||
</Popconfirm>
|
||
</div>
|
||
</Card>
|
||
);
|
||
})
|
||
}
|
||
</Col>
|
||
<Col xs={24} md={16}>
|
||
<Card
|
||
title={<Text style={{ color: '#fff' }}>{selected ? '编辑内容预设' : '新建内容预设'}</Text>}
|
||
style={{ background: 'rgba(255,255,255,0.04)', borderColor: 'rgba(255,255,255,0.1)' }}
|
||
>
|
||
<Form form={form} layout="vertical">
|
||
<Form.Item name="name" label="预设名称" rules={[{ required: true, message: '请输入名称' }]}>
|
||
<Input placeholder="例如:节目单第三项" />
|
||
</Form.Item>
|
||
<Form.Item name="appearancePresetId" label="关联外观预设">
|
||
<Select placeholder="选择外观预设" allowClear onChange={() => setFields({})}>
|
||
{appearancePresets.map(p => (
|
||
<Select.Option key={p.id} value={p.id}>{p.name}</Select.Option>
|
||
))}
|
||
</Select>
|
||
</Form.Item>
|
||
{ap && ap.fieldDefs && ap.fieldDefs.length > 0 && (
|
||
<div>
|
||
<Divider style={{ borderColor: 'rgba(255,255,255,0.1)', marginBottom: 12 }}>填写内容字段</Divider>
|
||
{ap.fieldDefs.map(fd => (
|
||
<Form.Item key={fd.key} label={`${fd.label || fd.key}`} style={{ marginBottom: 12 }}>
|
||
<Input
|
||
placeholder={fd.defaultValue || `请输入 ${fd.label || fd.key}`}
|
||
value={fields[fd.key] !== undefined ? fields[fd.key] : (fd.defaultValue || '')}
|
||
onChange={e => setFields(prev => ({ ...prev, [fd.key]: e.target.value }))}
|
||
/>
|
||
</Form.Item>
|
||
))}
|
||
</div>
|
||
)}
|
||
{ap && (!ap.fieldDefs || ap.fieldDefs.length === 0) && (
|
||
<Alert message="该外观预设没有定义字段" type="info" showIcon style={{ marginBottom: 12 }} />
|
||
)}
|
||
<Form.Item>
|
||
<Space>
|
||
<Button type="primary" onClick={handleSave} loading={saving}>
|
||
{selected ? '更新预设' : '保存为新预设'}
|
||
</Button>
|
||
<Button onClick={clearForm}>清空</Button>
|
||
</Space>
|
||
</Form.Item>
|
||
</Form>
|
||
{ap && (
|
||
<>
|
||
<Divider style={{ borderColor: 'rgba(255,255,255,0.1)' }} />
|
||
<div>
|
||
<Text strong style={{ color: '#aaa', fontSize: 12 }}>内容预设预览</Text>
|
||
<div style={{ marginTop: 8 }}>
|
||
<PreviewPanel preset={ap} contentData={previewContentData} />
|
||
</div>
|
||
</div>
|
||
</>
|
||
)}
|
||
</Card>
|
||
</Col>
|
||
</Row>
|
||
);
|
||
}
|
||
|
||
// ── Live Control Tab ────────────────────────────────────────────────────
|
||
function LiveControlTab({ contentPresets, appearancePresets, activeContentPresetId, activePresetId, autoHideDuration, onRefresh }) {
|
||
const [selectedIdx, setSelectedIdx] = React.useState(0);
|
||
const [activating, setActivating] = React.useState(false);
|
||
const [localAutoHide, setLocalAutoHide] = React.useState(autoHideDuration || 0);
|
||
const [autoHideEnabled, setAutoHideEnabled] = React.useState((autoHideDuration || 0) > 0);
|
||
const containerRef = React.useRef(null);
|
||
|
||
// Update local state when prop changes
|
||
React.useEffect(() => {
|
||
setLocalAutoHide(autoHideDuration || 0);
|
||
setAutoHideEnabled((autoHideDuration || 0) > 0);
|
||
}, [autoHideDuration]);
|
||
|
||
// Focus for keyboard navigation
|
||
React.useEffect(() => {
|
||
if (containerRef.current) containerRef.current.focus();
|
||
}, []);
|
||
|
||
const selectedCp = contentPresets[selectedIdx];
|
||
const selectedAp = selectedCp ? appearancePresets.find(p => p.id === selectedCp.appearancePresetId) : null;
|
||
|
||
// Determine preview content data
|
||
const previewData = React.useMemo(() => {
|
||
if (!selectedAp || !selectedCp) return {};
|
||
const data = {};
|
||
(selectedAp.fieldDefs || []).forEach(fd => { data[fd.key] = fd.defaultValue || ''; });
|
||
return { ...data, ...selectedCp.fields };
|
||
}, [selectedAp, selectedCp]);
|
||
|
||
const handleActivate = React.useCallback(async (cpId) => {
|
||
const id = cpId || (selectedCp ? selectedCp.id : null);
|
||
if (!id) return;
|
||
setActivating(true);
|
||
try {
|
||
const effectiveDuration = autoHideEnabled ? localAutoHide : 0;
|
||
await fetch('/api/content-presets/' + id + '/activate', {
|
||
method: 'POST',
|
||
headers: { 'Content-Type': 'application/json' },
|
||
body: JSON.stringify({ autoHideDuration: effectiveDuration })
|
||
});
|
||
onRefresh();
|
||
} finally {
|
||
setActivating(false);
|
||
}
|
||
}, [selectedCp, autoHideEnabled, localAutoHide, onRefresh]);
|
||
|
||
const handleClear = async () => {
|
||
await fetch('/api/clear', { method: 'POST' });
|
||
onRefresh();
|
||
};
|
||
|
||
const handleAutoHideChange = async (val) => {
|
||
setLocalAutoHide(val || 0);
|
||
const effectiveDuration = autoHideEnabled ? (val || 0) : 0;
|
||
await fetch('/api/settings', {
|
||
method: 'PUT',
|
||
headers: { 'Content-Type': 'application/json' },
|
||
body: JSON.stringify({ autoHideDuration: effectiveDuration })
|
||
});
|
||
onRefresh();
|
||
};
|
||
|
||
const handleAutoHideToggle = async (checked) => {
|
||
setAutoHideEnabled(checked);
|
||
const effectiveDuration = checked ? localAutoHide : 0;
|
||
await fetch('/api/settings', {
|
||
method: 'PUT',
|
||
headers: { 'Content-Type': 'application/json' },
|
||
body: JSON.stringify({ autoHideDuration: effectiveDuration })
|
||
});
|
||
onRefresh();
|
||
};
|
||
|
||
// Keyboard handler for the focused list container
|
||
const handleKeyDown = React.useCallback((e) => {
|
||
if (contentPresets.length === 0) return;
|
||
if (e.key === 'ArrowUp') {
|
||
e.preventDefault();
|
||
setSelectedIdx(i => Math.max(0, i - 1));
|
||
} else if (e.key === 'ArrowDown') {
|
||
e.preventDefault();
|
||
setSelectedIdx(i => Math.min(contentPresets.length - 1, i + 1));
|
||
} else if (e.key === 'Enter') {
|
||
e.preventDefault();
|
||
handleActivate();
|
||
}
|
||
}, [contentPresets.length, handleActivate]);
|
||
|
||
// Also listen globally
|
||
React.useEffect(() => {
|
||
const fn = (e) => {
|
||
if (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA') return;
|
||
if (e.key === 'ArrowUp') { e.preventDefault(); setSelectedIdx(i => Math.max(0, i - 1)); }
|
||
else if (e.key === 'ArrowDown') { e.preventDefault(); setSelectedIdx(i => Math.min(contentPresets.length - 1, i + 1)); }
|
||
else if (e.key === 'Enter') { e.preventDefault(); handleActivate(); }
|
||
};
|
||
window.addEventListener('keydown', fn);
|
||
return () => window.removeEventListener('keydown', fn);
|
||
}, [contentPresets.length, handleActivate]);
|
||
|
||
return (
|
||
<Row gutter={16}>
|
||
<Col xs={24} md={10}>
|
||
{/* Auto-hide settings */}
|
||
<Card
|
||
size="small"
|
||
style={{ marginBottom: 12, background: 'rgba(255,255,255,0.04)', borderColor: 'rgba(255,255,255,0.1)' }}
|
||
>
|
||
<Space wrap>
|
||
<Text style={{ color: '#aaa' }}>自动隐藏:</Text>
|
||
<Switch
|
||
checked={autoHideEnabled}
|
||
onChange={handleAutoHideToggle}
|
||
checkedChildren="开"
|
||
unCheckedChildren="关"
|
||
/>
|
||
{autoHideEnabled && (
|
||
<>
|
||
<InputNumber
|
||
min={500}
|
||
max={60000}
|
||
step={500}
|
||
value={localAutoHide}
|
||
onChange={handleAutoHideChange}
|
||
addonAfter="ms"
|
||
style={{ width: 140 }}
|
||
/>
|
||
<Text style={{ color: '#888', fontSize: 12 }}>
|
||
({(localAutoHide / 1000).toFixed(1)} 秒后隐藏)
|
||
</Text>
|
||
</>
|
||
)}
|
||
</Space>
|
||
</Card>
|
||
|
||
{/* Keyboard hints */}
|
||
<div style={{ marginBottom: 8, color: '#666', fontSize: 12 }}>
|
||
<span className="kbd-hint">↑</span> <span className="kbd-hint">↓</span> 切换
|
||
<span className="kbd-hint">Enter</span> 激活
|
||
</div>
|
||
|
||
{/* Content preset list */}
|
||
<div
|
||
ref={containerRef}
|
||
tabIndex={0}
|
||
onKeyDown={handleKeyDown}
|
||
style={{ outline: 'none' }}
|
||
>
|
||
{contentPresets.length === 0
|
||
? <Empty description={<span style={{color:'#666'}}>请先在"内容预设"标签页创建内容预设</span>} />
|
||
: contentPresets.map((cp, idx) => {
|
||
const isSelected = idx === selectedIdx;
|
||
const isActive = cp.id === activeContentPresetId;
|
||
const linkAp = appearancePresets.find(p => p.id === cp.appearancePresetId);
|
||
let cls = 'preset-list-item';
|
||
if (isSelected) cls += ' selected';
|
||
if (isActive) cls += ' active-live';
|
||
return (
|
||
<Card
|
||
key={cp.id}
|
||
size="small"
|
||
className={cls}
|
||
style={{
|
||
marginBottom: 6,
|
||
background: 'rgba(255,255,255,0.04)',
|
||
borderColor: isActive ? '#52c41a' : isSelected ? '#1890ff' : 'rgba(255,255,255,0.1)',
|
||
cursor: 'pointer'
|
||
}}
|
||
onClick={() => { setSelectedIdx(idx); }}
|
||
onDoubleClick={() => handleActivate(cp.id)}
|
||
>
|
||
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
|
||
<div style={{ flex: 1 }}>
|
||
<Space>
|
||
<Text style={{ color: '#fff', fontSize: 13, fontWeight: isSelected ? 600 : 400 }}>
|
||
{idx + 1}. {cp.name}
|
||
</Text>
|
||
{isActive && <Tag color="green" style={{ fontSize: 10 }}>直播中</Tag>}
|
||
</Space>
|
||
<br/>
|
||
<Text style={{ color: '#888', fontSize: 11 }}>
|
||
{linkAp ? linkAp.name : <span style={{color:'#ff7875'}}>外观未关联</span>}
|
||
</Text>
|
||
</div>
|
||
<Button
|
||
type={isActive ? 'default' : 'primary'}
|
||
size="small"
|
||
loading={activating && isSelected}
|
||
onClick={e => { e.stopPropagation(); handleActivate(cp.id); }}
|
||
style={{ minWidth: 52 }}
|
||
>
|
||
{isActive ? '重激活' : '激活'}
|
||
</Button>
|
||
</div>
|
||
</Card>
|
||
);
|
||
})
|
||
}
|
||
</div>
|
||
|
||
<div style={{ marginTop: 12 }}>
|
||
<Button danger block onClick={handleClear}>🔲 清空显示(透明)</Button>
|
||
</div>
|
||
</Col>
|
||
|
||
<Col xs={24} md={14}>
|
||
<Card
|
||
title={
|
||
<Space>
|
||
<Text style={{ color: '#aaa', fontSize: 12 }}>预览:</Text>
|
||
<Text style={{ color: '#fff', fontSize: 13 }}>
|
||
{selectedCp ? selectedCp.name : '(请选择内容预设)'}
|
||
</Text>
|
||
{selectedCp && selectedAp && <Tag color="blue" style={{ fontSize: 10 }}>{selectedAp.name}</Tag>}
|
||
</Space>
|
||
}
|
||
style={{ background: 'rgba(255,255,255,0.04)', borderColor: 'rgba(255,255,255,0.1)' }}
|
||
>
|
||
{selectedAp
|
||
? <PreviewPanel preset={selectedAp} contentData={previewData} />
|
||
: <div style={{ color: '#666', textAlign: 'center', padding: 40, fontSize: 13 }}>
|
||
{contentPresets.length === 0 ? '请先创建内容预设' : selectedCp ? '该内容预设未关联外观预设' : '请选择内容预设'}
|
||
</div>
|
||
}
|
||
|
||
{selectedCp && selectedAp && (
|
||
<div style={{ marginTop: 12 }}>
|
||
<Text style={{ color: '#888', fontSize: 12 }}>字段值:</Text>
|
||
<div style={{ marginTop: 6 }}>
|
||
{(selectedAp.fieldDefs || []).map(fd => (
|
||
<div key={fd.key} style={{ display: 'flex', gap: 8, marginBottom: 4, fontSize: 12 }}>
|
||
<Text style={{ color: '#666', minWidth: 80 }}>{fd.label || fd.key}:</Text>
|
||
<Text style={{ color: '#ccc' }}>{previewData[fd.key] || fd.defaultValue || '—'}</Text>
|
||
</div>
|
||
))}
|
||
</div>
|
||
</div>
|
||
)}
|
||
</Card>
|
||
</Col>
|
||
</Row>
|
||
);
|
||
}
|
||
|
||
// ── Main App ────────────────────────────────────────────────────────────
|
||
function App() {
|
||
const [data, setData] = React.useState({
|
||
presets: [],
|
||
contentPresets: [],
|
||
activePresetId: null,
|
||
activeContentPresetId: null,
|
||
settings: { autoHideDuration: 0 }
|
||
});
|
||
const [activeTab, setActiveTab] = React.useState('live');
|
||
const [loading, setLoading] = React.useState(true);
|
||
|
||
const fetchData = async () => {
|
||
try {
|
||
const res = await fetch('/api/presets');
|
||
const d = await res.json();
|
||
setData({
|
||
presets: d.presets || [],
|
||
contentPresets: d.contentPresets || [],
|
||
activePresetId: d.activePresetId || null,
|
||
activeContentPresetId: d.activeContentPresetId || null,
|
||
settings: d.settings || { autoHideDuration: 0 }
|
||
});
|
||
} catch(e) {
|
||
message.error('无法连接到服务器');
|
||
} finally {
|
||
setLoading(false);
|
||
}
|
||
};
|
||
|
||
React.useEffect(() => {
|
||
fetchData();
|
||
// WebSocket for real-time updates
|
||
const loc = window.location;
|
||
const proto = loc.protocol === 'https:' ? 'wss:' : 'ws:';
|
||
const ws = new WebSocket(proto + '//' + loc.host);
|
||
ws.onmessage = (e) => {
|
||
try {
|
||
const msg = JSON.parse(e.data);
|
||
if (msg.type === 'activePreset') {
|
||
fetchData();
|
||
}
|
||
} catch (_) {}
|
||
};
|
||
ws.onclose = () => {};
|
||
return () => ws.close();
|
||
}, []);
|
||
|
||
const tabs = [
|
||
{
|
||
key: 'live',
|
||
label: (
|
||
<Space>
|
||
<span>激活控制</span>
|
||
{data.activeContentPresetId && <Badge dot color="green" />}
|
||
</Space>
|
||
),
|
||
children: (
|
||
<LiveControlTab
|
||
contentPresets={data.contentPresets}
|
||
appearancePresets={data.presets}
|
||
activeContentPresetId={data.activeContentPresetId}
|
||
activePresetId={data.activePresetId}
|
||
autoHideDuration={data.settings.autoHideDuration}
|
||
onRefresh={fetchData}
|
||
/>
|
||
)
|
||
},
|
||
{
|
||
key: 'appearance',
|
||
label: '外观预设',
|
||
children: (
|
||
<AppearancePresetsTab
|
||
presets={data.presets}
|
||
onRefresh={fetchData}
|
||
/>
|
||
)
|
||
},
|
||
{
|
||
key: 'content',
|
||
label: '内容预设',
|
||
children: (
|
||
<ContentPresetsTab
|
||
contentPresets={data.contentPresets}
|
||
appearancePresets={data.presets}
|
||
onRefresh={fetchData}
|
||
/>
|
||
)
|
||
}
|
||
];
|
||
|
||
return (
|
||
<ConfigProvider
|
||
theme={{
|
||
algorithm: theme.darkAlgorithm,
|
||
token: {
|
||
colorBgBase: '#0a0c14',
|
||
colorBgContainer: 'rgba(255,255,255,0.04)',
|
||
borderRadius: 8,
|
||
}
|
||
}}
|
||
>
|
||
<Layout style={{ minHeight: '100vh', background: 'transparent' }}>
|
||
<Header style={{ background: 'rgba(0,0,0,0.6)', backdropFilter: 'blur(12px)', borderBottom: '1px solid rgba(255,255,255,0.08)' }}>
|
||
<div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', height: '100%' }}>
|
||
<div>
|
||
<Title level={4} style={{ margin: 0, color: '#fff', display: 'inline' }}>
|
||
OBS Lower Thirds Manager
|
||
</Title>
|
||
<Text style={{ color: '#888', marginLeft: 12, fontSize: 13 }}>管理面板</Text>
|
||
</div>
|
||
<Space>
|
||
<Text style={{ color: '#666', fontSize: 12 }}>
|
||
Widget URL: <code style={{ background: 'rgba(255,255,255,0.1)', padding: '2px 6px', borderRadius: 4 }}>
|
||
{window.location.origin}/widget.html
|
||
</code>
|
||
</Text>
|
||
<Button size="small" onClick={fetchData}>刷新</Button>
|
||
</Space>
|
||
</div>
|
||
</Header>
|
||
<Content style={{ padding: '20px 24px' }}>
|
||
{loading ? (
|
||
<div style={{ display: 'flex', justifyContent: 'center', paddingTop: 80 }}>
|
||
<Spin size="large" />
|
||
</div>
|
||
) : (
|
||
<Tabs
|
||
activeKey={activeTab}
|
||
onChange={setActiveTab}
|
||
items={tabs}
|
||
size="large"
|
||
/>
|
||
)}
|
||
</Content>
|
||
</Layout>
|
||
</ConfigProvider>
|
||
);
|
||
}
|
||
|
||
const root = ReactDOM.createRoot(document.getElementById('root'));
|
||
root.render(<App />);
|
||
</script>
|
||
</body>
|
||
</html>
|