-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add react editor, error connected with client-side only code
- Loading branch information
1 parent
63133c7
commit 937264f
Showing
8 changed files
with
433 additions
and
25 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import Editor, { Monaco } from '@monaco-editor/react'; | ||
import React, { useCallback, useLayoutEffect, useRef } from 'react'; | ||
import { useColorMode } from '@docusaurus/theme-common'; | ||
import { tsCompilerOptions } from '../monacoEditorConfig'; | ||
import BrowserOnly from '@docusaurus/BrowserOnly'; | ||
|
||
function useEvent<TFunction extends (...params: any[]) => any>(handler: TFunction) { | ||
const handlerRef = useRef(handler); | ||
|
||
// In a real implementation, this would run before layout effects | ||
useLayoutEffect(() => { | ||
handlerRef.current = handler; | ||
}); | ||
|
||
return useCallback((...args: Parameters<TFunction>) => { | ||
// In a real implementation, this would throw if called during render | ||
const fn = handlerRef.current; | ||
return fn(...args); | ||
}, []) as TFunction; | ||
} | ||
|
||
function handleEditorWillMount(monaco: Monaco) { | ||
const tsDefaults = monaco?.languages.typescript.typescriptDefaults; | ||
|
||
const source = ` | ||
declare module 'my-lib' { | ||
export function func(param: string): number; | ||
export interface SomeInterface { | ||
field: string; | ||
} | ||
} | ||
`; | ||
const libUri = 'my-lib/index.d.ts'; | ||
tsDefaults.addExtraLib(source, libUri); | ||
|
||
fetch('https://unpkg.com/@types/react/index.d.ts') | ||
.then(response => response.text()) | ||
.then(reactTypes => { | ||
tsDefaults.addExtraLib(reactTypes, 'react/index.d.ts'); | ||
}); | ||
|
||
fetch('https://unpkg.com/@types/react/jsx-runtime.d.ts') | ||
.then(response => response.text()) | ||
.then(reactTypes => { | ||
tsDefaults.addExtraLib(reactTypes, 'react/jsx-runtime.d.ts'); | ||
}); | ||
|
||
fetch('https://unpkg.com/@types/react-dom/index.d.ts') | ||
.then(response => response.text()) | ||
.then(reactDOMTypes => { | ||
tsDefaults.addExtraLib(reactDOMTypes, 'react-dom/index.d.ts'); | ||
}); | ||
|
||
tsDefaults.setCompilerOptions({ | ||
...tsCompilerOptions, | ||
}); | ||
} | ||
|
||
type Props = { | ||
code: string; | ||
onCodeChange: (value: string) => unknown; | ||
}; | ||
|
||
export default function PlaygroundReactEditor(props: Props) { | ||
const { code, onCodeChange } = props; | ||
const { colorMode, setColorMode } = useColorMode(); | ||
|
||
const handleChange = useEvent((value: string | undefined) => { | ||
onCodeChange(value ?? ''); | ||
}); | ||
|
||
return ( | ||
<BrowserOnly> | ||
{() => ( | ||
<div style={{ height: '100%', border: 'solid', borderColor: 'grey' }}> | ||
<Editor | ||
height="93%" | ||
defaultLanguage="typescript" | ||
value={code} | ||
onChange={handleChange} | ||
beforeMount={handleEditorWillMount} | ||
defaultPath="file:///main.tsx" | ||
theme={colorMode === 'dark' ? 'vs-dark' : 'light'} | ||
/> | ||
</div> | ||
)} | ||
</BrowserOnly> | ||
); | ||
} |
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,17 @@ | ||
import { languages } from 'monaco-editor'; | ||
|
||
export const tsCompilerOptions: languages.typescript.CompilerOptions = { | ||
target: languages.typescript.ScriptTarget.ESNext, | ||
allowNonTsExtensions: true, | ||
strict: true, | ||
esModuleInterop: true, | ||
moduleResolution: languages.typescript.ModuleResolutionKind.NodeJs, | ||
jsx: languages.typescript.JsxEmit.React, | ||
skipLibCheck: true, | ||
exactOptionalPropertyTypes: true, | ||
baseUrl: '.', | ||
lib: ['dom', 'es2021'], | ||
allowJs: true, | ||
isolatedModules: true, | ||
noEmit: true, | ||
}; |
Oops, something went wrong.