From 82bfe58df4dc1358f018d22d50b0ff0b71dac271 Mon Sep 17 00:00:00 2001 From: Khai Truong Date: Sat, 21 Dec 2024 01:51:33 +0700 Subject: [PATCH] refactor --- ext/js/display/display.js | 18 ++++++++---------- ext/js/display/element-overflow-controller.js | 5 ++--- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/ext/js/display/display.js b/ext/js/display/display.js index 3bc722570..49750618a 100644 --- a/ext/js/display/display.js +++ b/ext/js/display/display.js @@ -309,11 +309,6 @@ export class Display extends EventDispatcher { return this._frameVisible; } - /** @type {number} */ - get scrollY() { - return this._windowScroll.y; - } - /** */ async prepare() { // Theme @@ -387,10 +382,13 @@ export class Display extends EventDispatcher { } /** - * @param {number} y + * @param {Element} element */ - setScrollY(y) { - this._windowScroll.toY(y); + scrollUpToElementTop(element) { + const top = this._getElementTop(element); + if (this._windowScroll.y > top) { + this._windowScroll.toY(top); + } } /** @@ -1638,7 +1636,7 @@ export class Display extends EventDispatcher { node = definitionNodes[definitionIndex]; } } - let target = (index === 0 && definitionIndex <= 0) || node === null ? 0 : this.getElementTop(node); + let target = (index === 0 && definitionIndex <= 0) || node === null ? 0 : this._getElementTop(node); if (target !== 0) { if (this._aboveStickyHeader !== null) { @@ -1773,7 +1771,7 @@ export class Display extends EventDispatcher { * @param {Element} element * @returns {number} */ - getElementTop(element) { + _getElementTop(element) { const elementRect = element.getBoundingClientRect(); const documentRect = this._contentScrollBodyElement.getBoundingClientRect(); return elementRect.top - documentRect.top; diff --git a/ext/js/display/element-overflow-controller.js b/ext/js/display/element-overflow-controller.js index 293a97fd3..9e727e4d0 100644 --- a/ext/js/display/element-overflow-controller.js +++ b/ext/js/display/element-overflow-controller.js @@ -139,7 +139,6 @@ export class ElementOverflowController { */ _onToggleButtonClick(e) { const element = /** @type {Element} */ (e.currentTarget); - const entryTop = this._display.getElementTop(element); /** @type {(Element | null)[]} */ const collapsedElements = [ @@ -149,8 +148,8 @@ export class ElementOverflowController { for (const collapsedElement of collapsedElements) { if (collapsedElement === null) { continue; } const collapsed = collapsedElement.classList.toggle('collapsed'); - if (collapsed && this._display.scrollY > entryTop) { - this._display.setScrollY(entryTop); + if (collapsed) { + this._display.scrollUpToElementTop(element); } } }