Files
QOffice/src/hooks/useQOfficeCommand.ts
T
Курнат Андрей c16df37c95 Wire quick access commands
2026-06-02 20:39:30 +03:00

31 lines
938 B
TypeScript

import { useEffect, useRef } from "react";
export type QOfficeCommand = "open" | "save" | "export-pdf" | "print" | "undo" | "redo";
export type QOfficeCommandHandlers = Partial<Record<QOfficeCommand, () => void | Promise<void>>>;
export function useQOfficeCommand(handlers: QOfficeCommandHandlers) {
const handlersRef = useRef(handlers);
useEffect(() => {
handlersRef.current = handlers;
}, [handlers]);
useEffect(() => {
function onCommand(event: WindowEventMap["qoffice-command"]) {
const command = event.detail.command;
const handler = handlersRef.current[command];
if (handler) {
void handler();
return;
}
if (command === "undo" || command === "redo") {
globalThis.document.execCommand?.(command);
}
}
window.addEventListener("qoffice-command", onCommand);
return () => window.removeEventListener("qoffice-command", onCommand);
}, []);
}