|
|
@@ -1,4 +1,4 @@
|
|
|
-import {useContextSection} from '@hooks';
|
|
|
+import {useContext, useContextSection} from '@hooks';
|
|
|
import {context} from '../context';
|
|
|
import {Modal, TabPaneProps, TabsProps} from 'antd';
|
|
|
import {ReactNode} from 'react';
|
|
|
@@ -6,6 +6,8 @@ import css from './index.module.css';
|
|
|
import {useStore} from 'zustand';
|
|
|
import {tabStore} from '@stores';
|
|
|
import {useContextMenu, ItemParams} from 'react-contexify';
|
|
|
+import {DragEndEvent} from '@dnd-kit/core';
|
|
|
+import {arrayMove} from '@dnd-kit/sortable';
|
|
|
|
|
|
export type Tab = {
|
|
|
key: string;
|
|
|
@@ -15,10 +17,11 @@ export type Tab = {
|
|
|
export function useTabItems() {
|
|
|
const {host} = location;
|
|
|
|
|
|
- return useContextSection(context, function ([tabs]) {
|
|
|
+ const tabs = useContextSection(context, function ([tabs]) {
|
|
|
return tabs.map<Tab>(function (tab) {
|
|
|
return {
|
|
|
...tab,
|
|
|
+ originalTab: tab,
|
|
|
forceRender: true,
|
|
|
closable: tab.key !== '-1',
|
|
|
animated: true,
|
|
|
@@ -32,6 +35,23 @@ export function useTabItems() {
|
|
|
};
|
|
|
});
|
|
|
});
|
|
|
+ const [originalTab, dispatch] = useContext(context);
|
|
|
+
|
|
|
+ function onDragEnd({active, over}: DragEndEvent) {
|
|
|
+ if (!over) return;
|
|
|
+
|
|
|
+ const {id: fromId} = active,
|
|
|
+ {id: toId} = over;
|
|
|
+ const fromIdx = originalTab.findIndex(val => val.key === fromId),
|
|
|
+ toIdx = originalTab.findIndex(val => val.key === toId);
|
|
|
+
|
|
|
+ dispatch({
|
|
|
+ type: 'SORT',
|
|
|
+ payload: arrayMove(originalTab, fromIdx, toIdx),
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ return [tabs, onDragEnd] as const;
|
|
|
}
|
|
|
|
|
|
export function useTabActive() {
|