文件
chen 301194963b
一些检查失败了
automerge / automerge (push) Has been cancelled
react-native-android-build-apk / AssembleRelease (push) Has been cancelled
Build macOS M1 / Main (push) Has been cancelled
Joplin Continuous Integration / Main (macos-15-intel) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-11-vs2026-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-2025) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04-arm) (push) Has been cancelled
Joplin UI tests / Main (ubuntu-22.04) (push) Has been cancelled
Joplin UI tests / Main (windows-2025) (push) Has been cancelled
Joplin UI tests / Mobile (push) Has been cancelled
Close stale issues / ProcessStaleIssues (push) Has been cancelled
feat: Initial clean release of CHM
2026-11-04 11:29:39 +08:00

55 行
1.9 KiB
TypeScript

import { EditorNoteStatuses, State } from '@joplin/lib/reducer';
import { RefObject, useContext } 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';
import { WindowIdContext } from '../../NewWindowOrIFrame';
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, documentRef);
const windowId = useContext(WindowIdContext);
// 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: (_state: State, targetWindowId: string|null) => {
if (targetWindowId === windowId) return 2;
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, windowId]);
};
export default useWindowCommands;