/** * Collabora Online 清除高亮工具 * * 职责: 通过调用 Python 脚本清除文档中的高亮背景色 * * 核心逻辑: * 1. 使用通用的 callPythonScript 工具调用 ClearHighlights.py * 2. 将 ScriptResult 转换为业务层的 ClearHighlightResponse * * @encoding UTF-8 */ import { callPythonScript, type ScriptResult } from '../CallCustomScript'; /** * 清除高亮响应接口 */ export interface ClearHighlightResponse { success: boolean; message: string; count?: number; // 清除的高亮区域数量 timestamp: number; } /** * 清除高亮选项 */ export interface ClearHighlightOptions { color?: number; // 要清除的颜色值,默认 16776960 (黄色) timeout?: number; // 超时时间(毫秒),默认 10000ms } /** * 清除指定颜色的高亮 * * 通过调用 Collabora 服务端的 Python 脚本 (ClearHighlights.py) 来清除文档中所有指定颜色的背景高亮。 * * @param iframeWindow - Collabora iframe 的 contentWindow * @param options - 可选配置 * @param options.color - 要清除的颜色值 (LibreOffice Decimal 格式), 默认 16776960 = 黄色 * @param options.timeout - 超时时间(毫秒),默认 10000ms * @returns Promise * * @example * // 清除默认黄色高亮 * const result = await clearHighlights(iframeWindow); * console.log(result.message); // "Success: Cleared 5 regions." * * @example * // 清除自定义颜色高亮 * const result = await clearHighlights(iframeWindow, { color: 0xFF00FF }); // 紫色 */ export async function clearHighlights( iframeWindow: Window, options?: ClearHighlightOptions ): Promise { const color = options?.color ?? 16776960; // 默认黄色 const timeout = options?.timeout ?? 10000; // 默认10秒超时 try { // 使用通用的 callPythonScript 工具 const result: ScriptResult = await callPythonScript( iframeWindow, 'ClearHighlights.py', 'ClearSpecificColor', { color }, { timeout, verbose: true } ); // 将 ScriptResult 转换为 ClearHighlightResponse return { success: result.success, message: result.message, count: result.data?.count, timestamp: result.timestamp, }; } catch (error) { // 处理超时或其他错误 return { success: false, message: error instanceof Error ? error.message : '清除高亮失败', timestamp: Date.now(), }; } }