Skip to content

Commit

Permalink
Changed from WebView to MD TextBlock
Browse files Browse the repository at this point in the history
- Changed CharacterView Description from WebView to MarkdownTextBlock.

- Chnaged in staffview that same
  • Loading branch information
CosmicPredator committed Feb 16, 2024
1 parent c66e3c8 commit 507727e
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 181 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@
<ItemsRepeater
x:Name="TagsItemRepeater"
VerticalAlignment="Top"
ItemTemplate="{StaticResource TagsItemTemplate}"/>
ItemTemplate="{StaticResource TagsItemTemplate}" />
</ScrollViewer>
</Grid>
<Button
Expand Down
8 changes: 0 additions & 8 deletions src/AniMoe.App/ViewModels/MediaViewViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public class MediaViewViewModel: ObservableObject
private int MediaId;
private DispatcherQueue dispatcherQueue;
private bool showSummary = true;
private string descriptionText;

public List<int> Series
{
Expand Down Expand Up @@ -79,12 +78,6 @@ public bool IsLoading
set => SetProperty(ref isLoading, value);
}

public string DescriptionText
{
get => descriptionText;
set => SetProperty(ref descriptionText, value);
}

public IAsyncRelayCommand LoadView { get; }

public MediaViewViewModel(int mediaId, DispatcherQueue queue)
Expand All @@ -101,7 +94,6 @@ await dispatcherQueue.EnqueueAsync(async () =>
Loaded = false;
IsLoading = !Loaded;
Model = await Initialize.FetchData(MediaId);
DescriptionText = Model.Data.Media.Description.RemoveHtmlTags();
Series = Model.Data.Media.Stats.ScoreDistribution.Select(
x => x.Amount
).ToList();
Expand Down
17 changes: 4 additions & 13 deletions src/AniMoe.App/Views/CharacterView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -110,19 +110,10 @@
BorderThickness="1"
Canvas.ZIndex="1"
CornerRadius="10">
<TextBlock Text="{x:Bind ViewModel.Model.Data.Character.Description}" TextWrapping="Wrap" />
<!--<WebView2
x:Name="DescriptionWebView"
AllowFocusOnInteraction="False"
Canvas.ZIndex="0"
DefaultBackgroundColor="Transparent"
NavigationCompleted="DescriptionWebView_NavigationCompleted"
PointerWheelChanged="DescriptionWebView_PointerWheelChanged">
<WebView2.Resources>
<SolidColorBrush x:Name="BrushForThemeBackgroundColor" Color="Transparent" />
<SolidColorBrush x:Name="BrushForThemeBackgroundColor_HC" Color="Transparent" />
</WebView2.Resources>
</WebView2>-->
<toolkit:MarkdownTextBlock
Background="Transparent"
LinkClicked="MarkdownTextBlock_LinkClicked"
Text="{x:Bind ViewModel.Model.Data.Character.Description}" />
</Grid>
</Grid>
<Grid
Expand Down
82 changes: 6 additions & 76 deletions src/AniMoe.App/Views/CharacterView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,83 +55,8 @@ private void MasterScrollViewer_Loaded(object sender, RoutedEventArgs e)
{
SegmentedBox.SelectionChanged += SegmentedBox_SelectionChanged;
SegmentedBox.SelectedIndex = 0;
//await DescriptionWebView.EnsureCoreWebView2Async();
//await Task.Delay(500);
//DescriptionWebView.NavigateToString(
// mdToHtmlParser.Convert(ViewModel.Model.Data.Character.Description)
//);
}

//private async void DescriptionWebView_NavigationCompleted(WebView2 sender, Microsoft.Web.WebView2.Core.CoreWebView2NavigationCompletedEventArgs args)
//{
// string jsx = "document.body.offsetHeight";
// string heightString = await DescriptionWebView.ExecuteScriptAsync(jsx);
// Debug.WriteLine(heightString);

// if( double.TryParse(heightString, out double height) )
// {
// WebGrid.Height = height >= 800 ? height : height / 4;
// }

// string js = @"
// var summaryTags = document.getElementsByTagName('summary');
// for (var i = 0; i < summaryTags.length; i++) {
// summaryTags[i].addEventListener('click', function() {
// window.chrome.webview.postMessage('summaryClicked');
// });
// }
// function calculateContentHeight() {
// var body = document.body;
// var html = document.documentElement;
// var height = Math.max(body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight);
// window.chrome.webview.postMessage(height.toString());
// }
// calculateContentHeight();
// ";
// await DescriptionWebView.ExecuteScriptAsync(js);
// DescriptionWebView.WebMessageReceived += DescriptionWebView_WebMessageReceived;
//}

//private void DescriptionWebView_WebMessageReceived(WebView2 sender, Microsoft.Web.WebView2.Core.CoreWebView2WebMessageReceivedEventArgs args)
//{
// string message = args.TryGetWebMessageAsString();
// if( !string.IsNullOrEmpty(message) )
// {
// if( message == "summaryClicked" )
// {
// // Update WebView2 height when summary tag is clicked
// UpdateWebViewHeight();
// }
// else if( double.TryParse(message, out double contentHeight) )
// {
// // Use the content height value
// WebGrid.Height = contentHeight;
// }
// }
//}

//private async void UpdateWebViewHeight()
//{
// // Inject JavaScript code to recalculate content height after summary tag is clicked
// string js = "calculateContentHeight();";
// await DescriptionWebView.ExecuteScriptAsync(js);
//}

//private void DescriptionWebView_PointerWheelChanged(object sender, PointerRoutedEventArgs e)
//{
// try
// {
// DescriptionWebView.IsHitTestVisible = false;
// timer.Debounce(() => {
// DescriptionWebView.IsHitTestVisible = true;
// }, TimeSpan.FromMilliseconds(500));
// }
// catch( Exception ex )
// {
// Log.Error(ex, "Some Error Occured");
// }
//}

private void SegmentedBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Log.Information("Hello");
Expand Down Expand Up @@ -165,8 +90,13 @@ private void SegmentedBox_SelectionChanged(object sender, SelectionChangedEventA

public void Dispose()
{
//if (DescriptionWebView is not null) DescriptionWebView.Close();
GC.Collect();
}

private async void MarkdownTextBlock_LinkClicked(object sender, CommunityToolkit.WinUI.UI.Controls.LinkClickedEventArgs e)
{
await Windows.System.Launcher.LaunchUriAsync(
new Uri(e.Link));
}
}
}
7 changes: 4 additions & 3 deletions src/AniMoe.App/Views/MediaView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@
<TextBlock
FontSize="15"
MaxLines="3"
Text="{x:Bind ViewModel.DescriptionText}"
Text="{x:Bind ViewModel.Model.Data.Media.Description}"
TextWrapping="Wrap" />
<HyperlinkButton
Padding="0"
Expand All @@ -170,9 +170,10 @@
</Expander.Header>
<Expander.Content>
<Grid Margin="5">
<TextBlock
<toolkit:MarkdownTextBlock
FontSize="15"
Text="{x:Bind ViewModel.DescriptionText}"
Text="{x:Bind ViewModel.Model.Data.Media.Description}"
Background="Transparent"
TextWrapping="Wrap" />
</Grid>
</Expander.Content>
Expand Down
6 changes: 4 additions & 2 deletions src/AniMoe.App/Views/StaffView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,10 @@
BorderThickness="1"
Canvas.ZIndex="1"
CornerRadius="10">
<TextBlock Text="{x:Bind ViewModel.Model.Data.Staff.Description}"
TextWrapping="Wrap"/>
<toolkit:MarkdownTextBlock
Background="Transparent"
LinkClicked="MarkdownTextBlock_LinkClicked"
Text="{x:Bind ViewModel.Model.Data.Staff.Description}" />
<!--<WebView2
x:Name="DescriptionWebView"
AllowFocusOnInteraction="False"
Expand Down
84 changes: 6 additions & 78 deletions src/AniMoe.App/Views/StaffView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,85 +48,8 @@ public StaffView()
this.InitializeComponent();
}

//private async void DescriptionWebView_NavigationCompleted(WebView2 sender, Microsoft.Web.WebView2.Core.CoreWebView2NavigationCompletedEventArgs args)
//{
// string jsx = "document.body.offsetHeight";
// string heightString = await DescriptionWebView.ExecuteScriptAsync(jsx);
// Debug.WriteLine(heightString);
// if( double.TryParse(heightString, out double height) )
// {
// WebGrid.Height = height >= 800 ? height : height / 4;
// }

// string js = @"
// var summaryTags = document.getElementsByTagName('summary');
// for (var i = 0; i < summaryTags.length; i++) {
// summaryTags[i].addEventListener('click', function() {
// window.chrome.webview.postMessage('summaryClicked');
// });
// }
// function calculateContentHeight() {
// var body = document.body;
// var html = document.documentElement;
// var height = Math.max(body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight);
// window.chrome.webview.postMessage(height.toString());
// }
// calculateContentHeight();
// ";
// await DescriptionWebView.ExecuteScriptAsync(js);
// DescriptionWebView.WebMessageReceived += DescriptionWebView_WebMessageReceived;
//}

//private async void UpdateWebViewHeight()
//{
// // Inject JavaScript code to recalculate content height after summary tag is clicked
// string js = "calculateContentHeight();";
// await DescriptionWebView.ExecuteScriptAsync(js);
//}

//private void DescriptionWebView_WebMessageReceived(WebView2 sender, Microsoft.Web.WebView2.Core.CoreWebView2WebMessageReceivedEventArgs args)
//{
// string message = args.TryGetWebMessageAsString();
// if( !string.IsNullOrEmpty(message) )
// {
// if( message == "summaryClicked" )
// {
// // Update WebView2 height when summary tag is clicked
// UpdateWebViewHeight();
// }
// else if( double.TryParse(message, out double contentHeight) )
// {
// // Use the content height value
// WebGrid.Height = contentHeight;
// }
// }
//}

//private void DescriptionWebView_PointerWheelChanged(object sender, PointerRoutedEventArgs e)
//{
// try
// {
// DescriptionWebView.IsHitTestVisible = false;
// timer.Debounce(() => {
// DescriptionWebView.IsHitTestVisible = true;
// }, TimeSpan.FromMilliseconds(500));
// }
// catch( Exception ex )
// {
// Log.Error(ex, "Some Error Occured");
// }
//}

private void MasterScrollViewer_Loaded(object sender, RoutedEventArgs e)
{
//if (ViewModel.Model.Data.Staff.Description != null)
//{
// await DescriptionWebView.EnsureCoreWebView2Async();
// await Task.Delay(500);
// DescriptionWebView.NavigateToString(
// mdToHtmlParser.Convert(ViewModel.Model.Data.Staff.Description)
// );
//}
LoadStaffList();
}

Expand Down Expand Up @@ -190,8 +113,13 @@ private void CharImage_PointerPressed(object sender, PointerRoutedEventArgs e)

public void Dispose()
{
//if (DescriptionWebView is not null) DescriptionWebView.Close();
GC.Collect();
}

private async void MarkdownTextBlock_LinkClicked(object sender, LinkClickedEventArgs e)
{
await Windows.System.Launcher.LaunchUriAsync(
new Uri(e.Link));
}
}
}

0 comments on commit 507727e

Please sign in to comment.