Skip to content

Commit

Permalink
feat: doc support Sandpack component
Browse files Browse the repository at this point in the history
  • Loading branch information
caohuilin committed Aug 30, 2023
1 parent 33a2b1a commit c552877
Show file tree
Hide file tree
Showing 8 changed files with 478 additions and 144 deletions.
3 changes: 3 additions & 0 deletions packages/document/main-doc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
"access": "public",
"provenance": true
},
"dependencies": {
"@modern-js/sandpack-react": "workspace:*"
},
"peerDependencies": {
"@modern-js/builder-doc": "workspace:^2.32.1"
},
Expand Down
7 changes: 7 additions & 0 deletions packages/document/main-doc/src/components/Sandpack/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.light {
--sp-layout-height: 500px !important;
}

.dark {
--sp-layout-height: 500px !important;
}
28 changes: 28 additions & 0 deletions packages/document/main-doc/src/components/Sandpack/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import ModernSandpack, { ModernSandpackProps } from '@modern-js/sandpack-react';
import React, { PropsWithChildren } from 'react';
import { useDark } from 'rspress/runtime';

import './index.css';

const Sandpack = (props: PropsWithChildren<ModernSandpackProps>) => {
const dark = useDark();
const { children, ...otherProps } = props;
const files: Record<string, string> = {};
React.Children.forEach(children, (child: any) => {
if (child) {
const { meta, children } = child.props.children.props;
const matches = meta.match(/title="(.*)"/);
if (matches.length > 1) {
files[matches[1]] = children;
}
}
});
return (
<ModernSandpack
files={files}
theme={dark ? 'dark' : 'light'}
{...otherProps}
/>
);
};
export default Sandpack;
2 changes: 1 addition & 1 deletion packages/generator/generators/mwa-generator/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export const handleTemplateFile = async (
isMonorepoSubProject,
modernVersion,
packageManager,
isTsProject: language === Language.TS,
isTs: language === Language.TS,
},
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"@modern-js/eslint-config": "{{modernVersion}}",
"@modern-js/tsconfig":"{{modernVersion}}",
"@modern-js-app/eslint-config": "{{modernVersion}}",
{{#if isTsProject}}
{{#if isTs}}
"typescript": "~5.0.4",
"@types/jest": "~29.2.4",
"@types/node": "~16.11.7",
Expand Down
15 changes: 15 additions & 0 deletions packages/generator/sandpack-react/scripts/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ import { renderString } from '@modern-js/codesmith-api-handlebars';
import { getModernVersion } from '@modern-js/generator-utils';
import { Solution } from '@modern-js/generator-common';

const IgnoreFiles = [
'.nvmrc',
'.eslintrc.js.handlebars',
'src/.eslintrc.js.handlebars',
'.prettierrc',
'.vscode/extensions.json',
'.vscode/settings.json',
'.husky/pre-commit',
'README.md',
'.gitignore.handlebars',
];

async function handleTemplate(
templatePath: string,
data: Record<string, any> = {},
Expand All @@ -17,6 +29,9 @@ async function handleTemplate(
const templateFiles = await recursive(templatePath);
templateFiles.forEach(filePath => {
const file = filePath.replace(`${templatePath}/`, '');
if (IgnoreFiles.includes(file)) {
return;
}
if (fs.statSync(filePath).isFile()) {
if (file.endsWith('.handlebars')) {
files[
Expand Down
8 changes: 6 additions & 2 deletions packages/generator/sandpack-react/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@ import {
OpenInCodeSandboxButton,
SandpackSetup,
SandpackFiles,
SandpackThemeProp,
} from '@codesandbox/sandpack-react';
import { ModernTemplates } from './templates';

export type ModernSandpackProps = {
template: 'web-app' | 'npm-module';
customSetup?: SandpackSetup;
files?: SandpackFiles;
removeFiles: string[];
removeFiles?: string[];
options?: Record<string, any>;
initialCollapsedFolder?: string[];
theme?: SandpackThemeProp;
};

function fileterFiles(files: SandpackFiles, removeFiles: string[]) {
Expand All @@ -39,6 +41,7 @@ export default function ModernSandpack(props: ModernSandpackProps) {
removeFiles = [],
options = {},
initialCollapsedFolder = [],
theme = 'light',
} = props;
const initFiles = ModernTemplates[template];
const files = {
Expand All @@ -47,6 +50,7 @@ export default function ModernSandpack(props: ModernSandpackProps) {
};
return (
<SandpackProvider
theme={theme}
customSetup={{ environment: 'node', ...customSetup }}
files={files}
options={{
Expand All @@ -69,7 +73,7 @@ export default function ModernSandpack(props: ModernSandpackProps) {
]}
/>
<SandpackCodeEditor showLineNumbers showInlineErrors showTabs={false} />
<div style={{ position: 'absolute', right: 0, bottom: 0 }}>
<div style={{ position: 'absolute', right: '5px', bottom: '5px' }}>
<OpenInCodeSandboxButton />
</div>
</SandpackLayout>
Expand Down
Loading

0 comments on commit c552877

Please sign in to comment.