New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
insertChild() wrong index when re-inserting item into parent with higher index #1015
Comments
Since the item is already in the list, this case is not so clear: In order to be inserted into children list at a different index, the item has to be removed from it first. When you remove an item, the indices of all items above it shift down by one. And if the insertion index is above its current index, you'd assume it is in relation to these items that now shift, so one can argue that it makes sense to then also adjust the insertion index. Do you see what I mean? |
@lehni I do see what you mean. Although its strange that Projects children and other Items children behave differently in this regard. It makes more sense to me to guarantee the index provided. It is expected that when you splice into array that other items shift and thats fine, but I would always expect my spliced item to be at the index specified. I can see what you are trying to do though where when you |
Actually thinking about this more and how I imagine the most common use case is I want this item to be above or below something else... in which case for above you would give the target items index + 1, to be below the target items index, just like array splice. I can see how it would be weird that the order would be different depending on whether the item is already a child or not, and think you just shouldn't care about what index it lands at, just that it lands in the correct order. |
The reason why it's behaving differently right now is this line of code: I can't decide which approach is better... Both argumentations are valid, but I think I agree with you that the shift is more elegant, so I shall port this to You should then be able to use |
Here an example with layers that currently fails. |
Hmmm so thinking about this some more. I'm not so sure anymore. The issue I'm now seeing is that the inverse of I have 4 items.
That does seem strange and hard to reason about. There is also the case that I tell it to move to one above its current index or its current index and it always stays in place: I have 4 items:
In that case it effectively does nothing which is odd behaviour. To move up it has to be its index + 2. Perhaps |
Yes I agree, I think you're right about this... I was just looking into how jQuery and the DOM handles this case, but the functionality is not even provided, so I think we can make up our own rules here. |
When using
Item#insertChild()
on an item and specifying its current parent but at an indexn
wheren > Item#index
the resulting index isn - 1
:Example of issue
This is not the case for
addLayer()
which makes me think the index correcting code post removal of the item inItem#insertChildren()
is to blame asProject#addLayer()
just removes and splices and ends up with the correct result:Project#addLayer() test
The text was updated successfully, but these errors were encountered: