Finally, after a few hours of writing plugin almost from scratch, but now using example @kerrishotts provided as a base, it’s done. And now it works so much better and more smoothly - state keeps preserved, groups keep collapsed if they were collapsed. Applied same example for layer ordering. Also now have normal history states. Just amazing how one property can change everything.
But I’ve encountered some Ps bugs(?). Tested without a plugin, and it’s the same.
- If empty transparent layer is selected and you create new adjustment layer, it overwrites tat empty layer. Bug? Feature?
- If you have a layer group with a color and a layer (or a few) below that group and you want to make a group of the immediate bellow layer(s), this new group takes color of the group above (also all inner layers get that same color). Definitely a bug.
Thank you all so much for the help and patience with me
P. S. I’m not accepting @kerrishotts answer as a solution, because this is just a workaround to the initial issue with DOM