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

Markdown preview now detects local files better #375

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
83 changes: 71 additions & 12 deletions src/Notepads/Controls/Markdown/MarkdownExtensionView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,23 @@
using Notepads.Controls.TextEditor;
using Notepads.Extensions;
using Notepads.Services;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Microsoft.Toolkit.Uwp.UI;
using Windows.UI.Xaml.Media.Imaging;

public sealed partial class MarkdownExtensionView : UserControl, IContentPreviewExtension
{
private readonly ImageCache _imageCache = new ImageCache();

private bool _isExtensionEnabled;

private string _parentPath;

private readonly char[] ignoredChars = { '?', '#', ' ' };

public bool IsExtensionEnabled
{
get => _isExtensionEnabled;
Expand Down Expand Up @@ -78,18 +84,40 @@ private async void MarkdownTextBlock_ImageResolving(object sender, ImageResolvin

try
{
var imageUri = new Uri(e.Url);
if (Path.GetExtension(imageUri.AbsolutePath)?.ToLowerInvariant() == ".svg")
try
{
// SvgImageSource is not working properly when width and height are not set in uri
// I am disabling Svg parsing here.
// e.Image = await GetImageAsync(e.Url);
e.Handled = true;
var imageUri = new Uri(e.Url);
if (Path.GetExtension(imageUri.AbsolutePath)?.ToLowerInvariant() == ".svg")
{
// SvgImageSource is not working properly when width and height are not set in uri
// I am disabling Svg parsing here.
// e.Image = await GetImageAsync(e.Url);
e.Handled = true;
}
else
{
e.Image = await GetImageAsync(e.Url);
e.Handled = true;
}
}
else
catch (Exception)
{
e.Image = await GetImageAsync(e.Url);
e.Handled = true;
var ignoreIndex = e.Url.IndexOfAny(ignoredChars);
var imagePath = _parentPath + (ignoreIndex > -1
? e.Url.Remove(ignoreIndex).Replace('/', Path.DirectorySeparatorChar)
: e.Url.Replace('/', Path.DirectorySeparatorChar));
if (Path.GetExtension(imagePath)?.ToLowerInvariant() == ".svg")
{
// SvgImageSource is not working properly when width and height are not set in uri
// I am disabling Svg parsing here.
// e.Image = await GetImageAsync(e.Url);
e.Handled = true;
}
else
{
e.Image = await GetLocalImageAsync(imagePath);
e.Handled = true;
}
}
}
catch (Exception ex)
Expand Down Expand Up @@ -133,7 +161,28 @@ private async Task<ImageSource> GetImageAsync(string url)
//}
}

public void Bind(TextEditorCore editorCore)
private async Task<ImageSource> GetLocalImageAsync(string imagePath)
{
var imageFile = await StorageFile.GetFileFromPathAsync(imagePath);

using (var ms = await imageFile.OpenReadAsync())
{
if (Path.GetExtension(imagePath)?.ToLowerInvariant() == ".svg")
{
var image = new SvgImageSource();
await image.SetSourceAsync(ms);
return image;
}
else
{
var image = new BitmapImage();
await image.SetSourceAsync(ms);
return image;
}
}
}

public void Bind(TextEditorCore editorCore, string parentPath)
{
if (_editorCore != null)
{
Expand All @@ -146,6 +195,8 @@ public void Bind(TextEditorCore editorCore)
_editorCore.TextChanged += OnTextChanged;
_editorCore.TextWrappingChanged += OnTextWrappingChanged;
_editorCore.FontSizeChanged += OnFontSizeChanged;

_parentPath = parentPath;
}

private void OnTextChanged(object sender, RoutedEventArgs e)
Expand Down Expand Up @@ -213,8 +264,16 @@ private async void MarkdownTextBlock_OnLinkClicked(object sender, LinkClickedEve

try
{
var uri = new Uri(e.Link);
await Windows.System.Launcher.LaunchUriAsync(uri);
try
{
var uri = new Uri(e.Link);
await Windows.System.Launcher.LaunchUriAsync(uri);
}
catch (Exception)
{
var file = await StorageFile.GetFileFromPathAsync(_parentPath + e.Link.Replace('/', Path.DirectorySeparatorChar));
await Windows.System.Launcher.LaunchFileAsync(file);
}
}
catch (Exception ex)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Notepads/Controls/TextEditor/TextEditor.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ public void ShowHideContentPreview()
{
_contentPreviewExtension = ExtensionProvider?.GetContentPreviewExtension(FileType);
if (_contentPreviewExtension == null) return;
_contentPreviewExtension.Bind(TextEditorCore);
_contentPreviewExtension.Bind(TextEditorCore, System.IO.Path.GetDirectoryName(EditingFilePath) + System.IO.Path.DirectorySeparatorChar);
}

if (SplitPanel == null) LoadSplitView();
Expand Down
2 changes: 1 addition & 1 deletion src/Notepads/Extensions/IContentPreviewExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

public interface IContentPreviewExtension
{
void Bind(TextEditorCore editor);
void Bind(TextEditorCore editor, string parentPath);

bool IsExtensionEnabled { get; set; }

Expand Down
1 change: 1 addition & 0 deletions src/Notepads/Package.appxmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -1181,5 +1181,6 @@
<Capabilities>
<Capability Name="internetClient" />
<rescap:Capability Name="confirmAppClose"/>
<rescap:Capability Name="broadFileSystemAccess" />
</Capabilities>
</Package>