-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcalculate_edge.ts
More file actions
217 lines (193 loc) · 7.1 KB
/
calculate_edge.ts
File metadata and controls
217 lines (193 loc) · 7.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
// ░░░░░░░░▄▀░█▀▄░█▀▀░█▀▀░█░█░█░░░█▀█░█▀▄░░░░░█░░░█▀█░█░█░█▀█░█░█░▀█▀░▀▄░░░░░░░░
// ░░░░░░░▀▄░░█▀▄░█▀▀░█░█░█░█░█░░░█▀█░█▀▄░▀▀▀░█░░░█▀█░░█░░█░█░█░█░░█░░░▄▀░░░░░░░
// ░░░░░░░░░▀░▀░▀░▀▀▀░▀▀▀░▀▀▀░▀▀▀░▀░▀░▀░▀░░░░░▀▀▀░▀░▀░░▀░░▀▀▀░▀▀▀░░▀░░▀░░░░░░░░░
// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
// ┃ * Copyright (c) 2026, the Regular Layout Authors. This file is part * ┃
// ┃ * of the Regular Layout library, distributed under the terms of the * ┃
// ┃ * [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). * ┃
// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
import { DEFAULT_PHYSICS, type Physics } from "./constants";
import { insert_child } from "./insert_child";
import type { Layout, LayoutPath, Orientation, ViewWindow } from "./types";
/**
* Calculates an insertion point (which may involve splitting a single
* `"child-panel"` into a new `"split-panel"`), based on the cursor position.
* *
* @param col - The cursor column.
* @param row - The cursor row.
* @param panel - The `Layout` to insert into.
* @param slot - The slot identifier where the insert should occur
* @param drop_target - The `LayoutPath` (from `calculateIntersect`) of the
* panel to either insert next to, or split by.
* @returns A new `LayoutPath` reflecting the updated (maybe) `"split-panel"`,
* which is enough to draw the overlay.
*/
export function calculate_edge(
col: number,
row: number,
panel: Layout,
slot: string,
drop_target: LayoutPath,
box?: DOMRect,
physics: Physics = DEFAULT_PHYSICS,
): LayoutPath {
// Check root edges first
if (col < physics.SPLIT_ROOT_EDGE_TOLERANCE) {
return insert_root_edge(panel, slot, drop_target, [0], true, "horizontal");
}
if (col > 1 - physics.SPLIT_ROOT_EDGE_TOLERANCE) {
return insert_root_edge(
panel,
slot,
drop_target,
drop_target.path.length > 0 ? drop_target.path : [1],
false,
"horizontal",
);
}
if (row < physics.SPLIT_ROOT_EDGE_TOLERANCE) {
return insert_root_edge(panel, slot, drop_target, [0], true, "vertical");
}
if (row > 1 - physics.SPLIT_ROOT_EDGE_TOLERANCE) {
return insert_root_edge(
panel,
slot,
drop_target,
drop_target.path.length > 0 ? drop_target.path : [1],
false,
"vertical",
);
}
// Check panel edges
const is_column_edge =
drop_target.column_offset < physics.SPLIT_EDGE_TOLERANCE ||
drop_target.column_offset > 1 - physics.SPLIT_EDGE_TOLERANCE;
const is_row_edge =
drop_target.row_offset < physics.SPLIT_EDGE_TOLERANCE ||
drop_target.row_offset > 1 - physics.SPLIT_EDGE_TOLERANCE;
// If both edges triggered, choose closer axis
if (is_column_edge && is_row_edge) {
const col_distance = Math.abs(drop_target.column_offset - 0.5);
const row_distance = Math.abs(drop_target.row_offset - 0.5);
const col_scale =
(box?.width || 1) *
(drop_target.view_window.col_end - drop_target.view_window.col_start);
const row_scale =
(box?.height || 1) *
(drop_target.view_window.row_end - drop_target.view_window.row_start);
const use_column = col_distance * col_scale > row_distance * row_scale;
return insert_axis(
panel,
slot,
drop_target,
use_column
? drop_target.column_offset < physics.SPLIT_EDGE_TOLERANCE
: drop_target.row_offset < physics.SPLIT_EDGE_TOLERANCE,
use_column ? "horizontal" : "vertical",
);
}
if (is_column_edge) {
return insert_axis(
panel,
slot,
drop_target,
drop_target.column_offset < physics.SPLIT_EDGE_TOLERANCE,
"horizontal",
);
}
if (is_row_edge) {
return insert_axis(
panel,
slot,
drop_target,
drop_target.row_offset < physics.SPLIT_EDGE_TOLERANCE,
"vertical",
);
}
// Not at an edge - insert as a tab
return {
...drop_target,
path: [...drop_target.path, 0],
};
}
function insert_root_edge(
panel: Layout,
slot: string,
drop_target: LayoutPath,
path: number[],
is_before: boolean,
orientation: Orientation,
): LayoutPath {
return insert_axis(
panel,
slot,
{ ...drop_target, path, orientation },
is_before,
orientation,
);
}
function insert_axis(
panel: Layout,
slot: string,
drop_target: LayoutPath,
is_before: boolean,
axis_orientation: Orientation,
): LayoutPath {
let result_path: number[];
if (drop_target.orientation === axis_orientation) {
// Same orientation - insert into existing split
if (drop_target.path.length === 0) {
result_path = [is_before ? 0 : 1];
} else {
const last_index = drop_target.path[drop_target.path.length - 1];
result_path = [
...drop_target.path.slice(0, -1),
is_before ? last_index : last_index + 1,
];
}
} else {
// Different orientation - split the child panel
result_path = [...drop_target.path, is_before ? 0 : 1];
}
const new_panel = insert_child(panel, slot, result_path, axis_orientation);
const view_window = calculate_view_window(new_panel, result_path);
return {
...drop_target,
path: result_path,
slot: drop_target.slot,
is_edge: true,
orientation: axis_orientation,
view_window,
};
}
function calculate_view_window(panel: Layout, path: number[]): ViewWindow {
let view_window: ViewWindow = {
row_start: 0,
row_end: 1,
col_start: 0,
col_end: 1,
};
let current_panel = panel;
for (const step of path) {
if (current_panel.type === "child-panel") {
break;
}
const index = Math.min(step, current_panel.children.length - 1);
const is_vertical = current_panel.orientation === "vertical";
const start_key = is_vertical ? "row_start" : "col_start";
const end_key = is_vertical ? "row_end" : "col_end";
const total_size = view_window[end_key] - view_window[start_key];
const offset = current_panel.sizes
.slice(0, index)
.reduce((sum, size) => sum + size * total_size, view_window[start_key]);
view_window = {
...view_window,
[start_key]: offset,
[end_key]: offset + total_size * current_panel.sizes[index],
};
current_panel = current_panel.children[index];
}
return view_window;
}