-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: LivePreview * fix: add ReactLivePreview * fix:changeset * fix: delete .changeset/odd-icons-clap.md
- Loading branch information
1 parent
864278a
commit 8590b36
Showing
7 changed files
with
169 additions
and
9 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,5 @@ | ||
--- | ||
'kubed-documents': minor | ||
--- | ||
|
||
fix: LivePreview |
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,44 @@ | ||
import { LiveProvider } from 'react-live'; | ||
import React, { useState } from 'react'; | ||
import { useTheme } from '@kubed/components'; | ||
import { Language } from 'prism-react-renderer'; | ||
import LivePreview from './LivePreview'; | ||
import CodeEditor from './CodeEditor'; | ||
import { useCodeDemo } from './use-code-demo'; | ||
import { dark, light } from './prismTheme'; | ||
|
||
interface ReactLivePreviewProps { | ||
code: string; | ||
language: Language; | ||
scope: { [key: string]: any }; | ||
} | ||
const ReactLivePreview = ({ code: originCode, language, scope }: ReactLivePreviewProps) => { | ||
const theme = useTheme(); | ||
const prismTheme = theme.type === 'dark' ? dark : light; | ||
const [preViewCode, setPreViewCode] = useState<string>(originCode); | ||
const { code, noInline } = useCodeDemo({ scope, code: preViewCode }); | ||
|
||
return ( | ||
<LiveProvider | ||
language={language} | ||
noInline={noInline} | ||
code={code} | ||
scope={scope} | ||
theme={prismTheme} | ||
> | ||
<div> | ||
<LivePreview /> | ||
<CodeEditor | ||
code={originCode} | ||
onChange={(val) => { | ||
setPreViewCode(val); | ||
}} | ||
fontFamily={theme.font.mono} | ||
className="code-editor" | ||
/> | ||
</div> | ||
</LiveProvider> | ||
); | ||
}; | ||
|
||
export default ReactLivePreview; |
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,65 @@ | ||
import { get, cloneDeep } from 'lodash'; | ||
import * as React from 'react'; | ||
|
||
export const useCodeDemo = ({ scope, code: inputCode }) => { | ||
let code = cloneDeep(inputCode?.trim()); | ||
let noInline = false; | ||
|
||
const scopeKeys = Object.keys(scope); | ||
const scopeValues = scopeKeys.map((key) => { | ||
return { [key]: `${key}` }; | ||
}); | ||
|
||
// add 'React' to scopeValues | ||
scopeValues.push({ React: 'React' }); | ||
// convert scopeValues to object | ||
const imports = Object.assign({}, ...scopeValues); | ||
code = transformCode(code, imports); | ||
noInline = code.includes('render'); | ||
return { | ||
code, | ||
noInline, | ||
}; | ||
}; | ||
|
||
const importRegex = /^(import)\s(?!type(of\s|\s)(?!from)).*?$/gm; | ||
const exportDefaultRegex = /export\s+default\s+function\s+\w+\s*\(\s*\)\s*\{/; | ||
|
||
export const transformCode = (code: string, imports = {}, compName = 'App') => { | ||
let cleanedCode = code | ||
.replace(importRegex, (match) => { | ||
// get component name from the match ex. "import { Table } from '@nextui-org/react'" | ||
|
||
let componentName = match.match(/\w+/g)?.[1] || ''; | ||
// replace as | ||
componentName = componentName === 'as' ? match.match(/\w+/g)?.[2] || '' : componentName; | ||
|
||
const matchingImport = get(imports, componentName); | ||
|
||
if (matchingImport) { | ||
// remove the matching import | ||
return ''; | ||
} | ||
|
||
// if match includes './' or '../' then remove it | ||
if (match.includes('./') || match.includes('../')) { | ||
return ''; | ||
} | ||
|
||
return match; | ||
}) | ||
.replace(exportDefaultRegex, () => { | ||
// replace match with const Name = () => ( | ||
return `const ${compName} = () => {`; | ||
}) | ||
.replace('export', ''); | ||
|
||
// add render(<App/>) to cleanedCode if has const App = () => { | ||
if (cleanedCode.includes(`const App = () => {`)) { | ||
cleanedCode = `${cleanedCode}\nrender(<${compName}/>);`; | ||
} | ||
// delete comments from the code | ||
cleanedCode = cleanedCode.replace(/\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm, ''); | ||
|
||
return cleanedCode; | ||
}; |
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