RichTextEditor.xaml.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Documents;
  8. using System.Windows.Media;
  9. using InABox.Core;
  10. using InABox.WPF;
  11. using Syncfusion.Windows.Controls.RichTextBoxAdv;
  12. namespace InABox.DynamicGrid
  13. {
  14. public delegate void RichTextEditorChanged(object sender);
  15. public class ButtonVisibleConverter : AbstractConverter<RichTextEditorButtons, Visibility>
  16. {
  17. public Visibility Set { get; set; } = Visibility.Visible;
  18. public Visibility Unset { get; set; } = Visibility.Collapsed;
  19. public RichTextEditorButtons Flag { get; set; } = RichTextEditorButtons.None;
  20. public override Visibility Convert(RichTextEditorButtons value)
  21. {
  22. return value != RichTextEditorButtons.None && value.HasFlag(Flag) ? Set : Unset;
  23. }
  24. }
  25. /// <summary>
  26. /// Interaction logic for RichTextEditorControl.xaml
  27. /// </summary>
  28. public partial class RichTextEditor : UserControl
  29. {
  30. // public static readonly DependencyProperty HideToolbarProperty =
  31. // DependencyProperty.Register(nameof(HideToolbar), typeof(bool), typeof(RichTextEditor));
  32. public static readonly DependencyProperty ZoomFactorProperty =
  33. DependencyProperty.Register(nameof(ZoomFactor), typeof(double), typeof(RichTextEditor));
  34. public static readonly DependencyProperty ColorProperty =
  35. DependencyProperty.Register(
  36. "Color",
  37. typeof(Color),
  38. typeof(RichTextEditor),
  39. new PropertyMetadata(default(Color), OnBackgroundPropertyChanged));
  40. private bool bdirty;
  41. private bool bReady;
  42. private string curvalue = "";
  43. public RichTextEditorButtons VisibleButtons
  44. {
  45. get => ViewModel.VisibleButtons;
  46. set => ViewModel.VisibleButtons = value;
  47. }
  48. public RichTextEditor()
  49. {
  50. InitializeComponent();
  51. Bold.SmallIcon = Wpf.Resources.Bold16.AsBitmapImage(16, 16);
  52. Italic.SmallIcon = Wpf.Resources.Italic16.AsBitmapImage(16, 16);
  53. Underline.SmallIcon = Wpf.Resources.Underline16.AsBitmapImage(16, 16);
  54. AlignLeft.SmallIcon = Wpf.Resources.AlignTextLeft16.AsBitmapImage(16, 16);
  55. AlignCentre.SmallIcon = Wpf.Resources.AlignTextCenter16.AsBitmapImage(16, 16);
  56. AlignRight.SmallIcon = Wpf.Resources.AlignTextRight16.AsBitmapImage(16, 16);
  57. AlignJustify.SmallIcon = Wpf.Resources.AlignTextJustify16.AsBitmapImage(16, 16);
  58. Hyperlink.SmallIcon = Wpf.Resources.Hyperlink16.AsBitmapImage(16, 16);
  59. Picture.SmallIcon = Wpf.Resources.Picture16.AsBitmapImage(16, 16);
  60. Table.SmallIcon = Wpf.Resources.Table16.AsBitmapImage(16, 16);
  61. ZoomIn.SmallIcon = Wpf.Resources.zoomin.AsBitmapImage(16, 16);
  62. ZoomOut.SmallIcon = Wpf.Resources.zoomout.AsBitmapImage(16, 16);
  63. Editor.CaretBrush = new SolidColorBrush(Colors.Black);
  64. Editor.FontFamily = new FontFamily("Calibri");
  65. Editor.FontSize = 12.0F;
  66. Editor.Foreground = new SolidColorBrush(Colors.Black);
  67. ZoomFactor = 150;
  68. Text = "";
  69. }
  70. // public bool HideToolbar
  71. // {
  72. // get => (bool)GetValue(HideToolbarProperty);
  73. // set
  74. // {
  75. // SetValue(HideToolbarProperty, value);
  76. // Toolbar.Visibility = value ? Visibility.Collapsed : Visibility.Visible;
  77. // }
  78. // }
  79. public double ZoomFactor
  80. {
  81. get => (double)GetValue(ZoomFactorProperty);
  82. set
  83. {
  84. SetValue(ZoomFactorProperty, value);
  85. Editor.ZoomFactor = value;
  86. }
  87. }
  88. public string Text
  89. {
  90. get => Save();
  91. set => Load(value);
  92. }
  93. public RichTextEditorChanged? OnChanged { get; set; }
  94. public Color Color
  95. {
  96. get => (Color)GetValue(ColorProperty);
  97. set
  98. {
  99. SetValue(ColorProperty, value);
  100. Editor.Background = new SolidColorBrush(value);
  101. }
  102. }
  103. public void Load(string content)
  104. {
  105. if (content == null)
  106. content = "";
  107. content = content.Replace("background:#000000", "").Replace("background:NoColor;", "");
  108. var ms = new MemoryStream(Encoding.ASCII.GetBytes(content));
  109. Editor.Load(ms, FormatType.Html);
  110. VerticalAlignment = VerticalAlignment.Top;
  111. VerticalAlignment = VerticalAlignment.Stretch;
  112. curvalue = content;
  113. bdirty = false;
  114. }
  115. public string Save()
  116. {
  117. if (bdirty)
  118. {
  119. var ms = new MemoryStream();
  120. Editor.Save(ms, FormatType.Html);
  121. var reader = new StreamReader(ms);
  122. curvalue = Encoding.UTF8.GetString(ms.GetBuffer());
  123. curvalue = curvalue.Replace("background:#000000", "");
  124. bdirty = false;
  125. }
  126. return curvalue;
  127. }
  128. private void RichTextBoxAdv_ContentChanged(object obj, ContentChangedEventArgs args)
  129. {
  130. bdirty = true;
  131. }
  132. public void SetColor(Color color)
  133. {
  134. Editor.Background = new SolidColorBrush(color);
  135. }
  136. private static void OnBackgroundPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  137. {
  138. if (d is not RichTextEditor source) return;
  139. source.SetColor((Color)e.NewValue);
  140. }
  141. private void RichTextBoxAdv_LostFocus(object sender, RoutedEventArgs e)
  142. {
  143. if (bReady)
  144. OnChanged?.Invoke(this);
  145. bReady = true;
  146. }
  147. private void ZoomIn_Click(object sender, RoutedEventArgs e)
  148. {
  149. Editor.ZoomFactor = Editor.ZoomFactor * 1.5F;
  150. }
  151. private void ZoomOut_Click(object sender, RoutedEventArgs e)
  152. {
  153. Editor.ZoomFactor = Editor.ZoomFactor / 1.5F;
  154. }
  155. private void Editor_RequestNavigate(object obj, RequestNavigateEventArgs args)
  156. {
  157. var processStartInfo = new ProcessStartInfo(args.Hyperlink.NavigationLink);
  158. processStartInfo.UseShellExecute = true;
  159. Process.Start(processStartInfo);
  160. }
  161. /// <summary>
  162. /// Handles the ImageNodeVisited event of the richTextBoxAdv control.
  163. /// We are setting the image "source" property to empty to save the image inline
  164. /// See: https://help.syncfusion.com/uwp/richtextbox/faq-section/how-to-export-the-inserted-image-as-an-embedded-image-in-html-in-sfrichtextboxadv
  165. /// </summary>
  166. /// <param name="obj">The source of the event.</param>
  167. /// <param name="args">The <see cref="ImageNodeVisitedEventArgs"/> instance containing the event data.</param>
  168. private void HtmlImportExportSettings_OnImageNodeVisited(object obj, ImageNodeVisitedEventArgs args)
  169. {
  170. if (args.IsSaving)
  171. args.Source = string.Empty;
  172. }
  173. }
  174. }