@@ -114,40 +114,48 @@ impl<'a> ModifyInputsContext<'a> {
114114 Some ( new_id)
115115 }
116116
117- pub fn skip_artboards ( & self , output : & mut NodeOutput ) -> Option < ( NodeId , usize ) > {
118- while let NodeInput :: Node { node_id, output_index , .. } = & self . document_network . nodes . get ( & output. node_id ) ?. primary_input ( ) ? {
117+ pub fn skip_artboards ( & self , output : & mut NodeId ) -> Option < NodeId > {
118+ while let NodeInput :: Node { node_id, .. } = & self . document_network . nodes . get ( output) ?. primary_input ( ) ? {
119119 let sibling_node = self . document_network . nodes . get ( node_id) ?;
120120 if sibling_node. name != "Artboard" {
121- return Some ( ( * node_id, * output_index ) ) ;
121+ return Some ( * node_id) ;
122122 }
123- * output = NodeOutput :: new ( * node_id, * output_index )
123+ * output = * node_id;
124124 }
125125 None
126126 }
127127
128- pub fn create_layer ( & mut self , new_id : NodeId , output_node_id : NodeId , input_index : usize , skip_layer_nodes : usize ) -> Option < NodeId > {
128+ pub fn create_layer ( & mut self , new_id : NodeId , output_node_id : NodeId , skip_layer_nodes : usize ) -> Option < NodeId > {
129129 assert ! ( !self . document_network. nodes. contains_key( & new_id) , "Creating already existing layer" ) ;
130130
131- let mut output = NodeOutput :: new ( output_node_id, input_index) ;
131+ let mut output = output_node_id;
132+ let mut output_in_index = 0 ;
132133 let mut sibling_layer = None ;
133134 let mut shift = IVec2 :: new ( 0 , 3 ) ;
134135 // Locate the node output of the first sibling layer to the new layer
135- if let Some ( ( node_id, output_index) ) = self . skip_artboards ( & mut output) {
136+ let skipped_artboards = self . skip_artboards ( & mut output) ;
137+ // Connect to input 1 of the parent layer.
138+ output_in_index = self . document_network . nodes . get ( & output) . map_or ( 0 , |node| if node. is_layer ( ) { 1 } else { 0 } ) ;
139+ if let Some ( node_id) = skipped_artboards {
136140 let sibling_node = self . document_network . nodes . get ( & node_id) ?;
137141 if sibling_node. is_layer ( ) {
138142 // There is already a layer node
139143 sibling_layer = Some ( NodeOutput :: new ( node_id, 0 ) ) ;
140144 } else {
145+ info ! ( "Insert between" ) ;
141146 // The user has connected another node to the output. Insert a layer node between the output and the node.
142147 let node = resolve_document_node_type ( "Layer" ) . expect ( "Layer node" ) . default_document_node ( ) ;
143- let node_id = self . insert_between ( NodeId ( generate_uuid ( ) ) , NodeOutput :: new ( node_id, output_index) , output, node, 1 , 0 , IVec2 :: new ( -8 , 0 ) ) ?;
148+ let index = self . document_network . nodes . get ( & output) . map_or ( 0 , |node| if node. is_layer ( ) { 1 } else { 0 } ) ;
149+ let post = NodeOutput :: new ( output, index) ;
150+ let node_id = self . insert_between ( NodeId ( generate_uuid ( ) ) , NodeOutput :: new ( node_id, 0 ) , post, node, 1 , 0 , IVec2 :: new ( -8 , 0 ) ) ?;
144151 sibling_layer = Some ( NodeOutput :: new ( node_id, 0 ) ) ;
145152 }
146153
147154 // Skip some layer nodes
148155 for _ in 0 ..skip_layer_nodes {
149156 if let Some ( old_sibling) = & sibling_layer {
150- output = NodeOutput :: new ( old_sibling. node_id , 0 ) ;
157+ output = old_sibling. node_id ;
158+ output_in_index = 0 ;
151159 sibling_layer = self . document_network . nodes . get ( & old_sibling. node_id ) ?. inputs [ 0 ] . as_node ( ) . map ( |node| NodeOutput :: new ( node, 0 ) ) ;
152160 shift = IVec2 :: new ( 0 , 3 ) ;
153161 }
@@ -160,10 +168,11 @@ impl<'a> ModifyInputsContext<'a> {
160168
161169 // Create node
162170 let layer_node = resolve_document_node_type ( "Layer" ) . expect ( "Layer node" ) . default_document_node ( ) ;
171+
163172 let new_id = if let Some ( sibling_layer) = sibling_layer {
164- self . insert_between ( new_id, sibling_layer, output, layer_node, 0 , 0 , shift)
173+ self . insert_between ( new_id, sibling_layer, NodeOutput :: new ( output, output_in_index ) , layer_node, 0 , 0 , shift)
165174 } else {
166- self . insert_node_before ( new_id, output. node_id , 0 , layer_node, shift)
175+ self . insert_node_before ( new_id, output, output_in_index , layer_node, shift)
167176 } ;
168177
169178 // Update the document metadata structure
@@ -188,7 +197,7 @@ impl<'a> ModifyInputsContext<'a> {
188197 } else {
189198 parent. to_node ( )
190199 } ;
191- self . create_layer ( new_id, output_node_id, 0 , skip_layer_nodes)
200+ self . create_layer ( new_id, output_node_id, skip_layer_nodes)
192201 }
193202
194203 pub fn insert_artboard ( & mut self , artboard : Artboard , layer : NodeId ) -> Option < NodeId > {
@@ -295,11 +304,12 @@ impl<'a> ModifyInputsContext<'a> {
295304 return ;
296305 } ;
297306
307+ let input_index = if output_node. is_layer ( ) { 1 } else { 0 } ;
298308 let metadata = output_node. metadata . clone ( ) ;
299- let new_input = output_node. inputs . first ( ) . cloned ( ) . filter ( |input| input. as_node ( ) . is_some ( ) ) ;
309+ let new_input = output_node. inputs . get ( input_index ) . cloned ( ) . filter ( |input| input. as_node ( ) . is_some ( ) ) ;
300310 let node_id = NodeId ( generate_uuid ( ) ) ;
301311
302- output_node. inputs [ 0 ] = NodeInput :: node ( node_id, 0 ) ;
312+ output_node. inputs [ input_index ] = NodeInput :: node ( node_id, 0 ) ;
303313
304314 let Some ( node_type) = resolve_document_node_type ( name) else {
305315 warn ! ( "Node type \" {name}\" doesn't exist" ) ;
0 commit comments