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

TextToolbar - Switch Format from Custom Formatter #1514

Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions Microsoft.Toolkit.Uwp.SampleApp/Common/EnumConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ public EnumConverter(Type enumType)

public object Convert(object value, Type targetType, object parameter, string language)
{
return value;
return value.ToString();
}

public object ConvertBack(object value, Type targetType, object parameter, string language)
{
return Enum.Parse(_enumType, value.ToString());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<controls:TextToolbar x:Name="Toolbar"
Editor="{Binding ElementName=EditZone}"
IsEnabled="@[IsEnabled:Bool:True]"
Format="@[Format:Enum:Format.RichText]"
Format="@[Format:Enum:Format.RichText]@"
Background="#4C4F4F4F" />

<ScrollViewer Grid.Row="1">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
// ******************************************************************

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Toolkit.Uwp.SampleApp.Models;
using Microsoft.Toolkit.Uwp.SampleApp.SamplePages.TextToolbarSamples;
using Microsoft.Toolkit.Uwp.UI.Controls;
using Microsoft.Toolkit.Uwp.UI.Controls.TextToolbarButtons;
using Microsoft.Toolkit.Uwp.UI.Controls.TextToolbarFormats;
using Microsoft.Toolkit.Uwp.UI.Controls.TextToolbarFormats.MarkDown;
using Microsoft.Toolkit.Uwp.UI.Extensions;
using Windows.System;
Expand All @@ -38,17 +41,21 @@ public void OnXamlRendered(FrameworkElement control)
{
_toolbar = control.FindChildByName("Toolbar") as TextToolbar;

var editZone = control.FindChildByName("EditZone") as RichEditBox;
if (editZone != null)
if (control.FindChildByName("EditZone") is RichEditBox editZone)
{
editZone.TextChanged += EditZone_TextChanged;
}

_previewer = control.FindChildByName("Previewer") as MarkdownTextBlock;
if (_previewer != null)
if (control.FindChildByName("Previewer") is MarkdownTextBlock previewer)
{
_previewer = previewer;
_previewer.LinkClicked += Previewer_LinkClicked;
}

if (ToolbarFormat != null && (Format)ToolbarFormat.Value == Format.Custom)
{
UseCustomFormatter();
}
}

protected override void OnNavigatedTo(NavigationEventArgs e)
Expand All @@ -60,7 +67,7 @@ protected override void OnNavigatedTo(NavigationEventArgs e)
var button = _toolbar?.GetDefaultButton(ButtonType.Bold);
if (button != null)
{
button.Visibility = button.Visibility == Windows.UI.Xaml.Visibility.Visible ? Windows.UI.Xaml.Visibility.Collapsed : Windows.UI.Xaml.Visibility.Visible;
button.Visibility = button.Visibility == Visibility.Visible ? Visibility.Collapsed : Visibility.Visible;
}
});

Expand All @@ -73,22 +80,45 @@ protected override void OnNavigatedTo(NavigationEventArgs e)
{
UseCustomFormatter();
});

Shell.Current.RegisterNewCommand("Reset Layout", (sender, args) =>
{
ResetLayout();
});
}

private void UseCustomFormatter()
private void ResetLayout()
{
if (_toolbar == null)
{
return;
}

_toolbar.CustomButtons.Clear();
if (_toolbar.DefaultButtons != null)
{
foreach (var item in _toolbar.DefaultButtons)
{
if (item is ToolbarButton button)
{
button.Visibility = Visibility.Visible;
}
}
}
}

private void UseCustomFormatter()
{
if (_toolbar == null || ToolbarFormat == null)
{
return;
}

var formatter = new SampleFormatter(_toolbar);
_toolbar.Format = UI.Controls.TextToolbarFormats.Format.Custom;
ToolbarFormat.Value = Format.Custom;
_toolbar.Formatter = formatter;
}

private int DemoCounter { get; set; } = 0;

private void AddCustomButton()
{
if (_toolbar == null)
Expand Down Expand Up @@ -116,8 +146,7 @@ private void AddCustomButton()
ShortcutKey = shortcut,
Activation = (b) =>
{
var md = _toolbar.Formatter as MarkDownFormatter;
if (md != null)
if (_toolbar.Formatter is MarkDownFormatter md)
{
md.SetSelection($"[{demoText}]", $"[/{demoText}]");
}
Expand All @@ -143,19 +172,36 @@ private void Previewer_LinkClicked(object sender, LinkClickedEventArgs e)
}
}

private void EditZone_TextChanged(object sender, Windows.UI.Xaml.RoutedEventArgs e)
private void EditZone_TextChanged(object sender, RoutedEventArgs e)
{
if (_toolbar == null || _previewer == null)
{
return;
}

var md = _toolbar.Formatter as MarkDownFormatter;
if (md != null)
if (_toolbar.Formatter is MarkDownFormatter md)
{
string text = md.Text;
_previewer.Text = string.IsNullOrWhiteSpace(text) ? "Nothing to Preview" : text;
}
}

private int DemoCounter { get; set; } = 0;

private ValueHolder ToolbarFormat
{
get
{
if (DataContext is Sample sample)
{
if (sample.PropertyDescriptor.Expando is IDictionary<string, object> properties && properties.TryGetValue("Format", out var format))
{
return format as ValueHolder;
}
}

return null;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ private void OnButtonMapModified(object sender, NotifyCollectionChangedEventArgs
}

break;

case NotifyCollectionChangedAction.Reset:
BuildBar();
break;
}
}
}
Expand Down