By the way, it would be extremely helpful if there was some sort of programmatic workaround for this or even ideas for possible avenues other than the couple of things we’ve tried above.
I ran into this same behavior when inserting images as widgets. This appears to be due to the asynchronous nature of loading images. I was able to solve this by calling the .changed() method on the returned LineWidget object after the image finishes loading (via the onload attribute). Here is a pseudo example:
let lineWidget
let image = document.createElement('img')
let line = 0
image.src = '/path/to/image'
image.onload = () => lineWidget.changed()
lineWidget = doc.addLineWidget(line, image)
As for why this works sometimes, I assume it could be a race condition between the image (or element) loading in the DOM and the CodeMirror editor rendering the lines. I’m not sure if this is exactly the same problem, but the side effects definitely match.