File size: 1,756 Bytes
494c9e4
 
 
 
 
 
 
 
 
 
12617a9
494c9e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12617a9
494c9e4
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * 应用公共初始化逻辑
 * 提供 start.ts 和 compare.ts 共享的基础初始化功能
 */

import * as d3 from 'd3';
import { SimpleEventHandler } from './utils/SimpleEventHandler';
import { TextAnalysisAPI } from './api/GLTR_API';
import { initForceNarrowFromStorage } from './utils/responsive';
import { getTokenSurprisalColor, getByteSurprisalColor, HISTOGRAM_MIN_ALPHA } from './utils/SurprisalColorConfig';
import { initClientActivityPing } from './utils/clientActivityPing';

/**
 * 公共初始化返回对象
 */
export interface CommonAppContext {
    eventHandler: SimpleEventHandler;
    api: TextAnalysisAPI;
    tokenSurprisalColorScale: (value: number) => string;
    byteSurprisalColorScale: (value: number) => string;
    totalSurprisalFormat: (n: number | null) => string;
}

/**
 * 初始化公共应用组件
 * @param apiPrefix API 前缀(默认为空字符串)
 * @param element 事件处理器绑定的元素(默认为 document.body)
 * @returns 初始化后的公共对象
 */
export function initializeCommonApp(apiPrefix: string = '', element?: Element): CommonAppContext {
    initForceNarrowFromStorage();
    initClientActivityPing(apiPrefix);

    // 使用传入的元素或默认 body 元素
    const targetElement = element || document.body;
    
    const format = d3.format('.2f');
    return {
        eventHandler: new SimpleEventHandler(targetElement),
        api: new TextAnalysisAPI(apiPrefix),
        tokenSurprisalColorScale: (v) => getTokenSurprisalColor(v, HISTOGRAM_MIN_ALPHA),
        byteSurprisalColorScale: (v) => getByteSurprisalColor(v, 1, HISTOGRAM_MIN_ALPHA),
        totalSurprisalFormat: (n: number | null) => n !== null && Number.isFinite(n) ? format(n) : String(n)
    };
}