import React, { useMemo } from 'react' import { Text, Spacer, useTheme, Code, useToasts } from 'components' import DefaultTheme from 'components/styles/themes/default' import { isObject, MergeObject } from 'components/styles/theme-provider/theme-provider' import { LiveEditor, LiveProvider } from 'react-live' import makeCodeTheme from 'lib/components/playground/code-theme' import useClipboard from 'components/utils/use-clipboard' import CopyIcon from 'components/snippet/snippet-icon' import { useConfigs } from 'lib/config-context' export const getDeepDifferents = (source: T, target: T): T => { if (!isObject(target) || !isObject(source)) return target const sourceKeys = Object.keys(source) as Array let result = {} as T for (const key of sourceKeys) { const sourceValue = source[key] const targetValue = target[key] if (isObject(sourceValue) && isObject(targetValue)) { const childrenDiff = getDeepDifferents(sourceValue, { ...targetValue }) if (Object.keys(childrenDiff).length !== 0) { result[key] = childrenDiff } } else if (sourceValue !== targetValue) { result[key] = targetValue } } return result } const CustomizationCodes = () => { const theme = useTheme() const { isChinese } = useConfigs() const codeTheme = makeCodeTheme(theme) const { copy } = useClipboard() const [, setToast] = useToasts() const deepDifferents = useMemo(() => getDeepDifferents(DefaultTheme, theme), [ DefaultTheme, theme, ]) const userCodes = useMemo(() => { return `const myTheme = ${JSON.stringify(deepDifferents, null, 2)} /*** * Usage:: * export const App = () => { * return ( * * * * * ) * } **/` }, [deepDifferents]) const copyCode = () => { copy(userCodes) setToast({ text: 'Theme code copied.' }) } return (

{isChinese ? '主题代码' : 'Theme Codes'}

{isChinese ? ( 这里是你所有的变更,点击 copy 按钮即可使用在你自己的项目中。 ) : ( This is all your changes, click copy to use it in your own project. )}
) } export default CustomizationCodes