devexpress-richedit
Version:
DevExpress Rich Text Editor is an advanced word-processing tool designed for working with rich text documents.
69 lines (68 loc) • 3.37 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';
import { MixedSize } from '../utils/mixed-size';
export class CanvasScrollInfo {
static { this.VISIBLE_PAGES_RANGE = 2; }
static { this.VISIBLE_PAGES_RANGE_TOUCH = 0; }
static { this.VISIBLE_AREA_HEIGHT_MULTIPLIER = 2; }
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 zoomLevel = this.sizes.zoomLevel;
const scrollTop = MixedSize.fromUI(this.getScrollTop()).useScale(zoomLevel);
this.lastScrollLeft = this.canvas.scrollLeft;
if (this.startVisiblePageIndex >= 0 && scrollTop.UISize == this.lastScrollTop && !this._needUpdatePageIndexes)
return;
this._needUpdatePageIndexes = false;
this.startVisiblePageIndex = layout.findPageIndexByOffsetY(scrollTop.LayoutSize, this.sizes);
const viewportBottom = MixedSize.from(scrollTop).addUISize(this.getVisibleHeight());
this.endVisiblePageIndex = layout.findPageIndexByOffsetY(viewportBottom.LayoutSize, this.sizes);
this.lastScrollTop = scrollTop.UISize;
}
getVisibleInterval() {
const scrollTop = this.getScrollTop() / this.sizes.zoomLevel;
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;
}
}