-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
181 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { CameraControls, Cloud } from '@react-three/drei' | ||
import { Canvas } from '@react-three/fiber' | ||
|
||
export default function App() { | ||
return ( | ||
<Canvas camera={{ position: [0, -13, 0] }}> | ||
<Cloud speed={0.4} growth={6} /> | ||
<ambientLight intensity={Math.PI} /> | ||
<CameraControls /> | ||
</Canvas> | ||
) | ||
} |
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,8 @@ | ||
{ | ||
"name": "simple-box", | ||
"dependencies": { | ||
"three": "latest", | ||
"@react-three/fiber": "latest", | ||
"@react-three/drei": "latest" | ||
} | ||
} |
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,6 @@ | ||
html, | ||
body, | ||
#root { | ||
height: 100%; | ||
margin: unset; | ||
} |
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,70 @@ | ||
import cn from '@/lib/cn' | ||
import { crawl } from '@/utils/docs' | ||
import { Sandpack as SP } from '@codesandbox/sandpack-react' | ||
import fs from 'node:fs' | ||
import path from 'node:path' | ||
import { ComponentProps } from 'react' | ||
|
||
function getSandpackDependencies(folder: string) { | ||
const pkgPath = `${folder}/package.json` | ||
if (!fs.existsSync(pkgPath)) return null | ||
|
||
const str = fs.readFileSync(pkgPath, 'utf-8') | ||
return JSON.parse(str).dependencies as Record<string, string> | ||
} | ||
|
||
type File = { code: string } | ||
|
||
async function getSandpackFiles(folder: string, extensions = ['js', 'ts', 'jsx', 'tsx', 'css']) { | ||
const filepaths = await crawl( | ||
folder, | ||
(dir) => | ||
!dir.includes('node_modules') && extensions.map((ext) => dir.endsWith(ext)).some(Boolean), | ||
) | ||
// console.log('filepaths', filepaths) | ||
|
||
const files = filepaths.reduce( | ||
(acc, filepath) => { | ||
const relativeFilepath = path.relative(folder, filepath) | ||
|
||
return { | ||
...acc, | ||
[`/${relativeFilepath}`]: { | ||
code: fs.readFileSync(filepath, 'utf-8'), | ||
}, | ||
} | ||
}, | ||
{} as Record<string, File>, | ||
) | ||
|
||
return files | ||
} | ||
|
||
// https://sandpack.codesandbox.io/docs/getting-started/usage | ||
export const Sandpack = async ({ | ||
className, | ||
folder, | ||
...props | ||
}: { className: string; folder?: string } & ComponentProps<typeof SP>) => { | ||
// console.log('folder', folder) | ||
|
||
const files = folder ? await getSandpackFiles(folder) : props.files | ||
|
||
const pkgDeps = folder ? getSandpackDependencies(folder) : null | ||
const dependencies = pkgDeps ?? props.customSetup?.dependencies | ||
const customSetup = { | ||
...props.customSetup, | ||
dependencies, | ||
} | ||
|
||
const options = { | ||
...props.options, | ||
// editorHeight: 350 | ||
} | ||
|
||
return ( | ||
<div className={cn(className, 'sandpack')}> | ||
<SP {...props} files={files} customSetup={customSetup} options={options} /> | ||
</div> | ||
) | ||
} |
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 @@ | ||
export * from './Sandpack' |
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,49 @@ | ||
import type { Root } from 'hast' | ||
import { resolve } from 'path' | ||
import { visit } from 'unist-util-visit' | ||
|
||
// | ||
// <Sandpack folder="authoring-sandpack-cloud" /> | ||
// | ||
// { | ||
// type: 'mdxJsxFlowElement', | ||
// name: 'Sandpack', | ||
// attributes: [ | ||
// { | ||
// type: 'mdxJsxAttribute', | ||
// name: 'folder', | ||
// value: 'authoring-sandpack-cloud', | ||
// position: [Object] | ||
// }, | ||
// ... | ||
// ], | ||
// position: { | ||
// start: { line: 3, column: 1, offset: 2 }, | ||
// end: { line: 3, column: 32, offset: 33 } | ||
// }, | ||
// data: { _mdxExplicitJsx: true }, | ||
// children: [] | ||
// } | ||
|
||
// https://unifiedjs.com/learn/guide/create-a-rehype-plugin/ | ||
export function rehypeSandpack(dir: string) { | ||
return () => (tree: Root) => { | ||
visit(tree, null, function (node) { | ||
if ('name' in node && node.name === 'Sandpack') { | ||
// | ||
// Resolve folder path | ||
// | ||
|
||
const folderAttr = node.attributes | ||
.filter((node) => 'name' in node) | ||
.find((attr) => attr.name === 'folder') | ||
|
||
if (folderAttr) { | ||
const oldFolder = folderAttr?.value | ||
|
||
if (typeof oldFolder === 'string') folderAttr.value = `${resolve(dir, oldFolder)}` | ||
} | ||
} | ||
}) | ||
} | ||
} |
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
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