Skip to content

Commit

Permalink
add react editor, error connected with client-side only code
Browse files Browse the repository at this point in the history
  • Loading branch information
wkazmierczak committed Aug 20, 2024
1 parent 63133c7 commit 937264f
Show file tree
Hide file tree
Showing 8 changed files with 433 additions and 25 deletions.
3 changes: 2 additions & 1 deletion demos/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"rules": {
"prettier/prettier": ["error"],
"@typescript-eslint/no-explicit-any": [0, {}],
"no-constant-condition": [0]
"no-constant-condition": [0],
"@typescript-eslint/no-floating-promises": ["error"]
}
}
3 changes: 3 additions & 0 deletions docs/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
"no-constant-condition": [
0
],
"@typescript-eslint/no-floating-promises": [
"error"
],
"import/no-unresolved": [
2,
{
Expand Down
118 changes: 118 additions & 0 deletions docs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,24 @@
"typecheck": "tsc"
},
"dependencies": {
"@babel/standalone": "^7.25.3",
"@docusaurus/core": "^3.1.1",
"@docusaurus/plugin-client-redirects": "3.1.1",
"@docusaurus/preset-classic": "^3.1.1",
"@mdx-js/react": "^3.0.0",
"@monaco-editor/react": "^4.6.0",
"@types/jsoneditor": "^9.9.5",
"ajv": "^6.12.6",
"clsx": "^1.2.1",
"jsoneditor": "^10.1.0",
"monaco-editor": "^0.50.0",
"prism-react-renderer": "^2.1.0",
"react": "^18.0.0",
"react-dom": "^18.0.0",
"react-hot-toast": "^2.4.1",
"react-icons": "^4.12.0",
"react-tooltip": "^5.28.0",
"remeda": "^2.11.0",
"typewriter-effect": "^2.21.0"
},
"devDependencies": {
Expand Down
89 changes: 89 additions & 0 deletions docs/src/components/PlaygroundReactEditor.tsx
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>
);
}
17 changes: 17 additions & 0 deletions docs/src/monacoEditorConfig.ts
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,
};
Loading

0 comments on commit 937264f

Please sign in to comment.