From 5f2b79fa2bf962dd2a3c0d4a088ac28832e30c32 Mon Sep 17 00:00:00 2001 From: sabonerune <102559104+sabonerune@users.noreply.github.com> Date: Sat, 19 Oct 2024 21:35:06 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=E3=83=86=E3=82=AD=E3=82=B9?= =?UTF-8?q?=E3=83=88=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB=E3=81=AE=E5=8F=97?= =?UTF-8?q?=E3=81=91=E6=B8=A1=E3=81=97=E3=81=AE=E5=87=A6=E7=90=86=E3=81=AE?= =?UTF-8?q?=E3=83=AA=E3=83=95=E3=82=A1=E3=82=AF=E3=82=BF=E3=83=AA=E3=83=B3?= =?UTF-8?q?=E3=82=B0=20(#2298)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Hiroshiba --- src/backend/browser/sandbox.ts | 41 +++----------- src/backend/electron/main.ts | 99 +++------------------------------ src/backend/electron/preload.ts | 45 ++++----------- src/store/index.ts | 16 +++--- src/type/ipc.ts | 53 +++--------------- src/type/preload.ts | 20 ++++--- src/type/staticResources.ts | 20 ++++--- 7 files changed, 70 insertions(+), 224 deletions(-) diff --git a/src/backend/browser/sandbox.ts b/src/backend/browser/sandbox.ts index ba8e8b2eaa..ef340f9e84 100644 --- a/src/backend/browser/sandbox.ts +++ b/src/backend/browser/sandbox.ts @@ -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}`; @@ -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: ブラウザ版ではサポートされていません diff --git a/src/backend/electron/main.ts b/src/backend/electron/main.ts index fc5109f55c..6a606bf72d 100644 --- a/src/backend/electron/main.ts +++ b/src/backend/electron/main.ts @@ -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, @@ -47,7 +38,7 @@ import { isMac, defaultToolbarButtonSetting, EngineId, - UpdateInfo, + TextAsset, } from "@/type/preload"; type SingleInstanceLockData = { @@ -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[]; - -// 問い合わせの読み込み -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, }; @@ -543,36 +485,13 @@ registerIpcMainHandle({ }; }, - 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: () => { diff --git a/src/backend/electron/preload.ts b/src/backend/electron/preload.ts index 2fdd5af720..9951e8c6a2 100644 --- a/src/backend/electron/preload.ts +++ b/src/backend/electron/preload.ts @@ -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( {}, @@ -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 () => { diff --git a/src/store/index.ts b/src/store/index.ts index 16d9f14834..9cf5ba44c8 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -153,49 +153,49 @@ export const indexStore = createPartialStore({ 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"); }, }, diff --git a/src/type/ipc.ts b/src/type/ipc.ts index 63cd0eb514..425e9efdcf 100644 --- a/src/type/ipc.ts +++ b/src/type/ipc.ts @@ -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"; @@ -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[]; - }; - - 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: { diff --git a/src/type/preload.ts b/src/type/preload.ts index 8dc1d7cfad..08a4dcac69 100644 --- a/src/type/preload.ts +++ b/src/type/preload.ts @@ -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[]; + UpdateInfos: UpdateInfo[]; +}; + export interface Sandbox { getAppInfos(): Promise; - getHowToUseText(): Promise; - getPolicyText(): Promise; - getOssLicenses(): Promise[]>; - getUpdateInfos(): Promise; - getOssCommunityInfos(): Promise; - getQAndAText(): Promise; - getContactText(): Promise; - getPrivacyPolicyText(): Promise; + getTextAsset(textType: K): Promise; getAltPortInfos(): Promise; showAudioSaveDialog(obj: { title: string; diff --git a/src/type/staticResources.ts b/src/type/staticResources.ts index b8da5d927b..cab94d5e75 100644 --- a/src/type/staticResources.ts +++ b/src/type/staticResources.ts @@ -1,8 +1,12 @@ -export const HowToUseTextFileName = "howtouse.md"; -export const OssCommunityInfosFileName = "ossCommunityInfos.md"; -export const PolicyTextFileName = "policy.md"; -export const OssLicensesJsonFileName = "licenses.json"; -export const ContactTextFileName = "contact.md"; -export const QAndATextFileName = "qAndA.md"; -export const UpdateInfosJsonFileName = "updateInfos.json"; -export const PrivacyPolicyTextFileName = "privacyPolicy.md"; +import { TextAsset } from "./preload"; + +export const AssetTextFileNames: Record = { + Contact: "contact.md", + HowToUse: "howtouse.md", + OssCommunityInfos: "ossCommunityInfos.md", + OssLicenses: "licenses.json", + Policy: "policy.md", + PrivacyPolicy: "privacyPolicy.md", + QAndA: "qAndA.md", + UpdateInfos: "updateInfos.json", +};