Skip to content

Commit

Permalink
Collapse entry will scroll back to top of entry (#1682)
Browse files Browse the repository at this point in the history
* Collapse dictionary will scroll back to top of entry

* refactor

* remove redundant line
  • Loading branch information
khaitruong922 authored Dec 20, 2024
1 parent b39bd51 commit 7372f27
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
12 changes: 11 additions & 1 deletion ext/js/display/display.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export class Display extends EventDispatcher {
/** @type {OptionToggleHotkeyHandler} */
this._optionToggleHotkeyHandler = new OptionToggleHotkeyHandler(this);
/** @type {ElementOverflowController} */
this._elementOverflowController = new ElementOverflowController();
this._elementOverflowController = new ElementOverflowController(this);
/** @type {boolean} */
this._frameVisible = (pageType === 'search');
/** @type {HTMLElement} */
Expand Down Expand Up @@ -381,6 +381,16 @@ export class Display extends EventDispatcher {
}
}

/**
* @param {Element} element
*/
scrollUpToElementTop(element) {
const top = this._getElementTop(element);
if (this._windowScroll.y > top) {
this._windowScroll.toY(top);
}
}

/**
* @param {{clearable?: boolean, useBrowserHistory?: boolean}} details
*/
Expand Down
12 changes: 10 additions & 2 deletions ext/js/display/element-overflow-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@
import {EventListenerCollection} from '../core/event-listener-collection.js';

export class ElementOverflowController {
constructor() {
/**
* @param {import('./display.js').Display} display
*/
constructor(display) {
/** @type {import('./display.js').Display} */
this._display = display;
/** @type {Element[]} */
this._elements = [];
/** @type {?(number|import('core').Timeout)} */
Expand Down Expand Up @@ -141,7 +146,10 @@ export class ElementOverflowController {
];
for (const collapsedElement of collapsedElements) {
if (collapsedElement === null) { continue; }
collapsedElement.classList.toggle('collapsed');
const collapsed = collapsedElement.classList.toggle('collapsed');
if (collapsed) {
this._display.scrollUpToElementTop(element);
}
}
}

Expand Down

0 comments on commit 7372f27

Please sign in to comment.