Just to add to the discussion (and provide the obligatory code), To Front and To Back are provided to the API via a couple of free functions, toFront() and toBack() respectively. However, they both have horrible race conditions meaning if you call them too rapidly, the ordering gets overwritten by successive calls before the prior calls finish. You can fix that by creating a function that manages the list of things to reorder. The timing for each of the operations varies wildly—toBack() needs ~50ms to finish moving an object, toFront() needs ~1000ms. Here's an example function, toFrontFixed(), which will space out calls to toFront() to give each one a chance to finish:
const toFrontFixed = (()=>{
let queue=[];
let last=0;
const DELAY = 1000;
const burndownQueue = ()=>{
let o = queue.shift();
toFront(o);
last = Date.now();
if(queue.length){
setTimeout(burndownQueue,DELAY);
}
};
return (obj=>{
if(queue.length){
queue.push(obj);
} else {
let t = Date.now();
if(last+DELAY > t){
queue.push(obj);
setTimeout(burndownQueue,(last+DELAY-t));
} else {
toFront(obj);
last = t;
}
}
});
})();
That covered, the next major problem is how to order things. If there are some simple rules, like (Graphics at back, drawings in the middle, text on top), that could be pretty easy to manage. (For example, by simply finding all the lines and pushing them to back, then finding all the graphics and pushing them to back). If the rules are more complicated, so too will the script be.
If your use case is always "When I copy a page, I want the ordering to match the ordering on the source page", then there are two things that can be done:
1) That sounds like a legit bug that the Dev team needs to fix. I'll see if I can reproduce it and file a ticket for it.
2) A script could be written that would figure out a mapping between the source page's objects and the copy page's objects, then find the order they should be in and make changes to that ordering until they match. That's probably a little complicated, but should be doable.