antd-bridge.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. (function () {
  2. const React = window.React;
  3. const ReactDOM = window.ReactDOM;
  4. const antd = window.antd;
  5. const h = React.createElement;
  6. function cssVar(name, fallback) {
  7. try {
  8. const v = getComputedStyle(document.documentElement).getPropertyValue(name).trim();
  9. return v || fallback;
  10. } catch (e) { return fallback; }
  11. }
  12. function accent() { return cssVar('--accent', '#ff5c2b'); }
  13. function accentDark() { return cssVar('--accent-dark', '#e64718'); }
  14. function accentSoft() { return cssVar('--accent-soft', '#ffe0d2'); }
  15. function accentRgb() { return cssVar('--accent-rgb', '255,92,43'); }
  16. function buildTheme() {
  17. // 每次构造全新对象:antd v5 cssinjs 按 theme 的 hash 缓存注入样式,
  18. // 原地 mutate 不会重算 hash(换肤后会残留旧主色的样式),故必须返回新对象。
  19. return {
  20. token: {
  21. colorPrimary: accent(),
  22. colorInfo: accent(),
  23. colorSuccess: '#4f9d69',
  24. colorWarning: '#e0a83a',
  25. colorError: accentDark(),
  26. colorTextBase: '#17140f',
  27. colorText: '#2c271f',
  28. colorTextSecondary: '#7d7466',
  29. colorTextTertiary: '#a79d8d',
  30. colorTextQuaternary: '#c3baa9',
  31. colorBorder: '#e2dacb',
  32. colorBorderSecondary: '#efe9dc',
  33. colorBgContainer: '#fffdf8',
  34. colorBgElevated: '#fffdf8',
  35. colorBgLayout: '#f6f2e9',
  36. colorBgSpotlight: '#17140f',
  37. colorFillQuaternary: 'rgba(23,20,15,0.05)',
  38. colorFillTertiary: 'rgba(23,20,15,0.03)',
  39. borderRadius: 10,
  40. borderRadiusLG: 16,
  41. borderRadiusSM: 8,
  42. borderRadiusXS: 6,
  43. controlHeight: 36,
  44. controlHeightSM: 28,
  45. controlHeightLG: 44,
  46. fontFamily: '"PingFang SC","Hiragino Sans GB","Microsoft YaHei",-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,sans-serif',
  47. fontSize: 14,
  48. fontSizeHeading1: 30,
  49. fontSizeHeading2: 24,
  50. fontSizeHeading3: 19,
  51. lineWidth: 1,
  52. wireframe: false
  53. },
  54. components: {
  55. Button: {
  56. primaryShadow: '0 10px 26px rgba(' + accentRgb() + ',0.26)',
  57. defaultShadow: 'none',
  58. dangerShadow: 'none',
  59. fontWeight: 600,
  60. paddingInline: 18
  61. },
  62. Card: {
  63. headerBg: 'transparent',
  64. colorBorderSecondary: '#e2dacb',
  65. paddingLG: 18
  66. },
  67. Segmented: {
  68. itemSelectedBg: '#fffdf8',
  69. trackBg: '#efe9dc'
  70. },
  71. Table: {
  72. headerBg: '#f6f2e9',
  73. headerColor: '#7d7466',
  74. borderColor: '#e2dacb'
  75. },
  76. Modal: {
  77. contentBg: '#fffdf8',
  78. headerBg: 'transparent'
  79. },
  80. Drawer: {
  81. colorBgElevated: '#fffdf8'
  82. },
  83. Tabs: {
  84. itemColor: '#7d7466',
  85. itemSelectedColor: '#17140f',
  86. inkBarColor: accent()
  87. },
  88. Steps: {
  89. colorPrimary: accent(),
  90. navArrowColor: '#e2dacb'
  91. },
  92. Input: {
  93. activeBorderColor: '#17140f',
  94. hoverBorderColor: '#c3baa9'
  95. },
  96. InputNumber: {
  97. activeBorderColor: '#17140f',
  98. hoverBorderColor: '#c3baa9'
  99. },
  100. Select: {
  101. optionSelectedBg: accentSoft(),
  102. optionSelectedColor: '#17140f'
  103. },
  104. Slider: {
  105. railBg: '#e2dacb',
  106. railHoverBg: '#d3c9b7'
  107. },
  108. Switch: {
  109. colorPrimary: accent(),
  110. colorPrimaryHover: accent()
  111. },
  112. Upload: {
  113. colorPrimary: accent()
  114. },
  115. Empty: {
  116. colorTextDescription: '#a79d8d'
  117. },
  118. Statistic: {
  119. contentFontSize: 30
  120. },
  121. Tag: {
  122. defaultBg: '#fffdf8',
  123. defaultColor: '#2c271f'
  124. }
  125. }
  126. };
  127. }
  128. let theme = buildTheme();
  129. function syncThemeTokens() { theme = buildTheme(); } // 换肤:重读 CSS 变量并重建 theme
  130. document.addEventListener('pl:theme', function () {
  131. syncThemeTokens();
  132. retheme();
  133. });
  134. const roots = new Map();
  135. const lastRender = new Map();
  136. function currentLocale() {
  137. const lang = window.PL && window.PL.i18n ? window.PL.i18n.getLang() : 'zh';
  138. if (!antd.locale) return undefined;
  139. return lang === 'en' && antd.locale.enUS ? antd.locale.enUS : antd.locale.zhCN;
  140. }
  141. function render(container, element) {
  142. if (!container) return;
  143. let root = roots.get(container);
  144. if (!root || !container.isConnected) {
  145. root = ReactDOM.createRoot(container);
  146. roots.set(container, root);
  147. }
  148. lastRender.set(container, element);
  149. root.render(h(antd.ConfigProvider, { theme: theme, prefixCls: 'ant', locale: currentLocale() }, element));
  150. }
  151. function retheme() {
  152. roots.forEach(function (root, container) {
  153. if (!container || !container.isConnected) return;
  154. root.render(h(antd.ConfigProvider, { theme: theme, prefixCls: 'ant', locale: currentLocale() }, lastRender.get(container)));
  155. });
  156. }
  157. function reset(container) {
  158. const root = roots.get(container);
  159. if (root) {
  160. root.unmount();
  161. roots.delete(container);
  162. }
  163. }
  164. function pickValue(v) {
  165. if (v && v.target && 'value' in v.target) return v.target.value;
  166. return v;
  167. }
  168. function hydrate(root) {
  169. if (!root) return;
  170. root.querySelectorAll('[data-ui]').forEach(function (el) {
  171. const name = el.getAttribute('data-ui');
  172. const Comp = antd[name];
  173. if (!Comp) return;
  174. let props = {};
  175. try {
  176. props = JSON.parse(el.getAttribute('data-props') || '{}');
  177. } catch (e) {
  178. props = {};
  179. }
  180. const text = el.getAttribute('data-text');
  181. const event = el.getAttribute('data-on');
  182. const handlers = {};
  183. if (event) {
  184. handlers[event] = function (v) {
  185. el.dispatchEvent(new CustomEvent('antd:' + event, { detail: { value: pickValue(v) }, bubbles: true }));
  186. };
  187. }
  188. const children = text != null ? text : (el.textContent.trim() || undefined);
  189. const iconHtml = el.getAttribute('data-icon-html');
  190. if (iconHtml) {
  191. props.icon = h('span', { className: 'ant-btn-icon', dangerouslySetInnerHTML: { __html: iconHtml } });
  192. }
  193. render(el, h(Comp, Object.assign({}, props, handlers), children));
  194. });
  195. }
  196. window.PL = window.PL || {};
  197. window.PL.ui = {
  198. h: h,
  199. render: render,
  200. reset: reset,
  201. hydrate: hydrate,
  202. retheme: retheme,
  203. // 换肤:CSS 变量变化后同步 antd token 并重渲染全部挂载点
  204. syncTheme: function () { syncThemeTokens(); retheme(); },
  205. antd: antd,
  206. get theme() { return theme; },
  207. message: antd.message,
  208. notification: antd.notification,
  209. get locale() { return currentLocale(); }
  210. };
  211. })();