mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 08:12:24 +00:00
feat(voice): render the voice control from a presenter
createVoiceControl now returns a presenter instead of building the control with innerHTML: the session, cost, input, card and panel owners write its state by part and listen for interactions, and VoiceControl.vue renders it in the command dock, between the location bar and the visual presets as before. Focus moves wait for the render; the waveform's bars publish apart from the control and only when they change; the minutes field keeps a draft until its owner writes it. Owner tests use a real presenter on their own store, and view tests render the control.
This commit is contained in:
1 parent
2ffd7a4afd
commit
1de4f635ca
47 files changed
+1691
-1645
No files matched your search
+19
-129
@@ -7,79 +7,31 @@ import {
|
||||
/** Hide an idle card this long after its last update. */
|
||||
const VOICE_CARD_IDLE_MS = 14000;
|
||||
|
||||
const ELEMENT_IDS = Object.freeze({
|
||||
card: 'gev-voice-card',
|
||||
phase: 'gev-voice-card-phase',
|
||||
dismiss: 'gev-voice-card-dismiss',
|
||||
user: 'gev-voice-card-user',
|
||||
userText: 'gev-voice-card-user-text',
|
||||
userWho: 'gev-voice-card-user-who',
|
||||
pointer: 'gev-voice-card-pointer',
|
||||
pointerKind: 'gev-voice-card-pointer-kind',
|
||||
pointerText: 'gev-voice-card-pointer-text',
|
||||
assistant: 'gev-voice-card-assistant',
|
||||
assistantText: 'gev-voice-card-assistant-text',
|
||||
plan: 'gev-voice-card-plan',
|
||||
result: 'gev-voice-card-result',
|
||||
resultTitle: 'gev-voice-card-result-title',
|
||||
chips: 'gev-voice-card-chips',
|
||||
lines: 'gev-voice-card-lines',
|
||||
referents: 'gev-voice-card-referents',
|
||||
notes: 'gev-voice-card-notes',
|
||||
noteList: 'gev-voice-card-note-list',
|
||||
live: 'gev-voice-card-live',
|
||||
});
|
||||
|
||||
function setText(element, text) {
|
||||
if (element && element.textContent !== text) element.textContent = text;
|
||||
}
|
||||
|
||||
function setHidden(element, hidden) {
|
||||
if (element && element.hidden !== hidden) element.hidden = hidden;
|
||||
}
|
||||
|
||||
function fillList(list, items, render) {
|
||||
if (!list) return;
|
||||
const doc = list.ownerDocument;
|
||||
list.replaceChildren(
|
||||
...items.map((item) => {
|
||||
const node = doc.createElement('li');
|
||||
render(node, item, doc);
|
||||
return node;
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Voice card controls, nested in the voice control's markup (`control.js`)
|
||||
* Voice card controls. The card is part of the voice control (`control.js`),
|
||||
* beside its help and error trays, so Clean-UI and recording mode hide it with
|
||||
* the control. Receives provider-neutral session events through `handle` and
|
||||
* annotation outline outcomes through `outline`; owns only its listeners and
|
||||
* idle timer. All text is set through textContent. Captions update silently;
|
||||
* the polite live region carries final replies and result titles only.
|
||||
* annotation outline outcomes through `outline`, and presents the card's view
|
||||
* on the control; owns only its listener and idle timer. Captions update
|
||||
* silently; the polite live region carries final replies and result titles
|
||||
* only.
|
||||
*/
|
||||
export class VoiceCardControls {
|
||||
/**
|
||||
* @param {object} options
|
||||
* @param {HTMLElement} options.root Voice control root containing the card.
|
||||
* @param {object} options.ui The voice control presenter.
|
||||
* @param {number} [options.idleMs]
|
||||
* @param {Function} [options.setTimer]
|
||||
* @param {Function} [options.clearTimer]
|
||||
*/
|
||||
constructor({
|
||||
root,
|
||||
ui,
|
||||
idleMs = VOICE_CARD_IDLE_MS,
|
||||
setTimer = (fn, ms) => setTimeout(fn, ms),
|
||||
clearTimer = (id) => clearTimeout(id),
|
||||
onRender,
|
||||
}) {
|
||||
Object.assign(this, { idleMs, setTimer, clearTimer, onRender });
|
||||
this.elements = Object.fromEntries(
|
||||
Object.entries(ELEMENT_IDS).map(([key, id]) => [
|
||||
key,
|
||||
root?.querySelector?.(`#${id}`) || null,
|
||||
]),
|
||||
);
|
||||
Object.assign(this, { ui, idleMs, setTimer, clearTimer, onRender });
|
||||
this.state = initialVoiceCardState();
|
||||
this.enabled = true;
|
||||
this.presented = true;
|
||||
@@ -87,19 +39,16 @@ export class VoiceCardControls {
|
||||
this.consumedPresentationAnnouncement = -1;
|
||||
this.hideTimer = null;
|
||||
this.destroyed = false;
|
||||
this.listeners = new AbortController();
|
||||
if (!this.elements.card) return;
|
||||
this.elements.dismiss?.addEventListener(
|
||||
'click',
|
||||
() => this.handle({ type: 'dismiss' }),
|
||||
{ signal: this.listeners.signal },
|
||||
if (!ui) return;
|
||||
this.offDismiss = ui.on('card-dismiss', () =>
|
||||
this.handle({ type: 'dismiss' }),
|
||||
);
|
||||
this.render();
|
||||
}
|
||||
|
||||
/** Apply one session event (or `outline`/`dismiss`). */
|
||||
handle(event) {
|
||||
if (this.destroyed || !this.elements.card) return;
|
||||
if (this.destroyed || !this.ui) return;
|
||||
const next = reduceVoiceCard(this.state, event);
|
||||
if (next === this.state) return;
|
||||
if (
|
||||
@@ -123,7 +72,7 @@ export class VoiceCardControls {
|
||||
|
||||
/** A local submission allows future output after dismissal; keep its content. */
|
||||
rearm() {
|
||||
if (this.destroyed || !this.elements.card) return;
|
||||
if (this.destroyed || !this.ui) return;
|
||||
this.state = { ...this.state, dismissed: false };
|
||||
}
|
||||
|
||||
@@ -134,15 +83,14 @@ export class VoiceCardControls {
|
||||
|
||||
/** Change presentation only; session events and the idle deadline continue. */
|
||||
setEnabled(enabled) {
|
||||
if (this.destroyed || !this.elements.card) return;
|
||||
if (this.destroyed || !this.ui) return;
|
||||
this.enabled = Boolean(enabled);
|
||||
this.render();
|
||||
}
|
||||
|
||||
/** View selection changes presentation without changing the saved preference. */
|
||||
setPresented(presented) {
|
||||
if (this.destroyed || !this.elements.card || this.presented === presented)
|
||||
return;
|
||||
if (this.destroyed || !this.ui || this.presented === presented) return;
|
||||
if (!presented)
|
||||
this.consumedPresentationAnnouncement = this.announcementRevision;
|
||||
this.presented = presented;
|
||||
@@ -155,7 +103,7 @@ export class VoiceCardControls {
|
||||
this.hideTimer = null;
|
||||
if (this.destroyed) return;
|
||||
// Keep the card while the pointer or focus is inside it.
|
||||
if (this.elements.card.matches?.(':hover, :focus-within')) {
|
||||
if (this.ui.element('card')?.matches?.(':hover, :focus-within')) {
|
||||
this.scheduleIdle();
|
||||
return;
|
||||
}
|
||||
@@ -170,65 +118,7 @@ export class VoiceCardControls {
|
||||
}
|
||||
|
||||
render() {
|
||||
const el = this.elements;
|
||||
const view = this.snapshot();
|
||||
setHidden(el.card, !view.visible);
|
||||
el.card.dataset.busy = String(view.busy);
|
||||
setText(el.phase, view.phase);
|
||||
setHidden(el.user, !view.user.visible);
|
||||
setText(el.userText, view.user.text);
|
||||
setText(el.userWho, view.user.label);
|
||||
setHidden(el.pointer, !view.pointer.visible);
|
||||
setText(el.pointerKind, view.pointer.kind);
|
||||
setText(el.pointerText, view.pointer.text);
|
||||
setHidden(el.assistant, !view.assistant.visible);
|
||||
setText(el.assistantText, view.assistant.text);
|
||||
setHidden(el.plan, view.steps.length === 0);
|
||||
fillList(el.plan, view.steps, (node, step, doc) => {
|
||||
node.className = 'gev-voice-card-step';
|
||||
node.dataset.status = step.status;
|
||||
const label = doc.createElement('span');
|
||||
label.className = 'gev-voice-card-step-label';
|
||||
label.textContent = step.label;
|
||||
const status = doc.createElement('span');
|
||||
status.className = 'gev-voice-card-sr';
|
||||
status.textContent = step.statusText ? `, ${step.statusText}` : '';
|
||||
node.append(label, status);
|
||||
if (step.detail) {
|
||||
const detail = doc.createElement('span');
|
||||
detail.className = 'gev-voice-card-step-detail';
|
||||
detail.textContent = step.detail;
|
||||
node.append(detail);
|
||||
}
|
||||
});
|
||||
setHidden(el.result, !view.result.visible);
|
||||
setText(el.resultTitle, view.result.title);
|
||||
setHidden(el.chips, view.result.chips.length === 0);
|
||||
if (el.chips) {
|
||||
const doc = el.chips.ownerDocument;
|
||||
el.chips.replaceChildren(
|
||||
...view.result.chips.map((text) => {
|
||||
const chip = doc.createElement('span');
|
||||
chip.className = 'gev-voice-card-chip';
|
||||
chip.dataset.tone = text;
|
||||
chip.textContent = text;
|
||||
return chip;
|
||||
}),
|
||||
);
|
||||
}
|
||||
fillList(el.lines, view.result.lines, (node, line) => {
|
||||
node.textContent = line;
|
||||
});
|
||||
setHidden(el.referents, view.result.referents.length === 0);
|
||||
fillList(el.referents, view.result.referents, (node, ref) => {
|
||||
node.value = ref.n;
|
||||
node.textContent = ref.label;
|
||||
});
|
||||
setHidden(el.notes, view.result.notes.length === 0);
|
||||
fillList(el.noteList, view.result.notes, (node, note) => {
|
||||
node.textContent = note;
|
||||
});
|
||||
setText(el.live, view.announce);
|
||||
this.ui.replace('card', this.snapshot());
|
||||
this.onRender?.();
|
||||
}
|
||||
|
||||
@@ -246,11 +136,11 @@ export class VoiceCardControls {
|
||||
: { ...view, visible: false, announce: '' };
|
||||
}
|
||||
|
||||
/** Idempotent: removes listeners and the idle timer; markup stays owned by the control. */
|
||||
/** Idempotent: removes the listener and the idle timer; the control stays. */
|
||||
destroy() {
|
||||
if (this.destroyed) return;
|
||||
this.destroyed = true;
|
||||
this.cancelIdle();
|
||||
this.listeners.abort();
|
||||
this.offDismiss?.();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user