From 24644beeb645d653e9636e2fd742c5dc1c4401b4 Mon Sep 17 00:00:00 2001 From: Istvan Soos Date: Fri, 21 Feb 2025 16:00:40 +0100 Subject: [PATCH] Remove custom scroll handling. --- pkg/web_app/lib/script.dart | 2 - pkg/web_app/lib/src/scroll.dart | 78 --------------------------------- 2 files changed, 80 deletions(-) delete mode 100644 pkg/web_app/lib/src/scroll.dart diff --git a/pkg/web_app/lib/script.dart b/pkg/web_app/lib/script.dart index afd925eff2..7e4749c8a4 100644 --- a/pkg/web_app/lib/script.dart +++ b/pkg/web_app/lib/script.dart @@ -16,7 +16,6 @@ import 'src/likes.dart'; import 'src/mobile_nav.dart'; import 'src/page_updater.dart'; import 'src/screenshot_carousel.dart'; -import 'src/scroll.dart'; import 'src/search.dart'; import 'src/widget/widget.dart' show setupWidgets; @@ -36,7 +35,6 @@ void main() { void _setupAllEvents() { setupSearch(); - setupScroll(); setupFoldable(); setupHoverable(); setupMobileNav(); diff --git a/pkg/web_app/lib/src/scroll.dart b/pkg/web_app/lib/src/scroll.dart deleted file mode 100644 index 8544c3ba8b..0000000000 --- a/pkg/web_app/lib/src/scroll.dart +++ /dev/null @@ -1,78 +0,0 @@ -// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. - -// TODO: migrate to package:web -// ignore: deprecated_member_use -import 'dart:html'; - -void setupScroll() { - _setEventForAnchorScroll(); - window.onHashChange.listen((_) { - _scrollToHash(); - }); - _scrollToHash(); -} - -void _scrollToHash() { - final hash = window.location.hash; - if (hash.isNotEmpty) { - final id = hash.startsWith('#') ? hash.substring(1) : hash; - final list = - document.querySelectorAll('[id="${Uri.encodeQueryComponent(id)}"]'); - if (list.isEmpty) { - return; - } - // if there is an element on the current tab, scroll to it - for (final e in list) { - if (e.offsetHeight > 0) { - _scrollTo(e); - return; - } - } - // fallback, should not happen - _scrollTo(list.first); - } -} - -void _setEventForAnchorScroll() { - document.body!.onClick.listen((e) { - // locate the tag - var target = e.target as Element?; - while (target != null && - target.tagName.toLowerCase() != 'a' && - target.tagName.toLowerCase() != 'body') { - target = target.parent; - } - if (target is AnchorElement && - target.getAttribute('href') == target.hash && - (target.hash?.isNotEmpty ?? false)) { - final elem = document.querySelector(target.hash!); - if (elem != null) { - window.history - .pushState({}, document.title, target.hash); - e.preventDefault(); - _scrollTo(elem); - } - } - }); -} - -Future _scrollTo(Element elem) async { - // Chrome could provide inconsistent position data just after the page has - // been loaded. The first animation frame makes sure that the rendering is - // stabilized and the position data is correct. - await window.animationFrame; - final int stepCount = 30; - for (int i = 0; i < stepCount; i++) { - await window.animationFrame; - final int offsetTop = elem.offsetTop - 12; - final int scrollY = window.scrollY; - final int diff = offsetTop - scrollY; - // Stop early if the browser already jumped to it. - if (i == 0 && diff <= 12) { - break; - } - window.scrollTo(window.scrollX, scrollY + diff * (i + 1) ~/ stepCount); - } -}