@@ -30,155 +30,145 @@ import {
3030 * @returns A new `LayoutPath` reflecting the updated (maybe) `"split-panel"`,
3131 * which is enough to draw the overlay.
3232 */
33+ function handle_matching_orientation (
34+ col : number ,
35+ row : number ,
36+ panel : Layout ,
37+ slot : string ,
38+ drop_target : LayoutPath ,
39+ is_before : boolean ,
40+ ) : 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+ ] ) ;
66+
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+ }
81+ }
82+ }
83+
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+ } ;
106+ }
107+
33108export function calculate_split (
34109 col : number ,
35110 row : number ,
36111 panel : Layout ,
37112 slot : string ,
38113 drop_target : LayoutPath ,
39114) : LayoutPath {
40- if (
115+ const is_column_edge =
41116 drop_target . column_offset < SPLIT_EDGE_TOLERANCE ||
42- drop_target . column_offset > 1 - SPLIT_EDGE_TOLERANCE
43- ) {
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 ;
44124 if ( drop_target . orientation === "horizontal" ) {
45- const is_before = drop_target . column_offset < SPLIT_EDGE_TOLERANCE ;
46- if ( drop_target . path . length === 0 ) {
47- const insert_index = is_before ? 0 : 1 ;
48- const new_panel = insert_child ( panel , slot , [ insert_index ] ) ;
49- // When inserting before, point to new panel; when after, keep original
50- if ( is_before ) {
51- drop_target = calculate_intersection ( col , row , new_panel , false ) ;
52- } else {
53- const new_drop_target = calculate_intersection (
54- col ,
55- row ,
56- new_panel ,
57- false ,
58- ) ;
59- drop_target = {
60- ...new_drop_target ,
61- path : [ 0 ] ,
62- } ;
63- }
64- } else {
65- const path_without_last = drop_target . path . slice ( 0 , - 1 ) ;
66- const last_index = drop_target . path [ drop_target . path . length - 1 ] ;
67- const insert_index = is_before ? last_index : last_index + 1 ;
68- const new_panel = insert_child ( panel , slot , [
69- ...path_without_last ,
70- insert_index ,
71- ] ) ;
72- // When inserting before, point to new panel; when after, keep original
73- if ( is_before ) {
74- drop_target = calculate_intersection ( col , row , new_panel , false ) ;
75- } else {
76- // Keep the original panel but update view_window from new layout
77- const new_drop_target = calculate_intersection (
78- col ,
79- row ,
80- new_panel ,
81- false ,
82- ) ;
83- drop_target = {
84- ...new_drop_target ,
85- path : [ ...path_without_last , last_index ] ,
86- } ;
87- }
88- }
125+ drop_target = handle_matching_orientation (
126+ col ,
127+ row ,
128+ panel ,
129+ slot ,
130+ drop_target ,
131+ is_before ,
132+ ) ;
89133 } else {
90- const insert_index =
91- drop_target . column_offset < SPLIT_EDGE_TOLERANCE ? 0 : 1 ;
92- const original_path = drop_target . path ;
93- const new_panel = insert_child (
134+ const insert_index = is_before ? 0 : 1 ;
135+ drop_target = handle_cross_orientation (
136+ col ,
137+ row ,
94138 panel ,
95139 slot ,
96- [ ...original_path , insert_index ] ,
140+ drop_target ,
141+ insert_index ,
97142 "horizontal" ,
98143 ) ;
99- drop_target = calculate_intersection ( col , row , new_panel , false ) ;
100- // Override to point to the newly inserted panel
101- drop_target = {
102- ...drop_target ,
103- slot,
104- path : [ ...original_path , insert_index ] ,
105- } ;
106144 }
107145
108- if ( drop_target ) {
109- drop_target . is_edge = true ;
110- }
111- } else if (
112- drop_target . row_offset < SPLIT_EDGE_TOLERANCE ||
113- drop_target . row_offset > 1 - SPLIT_EDGE_TOLERANCE
114- ) {
146+ drop_target . is_edge = true ;
147+ } else if ( is_row_edge ) {
148+ const is_before = drop_target . row_offset < SPLIT_EDGE_TOLERANCE ;
115149 if ( drop_target . orientation === "vertical" ) {
116- const is_before = drop_target . row_offset < SPLIT_EDGE_TOLERANCE ;
117- if ( drop_target . path . length === 0 ) {
118- const insert_index = is_before ? 0 : 1 ;
119- const new_panel = insert_child ( panel , slot , [ insert_index ] ) ;
120- // When inserting before, point to new panel; when after, keep original
121- if ( is_before ) {
122- drop_target = calculate_intersection ( col , row , new_panel , false ) ;
123- } else {
124- const new_drop_target = calculate_intersection (
125- col ,
126- row ,
127- new_panel ,
128- false ,
129- ) ;
130- drop_target = {
131- ...new_drop_target ,
132- path : [ 0 ] ,
133- } ;
134- }
135- } else {
136- const path_without_last = drop_target . path . slice ( 0 , - 1 ) ;
137- const last_index = drop_target . path [ drop_target . path . length - 1 ] ;
138- const insert_index = is_before ? last_index : last_index + 1 ;
139- const new_panel = insert_child ( panel , slot , [
140- ...path_without_last ,
141- insert_index ,
142- ] ) ;
143- // When inserting before, point to new panel; when after, keep original
144- if ( is_before ) {
145- drop_target = calculate_intersection ( col , row , new_panel , false ) ;
146- } else {
147- // Keep the original panel but update view_window from new layout
148- const new_drop_target = calculate_intersection (
149- col ,
150- row ,
151- new_panel ,
152- false ,
153- ) ;
154- drop_target = {
155- ...new_drop_target ,
156- path : [ ...path_without_last , last_index ] ,
157- } ;
158- }
159- }
150+ drop_target = handle_matching_orientation (
151+ col ,
152+ row ,
153+ panel ,
154+ slot ,
155+ drop_target ,
156+ is_before ,
157+ ) ;
160158 } else {
161- const insert_index =
162- drop_target . row_offset < SPLIT_EDGE_TOLERANCE ? 0 : 1 ;
163- const original_path = drop_target . path ;
164- const new_panel = insert_child (
159+ const insert_index = is_before ? 0 : 1 ;
160+ drop_target = handle_cross_orientation (
161+ col ,
162+ row ,
165163 panel ,
166164 slot ,
167- [ ...original_path , insert_index ] ,
165+ drop_target ,
166+ insert_index ,
168167 "vertical" ,
169168 ) ;
170- drop_target = calculate_intersection ( col , row , new_panel , false ) ;
171- // Override to point to the newly inserted panel
172- drop_target = {
173- ...drop_target ,
174- slot,
175- path : [ ...original_path , insert_index ] ,
176- } ;
177169 }
178170
179- if ( drop_target ) {
180- drop_target . is_edge = true ;
181- }
171+ drop_target . is_edge = true ;
182172 }
183173
184174 return drop_target ;
0 commit comments