Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Electronのmain processで直接動作するコード以外からelectronに関する型を排除する #1278

Merged
merged 8 commits into from
Apr 5, 2023
23 changes: 23 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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の設定を見直してください",
},
],
},
],
},
},
],
};
9 changes: 7 additions & 2 deletions src/electron/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends keyof IpcIHData>(
Expand Down Expand Up @@ -286,4 +291,4 @@ const api: Sandbox = {
},
};

contextBridge.exposeInMainWorld("electron", api);
contextBridge.exposeInMainWorld(SandboxKey, api);
5 changes: 5 additions & 0 deletions src/type/globals.d.ts
Original file line number Diff line number Diff line change
@@ -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<undefined>; // setSinkIdを認識してくれないため
}

interface Window {
readonly [SandboxKey]: import("./preload").Sandbox;
}
}
7 changes: 4 additions & 3 deletions src/type/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
NativeThemeType,
EngineSetting,
EngineId,
MessageBoxReturnValue,
} from "@/type/preload";

/**
Expand Down Expand Up @@ -110,7 +111,7 @@ export type IpcIHData = {
message: string;
}
];
return: Electron.MessageBoxReturnValue;
return: MessageBoxReturnValue;
};

SHOW_QUESTION_DIALOG: {
Expand All @@ -134,7 +135,7 @@ export type IpcIHData = {
message: string;
}
];
return: Electron.MessageBoxReturnValue;
return: MessageBoxReturnValue;
};

SHOW_ERROR_DIALOG: {
Expand All @@ -144,7 +145,7 @@ export type IpcIHData = {
message: string;
}
];
return: Electron.MessageBoxReturnValue;
return: MessageBoxReturnValue;
};

OPEN_TEXT_EDIT_CONTEXT_MENU: {
Expand Down
17 changes: 13 additions & 4 deletions src/type/preload.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { IpcRenderer, IpcRendererEvent, nativeTheme } from "electron";
import { z } from "zod";
import { IpcSOData } from "./ipc";

Expand Down Expand Up @@ -187,8 +186,8 @@ export interface Sandbox {
isMaximizedWindow(): Promise<boolean>;
onReceivedIPCMsg<T extends keyof IpcSOData>(
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;
Expand Down Expand Up @@ -436,7 +435,8 @@ export type ToolbarButtonTagType = z.infer<typeof toolbarButtonTagSchema>;
export const toolbarSettingSchema = toolbarButtonTagSchema;
export type ToolbarSetting = z.infer<typeof toolbarSettingSchema>[];

export type NativeThemeType = typeof nativeTheme["themeSource"];
// base: typeof electron.nativeTheme["themeSource"];
export type NativeThemeType = "system" | "light" | "dark";

export type MoraDataType =
| "consonant"
Expand Down Expand Up @@ -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;
4 changes: 0 additions & 4 deletions src/type/shims-vue.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,3 @@ declare module "*.vue" {

export default component;
}

interface Window {
readonly electron: import("./preload").Sandbox;
}