fix: handle scrollYOffset in ScrollService

This commit is contained in:
Roman Hotsiy 2018-01-11 18:08:33 +02:00
parent 39d1ac5f22
commit 34a8fe4906
No known key found for this signature in database
GPG Key ID: 5CB7B3ACABA57CB0
2 changed files with 8 additions and 4 deletions

View File

@ -36,7 +36,7 @@ export class AppStore {
constructor(spec: OpenAPISpec, specUrl?: string, options: RedocRawOptions = {}) {
this.rawOptions = options;
this.options = new RedocNormalizedOptions(options);
this.scroll = new ScrollService();
this.scroll = new ScrollService(this.options);
this.spec = new SpecStore(spec, specUrl, this.options);
this.menu = new MenuStore(this.spec, this.scroll);
}

View File

@ -2,6 +2,7 @@ import { debounce, bind } from 'decko';
import { EventEmitter } from 'eventemitter3';
import { querySelector, isBrowser } from '../utils';
import { RedocNormalizedOptions } from './RedocNormalizedOptions';
const EVENT = 'scroll';
@ -9,7 +10,7 @@ export class ScrollService {
private _scrollParent: Window | HTMLElement | undefined;
private _emiter: EventEmitter;
private _prevOffsetY: number = 0;
constructor() {
constructor(private options: RedocNormalizedOptions) {
this._scrollParent = isBrowser ? window : undefined;
this._emiter = new EventEmitter();
this.bind();
@ -37,12 +38,12 @@ export class ScrollService {
isElementBellow(el: Element | null) {
if (el === null) return;
return el.getBoundingClientRect().top > 0;
return el.getBoundingClientRect().top > this.options.scrollYOffset();
}
isElementAbove(el: Element | null) {
if (el === null) return;
return Math.trunc(el.getBoundingClientRect().top) <= 0;
return Math.trunc(el.getBoundingClientRect().top) <= this.options.scrollYOffset();
}
subscribe(cb): () => void {
@ -55,6 +56,9 @@ export class ScrollService {
return;
}
element.scrollIntoView();
this._scrollParent &&
this._scrollParent.scrollBy &&
(this._scrollParent.scrollBy as any)(0, -this.options.scrollYOffset());
}
scrollIntoViewBySelector(selector: string) {