Optimize Playground

This commit is contained in:
2024-01-19 18:28:53 +08:00
parent 343152c9d3
commit f3dc9d0e4f
4 changed files with 38 additions and 13 deletions

View File

@@ -13,6 +13,7 @@ import Editor from '@/components/Playground/CodeEditor/Editor'
interface CodeEditorProps { interface CodeEditorProps {
theme?: ITheme theme?: ITheme
showFileSelector?: boolean
tsconfig?: ITsconfig tsconfig?: ITsconfig
files: IFiles files: IFiles
readonly?: boolean readonly?: boolean
@@ -30,6 +31,7 @@ interface CodeEditorProps {
const CodeEditor = ({ const CodeEditor = ({
theme, theme,
showFileSelector = true,
tsconfig, tsconfig,
files, files,
readonly, readonly,
@@ -119,6 +121,7 @@ const CodeEditor = ({
return ( return (
<> <>
<FlexBox data-component={'playground-code-editor'}> <FlexBox data-component={'playground-code-editor'}>
{showFileSelector && (
<FileSelector <FileSelector
files={files} files={files}
readonly={readonly} readonly={readonly}
@@ -132,6 +135,7 @@ const CodeEditor = ({
onAddFile={handleOnAddFile} onAddFile={handleOnAddFile}
onError={handleOnError} onError={handleOnError}
/> />
)}
<Editor <Editor
tsconfig={tsconfig} tsconfig={tsconfig}
theme={theme} theme={theme}

View File

@@ -36,4 +36,7 @@ const Output = ({ files, selectedFileName, importMap }: OutputProps) => {
) )
} }
Output.Preview = Preview
Output.Transform = Transform
export default Output export default Output

View File

@@ -4,6 +4,7 @@ import DiagnosticsOptions = languages.json.DiagnosticsOptions
import { IFile, IFiles, ILanguage } from '@/components/Playground/shared' import { IFile, IFiles, ILanguage } from '@/components/Playground/shared'
import tsconfigSchema from '@/components/Playground/tsconfig-schema.json' import tsconfigSchema from '@/components/Playground/tsconfig-schema.json'
import importMapSchema from '@/components/Playground/import-map-schema.json' import importMapSchema from '@/components/Playground/import-map-schema.json'
import { formatByteSize } from '@/util/common.tsx'
export const TS_CONFIG_FILE_NAME = 'tsconfig.json' export const TS_CONFIG_FILE_NAME = 'tsconfig.json'
export const MAIN_FILE_NAME = 'App.tsx' export const MAIN_FILE_NAME = 'App.tsx'
@@ -41,6 +42,20 @@ export const base64ToStr = (base64: string) => {
return '' return ''
} }
export const getFilesSize = (files: IFiles) =>
formatByteSize(strToU8(JSON.stringify(files)).byteLength)
export const base64ToFiles = (base64: string): IFiles => {
try {
return JSON.parse(base64ToStr(base64)) as IFiles
} catch (e) {
console.error(e)
return {}
}
}
export const filesToBase64 = (files: IFiles): string => strToBase64(JSON.stringify(files))
export const getFilesFromUrl = () => { export const getFilesFromUrl = () => {
let files: IFiles | undefined let files: IFiles | undefined
try { try {

View File

@@ -98,4 +98,7 @@ const Playground = ({ initFiles, initImportMapRaw, initTsconfigRaw }: Playground
) )
} }
Playground.CodeEditor = CodeEditor
Playground.Output = Output
export default Playground export default Playground