2023-05-22 09:59:29 +08:00
|
|
|
import { css } from '@emotion/react';
|
|
|
|
import { Button, ConfigProvider, Modal, Spin, Typography, message } from 'antd';
|
|
|
|
import { ThemeEditor, enUS, zhCN } from 'antd-token-previewer';
|
2022-11-30 11:05:41 +08:00
|
|
|
import type { ThemeConfig } from 'antd/es/config-provider/context';
|
2022-12-07 20:22:15 +08:00
|
|
|
import { Helmet } from 'dumi';
|
2023-05-22 09:59:29 +08:00
|
|
|
import React, { Suspense, useCallback, useEffect, useState } from 'react';
|
2022-12-27 15:16:14 +08:00
|
|
|
import type { JSONContent, TextContent } from 'vanilla-jsoneditor';
|
2022-12-07 20:22:15 +08:00
|
|
|
import useLocale from '../../hooks/useLocale';
|
2022-12-28 18:17:17 +08:00
|
|
|
|
|
|
|
const JSONEditor = React.lazy(() => import('../../theme/common/JSONEditor'));
|
|
|
|
|
|
|
|
function isObject(target: any) {
|
|
|
|
return Object.prototype.toString.call(target) === '[object Object]';
|
|
|
|
}
|
2022-12-07 20:22:15 +08:00
|
|
|
|
|
|
|
const locales = {
|
|
|
|
cn: {
|
|
|
|
title: '主题编辑器',
|
|
|
|
save: '保存',
|
2022-12-27 17:09:18 +08:00
|
|
|
edit: '编辑',
|
|
|
|
export: '导出',
|
2022-12-27 15:16:14 +08:00
|
|
|
editModelTitle: '编辑主题配置',
|
|
|
|
editJsonContentTypeError: '主题 JSON 格式错误',
|
|
|
|
editSuccessfully: '编辑成功',
|
2022-12-07 20:22:15 +08:00
|
|
|
saveSuccessfully: '保存成功',
|
2022-12-28 18:17:17 +08:00
|
|
|
initialEditor: '正在初始化编辑器...',
|
2022-12-07 20:22:15 +08:00
|
|
|
},
|
|
|
|
en: {
|
|
|
|
title: 'Theme Editor',
|
|
|
|
save: 'Save',
|
2022-12-27 17:09:18 +08:00
|
|
|
edit: 'Edit',
|
|
|
|
export: 'Export',
|
2022-12-27 15:16:14 +08:00
|
|
|
editModelTitle: 'edit Theme Config',
|
|
|
|
editJsonContentTypeError: 'The theme of the JSON format is incorrect',
|
|
|
|
editSuccessfully: 'Edited successfully',
|
2022-12-07 20:22:15 +08:00
|
|
|
saveSuccessfully: 'Saved successfully',
|
2022-12-28 18:17:17 +08:00
|
|
|
initialEditor: 'Initializing Editor...',
|
2022-12-07 20:22:15 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
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';
|
2022-11-09 12:28:04 +08:00
|
|
|
|
|
|
|
const CustomTheme = () => {
|
2022-12-07 20:22:15 +08:00
|
|
|
const [messageApi, contextHolder] = message.useMessage();
|
|
|
|
const [locale, lang] = useLocale(locales);
|
|
|
|
|
|
|
|
const [theme, setTheme] = React.useState<ThemeConfig>({});
|
|
|
|
|
2022-12-27 15:16:14 +08:00
|
|
|
const [editModelOpen, setEditModelOpen] = useState<boolean>(false);
|
|
|
|
const [editThemeFormatRight, setEditThemeFormatRight] = useState<boolean>(true);
|
|
|
|
const [themeConfigContent, setThemeConfigContent] = useState<JSONContent & TextContent>({
|
|
|
|
text: '{}',
|
|
|
|
json: undefined,
|
|
|
|
});
|
|
|
|
|
2023-05-22 09:59:29 +08:00
|
|
|
useEffect(() => {
|
2022-12-07 20:22:15 +08:00
|
|
|
const storedConfig = localStorage.getItem(ANT_DESIGN_V5_THEME_EDITOR_THEME);
|
|
|
|
if (storedConfig) {
|
2023-05-22 09:59:29 +08:00
|
|
|
const themeConfig = JSON.parse(storedConfig);
|
|
|
|
const originThemeConfig = {
|
|
|
|
json: themeConfig,
|
|
|
|
text: undefined,
|
|
|
|
};
|
|
|
|
setThemeConfigContent(originThemeConfig);
|
|
|
|
setTheme(themeConfig);
|
2022-12-07 20:22:15 +08:00
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const styles = useStyle();
|
|
|
|
|
|
|
|
const handleSave = () => {
|
|
|
|
localStorage.setItem(ANT_DESIGN_V5_THEME_EDITOR_THEME, JSON.stringify(theme));
|
|
|
|
messageApi.success(locale.saveSuccessfully);
|
|
|
|
};
|
|
|
|
|
2022-12-27 15:16:14 +08:00
|
|
|
const handleEditConfig = () => {
|
|
|
|
setEditModelOpen(true);
|
2022-12-07 20:22:15 +08:00
|
|
|
};
|
|
|
|
|
2022-12-27 15:16:14 +08:00
|
|
|
const editModelClose = useCallback(() => {
|
|
|
|
setEditModelOpen(false);
|
|
|
|
}, [themeConfigContent]);
|
|
|
|
|
|
|
|
const handleEditConfigChange = (newcontent, preContent, status) => {
|
|
|
|
setThemeConfigContent(newcontent);
|
2023-05-22 09:59:29 +08:00
|
|
|
setEditThemeFormatRight(!status.contentErrors);
|
2022-12-07 20:22:15 +08:00
|
|
|
};
|
2022-11-09 12:28:04 +08:00
|
|
|
|
2022-12-27 15:16:14 +08:00
|
|
|
const editSave = useCallback(() => {
|
2023-05-22 09:59:29 +08:00
|
|
|
const contentFormatError = !editThemeFormatRight;
|
|
|
|
|
|
|
|
if (contentFormatError) {
|
2022-12-27 15:16:14 +08:00
|
|
|
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);
|
2023-05-22 09:59:29 +08:00
|
|
|
}, [themeConfigContent, editThemeFormatRight]);
|
2022-12-27 15:16:14 +08:00
|
|
|
|
2022-12-27 17:09:18 +08:00
|
|
|
const handleExport = () => {
|
|
|
|
const file = new File([JSON.stringify(theme, null, 2)], `Ant Design Theme.json`, {
|
|
|
|
type: 'text/json; charset=utf-8;',
|
|
|
|
});
|
|
|
|
const tmpLink = document.createElement('a');
|
|
|
|
const objectUrl = URL.createObjectURL(file);
|
|
|
|
|
|
|
|
tmpLink.href = objectUrl;
|
|
|
|
tmpLink.download = file.name;
|
|
|
|
document.body.appendChild(tmpLink);
|
|
|
|
tmpLink.click();
|
|
|
|
|
|
|
|
document.body.removeChild(tmpLink);
|
|
|
|
URL.revokeObjectURL(objectUrl);
|
|
|
|
};
|
|
|
|
|
2022-11-09 12:28:04 +08:00
|
|
|
return (
|
|
|
|
<div>
|
2022-12-07 20:22:15 +08:00
|
|
|
<Helmet>
|
|
|
|
<title>{`${locale.title} - Ant Design`}</title>
|
|
|
|
<meta property="og:title" content={`${locale.title} - Ant Design`} />
|
|
|
|
</Helmet>
|
|
|
|
{contextHolder}
|
2022-11-22 17:00:28 +08:00
|
|
|
<ConfigProvider theme={{ inherit: false }}>
|
2022-12-07 20:22:15 +08:00
|
|
|
<div css={styles.header}>
|
|
|
|
<Typography.Title level={5} style={{ margin: 0 }}>
|
|
|
|
{locale.title}
|
|
|
|
</Typography.Title>
|
|
|
|
<div>
|
2022-12-27 15:16:14 +08:00
|
|
|
<Modal
|
|
|
|
open={editModelOpen}
|
|
|
|
title={locale.editModelTitle}
|
|
|
|
width={600}
|
|
|
|
okText={locale.save}
|
|
|
|
onOk={editSave}
|
|
|
|
onCancel={editModelClose}
|
|
|
|
>
|
2022-12-28 18:17:17 +08:00
|
|
|
<Suspense
|
|
|
|
fallback={
|
|
|
|
<div style={{ textAlign: 'center', width: '100%', padding: '24px 0' }}>
|
|
|
|
<Spin tip={locale.initialEditor} />
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<JSONEditor
|
|
|
|
content={themeConfigContent}
|
|
|
|
onChange={handleEditConfigChange}
|
|
|
|
mainMenuBar={false}
|
|
|
|
/>
|
|
|
|
</Suspense>
|
2022-12-27 15:16:14 +08:00
|
|
|
</Modal>
|
2022-12-27 17:09:18 +08:00
|
|
|
<Button onClick={handleExport} style={{ marginRight: 8 }}>
|
|
|
|
{locale.export}
|
2022-12-07 20:22:15 +08:00
|
|
|
</Button>
|
2022-12-27 17:09:18 +08:00
|
|
|
<Button onClick={handleEditConfig} style={{ marginRight: 8 }}>
|
|
|
|
{locale.edit}
|
2022-12-07 20:22:15 +08:00
|
|
|
</Button>
|
|
|
|
<Button type="primary" onClick={handleSave}>
|
|
|
|
{locale.save}
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-11-09 12:28:04 +08:00
|
|
|
<ThemeEditor
|
2022-11-22 17:00:28 +08:00
|
|
|
theme={{ name: 'Custom Theme', key: 'test', config: theme }}
|
2022-12-07 20:22:15 +08:00
|
|
|
style={{ height: 'calc(100vh - 64px - 56px)' }}
|
2022-11-19 13:47:33 +08:00
|
|
|
onThemeChange={(newTheme) => {
|
2022-11-09 12:28:04 +08:00
|
|
|
setTheme(newTheme.config);
|
2023-06-30 13:53:20 +08:00
|
|
|
setThemeConfigContent({
|
|
|
|
json: newTheme.config,
|
|
|
|
text: undefined,
|
|
|
|
});
|
2022-11-09 12:28:04 +08:00
|
|
|
}}
|
2022-12-07 20:22:15 +08:00
|
|
|
locale={lang === 'cn' ? zhCN : enUS}
|
2022-11-09 12:28:04 +08:00
|
|
|
/>
|
|
|
|
</ConfigProvider>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default CustomTheme;
|