10 Commits

23 changed files with 1067 additions and 1132 deletions

View File

@@ -24,6 +24,7 @@ This project is a front-end web UI of Oxygen Toolbox and needs to be used with t
# Requires
- Web Server (e.g. Nginx, Apache httpd)
- [API of Oxygen Toolbox](https://github.com/FatttSnake/oxygen-api) (v1.0.0 and later versions)
# Related projects

View File

@@ -24,6 +24,7 @@
# 环境要求
- Web 服务器(如 Nginx, Apache httpd
- [API of Oxygen Toolbox](https://github.com/FatttSnake/oxygen-api) (v1.0.0 及更高版本)
# 关联项目

View File

@@ -38,6 +38,7 @@
opacity: 0.6;
box-shadow: 2px 2px 10px 0 rgba(0,0,0,0.2);
cursor: pointer;
z-index: 1000;
}
}
}

View File

@@ -38,6 +38,7 @@
opacity: 0.6;
box-shadow: 2px 2px 10px 0 rgba(0,0,0,0.2);
cursor: pointer;
z-index: 1000;
}
}
}

View File

@@ -2,7 +2,7 @@ import MonacoEditor from '@monaco-editor/react'
import { Loader } from 'esbuild-wasm'
import '@/components/Playground/Output/Transform/transform.scss'
import { IFile, ITheme } from '@/components/Playground/shared'
import { cssToJs, jsonToJs } from '@/components/Playground/files'
import { cssToJsFromFile, jsonToJsFromFile } from '@/components/Playground/files'
import Compiler from '@/components/Playground/compiler'
import { MonacoEditorConfig } from '@/components/Playground/CodeEditor/Editor/monacoConfig'
@@ -39,10 +39,10 @@ const Transform = ({ file, theme }: OutputProps) => {
compile(code, 'jsx')
break
case 'css':
setCompiledCode(cssToJs(file))
setCompiledCode(cssToJsFromFile(file))
break
case 'json':
setCompiledCode(jsonToJs(file))
setCompiledCode(jsonToJsFromFile(file))
break
case 'xml':
setCompiledCode(code)

View File

@@ -1,8 +1,22 @@
import esbuild, { Loader, OnLoadArgs, Plugin, PluginBuild } from 'esbuild-wasm'
import esbuild, {
Loader,
OnLoadArgs,
OnLoadResult,
OnResolveArgs,
OnResolveResult,
Plugin,
PluginBuild
} from 'esbuild-wasm'
import localforage from 'localforage'
import axios from 'axios'
import { IFile, IFiles, IImportMap } from '@/components/Playground/shared'
import { addReactImport, cssToJs, jsonToJs } from '@/components/Playground/files'
import {
addReactImport,
cssToJs,
cssToJsFromFile,
jsonToJs,
jsonToJsFromFile
} from '@/components/Playground/files'
class Compiler {
private init = false
@@ -26,44 +40,47 @@ class Compiler {
}
}
transform = (code: string, loader: Loader) =>
new Promise<void>((resolve) => {
if (this.init) {
resolve()
return
}
const timer = setInterval(() => {
if (this.init) {
clearInterval(timer)
resolve()
private waitInit = async () => {
if (!this.init) {
await new Promise<void>((resolve) => {
const checkInit = () => {
if (this.init) {
resolve()
} else {
setTimeout(checkInit, 100)
}
}
}, 100)
}).then(() => {
return esbuild.transform(code, { loader })
})
compile = (files: IFiles, importMap: IImportMap, entryPoint: string) =>
new Promise<void>((resolve) => {
if (this.init) {
resolve()
return
}
const timer = setInterval(() => {
if (this.init) {
clearInterval(timer)
resolve()
}
}, 100)
}).then(() => {
return esbuild.build({
bundle: true,
entryPoints: [entryPoint],
format: 'esm',
metafile: true,
write: false,
plugins: [this.fileResolverPlugin(files, importMap)]
checkInit()
})
}
}
transform = async (code: string, loader: Loader) => {
await this.waitInit()
return esbuild.transform(code, { loader })
}
compile = async (files: IFiles, importMap: IImportMap, entryPoint: string) => {
await this.waitInit()
return esbuild.build({
bundle: true,
entryPoints: [entryPoint],
format: 'esm',
metafile: true,
write: false,
plugins: [this.fileResolverPlugin(files, importMap)]
})
}
compileCss = async (cssCode: string, basePath: string) => {
await this.waitInit()
return esbuild.build({
bundle: true,
entryPoints: [basePath],
write: false,
plugins: [this.cssCodeResolverPlugin(cssCode, basePath)]
})
}
stop = () => {
void esbuild.stop()
@@ -72,7 +89,7 @@ class Compiler {
private fileResolverPlugin = (files: IFiles, importMap: IImportMap): Plugin => ({
name: 'file-resolver-plugin',
setup: (build: PluginBuild) => {
build.onResolve({ filter: /.*/ }, (args: esbuild.OnResolveArgs) => {
build.onResolve({ filter: /.*/ }, (args: OnResolveArgs): OnResolveResult => {
if (args.kind === 'entry-point') {
return {
namespace: 'oxygen',
@@ -107,13 +124,13 @@ class Compiler {
}
}
let path = importMap.imports[args.path]
let path = importMap[args.path]
let tempPath = args.path
while (!path && tempPath.includes('/')) {
tempPath = tempPath.substring(0, tempPath.lastIndexOf('/'))
if (importMap.imports[tempPath]) {
if (importMap[tempPath]) {
const suffix = args.path.replace(tempPath, '')
const importUrl = new URL(importMap.imports[tempPath])
const importUrl = new URL(importMap[tempPath])
path = `${importUrl.origin}${importUrl.pathname}${suffix}${importUrl.search}`
}
}
@@ -122,7 +139,7 @@ class Compiler {
}
const pathUrl = new URL(path)
const externals = pathUrl.searchParams.get('external')?.split(',') ?? []
Object.keys(importMap.imports).forEach((item) => {
Object.keys(importMap).forEach((item) => {
if (!(item in externals)) {
externals.push(item)
}
@@ -134,54 +151,152 @@ class Compiler {
}
})
build.onLoad({ filter: /.*\.css$/ }, (args: OnLoadArgs) => {
const contents = cssToJs(files[args.path])
build.onLoad({ filter: /.*\.css$/ }, (args: OnLoadArgs): OnLoadResult => {
const contents = cssToJsFromFile(files[args.path])
return {
loader: 'js',
contents
}
})
build.onLoad({ filter: /.*\.json$/ }, (args: OnLoadArgs) => {
const contents = jsonToJs(files[args.path])
build.onLoad({ filter: /.*\.json$/ }, (args: OnLoadArgs): OnLoadResult => {
const contents = jsonToJsFromFile(files[args.path])
return {
loader: 'js',
contents
}
})
build.onLoad({ namespace: 'oxygen', filter: /.*/ }, (args: OnLoadArgs) => {
let file: IFile | undefined
build.onLoad(
{ namespace: 'oxygen', filter: /.*/ },
(args: OnLoadArgs): OnLoadResult | undefined => {
let file: IFile | undefined
void ['', '.tsx', '.jsx', '.ts', '.js'].forEach((suffix) => {
file = file || files[`${args.path}${suffix}`]
})
if (file) {
return {
loader: (() => {
switch (file.language) {
case 'javascript':
return 'jsx'
default:
return 'tsx'
}
})(),
contents: addReactImport(file.value)
void ['', '.tsx', '.jsx', '.ts', '.js'].forEach((suffix) => {
file = file || files[`${args.path}${suffix}`]
})
if (file) {
return {
loader: (() => {
switch (file.language) {
case 'javascript':
return 'jsx'
default:
return 'tsx'
}
})(),
contents: addReactImport(file.value)
}
}
}
})
)
build.onLoad({ filter: /.*/ }, async (args: OnLoadArgs) => {
const cached = await this.fileCache.getItem<esbuild.OnLoadResult>(args.path)
build.onLoad({ filter: /.*/ }, async (args: OnLoadArgs): Promise<OnLoadResult> => {
const cached = await this.fileCache.getItem<OnLoadResult>(args.path)
if (cached) {
return cached
}
const axiosResponse = await axios.get<string>(args.path)
const result: esbuild.OnLoadResult = {
loader: 'js',
contents: axiosResponse.data,
const axiosResponse = await axios.get<ArrayBuffer>(args.path, {
responseType: 'arraybuffer'
})
const contentType = axiosResponse.headers['content-type'] as string
const utf8Decoder = new TextDecoder('utf-8')
const result: OnLoadResult = {
loader: (() => {
if (
contentType.includes('javascript') ||
contentType.includes('css') ||
contentType.includes('json')
) {
return 'js'
}
return 'base64'
})(),
contents: await (async () => {
if (contentType.includes('css')) {
return cssToJs(
(
await this.compileCss(
utf8Decoder.decode(axiosResponse.data),
args.path
)
).outputFiles[0].text
)
}
if (contentType.includes('json')) {
return jsonToJs(utf8Decoder.decode(axiosResponse.data))
}
return new Uint8Array(axiosResponse.data)
})(),
resolveDir: args.path
}
await this.fileCache.setItem(args.path, result)
return result
})
}
})
private cssCodeResolverPlugin = (cssCode: string, basePath: string): Plugin => ({
name: 'css-code-resolver-plugin',
setup: (build: PluginBuild) => {
build.onResolve({ filter: /.*/ }, (args: OnResolveArgs): OnResolveResult => {
if (args.kind === 'entry-point') {
return {
namespace: 'default',
path: basePath
}
}
return {
namespace: 'default',
path: args.resolveDir.length
? new URL(args.path, args.resolveDir.substring(1)).href
: new URL(args.path, basePath).href
}
})
build.onLoad({ filter: /.*/ }, async (args: OnLoadArgs): Promise<OnLoadResult> => {
if (args.path === basePath) {
return {
loader: 'css',
contents: cssCode,
resolveDir: basePath
}
}
const cached = await this.fileCache.getItem<OnLoadResult>(args.path)
if (cached) {
return cached
}
const axiosResponse = await axios.get<ArrayBuffer>(args.path, {
responseType: 'arraybuffer'
})
const contentType = axiosResponse.headers['content-type'] as string
const utf8Decoder = new TextDecoder('utf-8')
const result: OnLoadResult = {
loader: (() => {
if (contentType.includes('css')) {
return 'js'
}
return 'dataurl'
})(),
contents: await (async () => {
if (contentType.includes('css')) {
return cssToJs(
(
await this.compileCss(
utf8Decoder.decode(axiosResponse.data),
args.path
)
).outputFiles[0].text
)
}
return new Uint8Array(axiosResponse.data)
})(),
resolveDir: args.path
}

View File

@@ -83,21 +83,25 @@ export const jsToBlob = (code: string) => {
return URL.createObjectURL(new Blob([code], { type: 'application/javascript' }))
}
export const jsonToJs = (file: IFile) => {
return `export default ${file.value}`
export const jsonToJs = (code: string) => {
return `export default ${code}`
}
export const cssToJs = (file: IFile) => {
export const jsonToJsFromFile = (file: IFile) => {
return jsonToJs(file.value)
}
export const cssToJs = (code: string, fileName?: string) => {
const randomId = new Date().getTime()
return `(() => {
let stylesheet = document.getElementById('style_${randomId}_${file.name}');
let stylesheet = document.getElementById('style_${randomId}${fileName ? `_${fileName}` : ''}');
if (!stylesheet) {
stylesheet = document.createElement('style')
stylesheet.setAttribute('id', 'style_${randomId}_${file.name}')
stylesheet.setAttribute('id', 'style_${randomId}_${fileName ? `_${fileName}` : ''}')
document.head.appendChild(stylesheet)
}
const styles = document.createTextNode(
\`${file.value}\`
\`${code}\`
)
stylesheet.innerHTML = ''
stylesheet.appendChild(styles)
@@ -105,6 +109,10 @@ export const cssToJs = (file: IFile) => {
`
}
export const cssToJsFromFile = (file: IFile) => {
return cssToJs(file.value, file.name)
}
export const addReactImport = (code: string) => {
if (!/^\s*import\s+React\s+/g.test(code)) {
return `import React from 'react';\n${code}`
@@ -118,18 +126,12 @@ export const tsconfigJsonDiagnosticsOptions: DiagnosticsOptions = {
{
uri: 'tsconfig.json',
fileMatch: ['tsconfig.json'],
schema: {
type: 'object',
properties: tsconfigSchema
}
schema: tsconfigSchema
},
{
uri: 'import-map.json',
fileMatch: ['import-map.json'],
schema: {
type: 'object',
properties: importMapSchema
}
schema: importMapSchema
}
]
}

View File

@@ -1,6 +1,4 @@
{
"imports": {
"type": "object",
"description": "Import map"
}
"additionalProperties": {"type": "string"}
}

View File

@@ -34,7 +34,7 @@ const Playground = ({
try {
setImportMap(JSON.parse(importMapRaw) as IImportMap)
} catch (e) {
setImportMap({ imports: {} })
setImportMap({})
}
}
if (!tsconfig) {

View File

@@ -14,20 +14,7 @@ export interface IFiles {
[key: string]: IFile
}
export interface ITemplate {
name: string
tsconfig: ITsconfig
importMap: IImportMap
files: IFiles
}
export interface ITemplates {
[key: string]: ITemplate
}
export interface IImportMap {
imports: Record<string, string>
}
export type IImportMap = Record<string, string>
export interface ITsconfig {
compilerOptions: CompilerOptions

View File

@@ -1,59 +0,0 @@
import { ITemplates } from '@/components/Playground/shared'
import { ENTRY_FILE_NAME, MAIN_FILE_NAME } from '@/components/Playground/files'
import baseTsconfig from '@/components/Playground/templates/base/tsconfig.json'
import baseImportMap from '@/components/Playground/templates/base/import-map.json'
import baseMain from '@/components/Playground/templates/base/main.tsx?raw'
import baseApp from '@/components/Playground/templates/base/App.tsx?raw'
import demoTsconfig from '@/components/Playground/templates/demo/tsconfig.json'
import demoImportMap from '@/components/Playground/templates/demo/import-map.json'
import demoMain from '@/components/Playground/templates/demo/main.tsx?raw'
import demoApp from '@/components/Playground/templates/demo/App.tsx?raw'
import demoAppCSS from '@/components/Playground/templates/demo/App.css?raw'
const templates: ITemplates = {
base: {
name: '基础',
tsconfig: baseTsconfig,
importMap: baseImportMap,
files: {
[ENTRY_FILE_NAME]: {
name: ENTRY_FILE_NAME,
language: 'typescript',
value: baseMain,
hidden: true
},
[MAIN_FILE_NAME]: {
name: MAIN_FILE_NAME,
language: 'typescript',
value: baseApp
}
}
},
demo: {
name: 'Demo',
tsconfig: demoTsconfig,
importMap: demoImportMap,
files: {
[ENTRY_FILE_NAME]: {
name: ENTRY_FILE_NAME,
language: 'typescript',
value: demoMain,
hidden: true
},
[MAIN_FILE_NAME]: {
name: MAIN_FILE_NAME,
language: 'typescript',
value: demoApp
},
['App.css']: {
name: 'App.css',
language: 'css',
value: demoAppCSS
}
}
}
}
export default templates

View File

@@ -1,5 +0,0 @@
const App = () => {
return <></>
}
export default App

View File

@@ -1,6 +0,0 @@
{
"imports": {
"react": "https://esm.sh/react@18.2.0",
"react-dom/client": "https://esm.sh/react-dom@18.2.0"
}
}

View File

@@ -1,10 +0,0 @@
import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App'
ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<App />
</React.StrictMode>
)

View File

@@ -1,21 +0,0 @@
{
"compilerOptions": {
"target": 7,
"useDefineForClassFields": true,
"module": 99,
"skipLibCheck": true,
"moduleResolution": 2,
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": 4,
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true,
"composite": true,
"types": ["node"],
"allowSyntheticDefaultImports": true
}
}

View File

@@ -1,65 +0,0 @@
:root {
font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
font-weight: 400;
line-height: 1.5;
color: rgb(255 255 255 / 87%);
text-rendering: optimizelegibility;
text-size-adjust: 100%;
background-color: #242424;
color-scheme: light dark;
font-synthesis: none;
}
#root {
max-width: 1280px;
padding: 2rem;
margin: 0 auto;
text-align: center;
}
body {
display: flex;
min-width: 320px;
min-height: 100vh;
margin: 0;
place-items: center;
}
h1 {
font-size: 3.2em;
line-height: 1.1;
}
button {
padding: 0.6em 1.2em;
font-family: inherit;
font-size: 1em;
font-weight: 500;
cursor: pointer;
background-color: #1a1a1a;
border: 1px solid transparent;
border-radius: 8px;
transition: border-color 0.25s;
}
button:hover {
border-color: #646cff;
}
button:focus,
button:focus-visible {
outline: 4px auto -webkit-focus-ring-color;
}
@media (prefers-color-scheme: light) {
:root {
color: #213547;
background-color: #fff;
}
button {
background-color: #f9f9f9;
}
}

View File

@@ -1,17 +0,0 @@
import { useState } from 'react'
import './App.css'
const App = () => {
const [count, setCount] = useState(0)
return (
<>
<h1>Hello World</h1>
<div className="card">
<button onClick={() => setCount((count) => count + 1)}>count is {count}</button>
</div>
</>
)
}
export default App

View File

@@ -1,6 +0,0 @@
{
"imports": {
"react": "https://esm.sh/react@18.2.0",
"react-dom/client": "https://esm.sh/react-dom@18.2.0"
}
}

View File

@@ -1,10 +0,0 @@
import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App'
ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<App />
</React.StrictMode>
)

View File

@@ -1,21 +0,0 @@
{
"compilerOptions": {
"target": 7,
"useDefineForClassFields": true,
"module": 99,
"skipLibCheck": true,
"moduleResolution": 2,
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": 4,
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true,
"composite": true,
"types": ["node"],
"allowSyntheticDefaultImports": true
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -169,18 +169,18 @@ const Base = () => {
)}
</>
),
width: '12em',
width: '14em',
align: 'center',
render: (_, record) => (
<>
<AntdSpace size={'middle'}>
{!record.compiled && !Object.keys(hasEdited).length && (
{!Object.keys(hasEdited).length && (
<Permission operationCode={['system:tool:modify:base']}>
<a
style={{ color: COLOR_PRODUCTION }}
onClick={handleOnCompileBtnClick(record)}
>
{record.compiled ? '重新编译' : '编译'}
</a>
</Permission>
)}
@@ -304,7 +304,10 @@ const Base = () => {
![
IMPORT_MAP_FILE_NAME,
TS_CONFIG_FILE_NAME
].includes(value)
].includes(value) &&
!value.endsWith('.d.ts') &&
!value.endsWith('.css') &&
!value.endsWith('.json')
)
.map((value) => ({ value, label: value }))}
placeholder={'请选择入口文件'}

View File

@@ -219,7 +219,7 @@ const Tools = () => {
<AntdForm.Item
name={'pass'}
style={{ marginTop: 10 }}
rules={[{ required: true }]}
rules={[{ required: true, message: '请选择审核结果' }]}
>
<AntdRadio.Group>
<AntdRadio value={true}></AntdRadio>