| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- (function () {
- const React = window.React;
- const ReactDOM = window.ReactDOM;
- const antd = window.antd;
- const h = React.createElement;
- function cssVar(name, fallback) {
- try {
- const v = getComputedStyle(document.documentElement).getPropertyValue(name).trim();
- return v || fallback;
- } catch (e) { return fallback; }
- }
- function accent() { return cssVar('--accent', '#ff5c2b'); }
- function accentDark() { return cssVar('--accent-dark', '#e64718'); }
- function accentSoft() { return cssVar('--accent-soft', '#ffe0d2'); }
- function accentRgb() { return cssVar('--accent-rgb', '255,92,43'); }
- function buildTheme() {
- // 每次构造全新对象:antd v5 cssinjs 按 theme 的 hash 缓存注入样式,
- // 原地 mutate 不会重算 hash(换肤后会残留旧主色的样式),故必须返回新对象。
- return {
- token: {
- colorPrimary: accent(),
- colorInfo: accent(),
- colorSuccess: '#4f9d69',
- colorWarning: '#e0a83a',
- colorError: accentDark(),
- colorTextBase: '#17140f',
- colorText: '#2c271f',
- colorTextSecondary: '#7d7466',
- colorTextTertiary: '#a79d8d',
- colorTextQuaternary: '#c3baa9',
- colorBorder: '#e2dacb',
- colorBorderSecondary: '#efe9dc',
- colorBgContainer: '#fffdf8',
- colorBgElevated: '#fffdf8',
- colorBgLayout: '#f6f2e9',
- colorBgSpotlight: '#17140f',
- colorFillQuaternary: 'rgba(23,20,15,0.05)',
- colorFillTertiary: 'rgba(23,20,15,0.03)',
- borderRadius: 10,
- borderRadiusLG: 16,
- borderRadiusSM: 8,
- borderRadiusXS: 6,
- controlHeight: 36,
- controlHeightSM: 28,
- controlHeightLG: 44,
- fontFamily: '"PingFang SC","Hiragino Sans GB","Microsoft YaHei",-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,sans-serif',
- fontSize: 14,
- fontSizeHeading1: 30,
- fontSizeHeading2: 24,
- fontSizeHeading3: 19,
- lineWidth: 1,
- wireframe: false
- },
- components: {
- Button: {
- primaryShadow: '0 10px 26px rgba(' + accentRgb() + ',0.26)',
- defaultShadow: 'none',
- dangerShadow: 'none',
- fontWeight: 600,
- paddingInline: 18
- },
- Card: {
- headerBg: 'transparent',
- colorBorderSecondary: '#e2dacb',
- paddingLG: 18
- },
- Segmented: {
- itemSelectedBg: '#fffdf8',
- trackBg: '#efe9dc'
- },
- Table: {
- headerBg: '#f6f2e9',
- headerColor: '#7d7466',
- borderColor: '#e2dacb'
- },
- Modal: {
- contentBg: '#fffdf8',
- headerBg: 'transparent'
- },
- Drawer: {
- colorBgElevated: '#fffdf8'
- },
- Tabs: {
- itemColor: '#7d7466',
- itemSelectedColor: '#17140f',
- inkBarColor: accent()
- },
- Steps: {
- colorPrimary: accent(),
- navArrowColor: '#e2dacb'
- },
- Input: {
- activeBorderColor: '#17140f',
- hoverBorderColor: '#c3baa9'
- },
- InputNumber: {
- activeBorderColor: '#17140f',
- hoverBorderColor: '#c3baa9'
- },
- Select: {
- optionSelectedBg: accentSoft(),
- optionSelectedColor: '#17140f'
- },
- Slider: {
- railBg: '#e2dacb',
- railHoverBg: '#d3c9b7'
- },
- Switch: {
- colorPrimary: accent(),
- colorPrimaryHover: accent()
- },
- Upload: {
- colorPrimary: accent()
- },
- Empty: {
- colorTextDescription: '#a79d8d'
- },
- Statistic: {
- contentFontSize: 30
- },
- Tag: {
- defaultBg: '#fffdf8',
- defaultColor: '#2c271f'
- }
- }
- };
- }
- let theme = buildTheme();
- function syncThemeTokens() { theme = buildTheme(); } // 换肤:重读 CSS 变量并重建 theme
- document.addEventListener('pl:theme', function () {
- syncThemeTokens();
- retheme();
- });
- const roots = new Map();
- const lastRender = new Map();
- function currentLocale() {
- const lang = window.PL && window.PL.i18n ? window.PL.i18n.getLang() : 'zh';
- if (!antd.locale) return undefined;
- return lang === 'en' && antd.locale.enUS ? antd.locale.enUS : antd.locale.zhCN;
- }
- function render(container, element) {
- if (!container) return;
- let root = roots.get(container);
- if (!root || !container.isConnected) {
- root = ReactDOM.createRoot(container);
- roots.set(container, root);
- }
- lastRender.set(container, element);
- root.render(h(antd.ConfigProvider, { theme: theme, prefixCls: 'ant', locale: currentLocale() }, element));
- }
- function retheme() {
- roots.forEach(function (root, container) {
- if (!container || !container.isConnected) return;
- root.render(h(antd.ConfigProvider, { theme: theme, prefixCls: 'ant', locale: currentLocale() }, lastRender.get(container)));
- });
- }
- function reset(container) {
- const root = roots.get(container);
- if (root) {
- root.unmount();
- roots.delete(container);
- }
- }
- function pickValue(v) {
- if (v && v.target && 'value' in v.target) return v.target.value;
- return v;
- }
- function hydrate(root) {
- if (!root) return;
- root.querySelectorAll('[data-ui]').forEach(function (el) {
- const name = el.getAttribute('data-ui');
- const Comp = antd[name];
- if (!Comp) return;
- let props = {};
- try {
- props = JSON.parse(el.getAttribute('data-props') || '{}');
- } catch (e) {
- props = {};
- }
- const text = el.getAttribute('data-text');
- const event = el.getAttribute('data-on');
- const handlers = {};
- if (event) {
- handlers[event] = function (v) {
- el.dispatchEvent(new CustomEvent('antd:' + event, { detail: { value: pickValue(v) }, bubbles: true }));
- };
- }
- const children = text != null ? text : (el.textContent.trim() || undefined);
- const iconHtml = el.getAttribute('data-icon-html');
- if (iconHtml) {
- props.icon = h('span', { className: 'ant-btn-icon', dangerouslySetInnerHTML: { __html: iconHtml } });
- }
- render(el, h(Comp, Object.assign({}, props, handlers), children));
- });
- }
- window.PL = window.PL || {};
- window.PL.ui = {
- h: h,
- render: render,
- reset: reset,
- hydrate: hydrate,
- retheme: retheme,
- // 换肤:CSS 变量变化后同步 antd token 并重渲染全部挂载点
- syncTheme: function () { syncThemeTokens(); retheme(); },
- antd: antd,
- get theme() { return theme; },
- message: antd.message,
- notification: antd.notification,
- get locale() { return currentLocale(); }
- };
- })();
|