init.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import type { SetupVxeTable } from './types';
  2. import { defineComponent, watch } from 'vue';
  3. import { usePreferences } from '@vben/preferences';
  4. import { useVbenForm } from '@vben-core/form-ui';
  5. import {
  6. VxeButton,
  7. VxeCheckbox,
  8. // VxeFormGather,
  9. // VxeForm,
  10. // VxeFormItem,
  11. VxeIcon,
  12. VxeInput,
  13. VxeLoading,
  14. VxeModal,
  15. VxePager,
  16. // VxeList,
  17. // VxeModal,
  18. // VxeOptgroup,
  19. // VxeOption,
  20. // VxePulldown,
  21. // VxeRadio,
  22. // VxeRadioButton,
  23. VxeRadioGroup,
  24. VxeSelect,
  25. VxeTooltip,
  26. VxeUI,
  27. // VxeSwitch,
  28. // VxeTextarea,
  29. } from 'vxe-pc-ui';
  30. import enUS from 'vxe-pc-ui/lib/language/en-US';
  31. // 导入默认的语言
  32. import zhCN from 'vxe-pc-ui/lib/language/zh-CN';
  33. import {
  34. VxeColgroup,
  35. VxeColumn,
  36. VxeGrid,
  37. VxeTable,
  38. VxeToolbar,
  39. } from 'vxe-table';
  40. import { extendsDefaultFormatter } from './extends';
  41. // 是否加载过
  42. let isInit = false;
  43. // eslint-disable-next-line import/no-mutable-exports
  44. export let useTableForm: typeof useVbenForm;
  45. // 部分组件,如果没注册,vxe-table 会报错,这里实际没用组件,只是为了不报错,同时可以减少打包体积
  46. const createVirtualComponent = (name = '') => {
  47. return defineComponent({
  48. name,
  49. });
  50. };
  51. export function initVxeTable() {
  52. if (isInit) {
  53. return;
  54. }
  55. VxeUI.component(VxeTable);
  56. VxeUI.component(VxeColumn);
  57. VxeUI.component(VxeColgroup);
  58. VxeUI.component(VxeGrid);
  59. VxeUI.component(VxeToolbar);
  60. VxeUI.component(VxeButton);
  61. // VxeUI.component(VxeButtonGroup);
  62. VxeUI.component(VxeCheckbox);
  63. // VxeUI.component(VxeCheckboxGroup);
  64. VxeUI.component(createVirtualComponent('VxeForm'));
  65. // VxeUI.component(VxeFormGather);
  66. // VxeUI.component(VxeFormItem);
  67. VxeUI.component(VxeIcon);
  68. VxeUI.component(VxeInput);
  69. // VxeUI.component(VxeList);
  70. VxeUI.component(VxeLoading);
  71. VxeUI.component(VxeModal);
  72. // VxeUI.component(VxeOptgroup);
  73. // VxeUI.component(VxeOption);
  74. VxeUI.component(VxePager);
  75. // VxeUI.component(VxePulldown);
  76. // VxeUI.component(VxeRadio);
  77. // VxeUI.component(VxeRadioButton);
  78. VxeUI.component(VxeRadioGroup);
  79. VxeUI.component(VxeSelect);
  80. // VxeUI.component(VxeSwitch);
  81. // VxeUI.component(VxeTextarea);
  82. VxeUI.component(VxeTooltip);
  83. isInit = true;
  84. }
  85. export function setupVbenVxeTable(setupOptions: SetupVxeTable) {
  86. const { configVxeTable, useVbenForm } = setupOptions;
  87. initVxeTable();
  88. useTableForm = useVbenForm;
  89. const preference = usePreferences();
  90. const localMap = {
  91. 'zh-CN': zhCN,
  92. 'en-US': enUS,
  93. };
  94. watch(
  95. [() => preference.theme.value, () => preference.locale.value],
  96. ([theme, locale]) => {
  97. VxeUI.setTheme(theme === 'dark' ? 'dark' : 'light');
  98. VxeUI.setI18n(locale, localMap[locale]);
  99. VxeUI.setLanguage(locale);
  100. },
  101. {
  102. immediate: true,
  103. },
  104. );
  105. extendsDefaultFormatter(VxeUI);
  106. configVxeTable(VxeUI);
  107. }