deathau / cm-typewriter-scroll-obsidian

Typewriter Scroll Obsidian Plugin
164 stars 15 forks source link

Unexpected line jumping #6

Closed walulula closed 3 years ago

walulula commented 3 years ago

Thank you so much for this plugin. It really helps me to focus on my writing.

Sadly I encountered a bug that occurs sometimes when breaking into a new line.

Kapture 2021-02-12 at 21 39 45s

walulula commented 3 years ago

I took a deeper look at the code and found the cause to be Obsidian unexpectedly changing the padding-bottom value of the CodeMirror-lines class. Overwriting padding-bottom through the onRefresh function fixes the problem for me.

function onRefresh(cm) {
    const halfWindowHeight = cm.getWrapperElement().offsetHeight / 2;
    const linesEl = cm.getScrollerElement().querySelector('.CodeMirror-lines');
    linesEl.style.paddingTop = `${halfWindowHeight}px`;
    linesEl.style.paddingBottom = `${halfWindowHeight}px`; // This line fixes the jumping
    if (cm.getSelection().length === 0) {
        cm.execCommand("scrollSelectionToCenter");
    }
}
macosxguru commented 3 years ago

Yup. I was having that problem and this fixed it.

Thanks.

macosxguru

frankreporting commented 3 years ago

Thanks for the fix! I noticed it wasn't just jumping a line at random. It effectively seemed to be overriding the line-centering, so that as long as you continued typing in the current paragraph (i.e. same line without a return carriage), you would get closer and closer to the bottom, until you reached the bottom, and stayed there. It effectively makes it feel like typewriter mode has been completely disabled until you hit to create a new line. Then it finally snaps back to center. So this bug was most noticeable on very long paragraphs.

Anyway the above fix works perfectly.