File size: 13,368 Bytes
78431ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/**
 * Polyscriptor Web UI β€” Main application entry point
 *
 * Central state + event bus, wires up components.
 * No framework, no build step β€” native ES modules.
 */

import { initEnginePanel } from './components/engine-panel.js';
import { initImageViewer } from './components/image-viewer.js';
import { initTranscriptionPanel } from './components/transcription-panel.js';
import { initBatchPanel } from './components/batch-panel.js';

// ── Global state ───────────────────────────────────────────────────────
export const state = {
    engines: [],
    currentEngine: null,
    engineLoaded: false,
    imageId: null,
    imageInfo: null,
    lines: [],           // [{index, text, confidence, bbox, region}]
    regions: [],         // [{id, bbox, num_lines}] β€” from latest segmentation
    isProcessing: false,
};

// ── Event bus ──────────────────────────────────────────────────────────
export const events = new EventTarget();
export function emit(name, detail) {
    events.dispatchEvent(new CustomEvent(name, { detail }));
}
export function on(name, fn) {
    events.addEventListener(name, e => fn(e.detail));
}

// ── API helper ─────────────────────────────────────────────────────────
export async function api(path, options = {}) {
    const resp = await fetch(path, {
        headers: { 'Content-Type': 'application/json', ...options.headers },
        ...options,
    });
    if (!resp.ok) {
        const err = await resp.json().catch(() => ({ detail: resp.statusText }));
        throw new Error(err.detail || err.message || 'API error');
    }
    return resp;
}

// ── Toast notifications ────────────────────────────────────────────────
export function toast(message, type = 'info', durationMs = 4000) {
    const container = document.getElementById('toast-container');
    const el = document.createElement('div');
    el.className = `toast toast-${type}`;
    el.textContent = message;
    container.appendChild(el);
    setTimeout(() => el.remove(), durationMs);
}

// ── GPU status widget ──────────────────────────────────────────────────
function shortName(name) {
    // Abbreviate long GPU names for the header
    return name
        .replace('NVIDIA ', '')
        .replace('GeForce ', '')
        .replace('Tesla ', '')
        .replace('Quadro ', '');
}

async function updateGpuStatus() {
    const widget = document.getElementById('gpu-status');
    try {
        const resp = await api('/api/gpu');
        const data = await resp.json();

        if (!data.available || data.gpus.length === 0) {
            widget.innerHTML = '<span class="gpu-card-name"><span>GPU: N/A</span></span>';
            return;
        }

        widget.innerHTML = data.gpus.map(g => {
            const usedPct = Math.round((g.memory_used_mb / g.memory_total_mb) * 100);
            const fillClass = usedPct >= 85 ? 'hot' : usedPct >= 60 ? 'warm' : '';
            const usedGb   = (g.memory_used_mb / 1000).toFixed(1);
            const totalGb  = (g.memory_total_mb / 1000).toFixed(0);
            const utilHtml = g.utilization_gpu_pct != null
                ? `<span class="gpu-util-pct">${g.utilization_gpu_pct}%</span>` : '';

            return `<div class="gpu-card">
                <div class="gpu-card-name">
                    <span title="${g.name}">${shortName(g.name)}</span>${utilHtml}
                </div>
                <div class="gpu-mem-bar">
                    <div class="gpu-mem-fill ${fillClass}" style="width:${usedPct}%"></div>
                </div>
                <div class="gpu-mem-label">${usedGb}/${totalGb} GB VRAM</div>
            </div>`;
        }).join('');
    } catch {
        widget.innerHTML = '<span style="font-size:.75rem;color:var(--text-muted)">GPU: error</span>';
    }
}

// ── Zoom controls ──────────────────────────────────────────────────────
let zoomLevel = 1.0;
const ZOOM_STEP = 0.25;
let   ZOOM_MIN  = 0.25; // updated per image in fitZoom() so large images are always reachable
const ZOOM_MAX  = 4.0;

function applyZoom(level) {
    const img    = document.getElementById('page-image');
    const canvas = document.getElementById('overlay-canvas');
    if (!img || !img.naturalWidth) return;

    zoomLevel = Math.max(ZOOM_MIN, Math.min(ZOOM_MAX, level));
    const w = Math.round(img.naturalWidth  * zoomLevel);
    const h = Math.round(img.naturalHeight * zoomLevel);

    img.style.width  = w + 'px';
    img.style.height = h + 'px';
    canvas.style.width  = w + 'px';
    canvas.style.height = h + 'px';

    document.getElementById('zoom-level').textContent =
        Math.round(zoomLevel * 100) + '%';
}

export function fitZoom() {
    const img    = document.getElementById('page-image');
    const scroll = document.getElementById('viewer-scroll');
    if (!img || !img.naturalWidth || !scroll) return;
    const scaleW = scroll.clientWidth  / img.naturalWidth;
    const scaleH = scroll.clientHeight / img.naturalHeight;
    const fit = Math.min(scaleW, scaleH, 1.0); // never zoom in beyond 100% on fit
    // Ensure the fit level is always reachable: lower ZOOM_MIN for large images (min 5%)
    ZOOM_MIN = Math.min(0.25, Math.max(0.05, fit));
    applyZoom(fit);
}

function initZoomControls() {
    document.getElementById('btn-zoom-in') .addEventListener('click', () => applyZoom(zoomLevel + ZOOM_STEP));
    document.getElementById('btn-zoom-out').addEventListener('click', () => applyZoom(zoomLevel - ZOOM_STEP));
    document.getElementById('btn-zoom-fit').addEventListener('click', fitZoom);

    // Mouse-wheel zoom in viewer β€” multiplicative for smooth feel
    document.getElementById('viewer-scroll').addEventListener('wheel', e => {
        if (!e.ctrlKey && !e.metaKey) return;
        e.preventDefault();
        const factor = e.deltaY < 0 ? 1.10 : 1 / 1.10;
        applyZoom(zoomLevel * factor);
    }, { passive: false });

    on('image-uploaded', () => {
        document.getElementById('zoom-toolbar').classList.remove('hidden');
        // fit after short delay to let image render
        setTimeout(fitZoom, 80);
    });

    // Also show toolbar when a batch item is displayed in the viewer
    on('batch-item-start', () => {
        document.getElementById('zoom-toolbar').classList.remove('hidden');
    });
}

// ── Sticky engine config (localStorage) ───────────────────────────────
const LS_ENGINE = 'polyscriptor_last_engine';
const LS_CONFIG = name => `polyscriptor_config_${name}`;

export function saveEngineConfig(engineName, configObj) {
    try {
        localStorage.setItem(LS_ENGINE, engineName);
        localStorage.setItem(LS_CONFIG(engineName), JSON.stringify(configObj));
    } catch { /* storage full or private mode */ }
}

export function loadSavedEngineName() {
    try { return localStorage.getItem(LS_ENGINE); } catch { return null; }
}

export function loadSavedEngineConfig(engineName) {
    try {
        const raw = localStorage.getItem(LS_CONFIG(engineName));
        return raw ? JSON.parse(raw) : null;
    } catch { return null; }
}

// ── Mobile tab helper ───────────────────────────────────────────────────
function mobileActivateTab(target) {
    const tabBtns = document.querySelectorAll('.tab-btn');
    const panels  = document.querySelectorAll('[data-panel]');
    if (!tabBtns.length) return;
    tabBtns.forEach(b => b.classList.toggle('active', b.dataset.target === target));
    panels.forEach(p => p.classList.toggle('panel-active', p.dataset.panel === target));
}

// ── Resizable panels ───────────────────────────────────────────────────
const LS_PANEL_LEFT  = 'polyscriptor_panel_left';
const LS_PANEL_RIGHT = 'polyscriptor_panel_right';

function initResizablePanels() {
    const app = document.getElementById('app');
    const handleLeft  = document.getElementById('resize-left');
    const handleRight = document.getElementById('resize-right');
    if (!handleLeft || !handleRight) return;

    // Restore saved widths
    const savedLeft  = localStorage.getItem(LS_PANEL_LEFT);
    const savedRight = localStorage.getItem(LS_PANEL_RIGHT);
    if (savedLeft)  document.documentElement.style.setProperty('--panel-left',  savedLeft);
    if (savedRight) document.documentElement.style.setProperty('--panel-right', savedRight);

    function startDrag(handle, isLeft) {
        handle.classList.add('dragging');
        document.body.style.cursor = 'col-resize';
        document.body.style.userSelect = 'none';

        const onMove = (e) => {
            const appRect = app.getBoundingClientRect();
            const x = (e.touches ? e.touches[0].clientX : e.clientX) - appRect.left;
            const totalW = appRect.width;

            if (isLeft) {
                const w = Math.max(160, Math.min(x, totalW * 0.4));
                const val = Math.round(w) + 'px';
                document.documentElement.style.setProperty('--panel-left', val);
                localStorage.setItem(LS_PANEL_LEFT, val);
            } else {
                const w = Math.max(200, Math.min(totalW - x, totalW * 0.5));
                const val = Math.round(w) + 'px';
                document.documentElement.style.setProperty('--panel-right', val);
                localStorage.setItem(LS_PANEL_RIGHT, val);
            }
        };

        const onUp = () => {
            handle.classList.remove('dragging');
            document.body.style.cursor = '';
            document.body.style.userSelect = '';
            document.removeEventListener('mousemove', onMove);
            document.removeEventListener('mouseup', onUp);
            document.removeEventListener('touchmove', onMove);
            document.removeEventListener('touchend', onUp);
        };

        document.addEventListener('mousemove', onMove);
        document.addEventListener('mouseup', onUp);
        document.addEventListener('touchmove', onMove, { passive: true });
        document.addEventListener('touchend', onUp);
    }

    handleLeft.addEventListener('mousedown',  e => { e.preventDefault(); startDrag(handleLeft,  true); });
    handleRight.addEventListener('mousedown', e => { e.preventDefault(); startDrag(handleRight, false); });
    handleLeft.addEventListener('touchstart',  e => startDrag(handleLeft,  true),  { passive: true });
    handleRight.addEventListener('touchstart', e => startDrag(handleRight, false), { passive: true });
}

// ── Keyboard shortcuts ─────────────────────────────────────────────────
function initKeyboardShortcuts() {
    document.addEventListener('keydown', e => {
        // Ignore when typing in an input / textarea / contenteditable
        const tag = e.target.tagName;
        const editable = e.target.isContentEditable;
        if (tag === 'INPUT' || tag === 'TEXTAREA' || tag === 'SELECT' || editable) return;

        // Ctrl+Enter β€” transcribe
        if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') {
            e.preventDefault();
            document.getElementById('btn-transcribe')?.click();
            return;
        }

        // ArrowLeft / ArrowRight β€” batch prev/next
        if (e.key === 'ArrowLeft')  { e.preventDefault(); document.getElementById('btn-nav-prev')?.click(); }
        if (e.key === 'ArrowRight') { e.preventDefault(); document.getElementById('btn-nav-next')?.click(); }
    });
}

// ── Prevent browser from opening dropped files in a new tab ────────────
function initGlobalDropBlocker() {
    document.addEventListener('dragover', e => e.preventDefault());
    document.addEventListener('drop',     e => e.preventDefault());
}

// ── Init ───────────────────────────────────────────────────────────────
document.addEventListener('DOMContentLoaded', () => {
    initEnginePanel();
    initImageViewer();
    initTranscriptionPanel();
    initBatchPanel();
    initZoomControls();
    initResizablePanels();
    initKeyboardShortcuts();
    initGlobalDropBlocker();
    updateGpuStatus();
    setInterval(updateGpuStatus, 15000); // refresh every 15s

    // On mobile: auto-switch tab after key events
    on('image-uploaded',        () => mobileActivateTab('image'));
    on('segment-preview',       () => mobileActivateTab('image'));
    on('transcription-start',   () => mobileActivateTab('results'));
});