devexpress-richedit
Version:
DevExpress Rich Text Editor is an advanced word-processing tool designed for working with rich text documents.
65 lines (64 loc) • 3.08 kB
JavaScript
import { Browser } from '@devexpress/utils/lib/browser';
import { BoundaryInterval } from '@devexpress/utils/lib/intervals/boundary';
import { FixedInterval } from '@devexpress/utils/lib/intervals/fixed';
import { ModelChangeType } from '../model/changes/enums';
export class CanvasScrollInfo {
constructor(canvas, sizes, internalApi) {
this.lastScrollTop = -1;
this.lastScrollLeft = -1;
this.startVisiblePageIndex = 0;
this.endVisiblePageIndex = 0;
this._needUpdatePageIndexes = false;
this.internalApi = internalApi;
this.init(canvas, sizes);
}
modelChanged(change) {
if (change.type == ModelChangeType.ZoomLevelChanged)
this.onCanvasSizeChanged();
}
init(canvas, sizes) {
this.canvas = canvas;
this.sizes = sizes;
this.renderPagesOffset = Browser.TouchUI ? CanvasScrollInfo.VISIBLE_PAGES_RANGE_TOUCH : CanvasScrollInfo.VISIBLE_PAGES_RANGE;
}
getStartRenderPageIndex() {
return Math.max(0, this.startVisiblePageIndex - this.renderPagesOffset);
}
getEndRenderPageIndex() {
return this.endVisiblePageIndex + this.renderPagesOffset;
}
renderPageIndexInterval() {
return FixedInterval.fromPositions(this.getStartRenderPageIndex(), this.getEndRenderPageIndex() + 1);
}
updatePageIndexesInfo(layout) {
if (!layout.pages.length)
return;
const scrollTop = this.getScrollTop();
this.lastScrollLeft = this.canvas.scrollLeft;
if (this.startVisiblePageIndex >= 0 && scrollTop == this.lastScrollTop && !this._needUpdatePageIndexes)
return;
this._needUpdatePageIndexes = false;
this.startVisiblePageIndex = layout.findPageIndexByOffsetY(scrollTop, this.sizes);
this.endVisiblePageIndex = layout.findPageIndexByOffsetY(scrollTop + this.getVisibleHeight(), this.sizes);
this.lastScrollTop = scrollTop;
}
getVisibleInterval() {
const scrollTop = this.getScrollTop();
const visibleHeight = this.getVisibleHeight();
const visibleHeightInterval = new FixedInterval(scrollTop, visibleHeight);
const fullRenderedHeight = visibleHeightInterval.length * CanvasScrollInfo.VISIBLE_AREA_HEIGHT_MULTIPLIER;
return BoundaryInterval.makeByConstInterval(new FixedInterval(Math.max(0, visibleHeightInterval.center - Math.floor(fullRenderedHeight / 2)), fullRenderedHeight));
}
getScrollTop() {
return this.canvas.scrollTop + (this.internalApi.getVerticalScrollOffset ? this.internalApi.getVerticalScrollOffset() : 0);
}
getVisibleHeight() {
return this.internalApi.getVisibleAreaHeight ? this.internalApi.getVisibleAreaHeight() : this.sizes.getVisibleAreaHeight(false);
}
onCanvasSizeChanged() {
this._needUpdatePageIndexes = true;
}
}
CanvasScrollInfo.VISIBLE_PAGES_RANGE = 2;
CanvasScrollInfo.VISIBLE_PAGES_RANGE_TOUCH = 0;
CanvasScrollInfo.VISIBLE_AREA_HEIGHT_MULTIPLIER = 2;