import React, { useCallback, useEffect, useState } from 'react'; import { enUS, zhCN, ThemeEditor } from 'antd-token-previewer'; import { Button, ConfigProvider, message, Modal, Typography } from 'antd'; import type { ThemeConfig } from 'antd/es/config-provider/context'; import { Helmet } from 'dumi'; import { css } from '@emotion/react'; import { EditOutlined } from '@ant-design/icons'; import type { JSONContent, TextContent } from 'vanilla-jsoneditor'; import useLocale from '../../hooks/useLocale'; import JSONEditor from './components/JSONEditor'; import { isObject } from './components/utils'; const locales = { cn: { title: '主题编辑器', save: '保存', reset: '重置', edit: '代码', editModelTitle: '编辑主题配置', editTitle: '在下方编辑你的主题 JSON 即可', editJsonContentTypeError: '主题 JSON 格式错误', editSuccessfully: '编辑成功', saveSuccessfully: '保存成功', }, en: { title: 'Theme Editor', save: 'Save', reset: 'Reset', edit: 'Code', editModelTitle: 'edit Theme Config', editTitle: 'Edit your theme JSON below', editJsonContentTypeError: 'The theme of the JSON format is incorrect', editSuccessfully: 'Edited successfully', saveSuccessfully: 'Saved successfully', }, }; const useStyle = () => ({ header: css({ display: 'flex', height: 56, alignItems: 'center', padding: '0 24px', justifyContent: 'space-between', borderBottom: '1px solid #F0F0F0', }), }); const ANT_DESIGN_V5_THEME_EDITOR_THEME = 'ant-design-v5-theme-editor-theme'; const CustomTheme = () => { const [messageApi, contextHolder] = message.useMessage(); const [locale, lang] = useLocale(locales); const [theme, setTheme] = React.useState({}); const [editModelOpen, setEditModelOpen] = useState(false); const [editThemeFormatRight, setEditThemeFormatRight] = useState(true); const [themeConfigContent, setThemeConfigContent] = useState({ text: '{}', json: undefined, }); useEffect(() => { const storedConfig = localStorage.getItem(ANT_DESIGN_V5_THEME_EDITOR_THEME); if (storedConfig) { setTheme(() => JSON.parse(storedConfig)); } }, []); useEffect(() => { if (editModelOpen === true) return; setThemeConfigContent({ json: theme as any, text: undefined, }); }, [theme, editModelOpen]); const styles = useStyle(); const handleSave = () => { localStorage.setItem(ANT_DESIGN_V5_THEME_EDITOR_THEME, JSON.stringify(theme)); messageApi.success(locale.saveSuccessfully); }; const handleReset = () => { setTheme({}); }; const handleEditConfig = () => { setEditModelOpen(true); }; const editModelClose = useCallback(() => { setEditModelOpen(false); }, [themeConfigContent]); const handleEditConfigChange = (newcontent, preContent, status) => { setThemeConfigContent(newcontent); if ( Array.isArray(status.contentErrors.validationErrors) && status.contentErrors.validationErrors.length === 0 ) { setEditThemeFormatRight(true); } else { setEditThemeFormatRight(false); } }; const editSave = useCallback(() => { if (!editThemeFormatRight) { message.error(locale.editJsonContentTypeError); return; } const themeConfig = themeConfigContent.text ? JSON.parse(themeConfigContent.text) : themeConfigContent.json; if (!isObject(themeConfig)) { message.error(locale.editJsonContentTypeError); return; } setTheme(themeConfig); editModelClose(); messageApi.success(locale.editSuccessfully); }, [themeConfigContent]); return (
{`${locale.title} - Ant Design`} {contextHolder}
{locale.title}
{locale.editTitle}
{ setTheme(newTheme.config); }} locale={lang === 'cn' ? zhCN : enUS} />
); }; export default CustomTheme;