1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- interface RegisterParams {
- key: string; // 唯一标识
- show:
- | ((...p: any) => Promise<"close" | undefined | void>)
- | ((...p: any) => "close" | undefined | void); // 显示函数
- hide?: Function; // 隐藏函数
- }
- class DialogManage {
- private _dialogs: Map<string, RegisterParams>; // 存储所有的dialogManage
- private _resolves: Map<string, Function>;
- constructor() {
- this._dialogs = new Map();
- this._resolves = new Map();
- }
- registerDialog({ key, show, hide }: RegisterParams) {
- this._dialogs.set(key, {
- key,
- show,
- hide,
- });
- }
- unregisterDialog(key: string) {
- this._dialogs.delete(key);
- }
- showDialog(key: string, ...args: any[]) {
- return new Promise((resolve) => {
- const dialog = this._dialogs.get(key);
- if (dialog?.show) {
- const res = dialog.show(...args);
- if (res === "close") {
- return resolve("close");
- }
- this._resolves.set(key, resolve);
- } else {
- console.error(`Dialog with key ${key} not found`);
- resolve("error");
- }
- });
- }
- hideDialog(key: string, ...args: any[]) {
- const dialog = this._dialogs.get(key);
- if (dialog) {
- dialog.hide && dialog.hide(...args);
- } else {
- console.error(`Dialog with key ${key} not found`);
- }
- let resolveFun = this._resolves.get(key);
- if (resolveFun && typeof resolveFun === "function") {
- resolveFun("close");
- this._resolves.delete(key);
- }
- }
- goTarget(key: string) {
- // const dialog = this._dialogs.get(key);
- let resolveFun = this._resolves.get(key);
- if (resolveFun && typeof resolveFun === "function") {
- resolveFun("target");
- this._resolves.delete(key);
- }
- }
- }
- const dialogManage = new DialogManage();
- // 为 Window 类型添加自定义属性 dialogManage
- global.dialogManage = dialogManage;
- export default dialogManage;
|