@wxn0brp/nya-dock 0.0.3 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,57 +1,169 @@
1
- export function movePanel(controller, sourceId, targetId, zone) {
2
- if (!controller._split || sourceId === targetId)
3
- return;
4
- if (controller._split.nodes.length === 0) {
5
- controller._split = {
6
- nodes: [sourceId, targetId],
7
- type: (zone === "left" || zone === "right") ? "row" : "column",
1
+ import { isStack } from "./tabs.js";
2
+ function removeNode(node, id) {
3
+ if (typeof node === "string")
4
+ return node === id ? null : node;
5
+ if (isStack(node)) {
6
+ if (!node.tabs.includes(id))
7
+ return node;
8
+ const tabs = node.tabs.filter(tab => tab !== id);
9
+ if (tabs.length === 0)
10
+ return null;
11
+ if (tabs.length === 1)
12
+ return tabs[0];
13
+ return {
14
+ ...node,
15
+ tabs,
16
+ active: Math.min(node.active, tabs.length - 1),
8
17
  };
9
- return;
10
18
  }
11
- function recursiveRemove(node) {
12
- if (typeof node === "string") {
13
- return node === sourceId ? null : node;
14
- }
15
- const [nodeA, nodeB] = node.nodes;
16
- const newA = recursiveRemove(nodeA);
17
- const newB = recursiveRemove(nodeB);
18
- if (newA && newB) {
19
- if (newA !== nodeA || newB !== nodeB) {
20
- return { ...node, nodes: [newA, newB] };
21
- }
19
+ const first = removeNode(node.nodes[0], id);
20
+ const second = removeNode(node.nodes[1], id);
21
+ if (!first)
22
+ return second;
23
+ if (!second)
24
+ return first;
25
+ if (first !== node.nodes[0] || second !== node.nodes[1])
26
+ return {
27
+ ...node,
28
+ nodes: [
29
+ first,
30
+ second,
31
+ ],
32
+ };
33
+ return node;
34
+ }
35
+ function insertTab(node, targetId, id) {
36
+ if (typeof node === "string") {
37
+ if (node !== targetId)
22
38
  return node;
23
- }
24
- return newA || newB;
39
+ return {
40
+ type: "stack",
41
+ tabs: [
42
+ node,
43
+ id,
44
+ ],
45
+ active: 1,
46
+ };
25
47
  }
26
- const treeAfterRemove = recursiveRemove(controller._split);
27
- const newSplitNode = {
28
- type: (zone === "left" || zone === "right") ? "row" : "column",
29
- nodes: (zone === "left" || zone === "top") ? [sourceId, targetId] : [targetId, sourceId],
48
+ if (isStack(node)) {
49
+ if (!node.tabs.includes(targetId))
50
+ return node;
51
+ if (node.tabs.includes(id))
52
+ return {
53
+ ...node,
54
+ active: node.tabs.indexOf(id),
55
+ };
56
+ const tabs = [
57
+ ...node.tabs,
58
+ id,
59
+ ];
60
+ return {
61
+ ...node,
62
+ tabs,
63
+ active: tabs.length - 1,
64
+ };
65
+ }
66
+ const first = insertTab(node.nodes[0], targetId, id);
67
+ const second = insertTab(node.nodes[1], targetId, id);
68
+ if (first !== node.nodes[0] || second !== node.nodes[1])
69
+ return {
70
+ ...node,
71
+ nodes: [
72
+ first,
73
+ second,
74
+ ],
75
+ };
76
+ return node;
77
+ }
78
+ function buildSplit(sourceId, target, zone) {
79
+ return {
80
+ type: zone === "left" || zone === "right" ? "row" : "column",
81
+ nodes: zone === "left" || zone === "top"
82
+ ? [
83
+ sourceId,
84
+ target,
85
+ ]
86
+ : [
87
+ target,
88
+ sourceId,
89
+ ],
30
90
  };
31
- function recursiveDock(node) {
32
- if (typeof node === "string") {
33
- return node === targetId ? newSplitNode : node;
34
- }
35
- const [nodeA, nodeB] = node.nodes;
36
- if (typeof nodeA === "string" && nodeA === targetId) {
37
- return { ...node, nodes: [newSplitNode, nodeB] };
38
- }
39
- if (typeof nodeB === "string" && nodeB === targetId) {
40
- return { ...node, nodes: [nodeA, newSplitNode] };
41
- }
42
- const newA = recursiveDock(nodeA);
43
- const newB = recursiveDock(nodeB);
44
- if (newA !== nodeA || newB !== nodeB) {
45
- return { ...node, nodes: [newA, newB] };
46
- }
47
- return node;
91
+ }
92
+ function dockAround(node, targetId, sourceId, zone) {
93
+ if (typeof node === "string") {
94
+ if (node !== targetId)
95
+ return node;
96
+ return buildSplit(sourceId, node, zone);
48
97
  }
49
- let finalTree;
50
- if (!treeAfterRemove) {
51
- finalTree = newSplitNode;
98
+ if (isStack(node)) {
99
+ if (!node.tabs.includes(targetId))
100
+ return node;
101
+ return buildSplit(sourceId, node, zone);
52
102
  }
53
- else {
54
- finalTree = recursiveDock(treeAfterRemove);
103
+ const first = dockAround(node.nodes[0], targetId, sourceId, zone);
104
+ const second = dockAround(node.nodes[1], targetId, sourceId, zone);
105
+ if (first !== node.nodes[0] || second !== node.nodes[1])
106
+ return {
107
+ ...node,
108
+ nodes: [
109
+ first,
110
+ second,
111
+ ],
112
+ };
113
+ return node;
114
+ }
115
+ function isSameStack(node, sourceId, targetId) {
116
+ if (isStack(node))
117
+ return node.tabs.includes(sourceId) && node.tabs.includes(targetId);
118
+ if (typeof node === "string")
119
+ return false;
120
+ return (isSameStack(node.nodes[0], sourceId, targetId) ||
121
+ isSameStack(node.nodes[1], sourceId, targetId));
122
+ }
123
+ function reorderTab(node, sourceId, targetId) {
124
+ if (typeof node === "string")
125
+ return node;
126
+ if (isStack(node)) {
127
+ if (!node.tabs.includes(sourceId) || !node.tabs.includes(targetId))
128
+ return node;
129
+ const tabs = node.tabs.filter(tab => tab !== sourceId);
130
+ tabs.splice(tabs.indexOf(targetId), 0, sourceId);
131
+ if (tabs.join() === node.tabs.join())
132
+ return node;
133
+ return {
134
+ ...node,
135
+ tabs,
136
+ active: tabs.indexOf(sourceId),
137
+ };
138
+ }
139
+ const first = reorderTab(node.nodes[0], sourceId, targetId);
140
+ const second = reorderTab(node.nodes[1], sourceId, targetId);
141
+ if (first !== node.nodes[0] || second !== node.nodes[1])
142
+ return {
143
+ ...node,
144
+ nodes: [
145
+ first,
146
+ second,
147
+ ],
148
+ };
149
+ return node;
150
+ }
151
+ export function movePanel(controller, sourceId, targetId, zone) {
152
+ if (!controller._split || sourceId === targetId)
153
+ return;
154
+ if (zone === "center" && isSameStack(controller._split, sourceId, targetId)) {
155
+ const reordered = reorderTab(controller._split, sourceId, targetId);
156
+ if (reordered !== controller._split)
157
+ controller._split = reordered;
158
+ return;
55
159
  }
56
- controller._split = finalTree;
160
+ const withoutSource = removeNode(controller._split, sourceId);
161
+ if (!withoutSource)
162
+ return;
163
+ const tree = zone === "center"
164
+ ? insertTab(withoutSource, targetId, sourceId)
165
+ : dockAround(withoutSource, targetId, sourceId, zone);
166
+ if (typeof tree === "string")
167
+ return;
168
+ controller._split = tree;
57
169
  }
@@ -0,0 +1,8 @@
1
+ import { NyaNode, NyaSplit, NyaStack, StructStack } from "../types.js";
2
+ export declare function isStack(node: unknown): node is NyaStack;
3
+ export declare function isStructStack(node: unknown): node is StructStack;
4
+ export declare function toStack(struct: StructStack): NyaStack;
5
+ export declare function activateTab(tree: NyaNode | NyaSplit | NyaStack, panelId: string): {
6
+ tree: NyaNode | NyaSplit | NyaStack;
7
+ active: number;
8
+ };
@@ -0,0 +1,71 @@
1
+ export function isStack(node) {
2
+ return (typeof node === "object" &&
3
+ node !== null &&
4
+ node.type === "stack");
5
+ }
6
+ export function isStructStack(node) {
7
+ return (typeof node === "object" &&
8
+ node !== null &&
9
+ Array.isArray(node.tabs));
10
+ }
11
+ export function toStack(struct) {
12
+ return {
13
+ type: "stack",
14
+ tabs: [
15
+ ...struct.tabs,
16
+ ],
17
+ active: struct.active ?? 0,
18
+ };
19
+ }
20
+ export function activateTab(tree, panelId) {
21
+ if (typeof tree === "string")
22
+ return {
23
+ tree,
24
+ active: -1,
25
+ };
26
+ if (isStack(tree)) {
27
+ const index = tree.tabs.indexOf(panelId);
28
+ if (index === -1 || index === tree.active)
29
+ return {
30
+ tree,
31
+ active: -1,
32
+ };
33
+ return {
34
+ tree: {
35
+ ...tree,
36
+ active: index,
37
+ },
38
+ active: index,
39
+ };
40
+ }
41
+ const first = activateTab(tree.nodes[0], panelId);
42
+ if (first.active !== -1) {
43
+ return {
44
+ tree: {
45
+ ...tree,
46
+ nodes: [
47
+ first.tree,
48
+ tree.nodes[1],
49
+ ],
50
+ },
51
+ active: first.active,
52
+ };
53
+ }
54
+ const second = activateTab(tree.nodes[1], panelId);
55
+ if (second.active !== -1) {
56
+ return {
57
+ tree: {
58
+ ...tree,
59
+ nodes: [
60
+ tree.nodes[0],
61
+ second.tree,
62
+ ],
63
+ },
64
+ active: second.active,
65
+ };
66
+ }
67
+ return {
68
+ tree,
69
+ active: -1,
70
+ };
71
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/nya-dock",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "description": "",
@@ -13,9 +13,13 @@
13
13
  "license": "MIT",
14
14
  "type": "module",
15
15
  "devDependencies": {
16
- "sass": "*",
17
- "tsc-alias": "*",
18
- "typescript": "*"
16
+ "@wxn0brp/biome": "*",
17
+ "sass": "^1",
18
+ "tsc-alias": "^1",
19
+ "typescript": "^7"
20
+ },
21
+ "dependencies": {
22
+ "@wxn0brp/flanker-ui": "^0.5.3"
19
23
  },
20
24
  "files": [
21
25
  "dist"
@@ -26,13 +30,11 @@
26
30
  "import": "./dist/index.js",
27
31
  "default": "./dist/index.js"
28
32
  },
33
+ "./style.css": "./dist/style.css",
29
34
  "./*": {
30
35
  "types": "./dist/*.d.ts",
31
36
  "import": "./dist/*.js",
32
37
  "default": "./dist/*.js"
33
38
  }
34
- },
35
- "dependencies": {
36
- "@wxn0brp/flanker-ui": "^0.4.3"
37
39
  }
38
40
  }