feat: 同步滚动开关

This commit is contained in:
xiaozzzi 2024-01-19 04:05:28 +08:00
parent 2c39c67ae9
commit c39640abd0

View File

@ -27,6 +27,10 @@ export class EPScroll {
*
*/
private _scrollTop: number = 0
/**
*
*/
private _open: boolean = true
constructor(editor: HTMLElement, previre: HTMLElement, cmw: CmWrapper | undefined) {
this._editor = editor
@ -35,26 +39,63 @@ export class EPScroll {
this._scrollTop = 0
}
/**
*
*/
public get scrollTop() {
return this._scrollTop
}
/**
*
*/
public scrollTopReset() {
this._scrollTop = 0
this._editor.scrollTo({ top: 0 })
this.toTop()
}
/**
*
*/
public scrollTopLast() {
this._editor.scrollTo({ top: this._scrollTop })
}
public sycnScroll(_event: Event | string, _source?: string, _lineno?: number, _colno?: number, _error?: Error): any {
/**
*
*/
public toTop() {
this._editor.scrollTo({ top: 0 })
}
/**
*
*/
public toBottom() {
this._editor.scrollTo({ top: 999999999 })
}
/**
* /
* @returns
*/
public open(): boolean {
this._open = !this._open
return this._open
}
public sycnScroll(_event: Event | string, _source?: string, _lineno?: number, _colno?: number, _error?: Error): void {
if (this._editor == undefined) {
return
}
// console.log(this._editor?.scrollHeight, this._editor?.clientHeight, this._editor?.scrollTop)
this._scrollTop = this._editor.scrollTop
// 如果不开启同步滚动, 则返回
if (!this._open) {
return
}
// 如果在头部附近
if (this._editor.scrollTop < 5) {
this._preview.scrollTo({ top: 0 })
@ -84,7 +125,7 @@ export class EPScroll {
// 将不可见的的 html 转换为 dom 对象, 是一个从 <html> 标签开始的 dom 对象
const invisibleDomAll = new DOMParser().parseFromString(invisibleHtml, 'text/html')
// body 下的内容才是由 markdown 转换而来的, 不可见内容转换的 dom 集合
const editorDoms = invisibleDomAll.body.querySelectorAll(matchHtmlTags)
// 预览页面的 dom 集合