From 4496f521b56a49c5d7ba3dbcc05c44d4b57b404a Mon Sep 17 00:00:00 2001 From: Manjot Sidhu Date: Tue, 10 Aug 2021 11:57:59 +0530 Subject: [PATCH] fix(ib): scroll to local links within page Signed-off-by: Manjot Sidhu --- lib/ui/views/ib/ib_page_view.dart | 33 ++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/lib/ui/views/ib/ib_page_view.dart b/lib/ui/views/ib/ib_page_view.dart index 7027bacf..205876b1 100644 --- a/lib/ui/views/ib/ib_page_view.dart +++ b/lib/ui/views/ib/ib_page_view.dart @@ -83,28 +83,38 @@ class _IbPageViewState extends State { ); } - void _onTapLink(String text, String href, String title) async { - // Confirm if it's a valid URL - if (!(await canLaunch(href))) { - print('[IB]: $href is not a valid link'); - return; + Future _scrollToWidget(String slug) async { + if (_slugMap.containsKey(slug)) { + await _hideButtonController.scrollToIndex(_slugMap[slug], + preferPosition: AutoScrollPosition.begin); + } else { + print('[IB]: $slug not present in map'); } + } - // If Interactive Book link + Future _onTapLink(String text, String href, String title) async { + // If Absolute Interactive Book link if (href.startsWith(EnvironmentConfig.IB_BASE_URL)) { // If URI is same as the current page if (_model.pageData.pageUrl.startsWith(href)) { // It's local link - // (TODO) Scroll to that local widget - return; + return _scrollToWidget(href.substring(1)); } else { // Try to navigate to another page using url // (TODO) We need [IbLandingViewModel] to be able to get Chapter using [httpUrl] - return; + return launchURL(href); } } - launchURL(href); + // Local links + if (href.startsWith('#')) { + return _scrollToWidget(href.substring(1)); + } + + // Confirm if it's a valid URL + if (await canLaunch(href)) { + return launchURL(href); + } } Widget _buildMarkdownImage(Uri uri, String title, String alt) { @@ -235,8 +245,7 @@ class _IbPageViewState extends State { var slug = IbEngineService.getSlug(title); Navigator.pop(context); - await _hideButtonController.scrollToIndex(_slugMap[slug], - preferPosition: AutoScrollPosition.begin); + await _scrollToWidget(slug); } Widget _buildTocListTile(String leading, String content,