Skip to content

Commit 7ec9499

Browse files
committed
fix: useCloseTab not working as intended
1 parent 09091e5 commit 7ec9499

File tree

6 files changed

+257
-223
lines changed

6 files changed

+257
-223
lines changed

src/App.vue

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
<script setup lang="ts">
2-
import { Direction } from "./lib/types";
2+
import { Direction, Tab } from "./lib/types";
33
import useLayout, { createLayout, createTab } from "./lib/useLayout";
44
import VLayout from "./lib/VLayout.vue";
55
66
const { tree, actions } = useLayout(
77
createLayout({
88
direction: Direction.Column,
9-
id: "1",
109
children: [
1110
createLayout({
12-
id: "1-1",
11+
children: [createTab({ title: "Tab 1" }), createTab({ title: "Tab 2" })],
12+
}),
13+
createLayout({
1314
children: [
14-
createTab({ title: "Tab 1", id: "1" }),
15-
createTab({ title: "Tab 2", id: "2" }),
16-
createTab({ title: "Tab 3", id: "3" }),
15+
createLayout({
16+
children: [createTab({ title: "Tab 3" }), createTab({ title: "Tab 4" })],
17+
}),
18+
createLayout({
19+
children: [createTab({ title: "Tab 5" }), createTab({ title: "Tab 6" })],
20+
}),
1721
],
1822
}),
1923
createLayout({
20-
id: "1-2",
21-
children: [createTab({ title: "Hello", id: "4" })],
24+
children: [createTab({ title: "Tab 7" })],
2225
}),
2326
],
2427
})
@@ -29,7 +32,7 @@ const { tree, actions } = useLayout(
2932
<VLayout :template="tree" :actions="actions">
3033
<template #tab="props">
3134
<div>
32-
{{ props.id }} => Lorem ipsum dolor sit amet, consectetur adipisicing elit. Perspiciatis
35+
{{ props.title }} => Lorem ipsum dolor sit amet, consectetur adipisicing elit. Perspiciatis
3336
quis eveniet consequuntur vel! Ducimus, eligendi? Et laboriosam reiciendis magni quidem cum,
3437
iste veritatis nam vero accusantium? Odit, facilis. Ipsam, at.
3538
</div>

src/lib/VLayout.vue

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
DraggedTab,
1111
TabTemplate,
1212
} from "./types";
13-
import { getType, getTab, createTab, findUiByPath, getRoot } from "./useLayout";
13+
import { getType, getTab, createTab, findUiByPath, getRoot, findTab } from "./useLayout";
1414
import VTabButton from "./VTabButton.vue";
1515
import VTabContent from "./VTabContent.vue";
1616
@@ -45,7 +45,6 @@ const close = (id: string) => {
4545
};
4646
4747
const dropped = ({ side, ev }: { ev: DragEvent; side: Side }) => {
48-
// TODO : create tab with events.onDrop
4948
let data: Record<string, unknown> = {};
5049
let tab: TabTemplate | undefined = undefined;
5150
@@ -56,28 +55,27 @@ const dropped = ({ side, ev }: { ev: DragEvent; side: Side }) => {
5655
if (
5756
data.type === UIType.Tab &&
5857
data.signature === "__dragged__tab__" &&
59-
typeof data.id === "string" &&
60-
Array.isArray(data.parents)
58+
typeof data.id === "string"
6159
) {
6260
const $data = data as unknown as DraggedTab;
63-
// TODO : we should remove the tab from its original position and create it here
6461
65-
const $tab = findUiByPath(
66-
$data.id,
67-
$data.parents,
68-
getRoot(template) as unknown as Layout<Layout>
69-
) as Tab;
62+
const $exist = findTab(data.id, template);
7063
71-
actions.useCloseTab($tab.id, $tab.parent);
64+
if ($exist && template.children.length === 1) {
65+
return;
66+
}
67+
68+
const $tab = findTab($data.id, getRoot(template));
7269
7370
tab = createTab($data);
71+
72+
if (tab && $tab) {
73+
actions.useOnDrop(tab, template as Layout, side);
74+
actions.useCloseTab($tab.id, $tab.parent);
75+
}
7476
} else {
7577
// TODO : allow user to create tab with drag event
7678
}
77-
78-
if (tab) {
79-
actions.useOnDrop(tab, template as Layout, side);
80-
}
8179
};
8280
</script>
8381

@@ -121,6 +119,7 @@ const dropped = ({ side, ev }: { ev: DragEvent; side: Side }) => {
121119
display: flex;
122120
flex-direction: column;
123121
flex: 1;
122+
padding: 2px;
124123
}
125124
126125
.clv__tabs-container {

src/lib/VTabButton.vue

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,12 @@ const close = () => {
1515
};
1616
1717
const onDragStart = (e: DragEvent) => {
18-
const parents = getParentsHierarchy(item).reverse().slice(1);
19-
2018
const data: DraggedTab = {
2119
id: item.id,
2220
type: UIType.Tab,
2321
title: item.title,
2422
data: item.data,
2523
signature: "__dragged__tab__",
26-
parents,
2724
};
2825
2926
e.dataTransfer?.setData("text/plain", JSON.stringify(data));

0 commit comments

Comments
 (0)