Skip to content

Commit

Permalink
new feature: save scroll position
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal Diviš authored and Michal Diviš committed Jul 14, 2022
1 parent 99e09d2 commit 2645c4c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
20 changes: 20 additions & 0 deletions README.md
Expand Up @@ -18,6 +18,16 @@ And use the `HtmlViewer` control
<darkhtmlviewer:HtmlViewer x:Name="htmlViewer" />
```

### Commands & methods
`LoadCommand` => `void Load(string html)`\
`ScrollCommand` => `Task ScrollAsync(string elementId)`\
`ScrollOnNextLoadCommand` => `void ScrollOnNextLoad(string elementId)`\
`SearchCommand` => `Task SearchAsync(string text)`\
`SearchOnNextLoadCommand` => `void SearchOnNextLoad(string text)`\
`SaveScrollPositionForNextLoadCommand` => `SaveScrollPositionForNextLoadAsync()`\
`PrintCommand` => `Task PrintAsync()`\
`ZoomCommand` => `void Zoom(double zoom)`

### Loading HTML content
To load content into the viewer, bind an HTML string to it's `HtmlContent` property
```XAML
Expand Down Expand Up @@ -70,6 +80,16 @@ To scroll to a specific element id, you have several options.
Content="Scroll to elementId on next load" />
```

### Save scroll position
Saves the current scroll position and tries to restore it next time HTML content is loaded. If `ScrollOnNextLoad` is used as well, this will be ignored

`SaveScrollPositionForNextLoadCommand`: will try to scroll to a specific element in the next loaded HTML file
```XAML
<Button
Command="{Binding ElementName=htmlViewer, Path=SaveScrollPositionForNextLoadCommand}"
Content="Save scroll position for next load" />
```

### Search

`SearchCommand`: finds a search term on the current page
Expand Down
5 changes: 5 additions & 0 deletions demos/DarkHtmlViewerBasicDemo/DemoView.xaml
Expand Up @@ -48,6 +48,11 @@
Command="{Binding ElementName=htmlViewer, Path=SearchOnNextLoadCommand}"
CommandParameter="{Binding ElementName=txtSearch, Path=Text}"
Content="Search on next load" />
<Button
Margin="5"
Padding="10,5,10,5"
Command="{Binding ElementName=htmlViewer, Path=SaveScrollPositionForNextLoadCommand}"
Content="Save scroll position for next load" />
<Button
Margin="5"
Padding="10,5,10,5"
Expand Down
33 changes: 33 additions & 0 deletions src/DarkHtmlViewer/HtmlViewer.xaml.cs
Expand Up @@ -57,6 +57,7 @@ public ICommand LinkClickedCommand
public ICommand LoadCommand => new DarkCommand<string>(Load);
public ICommand ScrollCommand => new DarkAsyncCommand<string>(ScrollAsync);
public ICommand ScrollOnNextLoadCommand => new DarkCommand<string>(ScrollOnNextLoad);
public ICommand SaveScrollPositionForNextLoadCommand => new DarkAsyncCommand(SaveScrollPositionForNextLoadAsync);
public ICommand SearchCommand => new DarkAsyncCommand<string>(SearchAsync);
public ICommand SearchOnNextLoadCommand => new DarkCommand<string>(SearchOnNextLoad);
public ICommand PrintCommand => new DarkAsyncCommand(PrintAsync);
Expand Down Expand Up @@ -191,6 +192,12 @@ private async void WebView2_NavigationCompleted(object sender, CoreWebView2Navig
await ScrollAsync(_scrollToNext);
_scrollToNext = null;
}
else if(_scrollPositionXNext is not null)
{
await ScrollAsync(_scrollPositionXNext, _scrollPositionYNext);
_scrollPositionXNext = null;
_scrollPositionYNext = null;
}

if (_textToFind is not null)
{
Expand All @@ -214,6 +221,15 @@ public async Task ScrollAsync(string elementId)
await webView2.ExecuteScriptAsync(script);
}

/// <summary>
/// Tries to scroll to a position
/// </summary>
private async Task ScrollAsync(string x, string y)
{
var script = $"window.scrollTo({x}, {y});";
await webView2.ExecuteScriptAsync(script);
}

/// <summary>
/// Sets the elementId to be scrolled to next time HTML content is loaded
/// </summary>
Expand All @@ -224,6 +240,23 @@ public void ScrollOnNextLoad(string elementId)

#endregion

#region Save scroll position

private string _scrollPositionXNext = null;
private string _scrollPositionYNext = null;

/// <summary>
/// Saves the current scroll position and tries to restore it next time HTML content is loaded
/// <para>If <see cref="ScrollOnNextLoad"/> is used as well, this will be ignored</para>
/// </summary>
public async Task SaveScrollPositionForNextLoadAsync()
{
_scrollPositionXNext = await webView2.ExecuteScriptAsync("window.scrollX");
_scrollPositionYNext = await webView2.ExecuteScriptAsync("window.scrollY");
}

#endregion

#region Search

private string _textToFind = null;
Expand Down

0 comments on commit 2645c4c

Please sign in to comment.