1010// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
1111
1212import { calculate_intersection } from "./calculate_intersect" ;
13+ import { SPLIT_EDGE_TOLERANCE } from "./constants" ;
1314import { insert_child } from "./insert_child" ;
14- import {
15- SPLIT_EDGE_TOLERANCE ,
16- type Layout ,
17- type LayoutPath ,
18- } from "./layout_config" ;
15+ import type { Layout , LayoutPath , Orientation } from "./layout_config" ;
1916
2017/**
2118 * Calculates an insertion point (which may involve splitting a single
@@ -30,146 +27,78 @@ import {
3027 * @returns A new `LayoutPath` reflecting the updated (maybe) `"split-panel"`,
3128 * which is enough to draw the overlay.
3229 */
33- function handle_matching_orientation (
30+ export function calculate_edge (
3431 col : number ,
3532 row : number ,
3633 panel : Layout ,
3734 slot : string ,
3835 drop_target : LayoutPath ,
39- is_before : boolean ,
4036) : LayoutPath {
41- if ( drop_target . path . length === 0 ) {
42- const insert_index = is_before ? 0 : 1 ;
43- const new_panel = insert_child ( panel , slot , [ insert_index ] ) ;
44- if ( is_before ) {
45- return calculate_intersection ( col , row , new_panel , false ) ;
46- } else {
47- const new_drop_target = calculate_intersection (
48- col ,
49- row ,
50- new_panel ,
51- false ,
52- ) ;
53- return {
54- ...new_drop_target ,
55- path : [ 0 ] ,
56- } ;
57- }
58- } else {
59- const path_without_last = drop_target . path . slice ( 0 , - 1 ) ;
60- const last_index = drop_target . path [ drop_target . path . length - 1 ] ;
61- const insert_index = is_before ? last_index : last_index + 1 ;
62- const new_panel = insert_child ( panel , slot , [
63- ...path_without_last ,
64- insert_index ,
65- ] ) ;
37+ const is_column_edge =
38+ drop_target . column_offset < SPLIT_EDGE_TOLERANCE ||
39+ drop_target . column_offset > 1 - SPLIT_EDGE_TOLERANCE ;
6640
67- if ( is_before ) {
68- return calculate_intersection ( col , row , new_panel , false ) ;
69- } else {
70- const new_drop_target = calculate_intersection (
71- col ,
72- row ,
73- new_panel ,
74- false ,
75- ) ;
76- return {
77- ...new_drop_target ,
78- path : [ ...path_without_last , last_index ] ,
79- } ;
80- }
41+ const is_row_edge =
42+ drop_target . row_offset < SPLIT_EDGE_TOLERANCE ||
43+ drop_target . row_offset > 1 - SPLIT_EDGE_TOLERANCE ;
44+
45+ if ( is_column_edge ) {
46+ return handle_axis (
47+ col ,
48+ row ,
49+ panel ,
50+ slot ,
51+ drop_target ,
52+ drop_target . column_offset ,
53+ "horizontal" ,
54+ ) ;
55+ } else if ( is_row_edge ) {
56+ return handle_axis (
57+ col ,
58+ row ,
59+ panel ,
60+ slot ,
61+ drop_target ,
62+ drop_target . row_offset ,
63+ "vertical" ,
64+ ) ;
8165 }
82- }
8366
84- function handle_cross_orientation (
85- col : number ,
86- row : number ,
87- panel : Layout ,
88- slot : string ,
89- drop_target : LayoutPath ,
90- insert_index : number ,
91- new_orientation : "horizontal" | "vertical" ,
92- ) : LayoutPath {
93- const original_path = drop_target . path ;
94- const new_panel = insert_child (
95- panel ,
96- slot ,
97- [ ...original_path , insert_index ] ,
98- new_orientation ,
99- ) ;
100- const new_drop_target = calculate_intersection ( col , row , new_panel , false ) ;
101- return {
102- ...new_drop_target ,
103- slot,
104- path : [ ...original_path , insert_index ] ,
105- } ;
67+ return drop_target ;
10668}
10769
108- export function calculate_split (
70+ function handle_axis (
10971 col : number ,
11072 row : number ,
11173 panel : Layout ,
11274 slot : string ,
11375 drop_target : LayoutPath ,
76+ axis_offset : number ,
77+ axis_orientation : Orientation ,
11478) : LayoutPath {
115- const is_column_edge =
116- drop_target . column_offset < SPLIT_EDGE_TOLERANCE ||
117- drop_target . column_offset > 1 - SPLIT_EDGE_TOLERANCE ;
118- const is_row_edge =
119- drop_target . row_offset < SPLIT_EDGE_TOLERANCE ||
120- drop_target . row_offset > 1 - SPLIT_EDGE_TOLERANCE ;
121-
122- if ( is_column_edge ) {
123- const is_before = drop_target . column_offset < SPLIT_EDGE_TOLERANCE ;
124- if ( drop_target . orientation === "horizontal" ) {
125- drop_target = handle_matching_orientation (
126- col ,
127- row ,
128- panel ,
129- slot ,
130- drop_target ,
131- is_before ,
132- ) ;
133- } else {
79+ const is_before = axis_offset < SPLIT_EDGE_TOLERANCE ;
80+ if ( drop_target . orientation === axis_orientation ) {
81+ if ( drop_target . path . length === 0 ) {
13482 const insert_index = is_before ? 0 : 1 ;
135- drop_target = handle_cross_orientation (
136- col ,
137- row ,
138- panel ,
139- slot ,
140- drop_target ,
141- insert_index ,
142- "horizontal" ,
143- ) ;
144- }
145-
146- drop_target . is_edge = true ;
147- } else if ( is_row_edge ) {
148- const is_before = drop_target . row_offset < SPLIT_EDGE_TOLERANCE ;
149- if ( drop_target . orientation === "vertical" ) {
150- drop_target = handle_matching_orientation (
151- col ,
152- row ,
153- panel ,
154- slot ,
155- drop_target ,
156- is_before ,
157- ) ;
83+ const new_panel = insert_child ( panel , slot , [ insert_index ] ) ;
84+ drop_target = calculate_intersection ( col , row , new_panel , false ) ;
15885 } else {
159- const insert_index = is_before ? 0 : 1 ;
160- drop_target = handle_cross_orientation (
161- col ,
162- row ,
163- panel ,
164- slot ,
165- drop_target ,
86+ const path_without_last = drop_target . path . slice ( 0 , - 1 ) ;
87+ const last_index = drop_target . path [ drop_target . path . length - 1 ] ;
88+ const insert_index = is_before ? last_index : last_index + 1 ;
89+ const new_panel = insert_child ( panel , slot , [
90+ ...path_without_last ,
16691 insert_index ,
167- "vertical" ,
168- ) ;
169- }
92+ ] ) ;
17093
171- drop_target . is_edge = true ;
94+ drop_target = calculate_intersection ( col , row , new_panel , false ) ;
95+ }
96+ } else {
97+ const path = [ ...drop_target . path , is_before ? 0 : 1 ] ;
98+ const new_panel = insert_child ( panel , slot , path , axis_orientation ) ;
99+ drop_target = calculate_intersection ( col , row , new_panel , false ) ;
172100 }
173101
102+ drop_target . is_edge = true ;
174103 return drop_target ;
175104}
0 commit comments