generated from vivid-lapin/ts
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
38 changed files
with
1,548 additions
and
512 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import fs from "fs" | ||
|
||
const main = async () => { | ||
const dts = await fs.promises.readFile("./dist/plugin.d.ts", "utf8") | ||
const license = await fs.promises.readFile(`./LICENSE`, "utf8") | ||
const licenses = ["MirakTest: " + license] | ||
const libs = ["electron"] | ||
for (const lib of libs) { | ||
const license = await fs.promises.readFile( | ||
`./node_modules/${lib}/LICENSE`, | ||
"utf8" | ||
) | ||
licenses.push(`${lib}: ${license}`) | ||
} | ||
const rewrited = `/** plugin.d.ts - Type definitions for creating plug-ins for MirakTest.\n---\n${licenses.join( | ||
"\n---\n" | ||
)}\n*/\n${dts}` | ||
await fs.promises.writeFile("./dist/plugin.d.ts", rewrited) | ||
} | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { RecoilState } from "recoil" | ||
import { PluginDefineInRenderer } from "../types/plugin" | ||
|
||
export declare global { | ||
interface Window { | ||
atoms?: RecoilState<unknown>[] | ||
plugins?: PluginDefineInRenderer[] | ||
contextMenus?: { [key: string]: Electron.MenuItemConstructorOptions } | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
import Axios from "axios" | ||
import { ipcRenderer, remote } from "electron" | ||
import React, { useEffect, useState } from "react" | ||
import Recoil, { RecoilState } from "recoil" | ||
import pkg from "../package.json" | ||
import { StateRoot } from "./State" | ||
import { Splash } from "./components/global/Splash" | ||
import { REUQEST_OPEN_WINDOW } from "./constants/ipc" | ||
import { | ||
RECOIL_SHARED_ATOM_KEYS, | ||
RECOIL_STORED_ATOM_KEYS, | ||
} from "./constants/recoil" | ||
import { OpenWindowArg } from "./types/ipc" | ||
import { | ||
InitPlugin, | ||
PluginDefineInRenderer, | ||
PluginInRendererArgs, | ||
} from "./types/plugin" | ||
import { ObjectLiteral } from "./types/struct" | ||
import { nativeImport } from "./utils/nativeImport" | ||
import { pluginValidator } from "./utils/plugin" | ||
|
||
export const PluginLoader: React.VFC<{ | ||
states: ObjectLiteral | ||
pluginPaths: string[] | ||
}> = ({ states, pluginPaths }) => { | ||
const [isLoading, setIsLoading] = useState(true) | ||
const [sharedAtoms, setSharedAtoms] = useState(RECOIL_SHARED_ATOM_KEYS) | ||
const [storedAtoms, setStoredAtoms] = useState(RECOIL_STORED_ATOM_KEYS) | ||
useEffect(() => { | ||
window.plugins = window.atoms = [] | ||
window.contextMenus = {} | ||
const contextMenus: { [key: string]: Electron.MenuItemConstructorOptions } = | ||
{} | ||
const openWindow = async (args: OpenWindowArg) => { | ||
return await ipcRenderer.invoke(REUQEST_OPEN_WINDOW, args) | ||
} | ||
const args: PluginInRendererArgs = { | ||
packages: { | ||
React, | ||
Recoil, | ||
Axios, | ||
Electron: remote, | ||
IpcRenderer: ipcRenderer, | ||
}, | ||
appInfo: { version: pkg.version }, | ||
functions: { | ||
openWindow, | ||
}, | ||
} | ||
;(async () => { | ||
const atoms: RecoilState<unknown>[] = [] | ||
const plugins: PluginDefineInRenderer[] = [] | ||
console.info("pluginPaths:", pluginPaths) | ||
const openedPlugins: PluginDefineInRenderer[] = [] | ||
for (const filePath of pluginPaths) { | ||
try { | ||
console.info("[Plugin] 取り込み中:", filePath) | ||
const module: { default: InitPlugin } | InitPlugin = | ||
await nativeImport(filePath) | ||
const plugin = | ||
"default" in module | ||
? module.default.renderer(args) | ||
: module.renderer(args) | ||
pluginValidator.parse(plugin) | ||
console.info( | ||
`[Plugin] 読込中: ${plugin.name} (${plugin.id}, ${plugin.version})` | ||
) | ||
if ( | ||
![ | ||
...plugin.storedAtoms, | ||
...plugin.sharedAtoms, | ||
...plugin.exposedAtoms, | ||
].every((atom) => atom.key.startsWith("plugins.")) | ||
) { | ||
throw new Error( | ||
`すべての露出した atom のキーは \`plugins.\` から開始する必要があります: ${plugin.id}` | ||
) | ||
} | ||
openedPlugins.push(plugin) | ||
} catch (error) { | ||
console.error("[Plugin] 読み込みエラー:", error) | ||
} | ||
} | ||
for (const plugin of openedPlugins) { | ||
try { | ||
await plugin.setup({ plugins: openedPlugins }) | ||
if (plugin.contextMenu) { | ||
contextMenus[plugin.id] = plugin.contextMenu | ||
} | ||
plugin.sharedAtoms.forEach((atom) => { | ||
const mached = atoms.find((_atom) => _atom.key === atom.key) | ||
if (!mached) { | ||
atoms.push(atom) | ||
} | ||
setSharedAtoms((atoms) => [...atoms, atom.key]) | ||
}) | ||
plugin.storedAtoms.forEach((atom) => { | ||
const mached = atoms.find((_atom) => _atom.key === atom.key) | ||
if (!mached) { | ||
atoms.push(atom) | ||
} | ||
setStoredAtoms((atoms) => [...atoms, atom.key]) | ||
}) | ||
plugin.exposedAtoms.forEach((atom) => { | ||
const mached = atoms.find((_atom) => _atom.key === atom.key) | ||
if (!mached) { | ||
atoms.push(atom) | ||
} | ||
}) | ||
plugins.push(plugin) | ||
} catch (error) { | ||
console.error( | ||
"[Plugin] setup 中にエラーが発生しました:", | ||
plugin.id, | ||
error | ||
) | ||
try { | ||
await plugin.destroy() | ||
} catch (error) { | ||
console.error( | ||
"[Plugin] destroy 中にエラーが発生しました:", | ||
plugin.id, | ||
error | ||
) | ||
} | ||
} | ||
} | ||
window.plugins = plugins | ||
window.atoms = atoms | ||
window.contextMenus = contextMenus | ||
setIsLoading(false) | ||
})() | ||
}, []) | ||
if (isLoading) { | ||
return <Splash /> | ||
} | ||
return ( | ||
<StateRoot | ||
states={states} | ||
sharedAtoms={sharedAtoms} | ||
storedAtoms={storedAtoms} | ||
/> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import React, { useEffect, useState } from "react" | ||
import { useRecoilBridgeAcrossReactRoots_UNSTABLE } from "recoil" | ||
import { ComponentShadowWrapper } from "./components/common/ComponentShadowWrapper" | ||
import { Splash } from "./components/global/Splash" | ||
import { Routes } from "./types/struct" | ||
import { CoiledContentPlayer } from "./windows/ContentPlayer" | ||
import { Settings } from "./windows/Settings" | ||
|
||
export const Router: React.VFC<{}> = () => { | ||
const RecoilBridge = useRecoilBridgeAcrossReactRoots_UNSTABLE() | ||
const [hash, setHash] = useState<Routes>("") | ||
useEffect(() => { | ||
const onHashChange = () => { | ||
const hash = window.location.hash.replace("#", "") | ||
setHash(hash) | ||
} | ||
window.addEventListener("hashchange", onHashChange) | ||
onHashChange() | ||
return () => window.removeEventListener("hashchange", onHashChange) | ||
}, []) | ||
if (hash === "ContentPlayer") { | ||
return <CoiledContentPlayer /> | ||
} else if (hash === "Settings") { | ||
return <Settings /> | ||
} else if (hash === "ProgramTable") { | ||
return <Settings /> | ||
} else { | ||
const Component = window.plugins?.find((plugin) => plugin.windows?.[hash]) | ||
?.windows[hash] | ||
if (Component) { | ||
return ( | ||
<ComponentShadowWrapper | ||
_id={hash} | ||
Component={() => ( | ||
<RecoilBridge> | ||
<Component /> | ||
</RecoilBridge> | ||
)} | ||
/> | ||
) | ||
} | ||
return <Splash>Error: 想定していない表示です({hash})</Splash> | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import React from "react" | ||
import { RecoilRoot } from "recoil" | ||
import { Router } from "./Router" | ||
import { RecoilApplier } from "./components/global/RecoilApplier" | ||
import { RecoilObserver } from "./components/global/RecoilObserver" | ||
import { ObjectLiteral } from "./types/struct" | ||
import { initializeState } from "./utils/recoil" | ||
|
||
export const StateRoot: React.VFC<{ | ||
states: ObjectLiteral | ||
sharedAtoms: string[] | ||
storedAtoms: string[] | ||
}> = ({ states, sharedAtoms, storedAtoms }) => { | ||
return ( | ||
<RecoilRoot | ||
initializeState={initializeState({ states, sharedAtoms, storedAtoms })} | ||
> | ||
<RecoilObserver /> | ||
<RecoilApplier /> | ||
<Router /> | ||
</RecoilRoot> | ||
) | ||
} |
Oops, something went wrong.