SOLVED: moveItems() scrambles item order


#1

tl;dr: moveItems() scrambles item order on first use.

Working under Mojave with the latest OO release, if I make a simple new outline like this…

  • A
  • B
  • C
  • D
  • E
  • F
  • G
  • H

…then select the C, D, E and F rows before executing this in the console window…

moveItems(document.editors[0].selection.items, rootItem.end)

…those rows do get moved to the bottom, but in scrambled (not simply backward) order, which is both a surprise and a problem.

The rows maintain their selected state, so if I then execute this…

moveItems(document.editors[0].selection.items, rootItem.beginning)

…the rows move to the top, as expected, and do not change order again–that is, they stay in the same scrambled order.

If I then move them back to the bottom, the same results–they keep the same scrambled order they inherited when I moved them the first time.

This smells like a bug, though perhaps my expectations are the problem.

Any thoughts on how to use moveItems() without corrupting item order?


#4

I deleted a couple of my own responses to this post that explored a homebrew solution to this problem, because it turns out that approach was unnecessary.

I’ve since learned there is a built-in itemsSortedByPosition() function.

So the fix for my problematic case above is simply this:

moveItems(itemsSortedByPosition(document.editors[0].selection.items), rootItem.end)

I hope this helps someone else!