1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-21 09:38:01 +02:00
joplin/packages/app-desktop/gui/WindowCommandsAndDialogs/utils/useWindowCommands.ts
Henry Heino 4a88d6ff7a
Desktop: Multiple window support (#11181)
Co-authored-by: Laurent Cozic <laurent22@users.noreply.github.com>
2024-11-08 15:32:05 +00:00

52 lines
1.7 KiB
TypeScript

import { EditorNoteStatuses } from '@joplin/lib/reducer';
import { RefObject } from 'react';
import usePrintToCallback from './usePrintToCallback';
import useWindowControl, { OnSetDialogState, WindowControl } from './useWindowControl';
import commands from '../commands';
import CommandService, { CommandRuntime, ComponentCommandSpec } from '@joplin/lib/services/CommandService';
import useNowEffect from '@joplin/lib/hooks/useNowEffect';
import { PluginStates } from '@joplin/lib/services/plugins/reducer';
interface Props {
documentRef: RefObject<Document|null>;
customCss: string;
plugins: PluginStates;
editorNoteStatuses: EditorNoteStatuses;
setDialogState: OnSetDialogState;
}
const useWindowCommands = ({ documentRef, customCss, plugins, editorNoteStatuses, setDialogState }: Props) => {
const onPrintCallback = usePrintToCallback({
customCss: customCss,
editorNoteStatuses: editorNoteStatuses,
plugins: plugins,
});
const windowControl = useWindowControl(setDialogState, onPrintCallback);
// This effect needs to run as soon as possible. Certain components may fail to load if window
// commands are not registered on their first render.
useNowEffect(() => {
const runtimeHandles = commands.map((command: ComponentCommandSpec<WindowControl>) => {
const runtime: CommandRuntime = {
getPriority: () => {
return documentRef.current?.hasFocus() ? 1 : 0;
},
...command.runtime(windowControl),
};
return CommandService.instance().registerRuntime(
command.declaration.name,
runtime,
true,
);
});
return () => {
for (const runtimeHandle of runtimeHandles) {
runtimeHandle.deregister();
}
};
}, [windowControl]);
};
export default useWindowCommands;