Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I scroll to the bottom of the editor? #889

Open
vbmark opened this issue May 12, 2022 · 1 comment
Open

How do I scroll to the bottom of the editor? #889

vbmark opened this issue May 12, 2022 · 1 comment
Labels

Comments

@vbmark
Copy link

vbmark commented May 12, 2022

I am adding text to the editor by the click of a button, but the newly added text is not visible because the editor does not automatically scroll.

Is there either a flag that I can set so that new content is detected and the scroll happens automatically, or is there a way via JavaScript to manually scroll to the bottom?

Thank you.

@samclarke samclarke added the Bug label Jun 14, 2022
@samclarke
Copy link
Owner

The editor should really do this so marking as a bug.

For now it can be done getting the documentElement from the body node and setting the scrollTop property.

Something like this should work:

const document = instance.getBody().ownerDocument;
const documentElement = document.documentElement;

// Save selection so can see where end marker is
instance.getRangeHelper().saveRange();
const marker = document.getElementById('sceditor-end-marker');
marker.style.display = 'inline-block';

// Work out where end marker is and current scroll position
const scrollTop = documentElement.scrollTop;
const scrollTo = (sceditor.dom.getOffset(marker).top + (marker.offsetHeight * 1.5)) - documentElement.offsetHeight;
marker.style.display = 'none';

// Only scroll if end marker isn't already visible
if (scrollTo > scrollTop || scrollTo + documentElement.offsetHeight < scrollTop) {
  documentElement.scrollTop = scrollTo;
}

// Restore selection
instance.getRangeHelper().restoreRange();

Demo: https://jsfiddle.net/t35bfp18/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants