RichTextEditor.xaml.cs 7.2 KB

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