Skip to content

Commit

Permalink
Electronのmain processで直接動作するコード以外からelectronに関する型を排除する (#1278)
Browse files Browse the repository at this point in the history
Co-authored-by: Hiroshiba <[email protected]>
  • Loading branch information
yamachu and Hiroshiba authored Apr 5, 2023
1 parent 1c22078 commit 876d519
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 13 deletions.
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;
}

0 comments on commit 876d519

Please sign in to comment.