Bladeren bron

Trying to get rich text editor; but not done yet.

Kenric Nugteren 2 jaren geleden
bovenliggende
commit
8d32bcb057

+ 5 - 0
InABox.DynamicGrid/DynamicEditorGrid.xaml.cs

@@ -587,6 +587,11 @@ namespace InABox.DynamicGrid
                             grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
                             fGeneralHeight += grid == EditorGrid ? element.MinHeight + 5.0F : 0.0F;
                         }
+                        else if(iHeight == int.MinValue)
+                        {
+                            grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
+                            fGeneralHeight += grid == EditorGrid ? element.MinHeight + 5.0F : 0.0F;
+                        }
                         else
                         {
                             grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(iHeight + 5.0F) });

+ 68 - 27
InABox.DynamicGrid/Editors/RichTextEditor.xaml

@@ -15,9 +15,65 @@
         <sf:JustifyAlignmentToggleConverter x:Key="JustifyAlignmentToggleConverter" />
     </UserControl.Resources>
     <Border BorderBrush="DarkGray" BorderThickness="0.75" CornerRadius="2">
-        <DockPanel>
+        <Grid>
+            <Grid.RowDefinitions>
+                <RowDefinition Height="Auto"/>
+                <RowDefinition Height="*"/>
+            </Grid.RowDefinitions>
 
-            <sf:ToolBarAdv x:Name="Toolbar" DockPanel.Dock="Top" Focusable="False" GripperVisibility="Collapsed"
+            <ToolBar x:Name="Toolbar" Grid.Row="0" IsTabStop="False">
+                <ComboBox Margin="12 2 0 2" Focusable="False" Width="160" x:Name="FontFamilyComboBox"
+                          SelectionChanged="fontFamilyCombo_SelectionChanged"
+                          ItemsSource="{Binding Path=FontFamilySource}" />
+                <ComboBox Margin="12 2 0 2" IsTabStop="False" Width="45" x:Name="FontSizeComboBox" SelectedIndex="3"
+                          ItemsSource="{Binding Path=FontSizeSource}" IsEditable="True"
+                          TextBoxBase.TextChanged="fontSizeCombo_TextChanged"/>
+                <Separator/>
+                <ToggleButton x:Name="BoldBtn"
+                              Command="EditingCommands.ToggleBold"
+                              CommandTarget="{Binding ElementName=RtbEditor}"
+                              IsTabStop="False"
+                              Margin="2"/>
+                <ToggleButton x:Name="ItalicBtn"
+                              Command="EditingCommands.ToggleItalic"
+                              CommandTarget="{Binding ElementName=RtbEditor}"
+                              IsTabStop="False"
+                              Margin="2"/>
+                <ToggleButton x:Name="UnderlineBtn"
+                              Command="EditingCommands.ToggleUnderline"
+                              CommandTarget="{Binding ElementName=RtbEditor}"
+                              IsTabStop="False"
+                              Margin="2"/>
+                <Separator/>
+                <ToggleButton x:Name="AlignLeftBtn"
+                              Command="EditingCommands.AlignLeft"
+                              CommandTarget="{Binding ElementName=RtbEditor}"
+                              IsTabStop="False"
+                              Margin="2"/>
+                <ToggleButton x:Name="AlignCenterBtn"
+                              Command="EditingCommands.AlignCenter"
+                              CommandTarget="{Binding ElementName=RtbEditor}"
+                              IsTabStop="False"
+                              Margin="2"/>
+                <ToggleButton x:Name="AlignRightBtn"
+                              Command="EditingCommands.AlignRight"
+                              CommandTarget="{Binding ElementName=RtbEditor}"
+                              IsTabStop="False"
+                              Margin="2"/>
+                <ToggleButton x:Name="AlignJustifyBtn"
+                              Command="EditingCommands.AlignJustify"
+                              CommandTarget="{Binding ElementName=RtbEditor}"
+                              IsTabStop="False"
+                              Margin="2"/>
+                <Separator/>
+                <Button x:Name="HyperlinkBtn"
+                              Click="HyperlinkBtn_Click"
+                              IsTabStop="False"
+                              Margin="2"/>
+            </ToolBar>
+
+            <!--sf:ToolBarAdv x:Name="Toolbar" DockPanel.Dock="Top" Focusable="False" GripperVisibility="Collapsed"
+                           Grid.Row="0"
                            IsOverflowOpen="False">
                 <ComboBox Margin="12 2 0 2" Focusable="False" Width="160" x:Name="fontFamilyCombo"
                           SelectedValue="{Binding Path=Selection.CharacterFormat.FontFamily, Mode=TwoWay, ElementName=Editor, Converter={StaticResource FontFamilyStringConverter}}"
@@ -29,26 +85,6 @@
                                        IsExpanded="False" MoreColorOptionVisibility="Collapsed"
                                        StandardPanelVisibility="Collapsed"
                                        Color="{Binding Path=Selection.CharacterFormat.FontColor ,Mode=TwoWay,ElementName=Editor}" />
-                <sf:ButtonAdv x:Name="Bold" Margin="12 2 0 2" Focusable="False" SizeMode="Small" IsCheckable="True"
-                              IsChecked="{Binding ElementName=Editor, Path=Selection.CharacterFormat.Bold, Mode=TwoWay}" />
-                <sf:ButtonAdv x:Name="Italic" Margin="12 2 0 2" Focusable="False" SizeMode="Small" IsCheckable="True"
-                              IsChecked="{Binding ElementName=Editor,Path=Selection.CharacterFormat.Italic,Mode=TwoWay}" />
-                <sf:ButtonAdv x:Name="Underline" Margin="12 2 0 2" Focusable="False" SizeMode="Small"
-                              IsCheckable="True"
-                              IsChecked="{Binding ElementName=Editor, Path=Selection.CharacterFormat.Underline, Mode=TwoWay, Converter={StaticResource UnderlineToggleConverter}}" />
-                <sf:ToolBarItemSeparator Margin="12 2 0 2" />
-                <sf:ButtonAdv x:Name="AlignLeft" Margin="12 2 0 2" Focusable="False" SizeMode="Small"
-                              IsCheckable="True"
-                              IsChecked="{Binding ElementName=Editor, Path=Selection.ParagraphFormat.TextAlignment, Converter={StaticResource LeftAlignmentToggleConverter}, Mode=TwoWay}" />
-                <sf:ButtonAdv x:Name="AlignCentre" Margin="12 2 0 2" Focusable="False" SizeMode="Small"
-                              IsCheckable="True"
-                              IsChecked="{Binding ElementName=Editor, Path=Selection.ParagraphFormat.TextAlignment, Converter={StaticResource CenterAlignmentToggleConverter}, Mode=TwoWay}" />
-                <sf:ButtonAdv x:Name="AlignRight" Margin="12 2 0 2" Focusable="False" SizeMode="Small"
-                              IsCheckable="True"
-                              IsChecked="{Binding ElementName=Editor, Path=Selection.ParagraphFormat.TextAlignment, Converter={StaticResource RightAlignmentToggleConverter}, Mode=TwoWay}" />
-                <sf:ButtonAdv x:Name="AlignJustify" Margin="12 2 0 2" Focusable="False" SizeMode="Small"
-                              IsCheckable="True"
-                              IsChecked="{Binding ElementName=Editor, Path=Selection.ParagraphFormat.TextAlignment, Converter={StaticResource JustifyAlignmentToggleConverter}, Mode=TwoWay}" />
                 <sf:ToolBarItemSeparator Margin="12 2 0 2" />
                 <sf:ButtonAdv x:Name="Hyperlink" Margin="12 2 0 2" Focusable="False" SizeMode="Small"
                               Command="sf:SfRichTextBoxAdv.ShowHyperlinkDialogCommand"
@@ -64,14 +100,19 @@
                               SmallIcon="pack://application:,,,/Resources/zoomin.png" Click="ZoomIn_Click" />
                 <sf:ButtonAdv x:Name="ZoomOut" Margin="12 2 0 2" Focusable="False" SizeMode="Small"
                               SmallIcon="pack://application:,,,/Resources/zoomout.png" Click="ZoomOut_Click" />
-            </sf:ToolBarAdv>
+            </sf:ToolBarAdv-->
 
-            <sf:SfRichTextBoxAdv x:Name="Editor" DockPanel.Dock="Top" EnableMiniToolBar="False"
+            <!--sf:SfRichTextBoxAdv x:Name="Editor" DockPanel.Dock="Top" EnableMiniToolBar="False"
                                  ContentChanged="RichTextBoxAdv_ContentChanged" BorderThickness="0"
                                  LostFocus="RichTextBoxAdv_LostFocus" OverridesDocumentBackground="True"
                                  LayoutType="Continuous"
-                                 RequestNavigate="Editor_RequestNavigate"/>
-
-        </DockPanel>
+                                 VerticalScrollBarVisibility="False"
+                                 Grid.Row="1"
+                                 RequestNavigate="Editor_RequestNavigate"/-->
+            <RichTextBox x:Name="RtbEditor"
+                         SelectionChanged="RtbEditor_SelectionChanged"
+                         TextChanged="RtbEditor_TextChanged"
+                         Grid.Row="1"/>
+        </Grid>
     </Border>
 </UserControl>

+ 110 - 13
InABox.DynamicGrid/Editors/RichTextEditor.xaml.cs

@@ -1,11 +1,21 @@
 using System.Diagnostics;
+using System.Drawing;
 using System.IO;
+using System.Linq;
 using System.Text;
 using System.Windows;
 using System.Windows.Controls;
+using System.Windows.Controls.Primitives;
+using System.Windows.Documents;
 using System.Windows.Media;
+using HTMLConverter;
 using InABox.WPF;
+using NPOI.OpenXmlFormats.Spreadsheet;
+using NPOI.OpenXmlFormats.Wordprocessing;
 using Syncfusion.Windows.Controls.RichTextBoxAdv;
+using Color = System.Windows.Media.Color;
+using Image = System.Windows.Controls.Image;
+using Inline = System.Windows.Documents.Inline;
 
 namespace InABox.DynamicGrid
 {
@@ -41,7 +51,17 @@ namespace InABox.DynamicGrid
 
             DataContext = new RichTextEditorViewModel();
 
-            Bold.SmallIcon = Properties.Resources.Bold16.AsBitmapImage(16, 16);
+            ConfigureButton(BoldBtn, Properties.Resources.Bold16);
+            ConfigureButton(ItalicBtn, Properties.Resources.Italic16);
+            ConfigureButton(UnderlineBtn, Properties.Resources.Underline16);
+
+            ConfigureButton(AlignLeftBtn, Properties.Resources.AlignTextLeft16);
+            ConfigureButton(AlignCenterBtn, Properties.Resources.AlignTextCenter16);
+            ConfigureButton(AlignRightBtn, Properties.Resources.AlignTextRight16);
+            ConfigureButton(AlignJustifyBtn, Properties.Resources.AlignTextJustify16);
+
+            ConfigureButton(HyperlinkBtn, Properties.Resources.Hyperlink16);
+            /*Bold.SmallIcon = Properties.Resources.Bold16.AsBitmapImage(16, 16);
             Italic.SmallIcon = Properties.Resources.Italic16.AsBitmapImage(16, 16);
             Underline.SmallIcon = Properties.Resources.Underline16.AsBitmapImage(16, 16);
             AlignLeft.SmallIcon = Properties.Resources.AlignTextLeft16.AsBitmapImage(16, 16);
@@ -52,13 +72,18 @@ namespace InABox.DynamicGrid
             Picture.SmallIcon = Properties.Resources.Picture16.AsBitmapImage(16, 16);
             Table.SmallIcon = Properties.Resources.Table16.AsBitmapImage(16, 16);
             ZoomIn.SmallIcon = Properties.Resources.zoomin.AsBitmapImage(16, 16);
-            ZoomOut.SmallIcon = Properties.Resources.zoomout.AsBitmapImage(16, 16);
-            Editor.CaretBrush = new SolidColorBrush(Colors.Black);
+            ZoomOut.SmallIcon = Properties.Resources.zoomout.AsBitmapImage(16, 16);*/
+            /*Editor.CaretBrush = new SolidColorBrush(Colors.Black);
             Editor.FontFamily = new FontFamily("Calibri");
             Editor.FontSize = 12.0F;
-            Editor.Foreground = new SolidColorBrush(Colors.Black);
+            Editor.Foreground = new SolidColorBrush(Colors.Black);*/
             ZoomFactor = 150;
-            Text = "";
+            //Text = "";
+        }
+
+        private void ConfigureButton(ButtonBase button, Bitmap image)
+        {
+            button.Content = new Image { Source = image.AsBitmapImage(16, 16), Width = 16, Height = 16 };
         }
 
         public bool HideToolbar
@@ -77,7 +102,7 @@ namespace InABox.DynamicGrid
             set
             {
                 SetValue(ZoomFactorProperty, value);
-                Editor.ZoomFactor = value;
+                //Editor.ZoomFactor = value;
             }
         }
 
@@ -95,7 +120,7 @@ namespace InABox.DynamicGrid
             set
             {
                 SetValue(ColorProperty, value);
-                Editor.Background = new SolidColorBrush(value);
+                //Editor.Background = new SolidColorBrush(value);
             }
         }
 
@@ -105,8 +130,15 @@ namespace InABox.DynamicGrid
                 content = "";
 
             content = content.Replace("background:#000000", "").Replace("background:NoColor;", "");
-            var ms = new MemoryStream(Encoding.ASCII.GetBytes(content));
-            Editor.Load(ms, FormatType.Html);
+            var xaml = content;
+            if (xaml.StartsWith("<html>"))
+            {
+                xaml = HtmlToXamlConverter.ConvertHtmlToXaml(xaml, false);
+            }
+            var ms = new MemoryStream(Encoding.ASCII.GetBytes(xaml));
+            var range = new TextRange(RtbEditor.Document.ContentStart, RtbEditor.Document.ContentEnd);
+            range.Load(ms, DataFormats.Text);
+            //Editor.Load(ms, FormatType.Html);
             VerticalAlignment = VerticalAlignment.Top;
             VerticalAlignment = VerticalAlignment.Stretch;
 
@@ -120,7 +152,9 @@ namespace InABox.DynamicGrid
             if (bdirty)
             {
                 var ms = new MemoryStream();
-                Editor.Save(ms, FormatType.Html);
+                //Editor.Save(ms, FormatType.Html);
+                var range = new TextRange(RtbEditor.Document.ContentStart, RtbEditor.Document.ContentEnd);
+                range.Save(ms, DataFormats.Xaml);
                 var reader = new StreamReader(ms);
                 curvalue = Encoding.UTF8.GetString(ms.GetBuffer());
                 curvalue = curvalue.Replace("background:#000000", "");
@@ -138,7 +172,7 @@ namespace InABox.DynamicGrid
 
         public void SetColor(Color color)
         {
-            Editor.Background = new SolidColorBrush(color);
+            //Editor.Background = new SolidColorBrush(color);
         }
 
         private static void OnBackgroundPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
@@ -157,12 +191,12 @@ namespace InABox.DynamicGrid
 
         private void ZoomIn_Click(object sender, RoutedEventArgs e)
         {
-            Editor.ZoomFactor = Editor.ZoomFactor * 1.5F;
+            //Editor.ZoomFactor = Editor.ZoomFactor * 1.5F;
         }
 
         private void ZoomOut_Click(object sender, RoutedEventArgs e)
         {
-            Editor.ZoomFactor = Editor.ZoomFactor / 1.5F;
+            //Editor.ZoomFactor = Editor.ZoomFactor / 1.5F;
         }
 
         private void Editor_RequestNavigate(object obj, RequestNavigateEventArgs args)
@@ -171,5 +205,68 @@ namespace InABox.DynamicGrid
             processStartInfo.UseShellExecute = true;
             Process.Start(processStartInfo);
         }
+
+        private bool SelectionHasProperty(DependencyProperty property, object? value)
+        {
+            var temp = RtbEditor.Selection.GetPropertyValue(property);
+            return temp != DependencyProperty.UnsetValue && temp.Equals(value);
+        }
+        private object? SelectionProperty(DependencyProperty property)
+        {
+            return RtbEditor.Selection.GetPropertyValue(property);
+        }
+
+        private void UpdateButtons()
+        {
+            BoldBtn.IsChecked = SelectionHasProperty(Inline.FontWeightProperty, FontWeights.Bold);
+            ItalicBtn.IsChecked = SelectionHasProperty(Inline.FontStyleProperty, FontStyles.Italic);
+            UnderlineBtn.IsChecked = SelectionHasProperty(Inline.TextDecorationsProperty, TextDecorations.Underline);
+
+            AlignLeftBtn.IsChecked = SelectionHasProperty(FlowDocument.TextAlignmentProperty, TextAlignment.Left);
+            AlignCenterBtn.IsChecked = SelectionHasProperty(FlowDocument.TextAlignmentProperty, TextAlignment.Center);
+            AlignRightBtn.IsChecked = SelectionHasProperty(FlowDocument.TextAlignmentProperty, TextAlignment.Right);
+            AlignJustifyBtn.IsChecked = SelectionHasProperty(FlowDocument.TextAlignmentProperty, TextAlignment.Justify);
+
+            FontFamilyComboBox.SelectedItem = SelectionProperty(FlowDocument.FontFamilyProperty);
+            FontSizeComboBox.Text = SelectionProperty(FlowDocument.FontSizeProperty)?.ToString();
+        }
+
+        private void RtbEditor_SelectionChanged(object sender, RoutedEventArgs e)
+        {
+            UpdateButtons();
+        }
+
+        private void fontFamilyCombo_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
+        {
+            if(FontFamilyComboBox.SelectedItem != null)
+            {
+                RtbEditor.Selection.ApplyPropertyValue(FlowDocument.FontFamilyProperty, FontFamilyComboBox.SelectedItem);
+            }
+        }
+
+        private void fontSizeCombo_TextChanged(object sender, TextChangedEventArgs e)
+        {
+            var currentValue = SelectionProperty(FlowDocument.FontSizeProperty)?.ToString();
+
+            var fontSize = FontSizeComboBox.Text;
+            if(double.TryParse(fontSize, out var size))
+            {
+                RtbEditor.Selection.ApplyPropertyValue(FlowDocument.FontSizeProperty, size);
+            }
+            else
+            {
+                FontSizeComboBox.Text = currentValue;
+            }
+        }
+
+        private void RtbEditor_TextChanged(object sender, TextChangedEventArgs e)
+        {
+            UpdateButtons();
+        }
+
+        private void HyperlinkBtn_Click(object sender, RoutedEventArgs e)
+        {
+            var selection = RtbEditor.Selection.Text;
+        }
     }
 }

+ 3 - 3
InABox.DynamicGrid/Editors/RichTextEditorControl.cs

@@ -10,7 +10,7 @@ namespace InABox.DynamicGrid
 
         public override int DesiredHeight()
         {
-            return int.MaxValue;
+            return int.MinValue;
         }
 
         public override int DesiredWidth()
@@ -25,7 +25,7 @@ namespace InABox.DynamicGrid
 
         public override void SetEnabled(bool enabled)
         {
-            Editor.Editor.IsReadOnly = !enabled;
+            //Editor.Editor.IsReadOnly = !enabled;
             Editor.HideToolbar = !enabled;
             Editor.ZoomFactor = 100;
             SetColor(enabled ? Color : Colors.WhiteSmoke);
@@ -39,7 +39,7 @@ namespace InABox.DynamicGrid
         protected override FrameworkElement CreateEditor()
         {
             using var profiler = new Profiler(true);
-            MinHeight = 250;
+            MinHeight = 80;
             Editor = new RichTextEditor
             {
                 VerticalAlignment = VerticalAlignment.Stretch,

+ 1 - 0
InABox.DynamicGrid/InABox.DynamicGrid.csproj

@@ -30,6 +30,7 @@
     <ItemGroup>
         <PackageReference Include="Extended.Wpf.Toolkit" Version="4.4.0" />
         <PackageReference Include="GhostScript.NetCore" Version="1.0.1" />
+        <PackageReference Include="HtmlToXamlConverter" Version="1.0.5727.24510" />
         <PackageReference Include="Syncfusion.Grid.WPF" Version="20.2.0.46" />
         <PackageReference Include="Syncfusion.PdfViewer.WPF" Version="20.2.0.46" />
         <PackageReference Include="Syncfusion.SfGrid.WPF" Version="20.2.0.46" />