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 22a36f6 commit 9c3c2d4
Show file tree
Hide file tree
Showing 6 changed files with 477 additions and 142 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;
}
29 changes: 29 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,29 @@
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();
console.log('dark', dark);
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}
{...otherProps}
theme={dark ? 'dark' : 'light'}
/>
);
};
export default Sandpack;
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: 0, bottom: '5px' }}>
<OpenInCodeSandboxButton />
</div>
</SandpackLayout>
Expand Down
Loading

0 comments on commit 9c3c2d4

Please sign in to comment.