Browse Source
将 `BasicCommand`、`TabsCommand` 和 `UpdateCommand` 从主进程迁移到 `common/event` 目录下,统一管理命令模块。同时,重构了 `PlatForm` 模块,使其使用新的 `ApiFactory` 进行 API 调用,提升了代码的可维护性和复用性。feat/icon
26 changed files with 444 additions and 312 deletions
@ -1,34 +1,23 @@ |
|||||
|
const { readFileSync, readFile } = require("node:fs") |
||||
|
|
||||
/* eslint-env node */ |
/* eslint-env node */ |
||||
require("@rushstack/eslint-patch/modern-module-resolution") |
require("@rushstack/eslint-patch/modern-module-resolution") |
||||
|
|
||||
|
const prettierConfig = JSON.parse(readFileSync("./.prettierrc.json", { encoding: "utf-8" })) |
||||
|
|
||||
module.exports = { |
module.exports = { |
||||
extends: [ |
extends: [ |
||||
"eslint:recommended", |
"eslint:recommended", |
||||
"plugin:vue/vue3-recommended", |
"plugin:vue/vue3-recommended", |
||||
"@electron-toolkit", |
"@electron-toolkit", |
||||
"@electron-toolkit/eslint-config-ts/eslint-recommended", |
"@electron-toolkit/eslint-config-ts/eslint-recommended", |
||||
"@vue/eslint-config-typescript/recommended", |
"@vue/eslint-config-typescript/recommended", |
||||
"@vue/eslint-config-prettier", |
"@vue/eslint-config-prettier", |
||||
], |
], |
||||
rules: { |
rules: { |
||||
"vue/require-default-prop": "off", |
"vue/require-default-prop": "off", |
||||
"vue/multi-word-component-names": "off", |
"vue/multi-word-component-names": "off", |
||||
"@typescript-eslint/no-explicit-any": "off", |
"@typescript-eslint/no-explicit-any": "off", |
||||
"prettier/prettier": [ |
"prettier/prettier": ["error", prettierConfig], |
||||
"error", |
}, |
||||
{ |
|
||||
tabWidth: 4, |
|
||||
useTabs: false, |
|
||||
semi: false, |
|
||||
singleQuote: false, |
|
||||
trailingComma: "all", |
|
||||
bracketSpacing: true, |
|
||||
arrowParens: "avoid", |
|
||||
printWidth: 140, |
|
||||
htmlWhitespaceSensitivity: "ignore", |
|
||||
proseWrap: "preserve", |
|
||||
endOfLine: "auto", |
|
||||
}, |
|
||||
], |
|
||||
}, |
|
||||
} |
} |
||||
|
@ -1,41 +1,41 @@ |
|||||
if (import.meta.env.DEV) { |
if (import.meta.env.DEV) { |
||||
// 引入之后可以热更新
|
// 引入之后可以热更新
|
||||
import("./languages/zh.json") |
import("./languages/zh.json") |
||||
import("./languages/en.json") |
import("./languages/en.json") |
||||
} |
} |
||||
|
|
||||
const datetimeFormats = { |
const datetimeFormats = { |
||||
en: { |
en: { |
||||
short: { |
short: { |
||||
year: "numeric", |
year: "numeric", |
||||
month: "short", |
month: "short", |
||||
day: "numeric", |
day: "numeric", |
||||
}, |
|
||||
long: { |
|
||||
year: "numeric", |
|
||||
month: "short", |
|
||||
day: "numeric", |
|
||||
weekday: "short", |
|
||||
hour: "numeric", |
|
||||
minute: "numeric", |
|
||||
}, |
|
||||
}, |
}, |
||||
zh: { |
long: { |
||||
short: { |
year: "numeric", |
||||
year: "numeric", |
month: "short", |
||||
month: "short", |
day: "numeric", |
||||
day: "numeric", |
weekday: "short", |
||||
}, |
hour: "numeric", |
||||
long: { |
minute: "numeric", |
||||
year: "numeric", |
|
||||
month: "short", |
|
||||
day: "numeric", |
|
||||
weekday: "short", |
|
||||
hour: "numeric", |
|
||||
minute: "numeric", |
|
||||
hour12: true, |
|
||||
}, |
|
||||
}, |
}, |
||||
|
}, |
||||
|
zh: { |
||||
|
short: { |
||||
|
year: "numeric", |
||||
|
month: "short", |
||||
|
day: "numeric", |
||||
|
}, |
||||
|
long: { |
||||
|
year: "numeric", |
||||
|
month: "short", |
||||
|
day: "numeric", |
||||
|
weekday: "short", |
||||
|
hour: "numeric", |
||||
|
minute: "numeric", |
||||
|
hour12: true, |
||||
|
}, |
||||
|
}, |
||||
} |
} |
||||
|
|
||||
export { datetimeFormats } |
export { datetimeFormats } |
||||
|
@ -1,13 +1,11 @@ |
|||||
import { Container, ContainerModule } from "inversify" |
import { Container, ContainerModule } from "inversify" |
||||
import BasicCommand from "./BasicCommand" |
import UpdateCommand from "common/event/Update/main/command" |
||||
import TabsCommand from "./TabsCommand" |
import PlatFormCommand from "common/event/PlatForm/main/command" |
||||
import UpdateCommand from "./UpdateCommand" |
import TabsCommand from "common/event/Tabs/main/command" |
||||
|
|
||||
// TODO 考虑迁移,将所有命令都注册common/event中
|
|
||||
|
|
||||
const modules = new ContainerModule(bind => { |
const modules = new ContainerModule(bind => { |
||||
bind("BasicCommand").to(BasicCommand).inSingletonScope() |
|
||||
bind("TabsCommand").to(TabsCommand).inSingletonScope() |
bind("TabsCommand").to(TabsCommand).inSingletonScope() |
||||
|
bind("PlatFormCommand").to(PlatFormCommand).inSingletonScope() |
||||
bind("UpdateCommand").to(UpdateCommand).inSingletonScope() |
bind("UpdateCommand").to(UpdateCommand).inSingletonScope() |
||||
}) |
}) |
||||
|
|
@ -1,7 +1,7 @@ |
|||||
import { inject } from "inversify" |
import { inject } from "inversify" |
||||
import Updater from "main/modules/updater" |
import Updater from "main/modules/updater" |
||||
|
|
||||
export default class BasicCommand { |
export default class PlatFormCommand { |
||||
constructor(@inject(Updater) private _Updater: Updater) {} |
constructor(@inject(Updater) private _Updater: Updater) {} |
||||
|
|
||||
async triggerHotUpdate() { |
async triggerHotUpdate() { |
@ -1,5 +1,5 @@ |
|||||
import { broadcast } from "main/utils" |
import { broadcast } from "main/utils" |
||||
import { AllKeys } from "../common" |
import { AllKeys } from "common/event/common" |
||||
|
|
||||
function emitHotUpdateReady(...argu) { |
function emitHotUpdateReady(...argu) { |
||||
broadcast<AllKeys>("hot-update-ready", ...argu) |
broadcast<AllKeys>("hot-update-ready", ...argu) |
@ -0,0 +1,53 @@ |
|||||
|
import { ElectronApiClient } from "common/lib/electron" |
||||
|
import { BrowserApiClient } from "common/lib/browser" |
||||
|
|
||||
|
// 定义抽象 API 接口
|
||||
|
export interface IApiClient { |
||||
|
call<T = any>(command: string, ...args: any[]): Promise<T> |
||||
|
on<K extends string>(channel: K, callback: (...args: any[]) => void): void |
||||
|
off<K extends string>(channel: K, callback: (...args: any[]) => void): void |
||||
|
offAll<K extends string>(channel: K): void |
||||
|
} |
||||
|
|
||||
|
class NullApiClient implements IApiClient { |
||||
|
async call<T = any>(command: string, ...args: any[]): Promise<T> { |
||||
|
args |
||||
|
console.warn(`API call to ${command} failed: API client not initialized`) |
||||
|
return undefined as any |
||||
|
} |
||||
|
|
||||
|
on<K extends string>(channel: K, callback: (...args: any[]) => void): void { |
||||
|
callback |
||||
|
console.warn(`Failed to register listener for ${channel}: API client not initialized`) |
||||
|
} |
||||
|
|
||||
|
off<K extends string>(channel: K, callback: (...args: any[]) => void): void { |
||||
|
callback |
||||
|
console.warn(`Failed to unregister listener for ${channel}: API client not initialized`) |
||||
|
} |
||||
|
|
||||
|
offAll<K extends string>(channel: K): void { |
||||
|
console.warn(`Failed to unregister all listeners for ${channel}: API client not initialized`) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// 创建 API 工厂
|
||||
|
export class ApiFactory { |
||||
|
private static instance: IApiClient = new NullApiClient() // 默认使用空实现
|
||||
|
|
||||
|
static setApiClient(client: IApiClient) { |
||||
|
this.instance = client |
||||
|
} |
||||
|
|
||||
|
static getApiClient(): IApiClient { |
||||
|
if (this.instance instanceof NullApiClient) { |
||||
|
// 根据环境选择合适的 API 客户端
|
||||
|
if (window.api && window.electron) { |
||||
|
this.instance = new ElectronApiClient() |
||||
|
} else { |
||||
|
this.instance = new BrowserApiClient() |
||||
|
} |
||||
|
} |
||||
|
return this.instance |
||||
|
} |
||||
|
} |
@ -0,0 +1,29 @@ |
|||||
|
import { IApiClient } from "./abstract" |
||||
|
|
||||
|
export class BrowserApiClient implements IApiClient { |
||||
|
call<T = any>(command: string, ...args: any[]): Promise<T> { |
||||
|
// 浏览器特定实现,可能使用 fetch 或其他方式
|
||||
|
const [service, method] = command.split(".") |
||||
|
return fetch(`/api/${service}/${method}`, { |
||||
|
method: "POST", |
||||
|
body: JSON.stringify(args), |
||||
|
headers: { "Content-Type": "application/json" }, |
||||
|
}).then(res => res.json()) |
||||
|
} |
||||
|
|
||||
|
// 实现其他方法...
|
||||
|
on<K extends string>(channel: K, callback: (...args: any[]) => void): void { |
||||
|
// 浏览器中可能使用 WebSocket 或其他方式
|
||||
|
console.log("不支持 on 方法", channel, callback) |
||||
|
} |
||||
|
|
||||
|
off<K extends string>(channel: K, callback: (...args: any[]) => void): void { |
||||
|
// 相应的解绑实现
|
||||
|
console.log("不支持 on 方法", channel, callback) |
||||
|
} |
||||
|
|
||||
|
offAll<K extends string>(channel: K): void { |
||||
|
// 相应的全部解绑实现
|
||||
|
console.log("不支持 on 方法", channel) |
||||
|
} |
||||
|
} |
@ -0,0 +1,20 @@ |
|||||
|
import { IApiClient } from "./abstract" |
||||
|
|
||||
|
export class ElectronApiClient implements IApiClient { |
||||
|
call<T = any>(command: string, ...args: any[]): Promise<T> { |
||||
|
// Electron 特定实现
|
||||
|
return window.api.call(command, ...args) |
||||
|
} |
||||
|
|
||||
|
on<K extends string>(channel: K, callback: (...args: any[]) => void): void { |
||||
|
window.api.on(channel, callback) |
||||
|
} |
||||
|
|
||||
|
off<K extends string>(channel: K, callback: (...args: any[]) => void): void { |
||||
|
window.api.off(channel, callback) |
||||
|
} |
||||
|
|
||||
|
offAll<K extends string>(channel: K): void { |
||||
|
window.api.offAll(channel) |
||||
|
} |
||||
|
} |
@ -1,12 +0,0 @@ |
|||||
// import { inject } from "inversify"
|
|
||||
// import Setting from "main/modules/setting"
|
|
||||
|
|
||||
// class SettingCommand {
|
|
||||
// constructor(@inject(Setting) private _Setting: Setting) {
|
|
||||
// console.log(this._Setting)
|
|
||||
// }
|
|
||||
|
|
||||
// getAll() {
|
|
||||
// return this._Setting.config()
|
|
||||
// }
|
|
||||
// }
|
|
Loading…
Reference in new issue