Plugin Error: Plugin is not permitted to make changes from the background. Use editDocument() for panel UI handlers, or return a Promise to extend an edit operation asynchronously.
I’m using editDocument. Is there something else I’m missing?
I would expect that the problem is that due to this being an async function, it returns a Promise. All code in it, therefore, gets run asynchronously, which means editDocument() probably doesn’t get called in the same tick as the UI-event, which then triggers the error.
Therefore, while you can put everything into the editDocument(), which apperently still works as the JS engine seems to run the code up to editDocument first (although that might not be consistent, so I wouldn’t count on that), but the “cleaner” way (that should deterministically work ) would be to have a non-async function getting triggered by the UI-event, which then calls editDocument() (which, of course, is still in the same tick as nothing asynchronous is going on), in which you can do whatever you need to do (may that be creating elements, or even asynchronous stuff, in which case the function passed to editDocument() must return a Promise).
When I moved the new Rectangle() statement into the editDocument() call I also had to move it before the await calls that I removed from the code above.
You’re right that it wasn’t in the same tick. I had to break things into two different steps and I’m doing some magic to get it to work.