Skip to content

Commit 1b8d84e

Browse files
✨ tab栏点击右键可以关闭当前,关闭其他,关闭左侧,关闭右侧脚本
1 parent 5335869 commit 1b8d84e

File tree

1 file changed

+116
-15
lines changed

1 file changed

+116
-15
lines changed

src/pages/options/routes/script/ScriptEditor.tsx

Lines changed: 116 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ function ScriptEditor() {
149149
const [scriptList, setScriptList] = useState<Script[]>([]);
150150
const [currentScript, setCurrentScript] = useState<Script>();
151151
const [selectSciptButtonAndTab, setSelectSciptButtonAndTab] =
152-
useState<string>();
152+
useState<string>("");
153+
const [rightOperationTab, setRightOperationTab] = useState();
153154
const setShow = (key: visibleItem, show: boolean) => {
154155
Object.keys(visible).forEach((k) => {
155156
visible[k] = false;
@@ -370,6 +371,81 @@ function ScriptEditor() {
370371
};
371372
}, [editors]);
372373

374+
// 对tab点击右键进行的操作
375+
useEffect(() => {
376+
let newEditors = [];
377+
let selectEditorIndex;
378+
// 1 关闭当前, 2关闭其它, 3关闭左侧, 4关闭右侧
379+
if (rightOperationTab) {
380+
// eslint-disable-next-line default-case
381+
switch (rightOperationTab.key) {
382+
case "1":
383+
newEditors = editors.filter(
384+
(item) => item.script.uuid !== rightOperationTab.uuid
385+
);
386+
if (newEditors.length > 0) {
387+
// 还有的话,如果之前有选中的,那么我们还是选中之前的,如果没有选中的我们就选中第一个
388+
if (
389+
rightOperationTab.selectSciptButtonAndTab ===
390+
rightOperationTab.uuid
391+
) {
392+
if (newEditors.length > 0) {
393+
newEditors[0].active = true;
394+
setSelectSciptButtonAndTab(newEditors[0].script.uuid);
395+
}
396+
} else {
397+
setSelectSciptButtonAndTab(
398+
rightOperationTab.selectSciptButtonAndTab
399+
);
400+
// 之前选中的tab
401+
editors.filter((item) => {
402+
if (
403+
item.script.uuid === rightOperationTab.selectSciptButtonAndTab
404+
) {
405+
item.active = true;
406+
} else {
407+
item.active = false;
408+
}
409+
return (
410+
item.script.uuid === rightOperationTab.selectSciptButtonAndTab
411+
);
412+
});
413+
}
414+
}
415+
setEditors([...newEditors]);
416+
break;
417+
// eslint-disable-next-line no-fallthrough
418+
case "2":
419+
// eslint-disable-next-line no-case-declarations, no-redeclare
420+
newEditors = editors.filter(
421+
(item) => item.script.uuid === rightOperationTab.uuid
422+
);
423+
setSelectSciptButtonAndTab(rightOperationTab.uuid);
424+
setEditors([...newEditors]);
425+
break;
426+
case "3":
427+
// eslint-disable-next-line array-callback-return
428+
editors.map((item, index) => {
429+
if (item.script.uuid === rightOperationTab.uuid) {
430+
selectEditorIndex = index;
431+
}
432+
});
433+
newEditors = editors.splice(selectEditorIndex);
434+
setEditors([...newEditors]);
435+
break;
436+
case "4":
437+
// eslint-disable-next-line array-callback-return
438+
editors.map((item, index) => {
439+
if (item.script.uuid === rightOperationTab.uuid) {
440+
selectEditorIndex = index;
441+
}
442+
});
443+
newEditors = editors.splice(0, selectEditorIndex + 1);
444+
setEditors([...newEditors]);
445+
}
446+
}
447+
}, [rightOperationTab]);
448+
373449
return (
374450
<div
375451
className="h-full flex flex-col"
@@ -568,7 +644,7 @@ function ScriptEditor() {
568644
// 如果已经打开则激活
569645
let flag = false;
570646
for (let i = 0; i < editors.length; i += 1) {
571-
if (editors[i].script.id === script.id) {
647+
if (editors[i].script.uuid === script.uuid) {
572648
editors[i].active = true;
573649
flag = true;
574650
} else {
@@ -651,8 +727,10 @@ function ScriptEditor() {
651727
// 如果关闭的是当前激活的, 则激活下一个
652728
if (i === prev.length - 1) {
653729
prev[i - 1].active = true;
730+
setSelectSciptButtonAndTab(prev[i - 1].script.uuid);
654731
} else {
655732
prev[i + 1].active = true;
733+
setSelectSciptButtonAndTab(prev[i - 1].script.uuid);
656734
}
657735
}
658736
prev.splice(i, 1);
@@ -665,20 +743,43 @@ function ScriptEditor() {
665743
destroyOnHide
666744
key={index!.toString()}
667745
title={
668-
<span
669-
style={{
670-
// eslint-disable-next-line no-nested-ternary
671-
color: e.isChanged
672-
? "rgb(var(--orange-5))" // eslint-disable-next-line no-nested-ternary
673-
: e.script.uuid === selectSciptButtonAndTab
674-
? "rgb(var(--green-7))"
675-
: e.active
676-
? "rgb(var(--green-7))"
677-
: "var(--color-text-1)",
678-
}}
746+
<Dropdown
747+
trigger="contextMenu"
748+
position="bl"
749+
droplist={
750+
<Menu
751+
// eslint-disable-next-line no-shadow
752+
onClickMenuItem={(key) => {
753+
setRightOperationTab({
754+
...rightOperationTab,
755+
key,
756+
uuid: e.script.uuid,
757+
selectSciptButtonAndTab,
758+
});
759+
}}
760+
>
761+
<Menu.Item key="1">关闭当前标签页</Menu.Item>
762+
<Menu.Item key="2">关闭其他标签页</Menu.Item>
763+
<Menu.Item key="3">关闭左侧标签页</Menu.Item>
764+
<Menu.Item key="4">关闭右侧标签页</Menu.Item>
765+
</Menu>
766+
}
679767
>
680-
{e.script.name}
681-
</span>
768+
<span
769+
style={{
770+
// eslint-disable-next-line no-nested-ternary
771+
color: e.isChanged
772+
? "rgb(var(--orange-5))" // eslint-disable-next-line no-nested-ternary
773+
: e.script.uuid === selectSciptButtonAndTab
774+
? "rgb(var(--green-7))"
775+
: e.active
776+
? "rgb(var(--green-7))"
777+
: "var(--color-text-1)",
778+
}}
779+
>
780+
{e.script.name}
781+
</span>
782+
</Dropdown>
682783
}
683784
/>
684785
))}

0 commit comments

Comments
 (0)