- 新增 cores.tsx 文件,实现核心工具模块 - 添加任务列表和事件总线功能 - 实现系统通知和任务处理逻辑 - 在 App.vue 中集成新功能 - 优化通知显示逻辑,支持不同内容类型的通知
58 lines
1.2 KiB
TypeScript
58 lines
1.2 KiB
TypeScript
import mitt, { Emitter } from 'mitt';
|
|
|
|
export interface TaskProps {
|
|
name: string;
|
|
custom?: any;
|
|
}
|
|
|
|
// 定义自定义事件类型
|
|
export type BusEvents = {
|
|
onNewTask: TaskProps | undefined;
|
|
};
|
|
|
|
export interface Task {
|
|
id: number;
|
|
handle: string;
|
|
data: any;
|
|
createTime: Date;
|
|
custom?: any;
|
|
}
|
|
|
|
export interface Core {
|
|
bus: Emitter<BusEvents>;
|
|
// eslint-disable-next-line no-unused-vars
|
|
showNotification(body: string, title?: string): Notification | undefined;
|
|
taskList: Map<String, Task>;
|
|
}
|
|
|
|
const bus = mitt<BusEvents>();
|
|
export function getSystemNotification(body: string, title?: string) {
|
|
if (!title) {
|
|
title = '通知';
|
|
}
|
|
return new Notification(title ?? '通知', {
|
|
body: body,
|
|
});
|
|
}
|
|
export function showSystemNotification(body: string, title?: string): Notification | undefined {
|
|
if (Notification.permission === 'granted') {
|
|
return getSystemNotification(body, title);
|
|
} else if (Notification.permission !== 'denied') {
|
|
Notification.requestPermission().then((permission) => {
|
|
if (permission === 'granted') {
|
|
return getSystemNotification(body, title);
|
|
}
|
|
});
|
|
}
|
|
return void 0;
|
|
}
|
|
const taskList = new Map<String, Task>();
|
|
|
|
export function useCore(): Core {
|
|
return {
|
|
bus,
|
|
showNotification: showSystemNotification,
|
|
taskList,
|
|
};
|
|
}
|