54 lines
1.5 KiB
TypeScript
54 lines
1.5 KiB
TypeScript
/**
|
|
* Collabora Online UNO 命令核心工具
|
|
*
|
|
* 职责: 提供基础的 UNO 命令发送和状态监听功能
|
|
*
|
|
* @encoding UTF-8
|
|
*/
|
|
|
|
/**
|
|
* 发送 UNO 命令到 Collabora iframe
|
|
* @param iframeWindow - iframe 的 contentWindow
|
|
* @param command - UNO 命令名称,如 '.uno:ExecuteSearch'
|
|
* @param args - 命令参数
|
|
*/
|
|
export function sendUnoCommand(
|
|
iframeWindow: Window,
|
|
command: string,
|
|
args: Record<string, unknown> = {}
|
|
): void {
|
|
const message = {
|
|
MessageId: 'Send_UNO_Command',
|
|
SendTime: Date.now(),
|
|
Values: {
|
|
Command: command,
|
|
Args: args,
|
|
},
|
|
};
|
|
|
|
console.log('[UNO] 发送命令:', command, args);
|
|
iframeWindow.postMessage(JSON.stringify(message), '*');
|
|
}
|
|
|
|
/**
|
|
* 获取文档状态 (用于检测命令队列完成)
|
|
* @param iframeWindow - iframe 的 contentWindow
|
|
*
|
|
* 说明: 发送 Get_State 命令作为"哨兵命令",利用 Collabora 的单线程命令队列机制。
|
|
* 当收到 Doc_ModifiedStatus 类型的回调时,证明前面队列中的所有命令都已执行完毕。
|
|
*
|
|
* 响应格式: { MessageId: 'Doc_ModifiedStatus', Values: {...} }
|
|
*/
|
|
export function unoGetState(iframeWindow: Window): void {
|
|
const message = {
|
|
MessageId: 'Get_State',
|
|
SendTime: Date.now(),
|
|
Values: {
|
|
CommandName: '.uno:ModifiedStatus',
|
|
},
|
|
};
|
|
|
|
console.log('[UNO] 发送 Get_State (.uno:ModifiedStatus) - 等待命令队列执行完成');
|
|
iframeWindow.postMessage(JSON.stringify(message), '*');
|
|
}
|