@@ -8,11 +8,6 @@ export function addPod(state, action) {
88 }
99 // update all other siblings' index
1010 // FIXME this might cause other pods to be re-rendered
11- state . pods [ parent ] . children . forEach ( ( { id } ) => {
12- if ( state . pods [ id ] . index >= index ) {
13- state . pods [ id ] . index += 1 ;
14- }
15- } ) ;
1611 const pod = {
1712 content : "" ,
1813 column : 1 ,
@@ -41,13 +36,10 @@ export function addPod(state, action) {
4136 // TODO the children no longer need to be ordered
4237 // TODO the frontend should handle differently for the children
4338 // state.pods[parent].children.splice(index, 0, id);
44- state . pods [ parent ] . children . push ( { id, type : pod . type } ) ;
39+ state . pods [ parent ] . children . splice ( index , 0 , { id, type : pod . type } )
4540 // DEBUG sort-in-place
4641 // TODO I can probably insert
4742 // CAUTION the sort expects -1,0,1, not true/false
48- state . pods [ parent ] . children . sort (
49- ( a , b ) => state . pods [ a . id ] . index - state . pods [ b . id ] . index
50- ) ;
5143 pod . ns = computeNamespace ( state . pods , id ) ;
5244}
5345
@@ -58,11 +50,6 @@ export function deletePod(state, action) {
5850 const index = parent . children . map ( ( { id } ) => id ) . indexOf ( id ) ;
5951
6052 // update all other siblings' index
61- parent . children . forEach ( ( { id } ) => {
62- if ( state . pods [ id ] . index >= index ) {
63- state . pods [ id ] . index -= 1 ;
64- }
65- } ) ;
6653 // remove all
6754 parent . children . splice ( index , 1 ) ;
6855 toDelete . forEach ( ( id ) => {
@@ -75,34 +62,20 @@ export function pastePod(state, action) {
7562 let pod = state . pods [ id ] ;
7663 // 1. remove the clipped pod
7764 let oldparent = state . pods [ pod . parent ] ;
78- oldparent . children . splice (
79- oldparent . children . map ( ( { id } ) => id ) . indexOf ( pod . id ) ,
80- 1
81- ) ;
82- oldparent . children . forEach ( ( { id } ) => {
83- if ( state . pods [ id ] . index > pod . index ) {
84- state . pods [ id ] . index -= 1 ;
85- }
86- } ) ;
65+ let oldindex = oldparent . children . map ( ( { id } ) => id ) . indexOf ( pod . id )
66+ if ( oldindex == - 1 ) {
67+ throw new Error ( "Pod not found" , pod . id )
68+ }
69+ oldparent . children . splice ( oldindex , 1 ) ;
70+ if ( oldparent . id === parent && index > oldindex ) {
71+ index -= 1
72+ }
8773 // 2. insert into the new position
8874 let newparent = state . pods [ parent ] ;
89- if ( newparent === oldparent && index > pod . index ) {
90- // need special adjustment if parent is not changed.
91- index -= 1 ;
92- }
93- newparent . children . forEach ( ( { id } ) => {
94- if ( state . pods [ id ] . index >= index ) {
95- state . pods [ id ] . index += 1 ;
96- }
97- } ) ;
98- newparent . children . push ( { id : pod . id , type : pod . type } ) ;
75+ newparent . children . splice ( index , 0 , { id : pod . id , type : pod . type } ) ;
9976 // 3. set the data of the pod itself
10077 pod . parent = parent ;
10178 pod . column = column ;
102- pod . index = index ;
103- newparent . children . sort (
104- ( a , b ) => state . pods [ a . id ] . index - state . pods [ b . id ] . index
105- ) ;
10679 // 4. update namespace
10780 function helper ( node ) {
10881 node . ns = computeNamespace ( state . pods , node . id ) ;
0 commit comments