196 lines
7.5 KiB
C#
196 lines
7.5 KiB
C#
|
|
using System.IO;
|
|
using System.ComponentModel;
|
|
using Avalonia;
|
|
using Avalonia.Controls;
|
|
using Avalonia.Layout;
|
|
using Avalonia.Platform.Storage;
|
|
using Avalonia.Controls.Primitives;
|
|
using Avalonia.Media;
|
|
using Avalonia.Input;
|
|
using Avalonia.Interactivity;
|
|
using Avalonia.VisualTree;
|
|
using CommunityToolkit.Mvvm.Input;
|
|
using Markdown.Avalonia;
|
|
|
|
namespace PostIt.Views.Blogs;
|
|
|
|
public partial class BlogsPage : ContentPage
|
|
{
|
|
private static readonly RelayCommand<string?> DisabledLinkCommand =
|
|
new(static _ => { }, static _ => false);
|
|
private bool? _isWideLayout;
|
|
private bool? _isCompactEditor;
|
|
private ViewModels.BlogsViewModel? _observedViewModel;
|
|
|
|
public BlogsPage()
|
|
{
|
|
InitializeComponent();
|
|
AddHandler(InputElement.GotFocusEvent, (_, _) => QueueLayoutUpdate(), RoutingStrategies.Bubble);
|
|
AddHandler(InputElement.LostFocusEvent, (_, _) => QueueLayoutUpdate(), RoutingStrategies.Bubble);
|
|
SizeChanged += (_, _) =>
|
|
Avalonia.Threading.Dispatcher.UIThread.Post(() =>
|
|
{
|
|
UpdateResponsiveLayout();
|
|
});
|
|
DataContextChanged += (_, _) => UpdateHyperlinkCommand();
|
|
AttachedToVisualTree += (_, _) => UpdateHyperlinkCommand();
|
|
DetachedFromVisualTree += (_, _) =>
|
|
{
|
|
if (_observedViewModel is not null)
|
|
_observedViewModel.PropertyChanged -= ViewModel_PropertyChanged;
|
|
_observedViewModel = null;
|
|
};
|
|
UpdateHyperlinkCommand();
|
|
}
|
|
|
|
private void QueueLayoutUpdate()
|
|
=> Avalonia.Threading.Dispatcher.UIThread.Post(UpdateResponsiveLayout);
|
|
|
|
private void UpdateResponsiveLayout()
|
|
{
|
|
var size = TopLevel.GetTopLevel(this)?.ClientSize ?? Bounds.Size;
|
|
if (size.Width <= 0 || size.Height <= 0)
|
|
return;
|
|
|
|
// Use the window size so compacting the navigation bar cannot change the orientation.
|
|
var isWide = size.Width >= size.Height;
|
|
var compact = this.FindAncestorOfType<MainView>()?.IsSoftKeyboardOpen == true
|
|
&& ArticleEditor.IsKeyboardFocusWithin && !ArticleEditor.IsPreviewVisible;
|
|
var sideBySide = compact && isWide;
|
|
if (_isCompactEditor != compact || _isWideLayout != isWide)
|
|
{
|
|
_isCompactEditor = compact;
|
|
DraftTitleTextBox.IsVisible = !compact || !System.OperatingSystem.IsAndroid();
|
|
PostEditor.ColumnDefinitions = new ColumnDefinitions(sideBySide ? "3*,2*" : "*");
|
|
SetArea(ArticleEditor, sideBySide ? 0 : 1, 0, rowSpan: sideBySide ? 3 : 1);
|
|
SetArea(DraftTitleTextBox, 0, sideBySide ? 1 : 0);
|
|
SetArea(AttachmentsBorder, sideBySide ? 1 : 2, sideBySide ? 1 : 0);
|
|
ArticleEditor.SourceEditor.Padding = compact ? new Thickness(2) : new Thickness(5, 5, 5, 15);
|
|
PostEditor.Margin = new Thickness(compact ? 4 : 12);
|
|
PostEditor.RowSpacing = compact ? 2 : 10;
|
|
}
|
|
SetArea(StatusBar, sideBySide ? 1 : isWide ? 3 : 5, sideBySide ? 2 : 0,
|
|
columnSpan: isWide && !sideBySide ? 3 : 1);
|
|
if (_isWideLayout == isWide)
|
|
return;
|
|
_isWideLayout = isWide;
|
|
|
|
MainGrid.RowDefinitions = new RowDefinitions(isWide ? "Auto,Auto,*,Auto" : "Auto,Auto,*,4,2*,Auto");
|
|
MainGrid.ColumnDefinitions = new ColumnDefinitions(isWide ? "2*,4,*" : "*");
|
|
var sidebarColumn = isWide ? 2 : 0;
|
|
SetArea(BlogsHeaderPanel, 0, sidebarColumn);
|
|
SetArea(PostsListBorder, 2, sidebarColumn);
|
|
SetArea(PostsSplitter, isWide ? 0 : 3, isWide ? 1 : 0, isWide ? 3 : 1);
|
|
SetArea(PostReader, isWide ? 0 : 4, 0, isWide ? 3 : 1);
|
|
SetArea(PostEditor, isWide ? 0 : 4, 0, isWide ? 3 : 1);
|
|
PostsSplitter.ResizeDirection = isWide ? GridResizeDirection.Columns : GridResizeDirection.Rows;
|
|
PostsSplitter.HorizontalAlignment = isWide ? HorizontalAlignment.Center : HorizontalAlignment.Stretch;
|
|
PostsSplitter.VerticalAlignment = isWide ? VerticalAlignment.Stretch : VerticalAlignment.Center;
|
|
PostsSplitter.Margin = isWide ? new Thickness(0, 12) : new Thickness(12, 0);
|
|
}
|
|
|
|
private static void SetArea(Control control, int row, int column, int rowSpan = 1, int columnSpan = 1)
|
|
{
|
|
Grid.SetRow(control, row);
|
|
Grid.SetColumn(control, column);
|
|
Grid.SetRowSpan(control, rowSpan);
|
|
Grid.SetColumnSpan(control, columnSpan);
|
|
}
|
|
|
|
private void UpdateHyperlinkCommand()
|
|
{
|
|
if (_observedViewModel is not null)
|
|
_observedViewModel.PropertyChanged -= ViewModel_PropertyChanged;
|
|
|
|
_observedViewModel = DataContext as ViewModels.BlogsViewModel;
|
|
if (_observedViewModel is not null)
|
|
_observedViewModel.PropertyChanged += ViewModel_PropertyChanged;
|
|
|
|
if (ArticleReader.Engine is IMarkdownEngine2 engine)
|
|
{
|
|
if (_observedViewModel is not null)
|
|
engine.HyperlinkCommand = _observedViewModel.OpenResourceCommand;
|
|
else
|
|
engine.HyperlinkCommand = DisabledLinkCommand;
|
|
}
|
|
|
|
UpdateSaveButtonHighlight();
|
|
}
|
|
|
|
private void ViewModel_PropertyChanged(object? sender, PropertyChangedEventArgs e)
|
|
{
|
|
if (e.PropertyName == nameof(ViewModels.BlogsViewModel.IsDraftModified))
|
|
UpdateSaveButtonHighlight();
|
|
}
|
|
|
|
private void UpdateSaveButtonHighlight()
|
|
{
|
|
if (SaveButton is null)
|
|
return;
|
|
|
|
if (_observedViewModel?.IsDraftModified == true)
|
|
{
|
|
SaveButton.FontWeight = FontWeight.Bold;
|
|
SaveButton.Background = new SolidColorBrush(Color.Parse("#FFF0C2"));
|
|
SaveButton.Foreground = new SolidColorBrush(Color.Parse("#663C00"));
|
|
}
|
|
else
|
|
{
|
|
SaveButton.FontWeight = FontWeight.Normal;
|
|
SaveButton.ClearValue(TemplatedControl.BackgroundProperty);
|
|
SaveButton.ClearValue(TemplatedControl.ForegroundProperty);
|
|
}
|
|
}
|
|
|
|
protected override async void OnApplyTemplate(TemplateAppliedEventArgs e)
|
|
{
|
|
base.OnApplyTemplate(e);
|
|
if (DataContext is ViewModels.BlogsViewModel vm)
|
|
{
|
|
await vm.InitializeAsync();
|
|
}
|
|
}
|
|
|
|
private async void AddAttachment_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
|
|
{
|
|
var topLevel = TopLevel.GetTopLevel(this);
|
|
if (topLevel is null || DataContext is not ViewModels.BlogsViewModel vm)
|
|
return;
|
|
|
|
var files = await topLevel.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions
|
|
{
|
|
Title = "Choisir des fichiers à joindre au billet",
|
|
AllowMultiple = true,
|
|
});
|
|
|
|
if (files.Count == 0)
|
|
return;
|
|
|
|
var uploads = new System.Collections.Generic.List<Yavsc.Api.Client.BlogUploadFile>(files.Count);
|
|
foreach (var file in files)
|
|
{
|
|
await using var stream = await file.OpenReadAsync();
|
|
using var memory = new MemoryStream();
|
|
await stream.CopyToAsync(memory);
|
|
uploads.Add(new Yavsc.Api.Client.BlogUploadFile(file.Name, memory.ToArray(), GetMimeType(file.Name)));
|
|
}
|
|
|
|
foreach (var upload in uploads)
|
|
vm.DraftAttachments.Add(upload);
|
|
}
|
|
|
|
private static string GetMimeType(string fileName)
|
|
{
|
|
var ext = Path.GetExtension(fileName)?.ToLowerInvariant();
|
|
return ext switch
|
|
{
|
|
".png" => "image/png",
|
|
".jpg" or ".jpeg" => "image/jpeg",
|
|
".webp" => "image/webp",
|
|
".gif" => "image/gif",
|
|
".pdf" => "application/pdf",
|
|
_ => "application/octet-stream"
|
|
};
|
|
}
|
|
}
|