diff --git a/.eslintrc.js b/.eslintrc.js index 783d64b80e..58db145b03 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -64,5 +64,28 @@ module.exports = { "no-console": "off", }, }, + // Electronのメインプロセス以外でelectronのimportを禁止する + { + files: ["./src/**/*.ts", "./src/**/*.vue"], + excludedFiles: [ + "./src/background.ts", + "./src/background/*.ts", + "./src/electron/*.ts", + ], + rules: { + "no-restricted-imports": [ + "error", + { + patterns: [ + { + group: ["electron"], + message: + "このファイル内でelectronはimportできません。許可されているファイル内へ移すか、ESLintの設定を見直してください", + }, + ], + }, + ], + }, + }, ], }; diff --git a/src/electron/preload.ts b/src/electron/preload.ts index b3561efb69..4fb0640f7b 100644 --- a/src/electron/preload.ts +++ b/src/electron/preload.ts @@ -5,7 +5,12 @@ import { IpcRendererEvent, } from "electron"; -import { Sandbox, ElectronStoreType, EngineId } from "@/type/preload"; +import { + Sandbox, + ElectronStoreType, + EngineId, + SandboxKey, +} from "@/type/preload"; import { IpcIHData, IpcSOData } from "@/type/ipc"; function ipcRendererInvoke( @@ -286,4 +291,4 @@ const api: Sandbox = { }, }; -contextBridge.exposeInMainWorld("electron", api); +contextBridge.exposeInMainWorld(SandboxKey, api); diff --git a/src/type/globals.d.ts b/src/type/globals.d.ts index 300dba2983..b165e46bf3 100644 --- a/src/type/globals.d.ts +++ b/src/type/globals.d.ts @@ -1,8 +1,13 @@ // Include global variables to build immer source code export * from "immer/src/types/globals"; +import { SandboxKey } from "./preload"; declare global { interface HTMLAudioElement { setSinkId(deviceID: string): Promise; // setSinkIdを認識してくれないため } + + interface Window { + readonly [SandboxKey]: import("./preload").Sandbox; + } } diff --git a/src/type/ipc.ts b/src/type/ipc.ts index f33120f566..df7b78b8ce 100644 --- a/src/type/ipc.ts +++ b/src/type/ipc.ts @@ -11,6 +11,7 @@ import { NativeThemeType, EngineSetting, EngineId, + MessageBoxReturnValue, } from "@/type/preload"; /** @@ -110,7 +111,7 @@ export type IpcIHData = { message: string; } ]; - return: Electron.MessageBoxReturnValue; + return: MessageBoxReturnValue; }; SHOW_QUESTION_DIALOG: { @@ -134,7 +135,7 @@ export type IpcIHData = { message: string; } ]; - return: Electron.MessageBoxReturnValue; + return: MessageBoxReturnValue; }; SHOW_ERROR_DIALOG: { @@ -144,7 +145,7 @@ export type IpcIHData = { message: string; } ]; - return: Electron.MessageBoxReturnValue; + return: MessageBoxReturnValue; }; OPEN_TEXT_EDIT_CONTEXT_MENU: { diff --git a/src/type/preload.ts b/src/type/preload.ts index 71ff3df43f..f717464a3d 100644 --- a/src/type/preload.ts +++ b/src/type/preload.ts @@ -1,4 +1,3 @@ -import { IpcRenderer, IpcRendererEvent, nativeTheme } from "electron"; import { z } from "zod"; import { IpcSOData } from "./ipc"; @@ -187,8 +186,8 @@ export interface Sandbox { isMaximizedWindow(): Promise; onReceivedIPCMsg( channel: T, - listener: (event: IpcRendererEvent, ...args: IpcSOData[T]["args"]) => void - ): IpcRenderer; + listener: (event: unknown, ...args: IpcSOData[T]["args"]) => void + ): void; closeWindow(): void; minimizeWindow(): void; maximizeWindow(): void; @@ -436,7 +435,8 @@ export type ToolbarButtonTagType = z.infer; export const toolbarSettingSchema = toolbarButtonTagSchema; export type ToolbarSetting = z.infer[]; -export type NativeThemeType = typeof nativeTheme["themeSource"]; +// base: typeof electron.nativeTheme["themeSource"]; +export type NativeThemeType = "system" | "light" | "dark"; export type MoraDataType = | "consonant" @@ -619,3 +619,12 @@ export type EngineDirValidationResult = | "alreadyExists"; export type VvppFilePathValidationResult = "ok" | "fileNotFound"; + +// base: Electron.MessageBoxReturnValue +// FIXME: MessageBoxUIの戻り値として使用したい値が決まったら書き換える +export interface MessageBoxReturnValue { + response: number; + checkboxChecked: boolean; +} + +export const SandboxKey = "electron" as const; diff --git a/src/type/shims-vue.d.ts b/src/type/shims-vue.d.ts index 6fb687a6d6..3144772c0d 100644 --- a/src/type/shims-vue.d.ts +++ b/src/type/shims-vue.d.ts @@ -5,7 +5,3 @@ declare module "*.vue" { export default component; } - -interface Window { - readonly electron: import("./preload").Sandbox; -}