hooks.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import {storeToRefs, useTabStore} from '@stores';
  2. import {useSortable} from '@vueuse/integrations/useSortable';
  3. import {ref} from 'vue';
  4. import {type Options} from 'sortablejs';
  5. import ContextMenu from '@imengyu/vue3-context-menu';
  6. import {useI18n} from 'vue-i18n';
  7. export function useTabSrotable() {
  8. const state = useTabStore();
  9. const {tabList} = storeToRefs(state);
  10. const tabRef = ref<HTMLUListElement>();
  11. useSortable(
  12. tabRef,
  13. tabList,
  14. {animation: 150} as Options,
  15. );
  16. return {tabRef};
  17. }
  18. export function useContextMenu() {
  19. const tabStore = useTabStore();
  20. function remove(key: string) {
  21. tabStore.dispatch({type: 'REMOVE', payload: key});
  22. }
  23. const {t} = useI18n();
  24. function onContextMenu(e: MouseEvent) {
  25. e.preventDefault();
  26. const target = e.target as HTMLLIElement;
  27. const key = target.getAttribute('data-key');
  28. if (!key || key === '-1') return;
  29. ContextMenu.showContextMenu({
  30. x: e.x,
  31. y: e.y,
  32. items: [
  33. {
  34. label: t('home.contextMenu.close'),
  35. onClick: remove.bind(null, key),
  36. },
  37. {
  38. label: t('home.contextMenu.closeRight'),
  39. onClick() {
  40. tabStore.dispatch({type: 'REMOVE_RIGHT', payload: key});
  41. },
  42. },
  43. {
  44. label: t('home.contextMenu.closeOther'),
  45. onClick() {
  46. tabStore.dispatch({type: 'REMOVE_OTHER', payload: key});
  47. },
  48. },
  49. {
  50. label: t('home.contextMenu.clear'),
  51. onClick() {
  52. tabStore.dispatch({type: 'CLEAR'});
  53. },
  54. },
  55. ],
  56. });
  57. }
  58. return {onContextMenu, remove};
  59. }