init.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import type { SetupVxeTable } from "./types";
  2. import { defineComponent, watch } from "vue";
  3. import { usePreferences } from "@vben/preferences";
  4. import { injectPluginsOptions } from "../plugins-context";
  5. import {
  6. VxeButton,
  7. VxeCheckbox,
  8. VxeIcon,
  9. VxeInput,
  10. VxeLoading,
  11. VxeModal,
  12. VxeNumberInput,
  13. VxePager,
  14. VxeRadioGroup,
  15. VxeSelect,
  16. VxeTooltip,
  17. VxeUI,
  18. VxeUpload
  19. } from "vxe-pc-ui";
  20. import enUS from "vxe-pc-ui/lib/language/en-US";
  21. // 导入默认的语言
  22. import zhCN from "vxe-pc-ui/lib/language/zh-CN";
  23. import { VxeColgroup, VxeColumn, VxeGrid, VxeTable, VxeToolbar } from "vxe-table";
  24. import { extendsDefaultFormatter } from "./extends";
  25. // 是否加载过
  26. let isInit = false;
  27. let tableFormFactory: ((...args: any[]) => any) | undefined;
  28. function normalizeVxeLocale<T extends Record<string, any>>(localeModule: T) {
  29. return (
  30. localeModule &&
  31. typeof localeModule === 'object' &&
  32. 'default' in localeModule
  33. ? localeModule.default
  34. : localeModule
  35. ) as T;
  36. }
  37. export function useTableForm(...args: any[]) {
  38. const pluginsOptions = injectPluginsOptions();
  39. const contextFormFactory = pluginsOptions?.form?.useVbenForm;
  40. const factory = tableFormFactory || contextFormFactory;
  41. if (!factory) {
  42. throw new Error(
  43. 'useTableForm is not initialized. Please provide useVbenForm via setupVbenVxeTable() or providePluginsOptions()',
  44. );
  45. }
  46. return factory(...args);
  47. }
  48. // 部分组件,如果没注册,vxe-table 会报错,这里实际没用组件,只是为了不报错,同时可以减少打包体积
  49. const createVirtualComponent = (name = '') => {
  50. return defineComponent({
  51. name,
  52. });
  53. };
  54. export function initVxeTable() {
  55. if (isInit) {
  56. return;
  57. }
  58. VxeUI.component(VxeTable);
  59. VxeUI.component(VxeColumn);
  60. VxeUI.component(VxeColgroup);
  61. VxeUI.component(VxeGrid);
  62. VxeUI.component(VxeToolbar);
  63. VxeUI.component(VxeButton);
  64. // VxeUI.component(VxeButtonGroup);
  65. VxeUI.component(VxeCheckbox);
  66. // VxeUI.component(VxeCheckboxGroup);
  67. VxeUI.component(createVirtualComponent('VxeForm'));
  68. // VxeUI.component(VxeFormGather);
  69. // VxeUI.component(VxeFormItem);
  70. VxeUI.component(VxeIcon);
  71. VxeUI.component(VxeInput);
  72. // VxeUI.component(VxeList);
  73. VxeUI.component(VxeLoading);
  74. VxeUI.component(VxeModal);
  75. VxeUI.component(VxeNumberInput);
  76. // VxeUI.component(VxeOptgroup);
  77. // VxeUI.component(VxeOption);
  78. VxeUI.component(VxePager);
  79. // VxeUI.component(VxePulldown);
  80. // VxeUI.component(VxeRadio);
  81. // VxeUI.component(VxeRadioButton);
  82. VxeUI.component(VxeRadioGroup);
  83. VxeUI.component(VxeSelect);
  84. // VxeUI.component(VxeSwitch);
  85. // VxeUI.component(VxeTextarea);
  86. VxeUI.component(VxeTooltip);
  87. VxeUI.component(VxeUpload);
  88. isInit = true;
  89. }
  90. export function setupVbenVxeTable(setupOptions: SetupVxeTable) {
  91. const { configVxeTable, useVbenForm: useVbenFormFromParam } = setupOptions;
  92. initVxeTable();
  93. // 优先使用参数传入的 useVbenForm,context 注入在 useTableForm 中获取
  94. if (useVbenFormFromParam) {
  95. tableFormFactory = useVbenFormFromParam;
  96. }
  97. const { isDark, locale } = usePreferences();
  98. const localMap = {
  99. 'zh-CN': normalizeVxeLocale(zhCN),
  100. 'en-US': normalizeVxeLocale(enUS),
  101. };
  102. watch(
  103. [() => isDark.value, () => locale.value],
  104. ([isDarkValue, localeValue]) => {
  105. VxeUI.setTheme(isDarkValue ? 'dark' : 'light');
  106. VxeUI.setI18n(localeValue, localMap[localeValue]);
  107. VxeUI.setLanguage(localeValue);
  108. },
  109. {
  110. immediate: true,
  111. },
  112. );
  113. extendsDefaultFormatter(VxeUI);
  114. configVxeTable(VxeUI);
  115. }