|
| 1 | +import React, { useEffect, useRef, useState } from 'react'; |
| 2 | +import { default as OriginalSourceCode } from 'dumi/theme-default/builtins/SourceCode'; |
| 3 | +import { codeToHtml } from 'shiki'; |
| 4 | +import { createTransformerFactory, rendererRich } from '@shikijs/twoslash/core'; |
| 5 | +import { createTwoslashFromCDN } from 'twoslash-cdn'; |
| 6 | +import { createStorage } from 'unstorage'; |
| 7 | +import indexedDbDriver from 'unstorage/drivers/indexedb'; |
| 8 | +import './index.less'; |
| 9 | + |
| 10 | +type ISourceCodeProps = Parameters<typeof OriginalSourceCode>[0]; |
| 11 | + |
| 12 | +const storage = createStorage({ |
| 13 | + driver: indexedDbDriver({ base: 'twoslash-cdn' }), |
| 14 | +}); |
| 15 | +const twoslash = createTwoslashFromCDN({ |
| 16 | + storage, |
| 17 | + compilerOptions: { |
| 18 | + strict: false, |
| 19 | + lib: ['esnext', 'dom'], |
| 20 | + }, |
| 21 | + twoSlashOptionsOverrides: { |
| 22 | + handbookOptions: { |
| 23 | + noErrors: true, |
| 24 | + }, |
| 25 | + }, |
| 26 | +}); |
| 27 | +const transformerTwoslash = createTransformerFactory(twoslash.runSync)({ |
| 28 | + renderer: rendererRich(), |
| 29 | +}); |
| 30 | + |
| 31 | +const specificVersionForATA = (str: string) => { |
| 32 | + const versions = { |
| 33 | + react: '18.2.0', |
| 34 | + antd: '4.22.5', |
| 35 | + }; |
| 36 | + const text = str |
| 37 | + .replace(/import .* from 'react';/g, (i) => i + ' // types: ' + versions.react) |
| 38 | + .replace(/import .*from 'antd.*;/g, (i) => i + ' // types: ' + versions.antd) |
| 39 | + .replace('dt-react-component', '.dt-react-component'); |
| 40 | + return text; |
| 41 | +}; |
| 42 | + |
| 43 | +export default function SourceCode({ children, lang, highlightLines }: ISourceCodeProps) { |
| 44 | + const [code, setCode] = useState(''); |
| 45 | + const [loading, setLoading] = useState(false); |
| 46 | + |
| 47 | + const ref = useRef<HTMLDivElement>(null); |
| 48 | + |
| 49 | + useEffect(() => { |
| 50 | + setLoading(true); |
| 51 | + twoslash |
| 52 | + .prepareTypes(specificVersionForATA(children)) |
| 53 | + .then(() => { |
| 54 | + return codeToHtml(children, { |
| 55 | + lang, |
| 56 | + theme: 'vitesse-light', |
| 57 | + transformers: [transformerTwoslash], |
| 58 | + }); |
| 59 | + }) |
| 60 | + .then(setCode) |
| 61 | + .finally(() => { |
| 62 | + setLoading(false); |
| 63 | + }); |
| 64 | + }, [children]); |
| 65 | + |
| 66 | + useEffect(() => { |
| 67 | + const handleKeyDown = (e: KeyboardEvent) => { |
| 68 | + if (e.key === 'a' && (e.metaKey || e.ctrlKey)) { |
| 69 | + e.preventDefault(); |
| 70 | + // Ctrl + A |
| 71 | + const selection = window.getSelection(); |
| 72 | + selection?.removeAllRanges(); |
| 73 | + const range = new Range(); |
| 74 | + range.selectNodeContents(ref.current!); |
| 75 | + selection?.addRange(range); |
| 76 | + } |
| 77 | + }; |
| 78 | + ref.current?.addEventListener('keydown', handleKeyDown); |
| 79 | + return () => { |
| 80 | + ref.current?.removeEventListener('keydown', handleKeyDown); |
| 81 | + }; |
| 82 | + }, []); |
| 83 | + |
| 84 | + if (loading) return <div className="sourceCode__loading">loading...</div>; |
| 85 | + return ( |
| 86 | + <div |
| 87 | + ref={ref} |
| 88 | + className="sourceCode__container" |
| 89 | + tabIndex={-1} |
| 90 | + dangerouslySetInnerHTML={{ __html: code }} |
| 91 | + /> |
| 92 | + ); |
| 93 | +} |
0 commit comments