Line height is giving me previous (older) value (i.e. lagging by one change)

I am trying to get the line height by using this function

const ln = view.state.doc.line(index+1)
view.visualLineAt(ln.from).height

inside the docChanged event. However I am getting the older line height as in height from previous view.
I am not sure if this a bug or I am doing something wrong.

I think when a change occurs, and your function is called for the update, the DOM hasn’t been flushed and the changes haven’t been yet applied to the DOM which is when the heights gets measured.

What I usually do is to use a view.requestMeasure() and do things in the callbacks from there.

1 Like

Assuming this code is running in an update listener, the DOM has been written when it runs, but not re-measured (to avoid layout trashing), so indeed, if you need accurate DOM geometry info, requestMeasure is what you want.

2 Likes

Thanks @lishid and @marijn …let me give view.requestMeasure() a try.

view.requestMeasure() worked for me. However the read and write functions are called on every change including moving the cursor position. Is there a way to limit calling it only when any line height changes?

They are called whenever you call requestMeasure, so this is under your control. You could check update.heightChanged and only measure when that is true.

1 Like

Thanks. This narrowed the calls down to anytime doc changes. However not sure if it a bug but update.heightChanged returns true for every doc changed, not just heightChanged.

I noticed. That should be fixed in the next release.

1 Like