Skip to content

Commit 492d244

Browse files
committed
Improve scroll handling and tooltip safety
Adds passive option to wheel event listener for better scroll performance and prevents errors when removing indicators in AspectRatioUtils. PresetManagerDialog now preserves scroll positions when updating the grid and uses requestAnimationFrame for layout updates. TooltipManager now safely checks for tooltip element before hiding.
1 parent b47be1a commit 492d244

3 files changed

Lines changed: 38 additions & 6 deletions

File tree

js/utils/AspectRatioUtils.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,12 +252,14 @@ export class AspectRatioUtils {
252252
state.indicator.addEventListener('wheel', (e) => {
253253
e.preventDefault();
254254
scrollWrapper.scrollLeft += e.deltaY;
255-
});
255+
}, { passive: false });
256256

257257
container.appendChild(state.indicator);
258258
} else if (!needsIndicator && state.indicator) {
259-
// Remove indicator
260-
container.removeChild(state.indicator);
259+
// Remove indicator (with safety check to prevent errors)
260+
if (state.indicator.parentNode === container) {
261+
container.removeChild(state.indicator);
262+
}
261263
state.indicator = null;
262264
}
263265
};

js/utils/PresetManagerDialog.js

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,22 @@ export class PresetManagerDialog {
478478
const presetsGrid = this.presetsGrid;
479479
if (!presetsGrid || !this.selectedCategory) return;
480480

481+
// Save scroll positions before clearing DOM
482+
const scrollPositions = new Map();
483+
484+
// Save main grid horizontal scroll
485+
const gridScrollLeft = presetsGrid.scrollLeft;
486+
487+
// Save each column's vertical scroll position
488+
const existingColumns = presetsGrid.querySelectorAll('.aspect-ratio-column');
489+
existingColumns.forEach(column => {
490+
const ratioText = column.querySelector('.aspect-ratio-column-title')?.textContent;
491+
const ratioList = column.querySelector('.aspect-ratio-column-list');
492+
if (ratioText && ratioList) {
493+
scrollPositions.set(ratioText, ratioList.scrollTop);
494+
}
495+
});
496+
481497
presetsGrid.innerHTML = '';
482498

483499
// Change to flex layout for columns (matching AspectRatioSelector structure)
@@ -556,8 +572,21 @@ export class PresetManagerDialog {
556572
this.presetPreviewResizeHandler = null;
557573
}
558574

559-
// Update scroll indicators after DOM renders (using setTimeout to ensure layout is complete)
560-
setTimeout(() => {
575+
// Update scroll indicators after DOM renders (using requestAnimationFrame to ensure layout is complete)
576+
requestAnimationFrame(() => {
577+
// Restore scroll positions after DOM is recreated
578+
// Restore horizontal scroll of main grid
579+
presetsGrid.scrollLeft = gridScrollLeft;
580+
581+
// Restore vertical scroll of each column
582+
columns.forEach(column => {
583+
const ratioText = column.querySelector('.aspect-ratio-column-title')?.textContent;
584+
const ratioList = column.querySelector('.aspect-ratio-column-list');
585+
if (ratioText && ratioList && scrollPositions.has(ratioText)) {
586+
ratioList.scrollTop = scrollPositions.get(ratioText);
587+
}
588+
});
589+
561590
// Update column scroll indicators
562591
AspectRatioUtils.updateColumnScrollIndicators(columns);
563592

@@ -575,7 +604,7 @@ export class PresetManagerDialog {
575604
AspectRatioUtils.updateColumnScrollIndicators(columns);
576605
};
577606
window.addEventListener('resize', this.presetPreviewResizeHandler);
578-
}, 0);
607+
});
579608
}
580609

581610
/**

js/utils/preset-manager/TooltipManager.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ export class TooltipManager {
253253
* Hides the tooltip
254254
*/
255255
hideTooltip() {
256+
if (!this.tooltipElement) return;
256257
this.tooltipElement.style.opacity = '0';
257258
}
258259

0 commit comments

Comments
 (0)