TabContent.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { TabItem, tabStore } from '/@/store/modules/tab';
  2. import type { PropType } from 'vue';
  3. import { getScaleAction, TabContentProps } from './tab.data';
  4. import { defineComponent, unref, computed } from 'vue';
  5. import { Dropdown } from '/@/components/Dropdown/index';
  6. import Icon from '/@/components/Icon/index';
  7. import { DoubleRightOutlined } from '@ant-design/icons-vue';
  8. import { appStore } from '/@/store/modules/app';
  9. import { TabContentEnum } from './tab.data';
  10. import { useTabDropdown } from './useTabDropdown';
  11. export default defineComponent({
  12. name: 'TabContent',
  13. props: {
  14. tabItem: {
  15. type: Object as PropType<TabItem>,
  16. default: null,
  17. },
  18. type: {
  19. type: Number as PropType<number>,
  20. default: TabContentEnum.TAB_TYPE,
  21. },
  22. trigger: {
  23. type: Array as PropType<string[]>,
  24. default: () => {
  25. return ['contextmenu'];
  26. },
  27. },
  28. },
  29. setup(props) {
  30. const getProjectConfigRef = computed(() => {
  31. return appStore.getProjectConfig;
  32. });
  33. const getIsScaleRef = computed(() => {
  34. const {
  35. menuSetting: { show: showMenu },
  36. headerSetting: { show: showHeader },
  37. } = unref(getProjectConfigRef);
  38. return !showMenu && !showHeader;
  39. });
  40. function handleContextMenu(e: Event) {
  41. if (!props.tabItem) return;
  42. const tableItem = props.tabItem;
  43. e.preventDefault();
  44. const index = unref(tabStore.getTabsState).findIndex((tab) => tab.path === tableItem.path);
  45. tabStore.commitCurrentContextMenuIndexState(index);
  46. tabStore.commitCurrentContextMenuState(props.tabItem);
  47. }
  48. /**
  49. * @description: 渲染图标
  50. */
  51. function renderIcon() {
  52. const { tabItem } = props;
  53. if (!tabItem) return;
  54. const icon = tabItem.meta && tabItem.meta.icon;
  55. if (!icon || !unref(getProjectConfigRef).multiTabsSetting.showIcon) return null;
  56. return <Icon icon={icon} class="align-middle " style={{ marginBottom: '2px' }} />;
  57. }
  58. function renderTabContent() {
  59. const { tabItem: { meta } = {} } = props;
  60. return (
  61. <div class={`multiple-tabs-content__content `} onContextmenu={handleContextMenu}>
  62. {renderIcon()}
  63. <span class="ml-1">{meta && meta.title}</span>
  64. </div>
  65. );
  66. }
  67. function renderExtraContent() {
  68. return (
  69. <span class={`multiple-tabs-content__extra `}>
  70. <DoubleRightOutlined />
  71. </span>
  72. );
  73. }
  74. const { getDropMenuList, handleMenuEvent } = useTabDropdown(props as TabContentProps);
  75. return () => {
  76. const { trigger, type } = props;
  77. const {
  78. multiTabsSetting: { showQuick },
  79. } = unref(getProjectConfigRef);
  80. const isTab = !showQuick ? true : type === TabContentEnum.TAB_TYPE;
  81. const scaleAction = getScaleAction(
  82. unref(getIsScaleRef) ? '缩小' : '放大',
  83. unref(getIsScaleRef)
  84. );
  85. const dropMenuList = unref(getDropMenuList) || [];
  86. return (
  87. <Dropdown
  88. dropMenuList={!isTab ? [scaleAction, ...dropMenuList] : dropMenuList}
  89. trigger={isTab ? trigger : ['hover']}
  90. onMenuEvent={handleMenuEvent}
  91. >
  92. {() => (isTab ? renderTabContent() : renderExtraContent())}
  93. </Dropdown>
  94. );
  95. };
  96. },
  97. });