Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ib): scroll to local links within page #125

Merged
merged 1 commit into from
Aug 12, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 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,38 @@ 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)) {
return launchURL(href);
}
}

Widget _buildMarkdownImage(Uri uri, String title, String alt) {
Expand Down Expand Up @@ -235,8 +245,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