Skip to content

Commit

Permalink
fix(ib): scroll to local links within page
Browse files Browse the repository at this point in the history
Signed-off-by: Manjot Sidhu <[email protected]>
  • Loading branch information
manjotsidhu committed Aug 10, 2021
1 parent 9e9287f commit 910e2c5
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions lib/ui/views/ib/ib_page_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,28 +83,39 @@ class _IbPageViewState extends State<IbPageView> {
);
}

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)) {
print('[IB]: $href is not a valid link');
return launchURL(href);
}
}

Widget _buildMarkdownImage(Uri uri, String title, String alt) {
Expand Down Expand Up @@ -235,8 +246,7 @@ class _IbPageViewState extends State<IbPageView> {
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,
Expand Down

0 comments on commit 910e2c5

Please sign in to comment.