文件
mynode/packages/app-desktop/gui/WindowCommandsAndDialogs/utils/useWindowControl.ts
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

79 行
2.3 KiB
TypeScript

import * as React from 'react';
import { RefObject, useMemo, useRef } from 'react';
import { DialogState } from '../types';
import { PrintCallback } from './usePrintToCallback';
import { _ } from '@joplin/lib/locale';
import announceForAccessibility from '../../utils/announceForAccessibility';
interface PromptSuggestion<T> {
key: string;
value: T;
label: string;
indentDepth?: number;
}
interface PromptOptions<T> {
label: string;
value: string;
suggestions: PromptSuggestion<T>[];
}
export interface WindowControl {
setState: (update: Partial<DialogState>)=> void;
showPrompt: <T>(options: PromptOptions<T>)=> Promise<T>;
printTo: PrintCallback;
announcePanelVisibility(panelName: string, visible: boolean): void;
getFocusedDocument(): Document;
}
export type OnSetDialogState = React.Dispatch<React.SetStateAction<DialogState>>;
const useWindowControl = (setDialogState: OnSetDialogState, onPrint: PrintCallback, windowDomRef: RefObject<Document>) => {
// Use refs to avoid reloading the output where possible -- reloading the window control
// may mean reloading all main window commands.
const onPrintRef = useRef(onPrint);
onPrintRef.current = onPrint;
return useMemo((): WindowControl => {
const control: WindowControl = {
setState: (newPartialState: Partial<DialogState>) => {
setDialogState(oldState => ({
...oldState,
...newPartialState,
}));
},
printTo: (target, options) => onPrintRef.current(target, options),
announcePanelVisibility: (panelName, visible) => {
announceForAccessibility(
visible ? _('Panel "%s" is visible', panelName) : _('Panel %s is hidden', panelName),
);
},
showPrompt: <T> (options: PromptOptions<T>) => {
return new Promise<T>((resolve) => {
control.setState({
promptOptions: {
label: options.label,
inputType: 'dropdown',
value: options.value,
autocomplete: options.suggestions,
onClose: async (answer: unknown) => {
if (answer) {
resolve((answer as PromptSuggestion<T>).value);
} else {
resolve(null);
}
control.setState({ promptOptions: null });
},
},
});
});
},
getFocusedDocument: () => {
return windowDomRef.current;
},
};
return control;
}, [setDialogState, windowDomRef]);
};
export default useWindowControl;