parent
4cc0f056c2
commit
a42b2ffcf4
|
After Width: | Height: | Size: 165 B |
|
After Width: | Height: | Size: 148 B |
@ -1,11 +1,22 @@
|
|||||||
import { IRangeStyleChange } from "../../interface/Listener"
|
import {
|
||||||
|
IIntersectionPageNoChange,
|
||||||
|
IPageSizeChange,
|
||||||
|
IRangeStyleChange,
|
||||||
|
IVisiblePageNoListChange
|
||||||
|
} from "../../interface/Listener"
|
||||||
|
|
||||||
export class Listener {
|
export class Listener {
|
||||||
|
|
||||||
public rangeStyleChange: IRangeStyleChange | null
|
public rangeStyleChange: IRangeStyleChange | null
|
||||||
|
public visiblePageNoListChange: IVisiblePageNoListChange | null
|
||||||
|
public intersectionPageNoChange: IIntersectionPageNoChange | null
|
||||||
|
public pageSizeChange: IPageSizeChange | null
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.rangeStyleChange = null
|
this.rangeStyleChange = null
|
||||||
|
this.visiblePageNoListChange = null
|
||||||
|
this.intersectionPageNoChange = null
|
||||||
|
this.pageSizeChange = null
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -0,0 +1,60 @@
|
|||||||
|
import { IEditorOption } from "../../interface/Editor"
|
||||||
|
import { debounce } from "../../utils"
|
||||||
|
import { Draw } from "../draw/Draw"
|
||||||
|
|
||||||
|
export class GlobalObserver {
|
||||||
|
|
||||||
|
private draw: Draw
|
||||||
|
private options: Required<IEditorOption>
|
||||||
|
private pageContainer: HTMLDivElement
|
||||||
|
private pageHeight: number
|
||||||
|
|
||||||
|
constructor(draw: Draw) {
|
||||||
|
this.draw = draw
|
||||||
|
this.options = draw.getOptions()
|
||||||
|
this.pageContainer = draw.getPageContainer()
|
||||||
|
const { height, pageGap } = this.options
|
||||||
|
this.pageHeight = height + pageGap
|
||||||
|
// 监听滚轮
|
||||||
|
setTimeout(() => {
|
||||||
|
if (!window.scrollY) {
|
||||||
|
this.observer()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
document.addEventListener('scroll', debounce(this.observer.bind(this), 150))
|
||||||
|
}
|
||||||
|
|
||||||
|
private observer() {
|
||||||
|
const rect = this.pageContainer.getBoundingClientRect()
|
||||||
|
const top = Math.abs(rect.top)
|
||||||
|
const bottom = top + window.innerHeight
|
||||||
|
const pageList = this.draw.getPageList()
|
||||||
|
// 计算显示页
|
||||||
|
let visiblePageNoList: number[] = []
|
||||||
|
let intersectionPageNo: number = 0
|
||||||
|
let intersectionMaxHeight: number = 0
|
||||||
|
for (let i = 0; i < pageList.length; i++) {
|
||||||
|
const curTop = i * this.pageHeight
|
||||||
|
const curBottom = (i + 1) * this.pageHeight
|
||||||
|
if (curTop > bottom) break
|
||||||
|
if (curBottom < top) continue
|
||||||
|
// 计算交叉高度
|
||||||
|
let intersectionHeight = 0
|
||||||
|
if (curTop < top && curBottom < bottom) {
|
||||||
|
intersectionHeight = curBottom - top
|
||||||
|
} else if (curTop > top && curBottom > bottom) {
|
||||||
|
intersectionHeight = bottom - curTop
|
||||||
|
} else {
|
||||||
|
intersectionHeight = rect.height
|
||||||
|
}
|
||||||
|
if (intersectionHeight > intersectionMaxHeight) {
|
||||||
|
intersectionMaxHeight = intersectionHeight
|
||||||
|
intersectionPageNo = i
|
||||||
|
}
|
||||||
|
visiblePageNoList.push(i)
|
||||||
|
}
|
||||||
|
this.draw.setIntersectionPageNo(intersectionPageNo)
|
||||||
|
this.draw.setVisiblePageNoList(visiblePageNoList)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -1,4 +1,5 @@
|
|||||||
export enum EditorComponent {
|
export enum EditorComponent {
|
||||||
MENU = 'menu',
|
MENU = 'menu',
|
||||||
MAIN = 'main'
|
MAIN = 'main',
|
||||||
|
FOOTER = 'footer'
|
||||||
}
|
}
|
||||||
Loading…
Reference in new issue