-
-
Notifications
You must be signed in to change notification settings - Fork 37
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
1 parent
97b27da
commit bf04a0e
Showing
11 changed files
with
224 additions
and
237 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
74 changes: 74 additions & 0 deletions
74
Website/src/activitys/ModConfActivity/components/ModConfView/index.tsx
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,74 @@ | ||
import { useLog } from "@Hooks/native/useLog"; | ||
import { ModFS, useModFS } from "@Hooks/useModFS"; | ||
import { IsolatedEval, IsolatedEvalOptions } from "@Native/IsolatedEval"; | ||
import { os } from "@Native/Os"; | ||
import * as React from "react"; | ||
import { libraries } from "./libs"; | ||
|
||
interface ModConfViewProps { | ||
children: string; | ||
modid: string; | ||
indexFile: string; | ||
cwd: string; | ||
standaloneFile?: string; | ||
} | ||
|
||
export const ModConfView = (props: ModConfViewProps) => { | ||
const { modFS } = useModFS(); | ||
|
||
const { modid, children } = props; | ||
const log = useLog(`Config-${modid}`); | ||
const format = React.useCallback<<K extends keyof ModFS>(key: K) => ModFS[K]>((key) => modFS(key, { MODID: modid }), []); | ||
|
||
const internalFetch = React.useCallback( | ||
(input: string | URL | Request, init?: RequestInit | undefined) => { | ||
return fetch(input, init); | ||
}, | ||
[modid] | ||
); | ||
|
||
const isoEval = React.useMemo(() => { | ||
const options: IsolatedEvalOptions = { | ||
libraries: libraries, | ||
indexFile: props.indexFile, | ||
cwd: props.cwd, | ||
scope: { | ||
log: log, | ||
__idname: modid, | ||
__filename: props.indexFile, | ||
__dirname: props.cwd, | ||
__modpath: format("MODULECWD"), | ||
window: { | ||
fetch: internalFetch, | ||
open: os.openURL, | ||
}, | ||
fetch: internalFetch, | ||
|
||
// @deprecated | ||
modid: modid, | ||
modpath: (path: string) => `${format("MODULECWD")}/${path}`, | ||
confpath: (path: string) => `${format("CONFCWD")}/${path}`, | ||
include: (modulePath: string, opt: { isolate: boolean } = { isolate: false }) => { | ||
if (opt.isolate) { | ||
modulePath = `${format("CONFCWD")}/${modulePath}`; | ||
} | ||
|
||
return isoEval.require(modulePath); | ||
}, | ||
}, | ||
standaloneFile: props.standaloneFile, | ||
}; | ||
|
||
return new IsolatedEval<React.FunctionComponent<any>>(options); | ||
}, [children, modid]); | ||
|
||
const box = React.useCallback((code: string) => isoEval.compileTransform(code), []); | ||
|
||
const Component = box(children as string); | ||
|
||
if (Component.exports.default) { | ||
return <Component.exports.default />; | ||
} | ||
|
||
return <></>; | ||
}; |
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,40 @@ | ||
import { useActivity } from "@Hooks/useActivity"; | ||
import React from "react"; | ||
import { SuFile } from "@Native/SuFile"; | ||
import { ModConfView } from "@Activitys/ModConfActivity/components/ModConfView"; | ||
import { PreviewErrorBoundary } from "../ModConfPlaygroundActivity"; | ||
|
||
interface ModConfActivityExtra { | ||
/** | ||
* ## This field is required by ModConf | ||
*/ | ||
indexFile: string; | ||
/** | ||
* ## This field is required by ModConf | ||
*/ | ||
cwd: string; | ||
modId: string; | ||
} | ||
|
||
const ModConfActivity = () => { | ||
const { extra } = useActivity<ModConfActivityExtra>(); | ||
|
||
const config: string = React.useMemo(() => { | ||
const notFound = 'import {Page} from "@mmrl/ui";export default () => <Page>Config file not found</Page>'; | ||
|
||
const file = new SuFile(extra.indexFile); | ||
if (file.exist()) { | ||
return file.read(); | ||
} else { | ||
return notFound; | ||
} | ||
}, []); | ||
|
||
return ( | ||
<PreviewErrorBoundary> | ||
<ModConfView cwd={extra.cwd} indexFile={extra.indexFile} modid={extra.modId} children={config} /> | ||
</PreviewErrorBoundary> | ||
); | ||
}; | ||
|
||
export { ModConfActivity, ModConfActivityExtra }; |
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 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
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
Oops, something went wrong.