tavern-harness 0.1.33 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +99 -0
- package/package.json +1 -1
- package/persistence-server.mjs +60 -1
- package/sandbox-server.mjs +11 -8
- package/src/App.tsx +1 -1
- package/src/components/CharactersView.tsx +1 -1
- package/src/components/ChatView.tsx +5 -3
- package/src/components/FileDisplayModal.tsx +105 -2
- package/src/components/SettingsView.tsx +194 -22
- package/src/components/Sidebar.tsx +26 -27
- package/src/components/WorkspaceFileManagerModal.tsx +2 -2
- package/src/components/shared.tsx +28 -9
- package/src/core/autoHideScrollbars.ts +191 -0
- package/src/core/browserPreferences.ts +6 -1
- package/src/core/i18n.ts +66 -3
- package/src/core/imageFilename.ts +21 -0
- package/src/core/imageGeneration.ts +32 -0
- package/src/core/prompts.ts +6 -1
- package/src/core/toolDefinitions.ts +21 -0
- package/src/core/tools/generatedSkillExecutor.ts +31 -2
- package/src/core/tools/toolExecutor.ts +104 -8
- package/src/db/database.ts +53 -12
- package/src/generated/changelog.json +140 -1
- package/src/main.tsx +3 -0
- package/src/store/store.ts +32 -17
- package/src/theme/chat.css +114 -54
- package/src/theme/global.css +122 -57
- package/src/theme/theme.ts +96 -43
- package/src/theme/views.css +110 -33
- package/src/types/models.ts +16 -1
- package/vite.config.ts +1 -1
|
@@ -1,20 +1,23 @@
|
|
|
1
|
-
import { memo, useRef, useState, type PointerEvent as ReactPointerEvent } from 'react';
|
|
1
|
+
import { memo, useEffect, useRef, useState, type PointerEvent as ReactPointerEvent } from 'react';
|
|
2
2
|
import { useStore } from '../store/store';
|
|
3
3
|
import { db, DEFAULT_GENERATION_SETTINGS } from '../db/database';
|
|
4
|
-
import { type ThemeMode } from '../theme/theme';
|
|
5
|
-
import { type ApiProvider, type AppLanguage, type ReasoningEffort, type VoiceProvider, type VoiceProviderKind, type VoiceId, enabledModelIds, modelAliases } from '../types/models';
|
|
4
|
+
import { type ThemeMode, type ThemePaletteId } from '../theme/theme';
|
|
5
|
+
import { type ApiProvider, type AppLanguage, type ImageProvider, type ImageProviderKind, type ReasoningEffort, type VoiceProvider, type VoiceProviderKind, type VoiceId, enabledModelIds, modelAliases } from '../types/models';
|
|
6
6
|
import { VOICE_PROVIDER_OPTIONS, previewVoice } from '../core/voice';
|
|
7
7
|
import { Icon, Modal } from './shared';
|
|
8
8
|
import { DeleteConfirmDialog } from './DeleteConfirmDialog';
|
|
9
9
|
import { isNetworkLikeError, toProxyUrl } from '../core/proxy';
|
|
10
10
|
import { useT } from '../core/i18n';
|
|
11
11
|
import { APP_VERSION, useHasUnseenVersion } from '../core/version';
|
|
12
|
+
import { IMAGE_MODELS } from '../core/imageGeneration';
|
|
12
13
|
import { VersionHistoryModal } from './VersionHistoryModal';
|
|
13
14
|
|
|
14
15
|
// ============================================================
|
|
15
16
|
// 设置视图(模型服务 / 超参数 / 主题)
|
|
16
17
|
// ============================================================
|
|
17
18
|
|
|
19
|
+
type SettingsTab = 'api' | 'model' | 'general';
|
|
20
|
+
|
|
18
21
|
export const SettingsView = memo(function SettingsView() {
|
|
19
22
|
const settings = useStore((s) => s.settings);
|
|
20
23
|
const setSettings = useStore((s) => s.setSettings);
|
|
@@ -24,6 +27,9 @@ export const SettingsView = memo(function SettingsView() {
|
|
|
24
27
|
const refreshProviders = useStore((s) => s.refreshProviders);
|
|
25
28
|
const voiceProviders = useStore((s) => s.voiceProviders);
|
|
26
29
|
const refreshVoiceProviders = useStore((s) => s.refreshVoiceProviders);
|
|
30
|
+
const imageProviders = useStore((s) => s.imageProviders);
|
|
31
|
+
const refreshImageProviders = useStore((s) => s.refreshImageProviders);
|
|
32
|
+
const [tab, setTab] = useState<SettingsTab>('api');
|
|
27
33
|
const [showResetConfirm, setShowResetConfirm] = useState(false);
|
|
28
34
|
const [resetting, setResetting] = useState(false);
|
|
29
35
|
const [showVersionHistory, setShowVersionHistory] = useState(false);
|
|
@@ -107,18 +113,33 @@ export const SettingsView = memo(function SettingsView() {
|
|
|
107
113
|
addToast(t('toast.genParamsReset'));
|
|
108
114
|
};
|
|
109
115
|
return (
|
|
110
|
-
<div className="view-page">
|
|
116
|
+
<div className="view-page settings-page">
|
|
111
117
|
<div className="view-col">
|
|
112
118
|
<div>
|
|
113
119
|
<h2 className="view-title">{t('settings.title')}</h2>
|
|
114
120
|
<p className="view-sub">{t('settings.sub')}</p>
|
|
115
121
|
</div>
|
|
116
122
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
123
|
+
<div className="side-nav view-tabs" data-hscroll role="tablist" aria-label={t('settings.title')}>
|
|
124
|
+
<button type="button" role="tab" aria-selected={tab === 'api'} className={`nav-chip ${tab === 'api' ? 'active' : ''}`} onClick={() => setTab('api')}>
|
|
125
|
+
{t('settings.tabApi')}
|
|
126
|
+
</button>
|
|
127
|
+
<button type="button" role="tab" aria-selected={tab === 'model'} className={`nav-chip ${tab === 'model' ? 'active' : ''}`} onClick={() => setTab('model')}>
|
|
128
|
+
{t('settings.tabModel')}
|
|
129
|
+
</button>
|
|
130
|
+
<button type="button" role="tab" aria-selected={tab === 'general'} className={`nav-chip ${tab === 'general' ? 'active' : ''}`} onClick={() => setTab('general')}>
|
|
131
|
+
{t('settings.tabGeneral')}
|
|
132
|
+
</button>
|
|
133
|
+
</div>
|
|
120
134
|
|
|
121
|
-
<div className="
|
|
135
|
+
<div className="settings-tab-panel" data-active={tab === 'api' ? 'true' : 'false'} role="tabpanel">
|
|
136
|
+
<ProviderManager providers={providers} onChanged={refreshProviders} />
|
|
137
|
+
<VoiceProviderManager providers={voiceProviders} onChanged={refreshVoiceProviders} />
|
|
138
|
+
<ImageProviderManager providers={imageProviders} onChanged={refreshImageProviders} />
|
|
139
|
+
</div>
|
|
140
|
+
|
|
141
|
+
<div className="settings-tab-panel" data-active={tab === 'model' ? 'true' : 'false'} role="tabpanel">
|
|
142
|
+
<div className="set-section">
|
|
122
143
|
<h3>{t('settings.toolExecution')}</h3>
|
|
123
144
|
<div className="set-row settings-tool-row">
|
|
124
145
|
<div>
|
|
@@ -278,9 +299,10 @@ export const SettingsView = memo(function SettingsView() {
|
|
|
278
299
|
</tr>
|
|
279
300
|
</tbody>
|
|
280
301
|
</table>
|
|
302
|
+
</div>
|
|
281
303
|
</div>
|
|
282
304
|
|
|
283
|
-
<div className="
|
|
305
|
+
<div className="settings-tab-panel" data-active={tab === 'general' ? 'true' : 'false'} role="tabpanel">
|
|
284
306
|
{/* 界面语言 */}
|
|
285
307
|
<div className="set-section">
|
|
286
308
|
<h3>{t('settings.language')}</h3>
|
|
@@ -300,18 +322,37 @@ export const SettingsView = memo(function SettingsView() {
|
|
|
300
322
|
{/* 主题 */}
|
|
301
323
|
<div className="set-section">
|
|
302
324
|
<h3>{t('settings.theme')}</h3>
|
|
303
|
-
<
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
325
|
+
<div className="theme-palette-grid" role="radiogroup" aria-label={t('settings.colorPalette')}>
|
|
326
|
+
{(['amber', 'jade', 'rose', 'indigo'] as ThemePaletteId[]).map((palette) => (
|
|
327
|
+
<button
|
|
328
|
+
type="button"
|
|
329
|
+
role="radio"
|
|
330
|
+
aria-checked={settings.themePalette === palette}
|
|
331
|
+
className={`theme-palette-option theme-palette-${palette}${settings.themePalette === palette ? ' active' : ''}`}
|
|
332
|
+
onClick={() => setSettings({ themePalette: palette })}
|
|
333
|
+
key={palette}
|
|
334
|
+
>
|
|
335
|
+
<span className="theme-palette-preview" aria-hidden="true">
|
|
336
|
+
<i /><i /><i />
|
|
337
|
+
</span>
|
|
338
|
+
<span>{t(`settings.palette.${palette}`)}</span>
|
|
339
|
+
</button>
|
|
340
|
+
))}
|
|
341
|
+
</div>
|
|
342
|
+
<label className="theme-mode-row">
|
|
343
|
+
<span>{t('settings.appearance')}</span>
|
|
344
|
+
<select
|
|
345
|
+
className="select"
|
|
346
|
+
aria-label={t('settings.appearance')}
|
|
347
|
+
value={settings.themeMode}
|
|
348
|
+
onChange={(e) => setSettings({ themeMode: e.target.value as ThemeMode })}
|
|
349
|
+
>
|
|
350
|
+
<option value="system">{t('settings.system')}</option>
|
|
351
|
+
<option value="light">{t('settings.light')}</option>
|
|
352
|
+
<option value="dark">{t('settings.dark')}</option>
|
|
353
|
+
</select>
|
|
354
|
+
</label>
|
|
313
355
|
</div>
|
|
314
|
-
</div>
|
|
315
356
|
|
|
316
357
|
<div className="set-section settings-about-section">
|
|
317
358
|
<div>
|
|
@@ -336,6 +377,7 @@ export const SettingsView = memo(function SettingsView() {
|
|
|
336
377
|
<Icon name="refresh" size={14} /> {t('settings.resetBtn')}
|
|
337
378
|
</button>
|
|
338
379
|
</div>
|
|
380
|
+
</div>
|
|
339
381
|
</div>
|
|
340
382
|
|
|
341
383
|
{showVersionHistory && <VersionHistoryModal onClose={() => setShowVersionHistory(false)} />}
|
|
@@ -463,6 +505,136 @@ function ApiKeyField({ value, onChange, required = false }: { value: string; onC
|
|
|
463
505
|
);
|
|
464
506
|
}
|
|
465
507
|
|
|
508
|
+
function ScrollingProviderName({ name }: { name: string }) {
|
|
509
|
+
const containerRef = useRef<HTMLSpanElement>(null);
|
|
510
|
+
const textRef = useRef<HTMLSpanElement>(null);
|
|
511
|
+
const [overflowing, setOverflowing] = useState(false);
|
|
512
|
+
|
|
513
|
+
useEffect(() => {
|
|
514
|
+
const updateOverflow = () => {
|
|
515
|
+
setOverflowing((textRef.current?.scrollWidth ?? 0) > (containerRef.current?.clientWidth ?? 0));
|
|
516
|
+
};
|
|
517
|
+
updateOverflow();
|
|
518
|
+
const observer = new ResizeObserver(updateOverflow);
|
|
519
|
+
if (containerRef.current) observer.observe(containerRef.current);
|
|
520
|
+
return () => observer.disconnect();
|
|
521
|
+
}, [name]);
|
|
522
|
+
|
|
523
|
+
return (
|
|
524
|
+
<span ref={containerRef} className={`p-name card-name-scroll${overflowing ? ' is-overflowing' : ''}`}>
|
|
525
|
+
<span ref={textRef} className="card-name-scroll-text">{name}</span>
|
|
526
|
+
{overflowing && <span className="card-name-scroll-text card-name-scroll-copy" aria-hidden="true">{name}</span>}
|
|
527
|
+
</span>
|
|
528
|
+
);
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
function ImageProviderManager({ providers, onChanged }: { providers: ImageProvider[]; onChanged: () => void }) {
|
|
532
|
+
const settings = useStore((s) => s.settings)!;
|
|
533
|
+
const setSettings = useStore((s) => s.setSettings);
|
|
534
|
+
const addToast = useStore((s) => s.addToast);
|
|
535
|
+
const t = useT();
|
|
536
|
+
const [editing, setEditing] = useState<ImageProvider | null>(null);
|
|
537
|
+
const [pendingDelete, setPendingDelete] = useState<ImageProvider | null>(null);
|
|
538
|
+
const providerName = (kind: ImageProviderKind) => t(`settings.imageProviderKind.${kind === 'bailian-token-plan' ? 'bailianTokenPlan' : kind}`);
|
|
539
|
+
|
|
540
|
+
const saveProvider = async (provider: ImageProvider) => {
|
|
541
|
+
if (provider.id != null) await db.imageProviders.update(provider.id, provider);
|
|
542
|
+
else await db.imageProviders.add(provider);
|
|
543
|
+
setEditing(null);
|
|
544
|
+
await onChanged();
|
|
545
|
+
addToast(t(provider.id != null ? 'toast.imageProviderUpdated' : 'toast.imageProviderAdded'));
|
|
546
|
+
};
|
|
547
|
+
|
|
548
|
+
return (
|
|
549
|
+
<div className="set-section">
|
|
550
|
+
<div className="settings-section-head">
|
|
551
|
+
<h3>{t('settings.imageProviderTitle')}</h3>
|
|
552
|
+
<button className="btn btn-sm settings-gen-reset" onClick={() => setEditing({ kind: 'bailian-token-plan', apiKey: '', isEnabled: true, createdAt: Date.now() })}>
|
|
553
|
+
<Icon name="plus" size={13} /> {t('settings.addProvider')}
|
|
554
|
+
</button>
|
|
555
|
+
</div>
|
|
556
|
+
<div style={{ display: 'flex', flexDirection: 'column', gap: 10 }}>
|
|
557
|
+
{providers.length === 0 && <div className="typo-body-sm text-muted">{t('settings.noImageProvider')}</div>}
|
|
558
|
+
{[...providers].sort((a, b) => b.createdAt - a.createdAt).map((provider) => (
|
|
559
|
+
<div key={provider.id} className={`provider-card ${provider.isEnabled ? '' : 'disabled'}`}>
|
|
560
|
+
<div className="p-head">
|
|
561
|
+
<label className="switch" title={provider.isEnabled ? t('settings.disable') : t('settings.enable')}>
|
|
562
|
+
<input type="checkbox" checked={provider.isEnabled} onChange={async () => { await db.imageProviders.update(provider.id!, { isEnabled: !provider.isEnabled }); await onChanged(); }} />
|
|
563
|
+
<span className="switch-slider" />
|
|
564
|
+
</label>
|
|
565
|
+
<ScrollingProviderName name={providerName(provider.kind)} />
|
|
566
|
+
<span className="p-status voice">{t('settings.imageServiceTag')}</span>
|
|
567
|
+
<div style={{ marginLeft: 'auto', display: 'flex', gap: 6 }}>
|
|
568
|
+
<button className="btn btn-sm" onClick={() => setEditing(provider)}>{t('common.edit')}</button>
|
|
569
|
+
<button className="btn btn-sm btn-danger" onClick={() => setPendingDelete(provider)}><Icon name="trash" size={12} /></button>
|
|
570
|
+
</div>
|
|
571
|
+
</div>
|
|
572
|
+
<div className="provider-inline-detail"><b>{t('settings.models')}:</b> {IMAGE_MODELS.join(', ')}</div>
|
|
573
|
+
</div>
|
|
574
|
+
))}
|
|
575
|
+
</div>
|
|
576
|
+
<div className="voice-default-card">
|
|
577
|
+
<div>
|
|
578
|
+
<div className="s-label">{t('settings.defaultImageModel')}</div>
|
|
579
|
+
<div className="s-desc">{t('settings.defaultImageModelDesc')}</div>
|
|
580
|
+
</div>
|
|
581
|
+
<select
|
|
582
|
+
className="select voice-default-select"
|
|
583
|
+
value={settings.activeImageProviderId != null && settings.defaultImageModel ? `${settings.activeImageProviderId}:${settings.defaultImageModel}` : ''}
|
|
584
|
+
onChange={(event) => {
|
|
585
|
+
const [providerId, model] = event.target.value.split(':');
|
|
586
|
+
void setSettings(event.target.value
|
|
587
|
+
? { activeImageProviderId: Number(providerId), defaultImageModel: model }
|
|
588
|
+
: { activeImageProviderId: null });
|
|
589
|
+
}}
|
|
590
|
+
>
|
|
591
|
+
<option value="">{t('settings.noDefaultImageProvider')}</option>
|
|
592
|
+
{providers.filter((provider) => provider.isEnabled).flatMap((provider) => IMAGE_MODELS.map((model) => (
|
|
593
|
+
<option key={`${provider.id}:${model}`} value={`${provider.id}:${model}`}>{providerName(provider.kind)} · {model}</option>
|
|
594
|
+
)))}
|
|
595
|
+
</select>
|
|
596
|
+
</div>
|
|
597
|
+
{editing && <Modal onClose={() => setEditing(null)}>
|
|
598
|
+
<div className="modal-head">
|
|
599
|
+
<span className="typo-dialog-title">{editing.id != null ? t('settings.editImageProvider') : t('settings.addImageProvider')}</span>
|
|
600
|
+
<button className="icon-btn" onClick={() => setEditing(null)}><Icon name="x" /></button>
|
|
601
|
+
</div>
|
|
602
|
+
<ImageProviderForm initial={editing} onSave={saveProvider} />
|
|
603
|
+
</Modal>}
|
|
604
|
+
{pendingDelete && <DeleteConfirmDialog title={t('settings.deleteImageProvider')} itemName={providerName(pendingDelete.kind)} onCancel={() => setPendingDelete(null)} onConfirm={async () => {
|
|
605
|
+
await db.imageProviders.delete(pendingDelete.id!);
|
|
606
|
+
if (settings.activeImageProviderId === pendingDelete.id) await setSettings({ activeImageProviderId: null });
|
|
607
|
+
setPendingDelete(null);
|
|
608
|
+
await onChanged();
|
|
609
|
+
}} />}
|
|
610
|
+
</div>
|
|
611
|
+
);
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
function ImageProviderForm({ initial, onSave }: { initial: ImageProvider; onSave: (provider: ImageProvider) => void }) {
|
|
615
|
+
const t = useT();
|
|
616
|
+
const [kind, setKind] = useState<ImageProviderKind>(initial.kind);
|
|
617
|
+
const [apiKey, setApiKey] = useState(initial.apiKey);
|
|
618
|
+
return <>
|
|
619
|
+
<div className="modal-body" style={{ display: 'flex', flexDirection: 'column', gap: 12 }}>
|
|
620
|
+
<div className="field">
|
|
621
|
+
<label>{t('settings.imageProvider')}</label>
|
|
622
|
+
<select className="select" value={kind} disabled={initial.id != null} onChange={(event) => setKind(event.target.value as ImageProviderKind)}>
|
|
623
|
+
<option value="bailian-token-plan">{t('settings.imageProviderKind.bailianTokenPlan')}</option>
|
|
624
|
+
</select>
|
|
625
|
+
</div>
|
|
626
|
+
<ApiKeyField value={apiKey} onChange={setApiKey} required />
|
|
627
|
+
<div className="field">
|
|
628
|
+
<label>{t('settings.models')}</label>
|
|
629
|
+
<div className="provider-inline-detail">{IMAGE_MODELS.join(', ')}</div>
|
|
630
|
+
</div>
|
|
631
|
+
</div>
|
|
632
|
+
<div className="modal-foot">
|
|
633
|
+
<button className="btn btn-primary" disabled={!apiKey.trim()} onClick={() => onSave({ ...initial, kind, apiKey: apiKey.trim(), isEnabled: true })}>{t('common.save')}</button>
|
|
634
|
+
</div>
|
|
635
|
+
</>;
|
|
636
|
+
}
|
|
637
|
+
|
|
466
638
|
function VoiceProviderManager({ providers, onChanged }: { providers: VoiceProvider[]; onChanged: () => void }) {
|
|
467
639
|
const settings = useStore((s) => s.settings)!;
|
|
468
640
|
const setSettings = useStore((s) => s.setSettings);
|
|
@@ -501,7 +673,7 @@ function VoiceProviderManager({ providers, onChanged }: { providers: VoiceProvid
|
|
|
501
673
|
<input type="checkbox" checked={provider.isEnabled} onChange={async () => { await db.voiceProviders.update(provider.id!, { isEnabled: !provider.isEnabled }); await onChanged(); }} />
|
|
502
674
|
<span className="switch-slider" />
|
|
503
675
|
</label>
|
|
504
|
-
<
|
|
676
|
+
<ScrollingProviderName name={providerName(provider.kind)} />
|
|
505
677
|
<span className="p-status voice">{t('settings.voiceServiceTag')}</span>
|
|
506
678
|
<div style={{ marginLeft: 'auto', display: 'flex', gap: 6 }}>
|
|
507
679
|
<button className="btn btn-sm" onClick={() => setEditing(provider)}>{t('common.edit')}</button>
|
|
@@ -630,7 +802,7 @@ function ProviderManager({ providers, onChanged }: { providers: ApiProvider[]; o
|
|
|
630
802
|
<input type="checkbox" checked={p.isEnabled} onChange={() => toggleEnabled(p)} />
|
|
631
803
|
<span className="switch-slider" />
|
|
632
804
|
</label>
|
|
633
|
-
<
|
|
805
|
+
<ScrollingProviderName name={p.name} />
|
|
634
806
|
<span className="p-status text">{t('settings.textServiceTag')}</span>
|
|
635
807
|
<div style={{ marginLeft: 'auto', display: 'flex', gap: 6 }}>
|
|
636
808
|
<button className="btn btn-sm" onClick={() => setEditing(p)}>{t('common.edit')}</button>
|
|
@@ -93,7 +93,7 @@ export function Sidebar() {
|
|
|
93
93
|
const saved = parseFloat(localStorage.getItem('tav-sidebar-w') || '0');
|
|
94
94
|
return saved >= SIDEBAR_MIN && saved <= SIDEBAR_MAX ? saved : 356;
|
|
95
95
|
});
|
|
96
|
-
const dragRef = useRef<{ startX: number; startW: number } | null>(null);
|
|
96
|
+
const dragRef = useRef<{ pointerId: number; startX: number; startW: number } | null>(null);
|
|
97
97
|
|
|
98
98
|
useEffect(() => {
|
|
99
99
|
const root = document.documentElement;
|
|
@@ -122,24 +122,13 @@ export function Sidebar() {
|
|
|
122
122
|
};
|
|
123
123
|
}, []);
|
|
124
124
|
|
|
125
|
-
useEffect(() =>
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
const onUp = () => {
|
|
133
|
-
dragRef.current = null;
|
|
134
|
-
document.body.classList.remove('resizing');
|
|
135
|
-
};
|
|
136
|
-
window.addEventListener('mousemove', onMove);
|
|
137
|
-
window.addEventListener('mouseup', onUp);
|
|
138
|
-
return () => {
|
|
139
|
-
window.removeEventListener('mousemove', onMove);
|
|
140
|
-
window.removeEventListener('mouseup', onUp);
|
|
141
|
-
};
|
|
142
|
-
}, []);
|
|
125
|
+
useEffect(() => () => document.body.classList.remove('resizing'), []);
|
|
126
|
+
|
|
127
|
+
const finishResize = (pointerId: number) => {
|
|
128
|
+
if (dragRef.current?.pointerId !== pointerId) return;
|
|
129
|
+
dragRef.current = null;
|
|
130
|
+
document.body.classList.remove('resizing');
|
|
131
|
+
};
|
|
143
132
|
|
|
144
133
|
useEffect(() => {
|
|
145
134
|
const isMobile = () => window.matchMedia('(max-width: 680px)').matches;
|
|
@@ -325,13 +314,13 @@ export function Sidebar() {
|
|
|
325
314
|
<button className={`nav-chip ${activeView === 'characters' ? 'active' : ''}`} onClick={() => { setActiveView('characters'); setMobileOpen(false); }}>
|
|
326
315
|
<Icon name="users" size={15} /> <span>{t('nav.characters')}</span>
|
|
327
316
|
</button>
|
|
328
|
-
<button className={`nav-chip ${activeView === 'stats' ? 'active' : ''}`} onClick={() => { setActiveView('stats'); setMobileOpen(false); }}>
|
|
329
|
-
<Icon name="trophy" size={15} /> <span>{t('nav.achievements')}</span>
|
|
330
|
-
</button>
|
|
331
317
|
<button className={`nav-chip ${activeView === 'settings' ? 'active' : ''}`} onClick={() => { setActiveView('settings'); setMobileOpen(false); }}>
|
|
332
318
|
<Icon name="settings" size={15} /> <span>{t('nav.settings')}</span>
|
|
333
319
|
{hasUnseenVersion && <span className="unseen-dot" title={t('changelog.unseen')} />}
|
|
334
320
|
</button>
|
|
321
|
+
<button className={`nav-chip ${activeView === 'stats' ? 'active' : ''}`} onClick={() => { setActiveView('stats'); setMobileOpen(false); }}>
|
|
322
|
+
<Icon name="trophy" size={15} /> <span>{t('nav.achievements')}</span>
|
|
323
|
+
</button>
|
|
335
324
|
</nav>
|
|
336
325
|
</div>
|
|
337
326
|
{mobileOpen && <button className="mobile-sidebar-backdrop" type="button" aria-label={t('common.close')} onClick={() => setMobileOpen(false)} />}
|
|
@@ -369,13 +358,13 @@ export function Sidebar() {
|
|
|
369
358
|
<button className={`nav-chip ${activeView === 'characters' ? 'active' : ''}`} onClick={() => setActiveView('characters')}>
|
|
370
359
|
<Icon name="users" size={13} /> {t('nav.characters')}
|
|
371
360
|
</button>
|
|
372
|
-
<button className={`nav-chip ${activeView === 'stats' ? 'active' : ''}`} onClick={() => setActiveView('stats')}>
|
|
373
|
-
<Icon name="trophy" size={13} /> {t('nav.achievements')}
|
|
374
|
-
</button>
|
|
375
361
|
<button className={`nav-chip ${activeView === 'settings' ? 'active' : ''}`} onClick={() => setActiveView('settings')}>
|
|
376
362
|
<Icon name="settings" size={13} /> {t('nav.settings')}
|
|
377
363
|
{hasUnseenVersion && <span className="unseen-dot" title={t('changelog.unseen')} />}
|
|
378
364
|
</button>
|
|
365
|
+
<button className={`nav-chip ${activeView === 'stats' ? 'active' : ''}`} onClick={() => setActiveView('stats')}>
|
|
366
|
+
<Icon name="trophy" size={13} /> {t('nav.achievements')}
|
|
367
|
+
</button>
|
|
379
368
|
</div>
|
|
380
369
|
</div>
|
|
381
370
|
|
|
@@ -546,10 +535,20 @@ export function Sidebar() {
|
|
|
546
535
|
<div
|
|
547
536
|
className="sidebar-resizer"
|
|
548
537
|
title={t('nav.resize')}
|
|
549
|
-
|
|
550
|
-
|
|
538
|
+
onPointerDown={(event) => {
|
|
539
|
+
if (!event.isPrimary || event.button !== 0) return;
|
|
540
|
+
event.currentTarget.setPointerCapture(event.pointerId);
|
|
541
|
+
dragRef.current = { pointerId: event.pointerId, startX: event.clientX, startW: sidebarW };
|
|
551
542
|
document.body.classList.add('resizing');
|
|
552
543
|
}}
|
|
544
|
+
onPointerMove={(event) => {
|
|
545
|
+
const drag = dragRef.current;
|
|
546
|
+
if (!drag || drag.pointerId !== event.pointerId) return;
|
|
547
|
+
const width = Math.min(SIDEBAR_MAX, Math.max(SIDEBAR_MIN, drag.startW + event.clientX - drag.startX));
|
|
548
|
+
setSidebarW(width);
|
|
549
|
+
}}
|
|
550
|
+
onPointerUp={(event) => finishResize(event.pointerId)}
|
|
551
|
+
onPointerCancel={(event) => finishResize(event.pointerId)}
|
|
553
552
|
/>
|
|
554
553
|
</aside>
|
|
555
554
|
</>
|
|
@@ -19,10 +19,10 @@ function extOf(path: string): string {
|
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
/** 与 file_display 相同的「按扩展名推断展示方式」逻辑 */
|
|
22
|
-
function kindFromPath(path: string): 'text' | 'html' | 'unsupported' {
|
|
22
|
+
function kindFromPath(path: string): 'text' | 'html' | 'image' | 'unsupported' {
|
|
23
23
|
const ext = extOf(path);
|
|
24
24
|
if (/^(html?)$/i.test(ext)) return 'html';
|
|
25
|
-
if (/^(png|jpe?g|gif|webp|svg|bmp|ico)$/i.test(ext)) return '
|
|
25
|
+
if (/^(png|jpe?g|gif|webp|svg|bmp|ico)$/i.test(ext)) return 'image';
|
|
26
26
|
return 'text';
|
|
27
27
|
}
|
|
28
28
|
|
|
@@ -232,18 +232,37 @@ export const Avatar = memo(function Avatar({
|
|
|
232
232
|
// 附件卡片:仅展示文件名(去掉图片/视频缩略图)
|
|
233
233
|
// ============================================================
|
|
234
234
|
|
|
235
|
-
export function AttachCard({ name, onRemove }: { name: string; onRemove?: () => void }) {
|
|
235
|
+
export function AttachCard({ name, src, onRemove }: { name: string; src?: string; onRemove?: () => void }) {
|
|
236
236
|
const t = useT();
|
|
237
|
+
const [previewing, setPreviewing] = useState(false);
|
|
238
|
+
const isImage = typeof src === 'string' && (src.startsWith('data:image/') || /\.(png|jpe?g|gif|webp|svg|bmp)(?:$|[?#])/i.test(src));
|
|
237
239
|
return (
|
|
238
|
-
|
|
239
|
-
<
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
240
|
+
<>
|
|
241
|
+
<div className={`attach-card ${isImage ? 'attach-card-image' : ''}`}>
|
|
242
|
+
{isImage ? (
|
|
243
|
+
<button type="button" className="attach-thumb-button" onClick={() => setPreviewing(true)} aria-label={name}>
|
|
244
|
+
<img className="attach-thumb" src={src} alt={name} />
|
|
245
|
+
</button>
|
|
246
|
+
) : <span className="attach-card-icon">📎</span>}
|
|
247
|
+
<span className="attach-card-name" title={name}>{name}</span>
|
|
248
|
+
{onRemove && (
|
|
249
|
+
<button className="attach-rm" onClick={onRemove} title={t('common.removeAttachment')}>
|
|
250
|
+
<Icon name="x" size={11} />
|
|
251
|
+
</button>
|
|
252
|
+
)}
|
|
253
|
+
</div>
|
|
254
|
+
{previewing && isImage && (
|
|
255
|
+
<Modal onClose={() => setPreviewing(false)} size="wide" className="image-preview-modal">
|
|
256
|
+
<div className="modal-head">
|
|
257
|
+
<span>{name}</span>
|
|
258
|
+
<button className="icon-btn" onClick={() => setPreviewing(false)} aria-label={t('common.close')}><Icon name="x" /></button>
|
|
259
|
+
</div>
|
|
260
|
+
<div className="image-preview-body" onClick={() => setPreviewing(false)}>
|
|
261
|
+
<img src={src} alt={name} />
|
|
262
|
+
</div>
|
|
263
|
+
</Modal>
|
|
245
264
|
)}
|
|
246
|
-
|
|
265
|
+
</>
|
|
247
266
|
);
|
|
248
267
|
}
|
|
249
268
|
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
// ============================================================
|
|
2
|
+
// 全局悬浮滚动条
|
|
3
|
+
//
|
|
4
|
+
// 原生滚动条会占据布局宽度、外观也随平台变化。这里把原生滚动条完全
|
|
5
|
+
// 隐藏(见 global.css),再以 position: fixed 的细条覆盖在滚动容器上:
|
|
6
|
+
// * 只在容器滚动时出现,静止一段时间后淡出;
|
|
7
|
+
// * 悬浮绘制,不参与 layout,出现/消失都不会让内容尺寸跳动。
|
|
8
|
+
//
|
|
9
|
+
// 滚动事件不冒泡,但可以在 document 上以 capture 捕获到所有容器。
|
|
10
|
+
// 启动时调用一次 installAutoHideScrollbars() 即可。
|
|
11
|
+
// ============================================================
|
|
12
|
+
|
|
13
|
+
const IDLE_HIDE_MS = 1400;
|
|
14
|
+
const THICKNESS = 6;
|
|
15
|
+
const EDGE_INSET = 3;
|
|
16
|
+
const MIN_THUMB = 28;
|
|
17
|
+
|
|
18
|
+
interface ThumbPair {
|
|
19
|
+
vertical: HTMLDivElement | null;
|
|
20
|
+
horizontal: HTMLDivElement | null;
|
|
21
|
+
timer: number;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
interface ScrollMetrics {
|
|
25
|
+
innerTop: number;
|
|
26
|
+
innerLeft: number;
|
|
27
|
+
innerWidth: number;
|
|
28
|
+
innerHeight: number;
|
|
29
|
+
scrollTop: number;
|
|
30
|
+
scrollLeft: number;
|
|
31
|
+
scrollWidth: number;
|
|
32
|
+
scrollHeight: number;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const pairs = new WeakMap<Element, ThumbPair>();
|
|
36
|
+
const dirty = new Set<Element>();
|
|
37
|
+
const active = new Set<Element>();
|
|
38
|
+
let layer: HTMLDivElement | null = null;
|
|
39
|
+
let frame = 0;
|
|
40
|
+
|
|
41
|
+
function getLayer(): HTMLDivElement {
|
|
42
|
+
if (layer?.isConnected) return layer;
|
|
43
|
+
layer = document.createElement('div');
|
|
44
|
+
layer.className = 'th-scrollbar-layer';
|
|
45
|
+
layer.setAttribute('aria-hidden', 'true');
|
|
46
|
+
document.body.appendChild(layer);
|
|
47
|
+
return layer;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function getPair(el: Element): ThumbPair {
|
|
51
|
+
let pair = pairs.get(el);
|
|
52
|
+
if (!pair) {
|
|
53
|
+
pair = { vertical: null, horizontal: null, timer: 0 };
|
|
54
|
+
pairs.set(el, pair);
|
|
55
|
+
}
|
|
56
|
+
return pair;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function getThumb(pair: ThumbPair, axis: 'vertical' | 'horizontal'): HTMLDivElement {
|
|
60
|
+
if (axis === 'vertical') {
|
|
61
|
+
if (!pair.vertical) {
|
|
62
|
+
pair.vertical = document.createElement('div');
|
|
63
|
+
pair.vertical.className = 'th-scrollbar-thumb th-scrollbar-thumb--v';
|
|
64
|
+
getLayer().appendChild(pair.vertical);
|
|
65
|
+
}
|
|
66
|
+
return pair.vertical;
|
|
67
|
+
}
|
|
68
|
+
if (!pair.horizontal) {
|
|
69
|
+
pair.horizontal = document.createElement('div');
|
|
70
|
+
pair.horizontal.className = 'th-scrollbar-thumb th-scrollbar-thumb--h';
|
|
71
|
+
getLayer().appendChild(pair.horizontal);
|
|
72
|
+
}
|
|
73
|
+
return pair.horizontal;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function measure(el: Element): ScrollMetrics | null {
|
|
77
|
+
if (el === document.documentElement) {
|
|
78
|
+
const se = document.scrollingElement ?? document.documentElement;
|
|
79
|
+
return {
|
|
80
|
+
innerTop: 0,
|
|
81
|
+
innerLeft: 0,
|
|
82
|
+
innerWidth: window.innerWidth,
|
|
83
|
+
innerHeight: window.innerHeight,
|
|
84
|
+
scrollTop: window.scrollY,
|
|
85
|
+
scrollLeft: window.scrollX,
|
|
86
|
+
scrollWidth: se.scrollWidth,
|
|
87
|
+
scrollHeight: se.scrollHeight,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
if (!(el instanceof HTMLElement) || !el.isConnected) return null;
|
|
91
|
+
const rect = el.getBoundingClientRect();
|
|
92
|
+
if (rect.width <= 0 || rect.height <= 0) return null;
|
|
93
|
+
return {
|
|
94
|
+
innerTop: rect.top + el.clientTop,
|
|
95
|
+
innerLeft: rect.left + el.clientLeft,
|
|
96
|
+
innerWidth: el.clientWidth,
|
|
97
|
+
innerHeight: el.clientHeight,
|
|
98
|
+
scrollTop: el.scrollTop,
|
|
99
|
+
scrollLeft: el.scrollLeft,
|
|
100
|
+
scrollWidth: el.scrollWidth,
|
|
101
|
+
scrollHeight: el.scrollHeight,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function positionVertical(node: HTMLDivElement, m: ScrollMetrics): boolean {
|
|
106
|
+
if (m.scrollHeight - m.innerHeight <= 1) return false;
|
|
107
|
+
const trackTop = m.innerTop + EDGE_INSET;
|
|
108
|
+
const trackHeight = m.innerHeight - EDGE_INSET * 2;
|
|
109
|
+
const thumbHeight = Math.max(MIN_THUMB, (trackHeight * m.innerHeight) / m.scrollHeight);
|
|
110
|
+
if (thumbHeight >= trackHeight) return false;
|
|
111
|
+
const maxScroll = m.scrollHeight - m.innerHeight;
|
|
112
|
+
const progress = maxScroll > 0 ? Math.min(1, Math.max(0, m.scrollTop / maxScroll)) : 0;
|
|
113
|
+
node.style.left = `${m.innerLeft + m.innerWidth - THICKNESS - EDGE_INSET}px`;
|
|
114
|
+
node.style.top = `${trackTop + (trackHeight - thumbHeight) * progress}px`;
|
|
115
|
+
node.style.height = `${thumbHeight}px`;
|
|
116
|
+
return true;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function positionHorizontal(node: HTMLDivElement, m: ScrollMetrics): boolean {
|
|
120
|
+
if (m.scrollWidth - m.innerWidth <= 1) return false;
|
|
121
|
+
const trackLeft = m.innerLeft + EDGE_INSET;
|
|
122
|
+
const trackWidth = m.innerWidth - EDGE_INSET * 2;
|
|
123
|
+
const thumbWidth = Math.max(MIN_THUMB, (trackWidth * m.innerWidth) / m.scrollWidth);
|
|
124
|
+
if (thumbWidth >= trackWidth) return false;
|
|
125
|
+
const maxScroll = m.scrollWidth - m.innerWidth;
|
|
126
|
+
const progress = maxScroll > 0 ? Math.min(1, Math.max(0, m.scrollLeft / maxScroll)) : 0;
|
|
127
|
+
node.style.top = `${m.innerTop + m.innerHeight - THICKNESS - EDGE_INSET}px`;
|
|
128
|
+
node.style.left = `${trackLeft + (trackWidth - thumbWidth) * progress}px`;
|
|
129
|
+
node.style.width = `${thumbWidth}px`;
|
|
130
|
+
return true;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function hidePair(pair: ThumbPair): void {
|
|
134
|
+
pair.vertical?.classList.remove('is-visible');
|
|
135
|
+
pair.horizontal?.classList.remove('is-visible');
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function update(el: Element): void {
|
|
139
|
+
const metrics = measure(el);
|
|
140
|
+
if (!metrics) return;
|
|
141
|
+
const pair = getPair(el);
|
|
142
|
+
const vertical = getThumb(pair, 'vertical');
|
|
143
|
+
const horizontal = getThumb(pair, 'horizontal');
|
|
144
|
+
const hasVertical = positionVertical(vertical, metrics);
|
|
145
|
+
const hasHorizontal = positionHorizontal(horizontal, metrics);
|
|
146
|
+
|
|
147
|
+
vertical.classList.toggle('is-visible', hasVertical);
|
|
148
|
+
horizontal.classList.toggle('is-visible', hasHorizontal);
|
|
149
|
+
active.add(el);
|
|
150
|
+
|
|
151
|
+
window.clearTimeout(pair.timer);
|
|
152
|
+
pair.timer = window.setTimeout(() => {
|
|
153
|
+
hidePair(pair);
|
|
154
|
+
active.delete(el);
|
|
155
|
+
// 容器已被移除时顺手回收浮层节点(WeakMap 无法清理 DOM 引用)。
|
|
156
|
+
if (!el.isConnected) {
|
|
157
|
+
pair.vertical?.remove();
|
|
158
|
+
pair.horizontal?.remove();
|
|
159
|
+
pairs.delete(el);
|
|
160
|
+
}
|
|
161
|
+
}, IDLE_HIDE_MS);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function flush(): void {
|
|
165
|
+
frame = 0;
|
|
166
|
+
for (const el of dirty) update(el);
|
|
167
|
+
dirty.clear();
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function onScroll(event: Event): void {
|
|
171
|
+
const target = event.target;
|
|
172
|
+
let el: Element;
|
|
173
|
+
if (target === document) el = document.documentElement;
|
|
174
|
+
else if (target instanceof Element) el = target;
|
|
175
|
+
else return;
|
|
176
|
+
dirty.add(el);
|
|
177
|
+
if (frame === 0) frame = window.requestAnimationFrame(flush);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export function installAutoHideScrollbars(): void {
|
|
181
|
+
if (typeof document === 'undefined') return;
|
|
182
|
+
document.addEventListener('scroll', onScroll, { capture: true, passive: true });
|
|
183
|
+
// 视口尺寸变化后旧坐标会失效,先收起,等下一次滚动重新定位。
|
|
184
|
+
window.addEventListener('resize', () => {
|
|
185
|
+
for (const el of active) {
|
|
186
|
+
const pair = pairs.get(el);
|
|
187
|
+
if (pair) hidePair(pair);
|
|
188
|
+
}
|
|
189
|
+
active.clear();
|
|
190
|
+
});
|
|
191
|
+
}
|