HEL-822: land HEL-821 A1+A2 source onto PR#4 base (db571ba)
- 13 modified / 8 added / 1 deleted per CHANGES.json (author package 76bc120b) - docs/requirements v1.10 + prototypes/hel-799 + PROVENANCE from bundle, verified byte-identical to63799f0- AGENTS.md + scripts/check_requirements.py restored from governance commit63799f0Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
+215
-95
@@ -1,124 +1,244 @@
|
||||
import { SoftText } from '../shared/SoftText';
|
||||
import { newId } from '../domain/id';
|
||||
import { useEffect, useLayoutEffect, useRef, useState } from 'react';
|
||||
import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react';
|
||||
import type { DemoState, Message } from '../domain/model';
|
||||
import { RealChatAdapter } from '../adapters/real';
|
||||
import { DemoAdapter, TurnGate } from '../adapters/demo';
|
||||
import { publicView } from '../domain/privacy';
|
||||
import { Icon, Modal } from '../shared/ui';
|
||||
export function ChatPanel({ state, onChange, onPresence, onVoice, onObservation, onClose, offline, fail, log, setLog, focusLife, real = false }: {
|
||||
import { snap } from '../shared/motion';
|
||||
import { motionOff } from '../stage/roomScene';
|
||||
import { LineStage, type PresentHow } from './lineStage';
|
||||
import { anchorOf, cursorAt, dwellFor, groupTurns, pagesOf, type Anchor, type Metrics, type ShownMessage } from './reading';
|
||||
|
||||
const START: Anchor = { mi: 0, offset: 0 };
|
||||
const metricsFor = (width: number): Metrics => { const compact = document.documentElement.classList.contains('compact'); return { width, font: compact ? 16 : innerWidth <= 720 ? 18 : 23, compact }; };
|
||||
const typingTarget = (el: Element | null) => !!el && (el.matches('textarea,input,select,[contenteditable="true"]'));
|
||||
|
||||
export function ChatPanel({ state, onChange, onPresence, onVoice, onObservation, onRecord, offline, fail, log, setLog, focusLife, real = false }: {
|
||||
real?: boolean;
|
||||
state: DemoState;
|
||||
onChange: (messages: Message[]) => void;
|
||||
onPresence: (p: string) => void;
|
||||
onVoice: (v: boolean) => void;
|
||||
onObservation: (s: string) => void;
|
||||
onClose: () => void;
|
||||
// A reading sheet (full text or the whole record) is open: she stays across the table, the room dims a little.
|
||||
onRecord: (open: boolean) => void;
|
||||
offline: boolean;
|
||||
fail: boolean;
|
||||
log: boolean;
|
||||
setLog: (b: boolean) => void;
|
||||
focusLife: string;
|
||||
}) {
|
||||
const [draft, setDraft] = useState(() => { try {
|
||||
return sessionStorage.getItem(real ? 'cg.real.draft' : 'cg.draft') || '';
|
||||
}
|
||||
catch {
|
||||
return '';
|
||||
} }), [reply, setReply] = useState(''), [busy, setBusy] = useState(false), [voice, setVoice] = useState(false), [permission, setPermission] = useState<'' | 'mic' | 'vision'>(''), [notice, setNotice] = useState(''), [denied, setDenied] = useState(false), [vision, setVision] = useState(false);
|
||||
const draftKey = real ? 'cg.real.draft' : 'cg.draft', readKey = real ? 'cg.real.reading' : 'cg.reading';
|
||||
const [draft, setDraft] = useState(() => { try { return sessionStorage.getItem(draftKey) || ''; } catch { return ''; } }), [reply, setReply] = useState(''), [busy, setBusy] = useState(false), [voice, setVoice] = useState(false), [permission, setPermission] = useState<'' | 'mic' | 'vision'>(''), [notice, setNotice] = useState(''), [flash, setFlash] = useState(''), [denied, setDenied] = useState(false), [vision, setVision] = useState(false);
|
||||
const gate = useRef(new TurnGate()), adapter = useRef(real ? new RealChatAdapter(epoch => { gate.current.epoch = epoch; }) : new DemoAdapter()), live = useRef(state.messages), logRef = useRef<HTMLDivElement>(null), voiceController = useRef<AbortController | null>(null);
|
||||
const [activeTurn, setActiveTurn] = useState(''), [interrupted, setInterrupted] = useState<{ text: string; turn: string } | null>(null);
|
||||
const replyLive = useRef(''), turnLive = useRef(''), dialogue = useRef<HTMLDivElement>(null), following = useRef(true);
|
||||
const replyLive = useRef(''), turnLive = useRef(''), sentTurn = useRef('');
|
||||
live.current = state.messages;
|
||||
const view = publicView(state), lastUser = [...view.messages].reverse().find(m => m.role === 'user'), lastHer = [...view.messages].reverse().find(m => m.role === 'companion');
|
||||
const name = state.identity.name;
|
||||
const change = (messages: Message[]) => { live.current = messages; onChange(messages); };
|
||||
|
||||
// ── What the stage shows: the newest turn of the (already privacy-filtered) record, plus the live reply. ──
|
||||
const view = useMemo(() => publicView(state), [state]);
|
||||
// The cut-off part of a stopped reply stays with its own turn (it is shown, never stored).
|
||||
const shown = useMemo<ShownMessage[]>(() => {
|
||||
const list: ShownMessage[] = [...view.messages];
|
||||
if (reply) list.push({ id: 'live-reply', role: 'companion', text: reply, lifeIds: [], turnId: activeTurn, live: 'generating' });
|
||||
else if (interrupted) {
|
||||
const at = list.map(m => m.turnId).lastIndexOf(interrupted.turn);
|
||||
list.splice(at < 0 ? list.length : at + 1, 0, { id: 'cut-reply', role: 'companion', text: interrupted.text, lifeIds: [], turnId: interrupted.turn, live: 'cancelled' });
|
||||
}
|
||||
return list;
|
||||
}, [view.messages, reply, activeTurn, interrupted]);
|
||||
const turn = useMemo(() => groupTurns(shown).at(-1), [shown]);
|
||||
const stageRef = useRef<HTMLDivElement>(null), lines = useRef<LineStage | null>(null), meter = useRef<HTMLElement>(null);
|
||||
const [metrics, setMetrics] = useState<Metrics>(() => metricsFor(Math.min(900, innerWidth * .88)));
|
||||
const pages = useMemo(() => pagesOf(turn, metrics), [turn, metrics]);
|
||||
// Reading position is kept as a place in the text (not a line number), so re-chunking never loses it.
|
||||
const restored = useRef<{ turn: string; anchor: Anchor; flow: boolean } | null>((() => { try { return JSON.parse(sessionStorage.getItem(readKey) || 'null'); } catch { return null; } })());
|
||||
const resume = restored.current && turn && restored.current.turn === turn.key ? restored.current : null;
|
||||
const [anchor, setAnchor] = useState<Anchor>(resume?.anchor || START), [flow, setFlowState] = useState(resume ? resume.flow : true), [full, setFull] = useState(false);
|
||||
const head = useRef<Anchor>(resume?.anchor || START), how = useRef<PresentHow>('jump'), turnKey = useRef(turn?.key || '');
|
||||
const cursor = cursorAt(pages, anchor), page = pages[cursor], atEnd = cursor >= pages.length - 1;
|
||||
const headIndex = cursorAt(pages, head.current);
|
||||
if (cursor > headIndex) head.current = anchor;
|
||||
const held = useRef(new Set<string>()), clock = useRef({ key: '', dwell: 0, total: 0 });
|
||||
const reading = useRef({ pages, cursor, flow }); reading.current = { pages, cursor, flow };
|
||||
|
||||
function stop() { gate.current.cancel(); if (replyLive.current) setInterrupted({ text: replyLive.current, turn: turnLive.current }); replyLive.current = ''; setBusy(false); setReply(''); onPresence('停下来了'); change(live.current.map(m => m.status === 'pending' ? { ...m, status: 'cancelled' } : m)); }
|
||||
function endVoice() { stop(); voiceController.current?.abort(); setVoice(false); onVoice(false); onPresence('在你面前'); }
|
||||
useEffect(() => { const current = gate.current; return () => { current.cancel(); if (real) onChange(live.current.map(m => m.status === 'pending' ? { ...m, status: 'cancelled' } : m)); voiceController.current?.abort(); onVoice(false); onObservation(''); }; }, [onVoice, onObservation, onChange, real]);
|
||||
useEffect(() => { try {
|
||||
sessionStorage.setItem(real ? 'cg.real.draft' : 'cg.draft', draft);
|
||||
}
|
||||
catch { /* Draft remains in memory. */ } }, [draft, real]);
|
||||
useEffect(() => { if (offline) {
|
||||
gate.current.resync();
|
||||
setBusy(false);
|
||||
setReply('');
|
||||
setVoice(false);
|
||||
onVoice(false);
|
||||
voiceController.current?.abort();
|
||||
} }, [offline, onVoice]);
|
||||
useEffect(() => { if (!vision)
|
||||
return; onObservation('模拟 · 刚刚看到,未采集影像'); const t = setTimeout(() => onObservation('模拟 · 已过期,不再当作眼前'), 30000); return () => clearTimeout(t); }, [vision, onObservation]);
|
||||
useEffect(() => { if (log) {
|
||||
const el = focusLife ? logRef.current?.querySelector(`[data-life="${CSS.escape(focusLife)}"]`) : null;
|
||||
if (el)
|
||||
el.scrollIntoView({ block: 'center' });
|
||||
else if (logRef.current)
|
||||
logRef.current.scrollTop = logRef.current.scrollHeight;
|
||||
} }, [log, focusLife, view.messages.length]);
|
||||
useLayoutEffect(() => { if (following.current && dialogue.current) dialogue.current.scrollTop = dialogue.current.scrollHeight; }, [reply, state.messages, interrupted]);
|
||||
async function send(retry?: Message) { if (!retry && real) retry = [...live.current].reverse().find(m => m.role === 'user' && m.status === 'failed' && m.text === draft.trim()); const text = retry?.text || draft.trim(); if (!text || offline)
|
||||
return; stop(); const turnId = retry?.turnId || newId(), signal = gate.current.begin(turnId); const msg: Message = { id: retry?.id || newId(), role: 'user', text, lifeIds: [], turnId, status: 'pending' }; change(retry ? live.current.map(m => m.id === retry.id ? msg : m) : [...live.current, msg]); if (!retry)
|
||||
setDraft(''); setActiveTurn(turnId); turnLive.current = turnId; following.current = true; setBusy(true); onPresence('在想'); setNotice(''); try {
|
||||
await adapter.current.send(text, turnId, gate.current.epoch, signal, event => { if (!gate.current.accepts(event))
|
||||
return; if (event.t === 'chat.delta') {
|
||||
replyLive.current += event.delta; setInterrupted(null); setReply(replyLive.current);
|
||||
onPresence('在说话');
|
||||
useEffect(() => { try { sessionStorage.setItem(draftKey, draft); } catch { /* Draft remains in memory. */ } }, [draft, draftKey]);
|
||||
useEffect(() => { try { if (turn) sessionStorage.setItem(readKey, JSON.stringify({ turn: turn.key, anchor, flow })); } catch { /* Position remains in memory. */ } }, [turn, anchor, flow, readKey]);
|
||||
useEffect(() => { if (offline) { gate.current.resync(); setBusy(false); setReply(''); setVoice(false); onVoice(false); voiceController.current?.abort(); } }, [offline, onVoice]);
|
||||
useEffect(() => { if (!vision) return; onObservation('模拟 · 刚刚看到,未采集影像'); const t = setTimeout(() => onObservation('模拟 · 已过期,不再当作眼前'), 30000); return () => clearTimeout(t); }, [vision, onObservation]);
|
||||
useEffect(() => { if (!flash) return; const t = setTimeout(() => setFlash(''), 2600); return () => clearTimeout(t); }, [flash]);
|
||||
useEffect(() => { onRecord(log || full); }, [log, full, onRecord]);
|
||||
useEffect(() => () => onRecord(false), [onRecord]);
|
||||
useEffect(() => { if (log) { const el = focusLife ? logRef.current?.querySelector(`[data-life="${CSS.escape(focusLife)}"]`) : null; if (el) el.scrollIntoView({ block: 'center' }); else if (logRef.current) logRef.current.scrollTop = logRef.current.scrollHeight; } }, [log, focusLife, view.messages.length]);
|
||||
|
||||
// ── Stage ──
|
||||
useLayoutEffect(() => { if (stageRef.current) lines.current = new LineStage(stageRef.current); return () => { lines.current = null; }; }, []);
|
||||
useLayoutEffect(() => {
|
||||
const el = stageRef.current; if (!el) return;
|
||||
const measure = () => setMetrics(m => { const next = metricsFor(el.clientWidth || m.width); if (next.width === m.width && next.font === m.font && next.compact === m.compact) return m; how.current = 'jump'; return next; });
|
||||
const ro = new ResizeObserver(measure), vv = window.visualViewport; ro.observe(el); addEventListener('resize', measure); vv?.addEventListener('resize', measure); measure();
|
||||
return () => { ro.disconnect(); removeEventListener('resize', measure); vv?.removeEventListener('resize', measure); };
|
||||
}, []);
|
||||
// A new turn (you sent something, or she spoke on her own) starts at its first line.
|
||||
useLayoutEffect(() => {
|
||||
const key = turn?.key || '';
|
||||
if (key === turnKey.current) return;
|
||||
turnKey.current = key; how.current = 'jump'; head.current = START; setAnchor(START);
|
||||
if (key && key === sentTurn.current) setFlowState(true);
|
||||
}, [turn?.key]);
|
||||
const signature = pages.map(p => p.key).join('|');
|
||||
useLayoutEffect(() => {
|
||||
lines.current?.present(pages, cursor, how.current, name, !motionOff());
|
||||
how.current = 'advance';
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps -- re-present only when the visible lines change
|
||||
}, [signature, cursor]);
|
||||
useLayoutEffect(() => {
|
||||
if (!page) return;
|
||||
const mine = page.message, cancelled = mine.live === 'cancelled' || (mine.status === 'cancelled' && mine.role === 'companion');
|
||||
const text = cancelled && page.last ? '未完成 · 已取消' : mine.role === 'user' && mine.status === 'failed' ? '没发出去' : mine.role === 'user' && mine.status === 'cancelled' && atEnd ? '本轮已取消' : atEnd && busy && mine.role === 'companion' ? '还在说' : '';
|
||||
lines.current?.mark({ text, waiting: atEnd && busy && mine.role === 'companion' });
|
||||
});
|
||||
useEffect(() => { lines.current?.rename(name); }, [name]);
|
||||
|
||||
// ── Reading clock. Holds (hover, selection, open sheets, hidden page) freeze it without changing your choice. ──
|
||||
useEffect(() => {
|
||||
let raf = 0, last = 0;
|
||||
const tick = (now: number) => {
|
||||
const dt = last ? Math.min(100, now - last) : 0; last = now;
|
||||
const { pages: list, cursor: at, flow: on } = reading.current, cur = list[at], c = clock.current;
|
||||
if (cur && cur.key !== c.key) { c.key = cur.key; c.dwell = 0; c.total = dwellFor(cur); }
|
||||
if (cur && on && !held.current.size && !document.hidden) {
|
||||
if (c.dwell < c.total) c.dwell += dt;
|
||||
if (c.dwell >= c.total && at < list.length - 1) { how.current = 'advance'; setAnchor(anchorOf(list, at + 1) || START); }
|
||||
}
|
||||
if (meter.current) meter.current.style.transform = `scaleX(${c.total ? Math.min(1, c.dwell / c.total) : 0})`;
|
||||
raf = requestAnimationFrame(tick);
|
||||
};
|
||||
raf = requestAnimationFrame(tick);
|
||||
return () => cancelAnimationFrame(raf);
|
||||
}, []);
|
||||
const [holding, setHolding] = useState(false);
|
||||
const hold = useCallback((why: string, on: boolean) => { if (on) held.current.add(why); else held.current.delete(why); setHolding(held.current.size > 0); }, []);
|
||||
useEffect(() => { hold('sheet', log || full); }, [log, full, hold]);
|
||||
useEffect(() => {
|
||||
const selection = () => { const s = getSelection(); hold('select', !!(s && String(s).length && stageRef.current?.contains(s.anchorNode))); };
|
||||
document.addEventListener('selectionchange', selection);
|
||||
return () => document.removeEventListener('selectionchange', selection);
|
||||
}, [hold]);
|
||||
|
||||
const goTo = (index: number, mode: PresentHow) => { const to = Math.max(0, Math.min(index, pages.length - 1)); if (to === cursor) return; how.current = mode; clock.current.key = ''; setAnchor(anchorOf(pages, to) || START); };
|
||||
const setFlow = (on: boolean, quiet = false) => { setFlowState(on); clock.current.dwell = 0; if (!quiet) setFlash(on ? '继续自动呈现' : '已暂停 · 可以慢慢回看'); };
|
||||
const back = (instant = false) => { if (flow) setFlow(false, true); goTo(cursor - 1, instant ? 'instant' : 'jump'); };
|
||||
const forward = (instant = false) => goTo(cursor + 1, instant ? 'instant' : 'advance');
|
||||
const toLatest = () => { how.current = 'jump'; setAnchor(head.current); setFlow(true, true); };
|
||||
// Keyboard review lands at once (no slide, no replayed ink). Sheets and dialogs own their keys.
|
||||
const keys = useRef({ back, forward, toggle: () => setFlow(!flow) }); keys.current = { back, forward, toggle: () => setFlow(!flow) };
|
||||
useEffect(() => {
|
||||
const key = (e: KeyboardEvent) => {
|
||||
if (e.defaultPrevented || e.altKey || e.ctrlKey || e.metaKey || typingTarget(document.activeElement) || document.querySelector('.modal.open') || document.documentElement.dataset.mode !== 'talk') return;
|
||||
if (e.key === 'ArrowLeft') { snap(); keys.current.back(true); }
|
||||
else if (e.key === 'ArrowRight') { snap(); keys.current.forward(true); }
|
||||
else if (e.key === ' ' && (document.activeElement === document.body || !document.activeElement)) { e.preventDefault(); keys.current.toggle(); }
|
||||
};
|
||||
addEventListener('keydown', key);
|
||||
return () => removeEventListener('keydown', key);
|
||||
}, []);
|
||||
// Esc closes the full-text sheet first (one layer at a time) and returns focus to the button that opened it.
|
||||
const fullButton = useRef<HTMLButtonElement>(null), historyButton = useRef<HTMLButtonElement>(null), fullClose = useRef<HTMLButtonElement>(null), logClose = useRef<HTMLButtonElement>(null);
|
||||
useEffect(() => {
|
||||
if (!full) return;
|
||||
fullClose.current?.focus({ preventScroll: true });
|
||||
const key = (e: KeyboardEvent) => { if (e.key !== 'Escape' || document.querySelector('.modal.open')) return; e.stopImmediatePropagation(); e.preventDefault(); setFull(false); };
|
||||
document.addEventListener('keydown', key, true);
|
||||
return () => { document.removeEventListener('keydown', key, true); const back = fullButton.current; if (back && !back.closest('[inert]')) back.focus({ preventScroll: true }); };
|
||||
}, [full]);
|
||||
const logWasOpen = useRef(log);
|
||||
useEffect(() => {
|
||||
if (log) logClose.current?.focus({ preventScroll: true });
|
||||
else if (logWasOpen.current && document.activeElement && (document.activeElement === document.body || document.activeElement.closest('.record'))) historyButton.current?.focus({ preventScroll: true });
|
||||
logWasOpen.current = log;
|
||||
}, [log]);
|
||||
|
||||
async function send(retry?: Message) {
|
||||
if (!retry && real) retry = [...live.current].reverse().find(m => m.role === 'user' && m.status === 'failed' && m.text === draft.trim());
|
||||
const text = retry?.text || draft.trim(); if (!text || offline) return;
|
||||
stop();
|
||||
const turnId = retry?.turnId || newId(), signal = gate.current.begin(turnId);
|
||||
const msg: Message = { id: retry?.id || newId(), role: 'user', text, lifeIds: [], turnId, status: 'pending' };
|
||||
sentTurn.current = turnId;
|
||||
change(retry ? live.current.map(m => m.id === retry.id ? msg : m) : [...live.current, msg]);
|
||||
if (!retry) setDraft('');
|
||||
setActiveTurn(turnId); turnLive.current = turnId; setBusy(true); onPresence('在想'); setNotice('');
|
||||
if (!flow) setFlash('新的一轮会自动呈现,随时可以暂停');
|
||||
try {
|
||||
await adapter.current.send(text, turnId, gate.current.epoch, signal, event => {
|
||||
if (!gate.current.accepts(event)) return;
|
||||
if (event.t === 'chat.delta') { replyLive.current += event.delta; setInterrupted(null); setReply(replyLive.current); onPresence('在说话'); }
|
||||
else if (event.t === 'chat.final') {
|
||||
change([...live.current.map(m => m.id === msg.id ? { ...m, status: 'sent' as const } : m), { id: newId(), role: 'companion', text: event.utterance.text, lifeIds: [], turnId }]);
|
||||
replyLive.current = ''; setInterrupted(null); setReply('');
|
||||
if (real) setDraft(current => current === text ? '' : current);
|
||||
setBusy(false);
|
||||
onPresence(voice ? '在听你说(模拟)' : '在你面前');
|
||||
}
|
||||
}, fail);
|
||||
}
|
||||
else if (event.t === 'chat.final') {
|
||||
change([...live.current.map(m => m.id === msg.id ? { ...m, status: 'sent' as const } : m), { id: newId(), role: 'companion', text: event.utterance.text, lifeIds: [], turnId }]);
|
||||
replyLive.current = ''; setInterrupted(null); setReply('');
|
||||
if (real) setDraft(current => current === text ? '' : current);
|
||||
catch (e) {
|
||||
if (signal.aborted) return;
|
||||
change(live.current.map(m => m.id === msg.id ? { ...m, status: 'failed' } : m));
|
||||
setBusy(false);
|
||||
onPresence(voice ? '在听你说(模拟)' : '在你面前');
|
||||
} }, fail);
|
||||
}
|
||||
catch (e) {
|
||||
if (signal.aborted)
|
||||
return;
|
||||
change(live.current.map(m => m.id === msg.id ? { ...m, status: 'failed' } : m));
|
||||
setBusy(false);
|
||||
if (replyLive.current) setInterrupted({ text: replyLive.current, turn: turnId }); replyLive.current = ''; setReply('');
|
||||
setDraft(text);
|
||||
setNotice(real && (e instanceof TypeError || e instanceof SyntaxError) ? '连接中断了,你写的话还在,可以重试。' : (e as Error).message);
|
||||
onPresence('在你面前');
|
||||
} }
|
||||
const setVoiceMode = () => { if (real) { setNotice('语音尚未接通,文字仍可使用。'); return; } if (voice) {
|
||||
endVoice();
|
||||
return;
|
||||
} setPermission('mic'); };
|
||||
const herText = reply || interrupted?.text || lastHer?.text || '我在。你想说什么?';
|
||||
const herTurn = reply ? activeTurn : interrupted ? interrupted.turn : lastHer?.turnId || lastHer?.id || 'greeting';
|
||||
return <><div className="chat-layout"><div className="talk-scene-space" aria-hidden="true"/><div className="dialog"><div className="dialog-scroll" ref={dialogue} tabIndex={0} aria-label="当前对话,可滚动阅读全文" onScroll={() => { const el = dialogue.current; if (el) following.current = el.scrollHeight - el.clientHeight - el.scrollTop < 40; }}>
|
||||
{lastUser && <div className="you"><div className="speaker">你</div><SoftText identity={lastUser.turnId || lastUser.id} text={lastUser.text}/>{lastUser.status === 'failed' && <div className="failed">没发出去 <button className="retry" onClick={() => void send(lastUser)}>重试</button></div>}{lastUser.status === 'cancelled' && <small>本轮已取消</small>}</div>}
|
||||
<div className={`her ${interrupted ? 'cut' : ''}`}><div className="speaker">{state.identity.name}</div><SoftText identity={herTurn} text={herText}/>{reply && <span className="caret"/>}{interrupted && <small className="partial-note">这段未完成,未存入聊天记录</small>}</div>
|
||||
</div><div className="hint"><span role="status">{state.identity.name} · {notice || (busy ? reply ? '正在回应…' : `${state.identity.name}在想…` : real ? '文字已接通 · 完整内容可滚动阅读' : '演示回复 · 未连接真实模型')}</span>{busy && <button className="btn-text" onClick={stop}>取消本轮回复</button>}</div></div>
|
||||
{voice && <div className="voicebar glass"><span className="live"><i /></span><span className="vstate">{busy ? '她在想(模拟)' : '在听你说(模拟,无收音)'}</span><button className="btn-ghost" onClick={() => { stop(); setNotice(fail?'刚才没听清,你再说一遍?(模拟识别失败)':'她停下来了,在等你。'); }}>打断她</button><button className="end" onClick={endVoice}>结束语音</button></div>}
|
||||
<div className="composer glass"><button className={`btn-round cam ${vision ? 'on' : ''}`} aria-label="视觉观察" onClick={() => { if (real) { setNotice('视觉尚未接通,不采集影像。'); return; } if (vision) {
|
||||
setVision(false);
|
||||
onObservation('窗户关上了');
|
||||
}
|
||||
else
|
||||
setPermission('vision'); }}><Icon name="eye"/></button><textarea aria-label="想和她说的话" rows={1} maxLength={4000} value={draft} placeholder="想和她说点什么…" onChange={e => setDraft(e.target.value)} onKeyDown={e => { if (e.key === 'Enter' && !e.shiftKey && !e.nativeEvent.isComposing && e.keyCode !== 229) {
|
||||
e.preventDefault();
|
||||
void send();
|
||||
} }}/><button className="btn-round send" disabled={!draft.trim() || offline} aria-label="发送" onClick={() => void send()}><Icon name="up"/></button><button className={`btn-round log-btn ${log ? 'on' : ''}`} aria-label="完整记录" onClick={() => setLog(!log)}><Icon name="chat"/></button><button className={`mic ${denied ? 'denied' : ''}`} aria-label={real ? '语音尚未接通' : voice ? '停止语音' : '开始模拟语音'} disabled={offline} onClick={setVoiceMode}><Icon name={voice ? 'stop' : 'mic'}/></button></div>
|
||||
</div><aside className="log-panel glass read" hidden={!log}><header><h2>你们说过的话</h2><button className="btn-round sm" aria-label="关闭记录" onClick={() => setLog(false)}><Icon name="x"/></button></header><div className="scroll" ref={logRef}><div className="day">{real ? '服务器聊天记录' : '本机演示记录'}</div>{view.messages.map(m => <div key={m.id} data-life={m.lifeIds[0]} className={`msg ${m.role === 'user' ? 'me' : ''} ${m.status === 'failed' ? 'fail' : ''} ${focusLife && m.lifeIds.includes(focusLife) ? 'hl' : ''}`}><div className="who-l">{m.role === 'user' ? '你' : state.identity.name}{m.proactive && <span className="tagm proactive">她主动说的</span>}</div><p>{m.text}</p>{m.status === 'failed' && <button className="btn-text" onClick={() => void send(m)}>没发出去 · 重试</button>}{m.status === 'cancelled' && <small>已取消</small>}</div>)}</div></aside>
|
||||
{permission && <Modal title={permission === 'mic' ? '开启模拟语音?' : '打开模拟视觉窗?'} onClose={() => { setPermission(''); setDenied(true); setNotice('没有开启。真实服务尚未接通,文字仍可使用。'); }} confirm="开始模拟" onConfirm={() => { if (permission === 'mic') {
|
||||
setDenied(false);
|
||||
setVoice(true);
|
||||
onVoice(true);
|
||||
voiceController.current = new AbortController();
|
||||
onPresence('在听你说(模拟)');
|
||||
setNotice('模拟语音没有收音,可继续用文字输入。');
|
||||
}
|
||||
else {
|
||||
if (fail) {
|
||||
onObservation('模拟 · 看不清,视觉服务未接通');
|
||||
setNotice('模拟观察失败,文字仍可使用。');
|
||||
if (replyLive.current) setInterrupted({ text: replyLive.current, turn: turnId });
|
||||
replyLive.current = ''; setReply('');
|
||||
setDraft(text);
|
||||
setNotice(real && (e instanceof TypeError || e instanceof SyntaxError) ? '连接中断了,你写的话还在,可以重试。' : (e as Error).message);
|
||||
onPresence('在你面前');
|
||||
}
|
||||
else
|
||||
setVision(true);
|
||||
} setPermission(''); }}>本次仅模拟界面状态,不申请麦克风或摄像头权限,不采集声音或影像,也不向外接服务发送请求。</Modal>}
|
||||
<button hidden className="sr" onClick={onClose}>退出交流</button></>;
|
||||
}
|
||||
const setVoiceMode = () => { if (real) { setNotice('语音尚未接通,文字仍可使用。'); return; } if (voice) { endVoice(); return; } setPermission('mic'); };
|
||||
const lastUser = [...view.messages].reverse().find(m => m.role === 'user');
|
||||
const status = notice || flash || (busy ? (reply ? `${name}正在回应…` : `${name}在想…`) : !pages.length ? '你们还没有说过话。' : real ? '' : '演示回复 · 未连接真实模型');
|
||||
const unread = pages.length - 1 - cursor, single = pages.length <= 1 && !busy, fullMessage = page?.message;
|
||||
const textarea = useRef<HTMLTextAreaElement>(null);
|
||||
useLayoutEffect(() => { const t = textarea.current; if (!t) return; t.style.height = 'auto'; t.style.height = Math.min(t.scrollHeight, 88) + 'px'; }, [draft]);
|
||||
const typing = (on: boolean) => document.documentElement.classList.toggle('typing', on);
|
||||
useEffect(() => () => { document.documentElement.classList.remove('typing'); }, []);
|
||||
const record = reply ? shown.filter(m => !m.live) : shown;
|
||||
// While a reading sheet is open the table words are out of reach (the sheet holds them all); layout effect so the
|
||||
// focus hand-back after closing never lands on an inert button.
|
||||
const conversation = useRef<HTMLElement>(null);
|
||||
useLayoutEffect(() => { conversation.current?.toggleAttribute('inert', log || full); }, [log, full]);
|
||||
|
||||
return <><section ref={conversation} className={`conversation ${single ? 'single-page' : ''} ${flow ? '' : 'reading-paused'} ${holding && flow ? 'reading-held' : ''}`} aria-label="当前对话">
|
||||
{voice && <div className="voice-row"><span className="voice-wave" aria-hidden="true"><i/><i/><i/><i/></span><span>{busy ? '她在想(模拟)' : '在听你说(模拟,无收音)'}</span><button onClick={() => { stop(); setNotice(fail ? '刚才没听清,你再说一遍?(模拟识别失败)' : '她停下来了,在等你。'); }}>打断她</button><button onClick={endVoice}>结束语音</button></div>}
|
||||
<div className="stage" ref={stageRef} aria-hidden="true" onPointerEnter={e => { if (e.pointerType === 'mouse') hold('hover', true); }} onPointerLeave={() => hold('hover', false)}/>
|
||||
<p className="sr" aria-live="polite" aria-atomic="true">{page ? `${page.message.role === 'user' ? '你' : name}:${page.text}` : ''}</p>
|
||||
{status && <div className="talk-status" role="status"><span>{status}</span>{lastUser?.status === 'failed' && !busy && <button className="retry" onClick={() => void send(lastUser)}>重试</button>}</div>}
|
||||
<div className="reader-bar">
|
||||
<button className="flow" aria-pressed={flow} onClick={() => setFlow(!flow)}><Icon name={flow ? 'pause' : 'play'}/><span>{flow ? '暂停' : '继续'}</span></button>
|
||||
<button className="icon" aria-label="上一句" disabled={cursor <= 0} onClick={() => back()}><Icon name="back"/></button>
|
||||
<span className="count" aria-label={pages.length ? `第 ${cursor + 1} 句,共 ${pages.length} 句${busy ? ',还在说' : ''}` : '还没有对话'}><span>{pages.length ? `${cursor + 1} / ${pages.length}${busy ? '…' : ''}` : '0 / 0'}</span><i ref={meter}/></span>
|
||||
<button className="icon" aria-label="下一句" disabled={atEnd} onClick={() => forward()}><Icon name="next"/></button>
|
||||
<button className="read-all" ref={fullButton} disabled={!page} onClick={() => setFull(true)}>全文</button>
|
||||
{busy && <button className="stop" onClick={stop}>停止回复</button>}
|
||||
{!flow && cursor < cursorAt(pages, head.current) && <button className="latest" onClick={toLatest}><Icon name="down"/><span>{unread > 0 ? `回到读到的地方 · 后面还有 ${unread} 句` : '回到读到的地方'}</span></button>}
|
||||
</div>
|
||||
<div className="composer"><button className={`round log-btn ${log ? 'on' : ''}`} ref={historyButton} aria-label="完整记录" aria-expanded={log} onClick={() => setLog(!log)}><Icon name="lines"/></button>
|
||||
<textarea ref={textarea} aria-label="想和她说的话" rows={1} maxLength={4000} value={draft} placeholder="想和她说点什么…" onFocus={() => typing(true)} onBlur={() => typing(false)} onChange={e => setDraft(e.target.value)} onKeyDown={e => { if (e.key === 'Enter' && !e.shiftKey && !e.nativeEvent.isComposing && e.keyCode !== 229) { e.preventDefault(); void send(); } }}/>
|
||||
<button className={`round cam ${vision ? 'on' : ''}`} aria-label={real ? '视觉观察尚未接通' : '视觉观察(模拟)'} onClick={() => { if (real) { setNotice('视觉尚未接通,不采集影像。'); return; } if (vision) { setVision(false); onObservation('窗户关上了'); } else setPermission('vision'); }}><Icon name="eye"/></button>
|
||||
<button className="round send" disabled={!draft.trim() || offline} aria-label="发送" onClick={() => void send()}><Icon name="up"/></button>
|
||||
<button className={`voice-button ${denied ? 'denied' : ''}`} aria-label={real ? '语音尚未接通' : voice ? '停止语音' : '开始模拟语音'} disabled={offline} onClick={setVoiceMode}><Icon name={voice ? 'stop' : 'mic'}/></button>
|
||||
</div>
|
||||
</section>
|
||||
<aside className="record log-panel glass read" hidden={!log} role="dialog" aria-modal="false" aria-labelledby="record-title"><header><div><p className="eyebrow">{real ? '服务器聊天记录' : '完整记录 · 本机演示'}</p><h2 id="record-title">你们说过的话</h2></div><button className="btn-round sm" ref={logClose} aria-label="关闭记录" onClick={() => setLog(false)}><Icon name="x"/></button></header>
|
||||
<div className="scroll" ref={logRef}><p className="guide">内容完整保留;关闭后回到刚才那一句,自动呈现会接着走。</p>{record.map(m => <article key={m.id} data-life={m.lifeIds[0]} data-role={m.role} className={`message ${m.status === 'failed' ? 'fail' : ''} ${focusLife && m.lifeIds.includes(focusLife) ? 'hl' : ''}`}><div className="author"><i/>{m.role === 'user' ? '你' : name}{m.proactive && <span className="tagm">她主动说的</span>}</div><p>{m.text}</p>{m.status === 'failed' && <button className="btn-text" onClick={() => void send(m)}>没发出去 · 重试</button>}{m.status === 'cancelled' && <span className="incomplete">已取消</span>}{m.live === 'cancelled' && <span className="incomplete">已停止 · 这是未完成片段,未存入聊天记录</span>}</article>)}{reply && <article className="message" data-role="companion"><div className="author"><i/>{name}</div><p>{reply}</p><span className="incomplete">还在说…</span></article>}</div></aside>
|
||||
{full && fullMessage && <aside className="record full glass read" role="dialog" aria-modal="false" aria-labelledby="full-title"><header><div><p className="eyebrow">{fullMessage.role === 'user' ? '你' : name} · 全文</p><h2 id="full-title">这一段,慢慢看</h2></div><button className="btn-round sm" ref={fullClose} aria-label="关闭全文" onClick={() => setFull(false)}><Icon name="x"/></button></header>
|
||||
<div className="scroll"><p className="guide">整段完整可读。关闭后回到刚才那一句。</p><article className="message" data-role={fullMessage.role}><div className="author"><i/>{fullMessage.role === 'user' ? '你' : name}</div><p>{fullMessage.text}</p>{fullMessage.live === 'generating' && <span className="incomplete">还在说…</span>}{fullMessage.live === 'cancelled' && <span className="incomplete">这段回复已停止,内容未完成,未存入聊天记录。</span>}</article></div></aside>}
|
||||
{permission && <Modal title={permission === 'mic' ? '开启模拟语音?' : '打开模拟视觉窗?'} onClose={() => { setPermission(''); setDenied(true); setNotice('没有开启。真实服务尚未接通,文字仍可使用。'); }} confirm="开始模拟" onConfirm={() => { if (permission === 'mic') { setDenied(false); setVoice(true); onVoice(true); voiceController.current = new AbortController(); onPresence('在听你说(模拟)'); setNotice('模拟语音没有收音,可继续用文字输入。'); } else { if (fail) { onObservation('模拟 · 看不清,视觉服务未接通'); setNotice('模拟观察失败,文字仍可使用。'); } else setVision(true); } setPermission(''); }}>本次仅模拟界面状态,不申请麦克风或摄像头权限,不采集声音或影像,也不向外接服务发送请求。</Modal>}
|
||||
</>;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
import { graphemes, type Page } from './reading';
|
||||
// The lines on your side of the table (HEL-799 talk.js present/pin/leave). React owns the element; this class owns
|
||||
// its children so finished lines are never re-rendered, re-typeset or re-animated. Text is always textContent.
|
||||
const EASE = 'cubic-bezier(.22,1,.36,1)';
|
||||
export type PresentHow = 'advance' | 'jump' | 'instant';
|
||||
export interface LineState { text: string; waiting: boolean }
|
||||
|
||||
export class LineStage {
|
||||
constructor(private stage: HTMLElement) {}
|
||||
// A line inks in only the first time it is read; review, returns and resizes show it as it is.
|
||||
private inked = new Set<string>();
|
||||
private line(page: Page, current: boolean, name: string, ink: boolean) {
|
||||
if (ink) this.inked.add(page.key);
|
||||
const el = document.createElement('div');
|
||||
el.className = 'line' + (current ? ' is-current' : ' is-prev');
|
||||
el.dataset.role = page.message.role; el.dataset.key = page.key;
|
||||
const who = document.createElement('span'); who.className = 'by';
|
||||
const dot = document.createElement('i'), label = document.createElement('b'), state = document.createElement('em');
|
||||
label.textContent = page.message.role === 'user' ? '你' : name; label.title = label.textContent; state.className = 'state';
|
||||
who.append(dot, label, state);
|
||||
const p = document.createElement('p');
|
||||
if (ink) {
|
||||
// Her current line inks in once, character by character (capped so long lines don't drag).
|
||||
p.className = 'ink';
|
||||
graphemes(page.text).forEach((c, i) => { const s = document.createElement('span'); s.style.setProperty('--i', String(Math.min(i, 48))); s.textContent = c; p.append(s); });
|
||||
} else p.textContent = page.text;
|
||||
el.append(who, p);
|
||||
return el;
|
||||
}
|
||||
// Take a line out of the flow at its current place, so the remaining lines lay out once at their final size.
|
||||
private pin(node: HTMLElement, rect: DOMRect, stageRect: DOMRect) {
|
||||
node.classList.add('leaving');
|
||||
Object.assign(node.style, { position: 'absolute', left: rect.left - stageRect.left + 'px', top: rect.top - stageRect.top + 'px', width: rect.width + 'px', transformOrigin: '50% 0' });
|
||||
}
|
||||
private leave(node: HTMLElement, rect: DOMRect, stageRect: DOMRect, quick = false, lift = quick ? -4 : -14, shrink = 1) {
|
||||
this.pin(node, rect, stageRect);
|
||||
node.animate([{ opacity: getComputedStyle(node).opacity, transform: 'none', filter: 'blur(0)' }, { opacity: 0, transform: `translateY(${lift}px) scale(${shrink})`, filter: 'blur(5px)' }],
|
||||
{ duration: quick ? 240 : 520, easing: EASE, fill: 'forwards' }).finished.then(() => node.remove()).catch(() => node.remove());
|
||||
}
|
||||
clear() { this.stage.replaceChildren(); }
|
||||
// Shows the current page with the one before it. Returns early when the same lines are already on screen, so
|
||||
// a growing stream, a status change or a re-render never replays anything.
|
||||
present(pages: Page[], cursor: number, how: PresentHow, name: string, animate: boolean) {
|
||||
const stage = this.stage, page = pages[cursor];
|
||||
if (!page) { stage.replaceChildren(); return; }
|
||||
const prev = pages[cursor - 1], want = [prev, page].filter((p): p is Page => !!p);
|
||||
const have = [...stage.querySelectorAll<HTMLElement>('.line:not(.leaving)')], keys = have.map(n => n.dataset.key);
|
||||
const inkFor = (p: Page) => p === page && animate && p.message.role === 'companion' && !this.inked.has(p.key);
|
||||
if (how === 'instant' || !animate) {
|
||||
// Keyboard review and reduced motion: show the target lines as they are — no slide, no re-played ink.
|
||||
if (keys.join() === want.map(p => p.key).join() && how !== 'instant') return;
|
||||
for (const n of stage.querySelectorAll('.line.leaving')) n.remove();
|
||||
stage.replaceChildren(...want.map(p => this.line(p, p === page, name, false)));
|
||||
return;
|
||||
}
|
||||
if (keys.join() === want.map(p => p.key).join()) return;
|
||||
const stageRect = stage.getBoundingClientRect();
|
||||
// Forward by one: the finished line lifts toward the "previous" slot while a small copy settles there; the
|
||||
// oldest drifts away. Both copies keep a fixed font size; only transform/opacity/filter move.
|
||||
if (how === 'advance' && prev && keys[keys.length - 1] === prev.key) {
|
||||
const first = new Map(have.map(n => [n, n.getBoundingClientRect()]));
|
||||
for (const n of have) if (n.dataset.key !== prev.key) this.leave(n, first.get(n)!, stageRect);
|
||||
const keep = have.find(n => n.dataset.key === prev.key)!, was = first.get(keep)!;
|
||||
const settled = this.line(prev, false, name, false), fresh = this.line(page, true, name, inkFor(page));
|
||||
this.pin(keep, was, stageRect);
|
||||
stage.append(settled, fresh);
|
||||
const to = settled.getBoundingClientRect();
|
||||
if (to.height) {
|
||||
const shrink = parseFloat(getComputedStyle(settled.querySelector('p')!).fontSize) / parseFloat(getComputedStyle(keep.querySelector('p')!).fontSize);
|
||||
this.leave(keep, was, stageRect, false, to.top - was.top, shrink);
|
||||
settled.animate([{ opacity: 0, transform: `translateY(${Math.round((was.top - to.top) * .35)}px)` }, { opacity: getComputedStyle(settled).opacity, transform: 'none' }], { duration: 620, easing: EASE });
|
||||
} else this.leave(keep, was, stageRect); // compact screens hide the previous line
|
||||
fresh.animate([{ opacity: 0, transform: 'translateY(16px)', filter: 'blur(8px)' }, { opacity: 1, transform: 'none', filter: 'blur(0)' }], { duration: 680, easing: EASE });
|
||||
return;
|
||||
}
|
||||
// Jumps (review, new turn, resize) crossfade instead of sliding, so direction never lies.
|
||||
for (const n of have) this.leave(n, n.getBoundingClientRect(), stageRect, true);
|
||||
for (const p of want) {
|
||||
const el = this.line(p, p === page, name, inkFor(p)); stage.append(el);
|
||||
el.animate([{ opacity: 0, filter: 'blur(6px)' }, { opacity: 1, filter: 'blur(0)' }], { duration: 520, delay: 90, easing: EASE, fill: 'backwards' });
|
||||
}
|
||||
}
|
||||
// Status words beside the current speaker ("还在说", "未完成 · 已取消"); changing them never touches the text.
|
||||
mark(state: LineState) {
|
||||
const cur = this.stage.querySelector('.line.is-current:not(.leaving)'); if (!cur) return;
|
||||
const s = cur.querySelector('.state'); if (s && s.textContent !== state.text) s.textContent = state.text;
|
||||
cur.classList.toggle('waiting', state.waiting);
|
||||
}
|
||||
rename(name: string) {
|
||||
for (const n of this.stage.querySelectorAll<HTMLElement>('.line[data-role="companion"] .by b')) { n.textContent = name; n.title = name; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
import type { Message } from '../domain/model';
|
||||
// Reading logic carried over from the verified HEL-793/795 prototype (PR#8) and HEL-799 (approved 01a16810), moved
|
||||
// onto the real message flow: replies are shown one or two lines at a time and flow on by themselves.
|
||||
|
||||
// A message as the stage sees it: stored messages plus, at most, the reply still being written or cut off.
|
||||
export interface ShownMessage extends Message { live?: 'generating' | 'cancelled' }
|
||||
export interface Turn { key: string; items: ShownMessage[] }
|
||||
export interface Page { key: string; text: string; index: number; last: boolean; mi: number; message: ShownMessage }
|
||||
export interface Metrics { width: number; font: number; compact: boolean }
|
||||
|
||||
export const graphemes = (text: string): string[] => typeof Intl.Segmenter === 'function' ? [...new Intl.Segmenter('zh', { granularity: 'grapheme' }).segment(text)].map(s => s.segment) : Array.from(text);
|
||||
|
||||
// A turn starts with your message (or with something she says on her own). Server turn ids keep a reply with the
|
||||
// message it answers; older local records without ids fall back to their order.
|
||||
export function groupTurns(messages: ShownMessage[]): Turn[] {
|
||||
const turns: Turn[] = [];
|
||||
for (const m of messages) {
|
||||
const cur = turns[turns.length - 1], head = cur?.items[0];
|
||||
const fresh = !cur || m.role === 'user' || !!m.proactive || (!!m.turnId && !!head?.turnId && m.turnId !== head.turnId);
|
||||
if (fresh || !cur) turns.push({ key: m.turnId || m.id, items: [m] }); else cur.items.push(m);
|
||||
}
|
||||
return turns;
|
||||
}
|
||||
|
||||
export function chunks(text: string, { width, font, compact }: Metrics): string[] {
|
||||
const perLine = Math.floor(Math.max(220, Math.min(width, 900) - 24) / font), max = Math.max(14, Math.min(60, Math.floor(perLine * (compact ? 1.6 : 2))));
|
||||
const out: string[] = []; let line = '', n = 0;
|
||||
for (const ch of graphemes(text)) {
|
||||
if (ch === '\n') { if (line.trim()) { out.push(line); line = ''; n = 0; } continue; }
|
||||
// Keep closing punctuation with its sentence; never produce an empty line.
|
||||
if (!line && out.length && /[。!?!?,、;:,;:」』))”]/u.test(ch)) { out[out.length - 1] += ch; continue; }
|
||||
line += ch; n++;
|
||||
// Short sentences travel together (as in the approved still); a line ends at a sentence once it has some weight.
|
||||
if (/[。!?!?]/u.test(ch) && n >= Math.min(22, max * .45)) { out.push(line); line = ''; n = 0; }
|
||||
else if (n >= max) {
|
||||
// Long text without punctuation still breaks, so nothing ever overflows the stage.
|
||||
const cut = Math.max(line.lastIndexOf(','), line.lastIndexOf('、'), line.lastIndexOf(';'));
|
||||
if (cut > max * .55) { out.push(line.slice(0, cut + 1)); line = line.slice(cut + 1); n = graphemes(line).length; } else { out.push(line); line = ''; n = 0; }
|
||||
}
|
||||
}
|
||||
if (line.trim()) out.push(line);
|
||||
return out;
|
||||
}
|
||||
|
||||
export function pagesOf(turn: Turn | undefined, metrics: Metrics): Page[] {
|
||||
const list: Page[] = [];
|
||||
if (!turn) return list;
|
||||
turn.items.forEach((message, mi) => {
|
||||
const parts = chunks(message.text, metrics);
|
||||
// The newest chunk of a reply still being written is held back (it may still grow or merge with the next
|
||||
// sentence), so a line never changes or re-animates after it has appeared.
|
||||
const held = message.live === 'generating' && parts.length ? 1 : 0;
|
||||
// Keys follow the turn and position, not the message id, so a streamed reply keeps its lines when it is stored.
|
||||
for (let index = 0; index < parts.length - held; index++) list.push({ key: `${turn.key}:${mi}:${index}`, text: parts[index], index, last: index === parts.length - 1, mi, message });
|
||||
});
|
||||
return list;
|
||||
}
|
||||
|
||||
// Each line stays on screen in proportion to its length (about 2.4–8 s for her, shorter for yours).
|
||||
export function dwellFor(page: Page) {
|
||||
const n = graphemes(page.text).length;
|
||||
return page.message.role === 'user' ? Math.min(3000, Math.max(1400, 900 + n * 60)) : Math.min(8000, Math.max(2400, 1500 + n * 150));
|
||||
}
|
||||
|
||||
// Reading position as a character offset inside one message, so a re-chunk (resize, keyboard) keeps your place.
|
||||
export interface Anchor { mi: number; offset: number }
|
||||
export function anchorOf(pages: Page[], cursor: number): Anchor | null {
|
||||
const page = pages[cursor];
|
||||
if (!page) return null;
|
||||
let offset = 0;
|
||||
for (const p of pages) { if (p === page) break; if (p.mi === page.mi) offset += p.text.length; }
|
||||
return { mi: page.mi, offset };
|
||||
}
|
||||
export function cursorAt(pages: Page[], anchor: Anchor | null): number {
|
||||
if (!anchor) return 0;
|
||||
let used = 0, best = -1;
|
||||
for (let i = 0; i < pages.length; i++) {
|
||||
if (pages[i].mi < anchor.mi) { best = i; continue; }
|
||||
if (pages[i].mi > anchor.mi) break;
|
||||
best = i;
|
||||
if (used + pages[i].text.length > anchor.offset) return i;
|
||||
used += pages[i].text.length;
|
||||
}
|
||||
return Math.max(0, best);
|
||||
}
|
||||
Reference in New Issue
Block a user