-
-
Notifications
You must be signed in to change notification settings - Fork 51.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs: prevent highlight code initially #44344
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
@@ -1,8 +1,10 @@ | ||
import React, { Suspense } from 'react'; | ||
import type { IPreviewerProps } from 'dumi'; | ||
import { Skeleton } from 'antd'; | ||
import { Skeleton, Alert } from 'antd'; | ||
import { createStyles } from 'antd-style'; | ||
|
||
const { ErrorBoundary } = Alert; | ||
|
||
const Previewer = React.lazy(() => import('./Previewer')); | ||
|
||
const useStyle = createStyles(({ css }) => ({ | ||
|
@@ -16,21 +18,23 @@ const useStyle = createStyles(({ css }) => ({ | |
export default (props: IPreviewerProps) => { | ||
const { styles } = useStyle(); | ||
return ( | ||
<Suspense | ||
fallback={ | ||
<Skeleton.Node | ||
active | ||
className={styles.skeletonWrapper} | ||
style={{ | ||
width: '100%', | ||
height: '100%', | ||
}} | ||
> | ||
{' '} | ||
</Skeleton.Node> | ||
} | ||
> | ||
<Previewer {...props} /> | ||
</Suspense> | ||
<ErrorBoundary> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个是啥场景要加个 ErrorBoundary There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CodePreview 代码本身写错了一行代码直接白了,顺手加个。 |
||
<Suspense | ||
fallback={ | ||
<Skeleton.Node | ||
active | ||
className={styles.skeletonWrapper} | ||
style={{ | ||
width: '100%', | ||
height: '100%', | ||
}} | ||
> | ||
{' '} | ||
</Skeleton.Node> | ||
} | ||
> | ||
<Previewer {...props} /> | ||
</Suspense> | ||
</ErrorBoundary> | ||
); | ||
}; |
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 |
---|---|---|
@@ -1,37 +1,101 @@ | ||
import React from 'react'; | ||
import React, { useEffect, useMemo } from 'react'; | ||
import Prism from 'prismjs'; | ||
import toReactElement from 'jsonml-to-react-element'; | ||
import JsonML from 'jsonml.js/lib/utils'; | ||
import { Tabs } from 'antd'; | ||
|
||
const LANGS = { | ||
tsx: 'TypeScript', | ||
jsx: 'JavaScript', | ||
style: 'CSS', | ||
}; | ||
|
||
interface CodePreviewProps { | ||
codes?: Record<PropertyKey, string>; | ||
toReactComponent?: (node: any) => React.ReactNode; | ||
sourceCode?: string; | ||
jsxCode?: string; | ||
styleCode?: string; | ||
onCodeTypeChange?: (activeKey: string) => void; | ||
} | ||
|
||
const CodePreview: React.FC<CodePreviewProps> = ({ toReactComponent, codes, onCodeTypeChange }) => { | ||
const langList = Object.keys(codes).sort().reverse(); | ||
function toReactComponent(jsonML: any) { | ||
return toReactElement(jsonML, [ | ||
[ | ||
(node: any) => JsonML.isElement(node) && JsonML.getTagName(node) === 'pre', | ||
(node: any, index: any) => { | ||
// ref: /~https://github.com/benjycui/bisheng/blob/master/packages/bisheng/src/bisheng-plugin-highlight/lib/browser.js#L7 | ||
const attr = JsonML.getAttributes(node); | ||
return React.createElement( | ||
'pre', | ||
{ | ||
key: index, | ||
className: `language-${attr.lang}`, | ||
}, | ||
React.createElement('code', { | ||
dangerouslySetInnerHTML: { __html: attr.highlighted }, | ||
}), | ||
); | ||
}, | ||
], | ||
]); | ||
} | ||
|
||
const CodePreview: React.FC<CodePreviewProps> = ({ | ||
sourceCode = '', | ||
jsxCode = '', | ||
styleCode = '', | ||
onCodeTypeChange, | ||
}) => { | ||
// 避免 Tabs 数量不稳定的闪动问题 | ||
const initialCodes = {}; | ||
if (sourceCode) { | ||
initialCodes.tsx = ''; | ||
} | ||
if (jsxCode) { | ||
initialCodes.jsx = ''; | ||
} | ||
if (styleCode) { | ||
initialCodes.style = ''; | ||
} | ||
const [highlightedCodes, setHighlightedCodes] = React.useState(initialCodes); | ||
|
||
useEffect(() => { | ||
const codes = { | ||
tsx: Prism.highlight(sourceCode, Prism.languages.javascript, 'jsx'), | ||
jsx: Prism.highlight(jsxCode, Prism.languages.javascript, 'jsx'), | ||
style: Prism.highlight(styleCode, Prism.languages.css, 'css'), | ||
}; | ||
// 去掉空的代码类型 | ||
Object.keys(codes).forEach((key) => { | ||
if (!codes[key]) { | ||
delete codes[key]; | ||
} | ||
}); | ||
setHighlightedCodes(codes); | ||
}, [jsxCode, sourceCode, styleCode]); | ||
|
||
const langList = Object.keys(highlightedCodes); | ||
const items = useMemo( | ||
() => | ||
langList.map((lang) => ({ | ||
label: LANGS[lang], | ||
key: lang, | ||
children: toReactComponent(['pre', { lang, highlighted: highlightedCodes[lang] }]), | ||
})), | ||
[JSON.stringify(highlightedCodes)], | ||
); | ||
|
||
if (!langList.length) { | ||
return null; | ||
} | ||
|
||
if (langList.length === 1) { | ||
return toReactComponent([ | ||
'pre', | ||
{ lang: langList[0], highlighted: codes[langList[0]], className: 'highlight' }, | ||
{ lang: langList[0], highlighted: highlightedCodes[langList[0]], className: 'highlight' }, | ||
]); | ||
} | ||
return ( | ||
<Tabs | ||
centered | ||
className="highlight" | ||
onChange={onCodeTypeChange} | ||
items={langList.map((lang) => ({ | ||
label: LANGS[lang], | ||
key: lang, | ||
children: toReactComponent(['pre', { lang, highlighted: codes[lang] }]), | ||
}))} | ||
/> | ||
); | ||
|
||
return <Tabs centered className="highlight" onChange={onCodeTypeChange} items={items} />; | ||
}; | ||
|
||
export default CodePreview; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
原来的写法是直接放 render 里。。。