Skip to content

Commit

Permalink
refactor: テキストファイルの受け渡しの処理のリファクタリング (#2298)
Browse files Browse the repository at this point in the history
Co-authored-by: Hiroshiba <[email protected]>
  • Loading branch information
sabonerune and Hiroshiba authored Oct 19, 2024
1 parent cb6c597 commit 5f2b79f
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 224 deletions.
41 changes: 8 additions & 33 deletions src/backend/browser/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,7 @@ import {
Sandbox,
ThemeConf,
} from "@/type/preload";
import {
ContactTextFileName,
HowToUseTextFileName,
OssCommunityInfosFileName,
OssLicensesJsonFileName,
PolicyTextFileName,
PrivacyPolicyTextFileName,
QAndATextFileName,
UpdateInfosJsonFileName,
} from "@/type/staticResources";
import { AssetTextFileNames } from "@/type/staticResources";

// TODO: base pathを設定できるようにするか、ビルド時埋め込みにする
const toStaticPath = (fileName: string) => `/${fileName}`;
Expand Down Expand Up @@ -72,29 +63,13 @@ export const api: Sandbox = {
};
return Promise.resolve(appInfo);
},
getHowToUseText() {
return fetch(toStaticPath(HowToUseTextFileName)).then((v) => v.text());
},
getPolicyText() {
return fetch(toStaticPath(PolicyTextFileName)).then((v) => v.text());
},
getOssLicenses() {
return fetch(toStaticPath(OssLicensesJsonFileName)).then((v) => v.json());
},
getUpdateInfos() {
return fetch(toStaticPath(UpdateInfosJsonFileName)).then((v) => v.json());
},
getOssCommunityInfos() {
return fetch(toStaticPath(OssCommunityInfosFileName)).then((v) => v.text());
},
getQAndAText() {
return fetch(toStaticPath(QAndATextFileName)).then((v) => v.text());
},
getContactText() {
return fetch(toStaticPath(ContactTextFileName)).then((v) => v.text());
},
getPrivacyPolicyText() {
return fetch(toStaticPath(PrivacyPolicyTextFileName)).then((v) => v.text());
async getTextAsset(textType) {
const fileName = AssetTextFileNames[textType];
const v = await fetch(toStaticPath(fileName));
if (textType === "OssLicenses" || textType === "UpdateInfos") {
return v.json();
}
return v.text();
},
getAltPortInfos() {
// NOTE: ブラウザ版ではサポートされていません
Expand Down
99 changes: 9 additions & 90 deletions src/backend/electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,7 @@ import { registerIpcMainHandle, ipcMainSendProxy, IpcMainHandle } from "./ipc";
import { getConfigManager } from "./electronConfig";
import { EngineAndVvppController } from "./engineAndVvppController";
import { failure, success } from "@/type/result";
import {
ContactTextFileName,
HowToUseTextFileName,
OssCommunityInfosFileName,
OssLicensesJsonFileName,
PolicyTextFileName,
PrivacyPolicyTextFileName,
QAndATextFileName,
UpdateInfosJsonFileName,
} from "@/type/staticResources";
import { AssetTextFileNames } from "@/type/staticResources";
import {
ThemeConf,
EngineInfo,
Expand All @@ -47,7 +38,7 @@ import {
isMac,
defaultToolbarButtonSetting,
EngineId,
UpdateInfo,
TextAsset,
} from "@/type/preload";

type SingleInstanceLockData = {
Expand Down Expand Up @@ -249,55 +240,6 @@ function readThemeFiles() {
return themes;
}

// 使い方テキストの読み込み
const howToUseText = fs.readFileSync(
path.join(__static, HowToUseTextFileName),
"utf-8",
);

// OSSコミュニティ情報の読み込み
const ossCommunityInfos = fs.readFileSync(
path.join(__static, OssCommunityInfosFileName),
"utf-8",
);

// 利用規約テキストの読み込み
const policyText = fs.readFileSync(
path.join(__static, PolicyTextFileName),
"utf-8",
);

// OSSライセンス情報の読み込み
const ossLicenses = JSON.parse(
fs.readFileSync(path.join(__static, OssLicensesJsonFileName), {
encoding: "utf-8",
}),
) as Record<string, string>[];

// 問い合わせの読み込み
const contactText = fs.readFileSync(
path.join(__static, ContactTextFileName),
"utf-8",
);

// Q&Aの読み込み
const qAndAText = fs.readFileSync(
path.join(__static, QAndATextFileName),
"utf-8",
);

// アップデート情報の読み込み
const updateInfos = JSON.parse(
fs.readFileSync(path.join(__static, UpdateInfosJsonFileName), {
encoding: "utf-8",
}),
) as UpdateInfo[];

const privacyPolicyText = fs.readFileSync(
path.join(__static, PrivacyPolicyTextFileName),
"utf-8",
);

const appState = {
willQuit: false,
};
Expand Down Expand Up @@ -543,36 +485,13 @@ registerIpcMainHandle<IpcMainHandle>({
};
},

GET_HOW_TO_USE_TEXT: () => {
return howToUseText;
},

GET_POLICY_TEXT: () => {
return policyText;
},

GET_OSS_LICENSES: () => {
return ossLicenses;
},

GET_UPDATE_INFOS: () => {
return updateInfos;
},

GET_OSS_COMMUNITY_INFOS: () => {
return ossCommunityInfos;
},

GET_CONTACT_TEXT: () => {
return contactText;
},

GET_Q_AND_A_TEXT: () => {
return qAndAText;
},

GET_PRIVACY_POLICY_TEXT: () => {
return privacyPolicyText;
GET_TEXT_ASSET: async (_, textType) => {
const fileName = path.join(__static, AssetTextFileNames[textType]);
const text = await fs.promises.readFile(fileName, "utf-8");
if (textType === "OssLicenses" || textType === "UpdateInfos") {
return JSON.parse(text) as TextAsset[typeof textType];
}
return text;
},

GET_ALT_PORT_INFOS: () => {
Expand Down
45 changes: 12 additions & 33 deletions src/backend/electron/preload.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { contextBridge, ipcRenderer } from "electron";

import { IpcRendererInvoke } from "./ipc";
import { Sandbox, ConfigType, EngineId, SandboxKey } from "@/type/preload";
import type { IpcRendererInvoke } from "./ipc";
import {
ConfigType,
EngineId,
Sandbox,
SandboxKey,
TextAsset,
} from "@/type/preload";

const ipcRendererInvokeProxy = new Proxy(
{},
Expand All @@ -18,36 +23,10 @@ const api: Sandbox = {
return await ipcRendererInvokeProxy.GET_APP_INFOS();
},

getHowToUseText: async () => {
return await ipcRendererInvokeProxy.GET_HOW_TO_USE_TEXT();
},

getPolicyText: async () => {
return await ipcRendererInvokeProxy.GET_POLICY_TEXT();
},

getOssLicenses: async () => {
return await ipcRendererInvokeProxy.GET_OSS_LICENSES();
},

getUpdateInfos: async () => {
return await ipcRendererInvokeProxy.GET_UPDATE_INFOS();
},

getContactText: async () => {
return await ipcRendererInvokeProxy.GET_CONTACT_TEXT();
},

getQAndAText: async () => {
return await ipcRendererInvokeProxy.GET_Q_AND_A_TEXT();
},

getOssCommunityInfos: async () => {
return await ipcRendererInvokeProxy.GET_OSS_COMMUNITY_INFOS();
},

getPrivacyPolicyText: async () => {
return await ipcRendererInvokeProxy.GET_PRIVACY_POLICY_TEXT();
getTextAsset: (textType) => {
return ipcRendererInvokeProxy.GET_TEXT_ASSET(textType) as Promise<
TextAsset[typeof textType]
>;
},

getAltPortInfos: async () => {
Expand Down
16 changes: 8 additions & 8 deletions src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,49 +153,49 @@ export const indexStore = createPartialStore<IndexStoreTypes>({

GET_HOW_TO_USE_TEXT: {
async action() {
return await window.backend.getHowToUseText();
return await window.backend.getTextAsset("HowToUse");
},
},

GET_CONTACT_TEXT: {
async action() {
return await window.backend.getContactText();
return await window.backend.getTextAsset("Contact");
},
},

GET_Q_AND_A_TEXT: {
async action() {
return await window.backend.getQAndAText();
return await window.backend.getTextAsset("QAndA");
},
},

GET_POLICY_TEXT: {
async action() {
return await window.backend.getPolicyText();
return await window.backend.getTextAsset("PrivacyPolicy");
},
},

GET_OSS_LICENSES: {
async action() {
return await window.backend.getOssLicenses();
return await window.backend.getTextAsset("OssLicenses");
},
},

GET_UPDATE_INFOS: {
async action() {
return await window.backend.getUpdateInfos();
return await window.backend.getTextAsset("UpdateInfos");
},
},

GET_OSS_COMMUNITY_INFOS: {
async action() {
return await window.backend.getOssCommunityInfos();
return await window.backend.getTextAsset("OssCommunityInfos");
},
},

GET_PRIVACY_POLICY_TEXT: {
async action() {
return await window.backend.getPrivacyPolicyText();
return await window.backend.getTextAsset("PrivacyPolicy");
},
},

Expand Down
53 changes: 9 additions & 44 deletions src/type/ipc.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import {
AppInfos,
ConfigType,
EngineInfo,
EngineDirValidationResult,
EngineId,
EngineInfo,
EngineSettingType,
HotkeySettingType,
MessageBoxReturnValue,
NativeThemeType,
TextAsset,
ThemeConf,
ToolbarSettingType,
UpdateInfo,
NativeThemeType,
EngineSettingType,
EngineId,
MessageBoxReturnValue,
} from "@/type/preload";
import { AltPortInfos } from "@/store/type";
import { Result } from "@/type/result";
Expand All @@ -24,44 +24,9 @@ export type IpcIHData = {
return: AppInfos;
};

GET_HOW_TO_USE_TEXT: {
args: [];
return: string;
};

GET_POLICY_TEXT: {
args: [];
return: string;
};

GET_OSS_LICENSES: {
args: [];
return: Record<string, string>[];
};

GET_UPDATE_INFOS: {
args: [];
return: UpdateInfo[];
};

GET_OSS_COMMUNITY_INFOS: {
args: [];
return: string;
};

GET_CONTACT_TEXT: {
args: [];
return: string;
};

GET_Q_AND_A_TEXT: {
args: [];
return: string;
};

GET_PRIVACY_POLICY_TEXT: {
args: [];
return: string;
GET_TEXT_ASSET: {
args: [textType: keyof TextAsset];
return: TextAsset[keyof TextAsset];
};

GET_ALT_PORT_INFOS: {
Expand Down
20 changes: 12 additions & 8 deletions src/type/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,16 +209,20 @@ export const defaultToolbarButtonSetting: ToolbarSettingType = [
"REDO",
];

export type TextAsset = {
Contact: string;
HowToUse: string;
OssCommunityInfos: string;
Policy: string;
PrivacyPolicy: string;
QAndA: string;
OssLicenses: Record<string, string>[];
UpdateInfos: UpdateInfo[];
};

export interface Sandbox {
getAppInfos(): Promise<AppInfos>;
getHowToUseText(): Promise<string>;
getPolicyText(): Promise<string>;
getOssLicenses(): Promise<Record<string, string>[]>;
getUpdateInfos(): Promise<UpdateInfo[]>;
getOssCommunityInfos(): Promise<string>;
getQAndAText(): Promise<string>;
getContactText(): Promise<string>;
getPrivacyPolicyText(): Promise<string>;
getTextAsset<K extends keyof TextAsset>(textType: K): Promise<TextAsset[K]>;
getAltPortInfos(): Promise<AltPortInfos>;
showAudioSaveDialog(obj: {
title: string;
Expand Down
Loading

0 comments on commit 5f2b79f

Please sign in to comment.