// @ts-nocheck import { automationMode, hfSpaceMode, normalizeVisionModel, readStoredVisionModel, readStoredVisionModelRaw, visionModelLabel, visionModelStorageKey, defaultVisionModel } from "./config.js"; import { state } from "./state.js"; export function setNativeInputValue(input, value) { if (!input || !input.isConnected) return false; const proto = input instanceof HTMLTextAreaElement ? HTMLTextAreaElement.prototype : HTMLInputElement.prototype; try { const setter = Object.getOwnPropertyDescriptor(proto, 'value')?.set; if (setter) setter.call(input, value); else input.value = value; input.dispatchEvent(new InputEvent('input', { bubbles: true, inputType: 'insertText', data: value })); input.dispatchEvent(new Event('change', { bubbles: true })); return true; } catch (error) { window['__trackwhatDesktopCaptureTrace']?.('frame.set.error', { message: error?.message || String(error) }); return false; } } export function setFrameValue(dataUrl) { const payload = JSON.stringify({ ts: Date.now(), nonce: Math.random().toString(36).slice(2), dataUrl: dataUrl || '' }); window.__trackwhatDesktopFrameData = payload; window['__trackwhatDesktopCaptureTrace']?.('frame.set', { length: payload.length, hasDataUrl: Boolean(dataUrl) }); return true; } export function setGradioValue(selector, value) { const input = document.querySelector(`${selector} textarea, ${selector} input`); if (!input) return false; setNativeInputValue(input, value || ''); return true; } export function visionModelInputs() { return Array.from(document.querySelectorAll('#vision-model input[type="radio"]')); } export function readVisionModelControlValue() { const checked = visionModelInputs().find((input) => input.checked); return normalizeVisionModel(checked?.value); } function storeVisionModel(value) { const normalized = normalizeVisionModel(value); try { window.localStorage?.setItem(visionModelStorageKey, normalized); } catch {} } function findVisionModelInput(value) { const normalized = normalizeVisionModel(value); const label = visionModelLabel(normalized); return visionModelInputs().find((input) => normalizeVisionModel(input.value) === normalized) || visionModelInputs().find((input) => (input.closest('label')?.textContent || '').trim() === label); } function setVisionModel(value) { const normalized = normalizeVisionModel(value); const input = findVisionModelInput(normalized); if (input && !input.checked) input.click(); storeVisionModel(normalized); } export function mountVisionModelControl() { const host = document.getElementById('vision-model'); if (!host || !visionModelInputs().length) return; if (host.dataset.bound !== 'true') { host.dataset.bound = 'true'; host.addEventListener('change', () => storeVisionModel(readVisionModelControlValue())); host.addEventListener('input', () => storeVisionModel(readVisionModelControlValue())); } const storedVisionModel = readStoredVisionModelRaw(); if (hfSpaceMode && !automationMode && storedVisionModel === 'fast-test') { setVisionModel(defaultVisionModel); return; } setVisionModel(storedVisionModel ? normalizeVisionModel(storedVisionModel) : (state.automationMode ? 'fast-test' : readStoredVisionModel())); } export function selectTrackForEdit(row) { const trackId = row?.dataset?.trackId || ''; if (!trackId || row.dataset.trackUnknown === 'true') return; setGradioValue('#selected-track-id', trackId); for (const selected of document.querySelectorAll('#tracks-grid tr.is-selected-track')) selected.classList.remove('is-selected-track'); row.classList.add('is-selected-track'); window.setTimeout(() => { document.querySelector('#select-track-for-edit-btn button, #select-track-for-edit-btn')?.click(); }, 0); } export function mountTrackSelection() { if (window.__trackwhatTrackSelectionMounted) return; window.__trackwhatTrackSelectionMounted = true; document.addEventListener('click', (event) => { const row = event.target?.closest?.('#tracks-grid .tw-track-row'); if (row) selectTrackForEdit(row); }); document.addEventListener('keydown', (event) => { if (event.key !== 'Enter' && event.key !== ' ') return; const row = event.target?.closest?.('#tracks-grid .tw-track-row'); if (!row) return; event.preventDefault(); selectTrackForEdit(row); }); }