RichTextEditor.xaml.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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.WPF;
  9. using Syncfusion.Windows.Controls.RichTextBoxAdv;
  10. namespace InABox.DynamicGrid
  11. {
  12. public delegate void RichTextEditorChanged(object sender);
  13. /// <summary>
  14. /// Interaction logic for RichTextEditorControl.xaml
  15. /// </summary>
  16. public partial class RichTextEditor : UserControl
  17. {
  18. public static readonly DependencyProperty HideToolbarProperty =
  19. DependencyProperty.Register(nameof(HideToolbar), typeof(bool), typeof(RichTextEditor));
  20. public static readonly DependencyProperty ZoomFactorProperty =
  21. DependencyProperty.Register(nameof(ZoomFactor), typeof(double), typeof(RichTextEditor));
  22. public static readonly DependencyProperty ColorProperty =
  23. DependencyProperty.Register(
  24. "Color",
  25. typeof(Color),
  26. typeof(RichTextEditor),
  27. new PropertyMetadata(default(Color), OnBackgroundPropertyChanged));
  28. private bool bdirty;
  29. private bool bLoaded;
  30. private bool bReady;
  31. private string curvalue = "";
  32. public RichTextEditor()
  33. {
  34. InitializeComponent();
  35. DataContext = new RichTextEditorViewModel();
  36. Bold.SmallIcon = Properties.Resources.Bold16.AsBitmapImage(16, 16);
  37. Italic.SmallIcon = Properties.Resources.Italic16.AsBitmapImage(16, 16);
  38. Underline.SmallIcon = Properties.Resources.Underline16.AsBitmapImage(16, 16);
  39. AlignLeft.SmallIcon = Properties.Resources.AlignTextLeft16.AsBitmapImage(16, 16);
  40. AlignCentre.SmallIcon = Properties.Resources.AlignTextCenter16.AsBitmapImage(16, 16);
  41. AlignRight.SmallIcon = Properties.Resources.AlignTextRight16.AsBitmapImage(16, 16);
  42. AlignJustify.SmallIcon = Properties.Resources.AlignTextJustify16.AsBitmapImage(16, 16);
  43. Hyperlink.SmallIcon = Properties.Resources.Hyperlink16.AsBitmapImage(16, 16);
  44. Picture.SmallIcon = Properties.Resources.Picture16.AsBitmapImage(16, 16);
  45. Table.SmallIcon = Properties.Resources.Table16.AsBitmapImage(16, 16);
  46. ZoomIn.SmallIcon = Properties.Resources.zoomin.AsBitmapImage(16, 16);
  47. ZoomOut.SmallIcon = Properties.Resources.zoomout.AsBitmapImage(16, 16);
  48. Editor.CaretBrush = new SolidColorBrush(Colors.Black);
  49. Editor.FontFamily = new FontFamily("Calibri");
  50. Editor.FontSize = 12.0F;
  51. Editor.Foreground = new SolidColorBrush(Colors.Black);
  52. ZoomFactor = 150;
  53. Text = "";
  54. }
  55. public bool HideToolbar
  56. {
  57. get => (bool)GetValue(HideToolbarProperty);
  58. set
  59. {
  60. SetValue(HideToolbarProperty, value);
  61. Toolbar.Visibility = value ? Visibility.Collapsed : Visibility.Visible;
  62. }
  63. }
  64. public double ZoomFactor
  65. {
  66. get => (double)GetValue(ZoomFactorProperty);
  67. set
  68. {
  69. SetValue(ZoomFactorProperty, value);
  70. Editor.ZoomFactor = value;
  71. }
  72. }
  73. public string Text
  74. {
  75. get => Save();
  76. set => Load(value);
  77. }
  78. public RichTextEditorChanged OnChanged { get; set; }
  79. public Color Color
  80. {
  81. get => (Color)GetValue(ColorProperty);
  82. set
  83. {
  84. SetValue(ColorProperty, value);
  85. Editor.Background = new SolidColorBrush(value);
  86. }
  87. }
  88. public void Load(string content)
  89. {
  90. if (content == null)
  91. content = "";
  92. content = content.Replace("background:#000000", "").Replace("background:NoColor;", "");
  93. var ms = new MemoryStream(Encoding.ASCII.GetBytes(content));
  94. Editor.Load(ms, FormatType.Html);
  95. VerticalAlignment = VerticalAlignment.Top;
  96. VerticalAlignment = VerticalAlignment.Stretch;
  97. curvalue = content;
  98. bdirty = false;
  99. bLoaded = true;
  100. }
  101. public string Save()
  102. {
  103. if (bdirty)
  104. {
  105. var ms = new MemoryStream();
  106. Editor.Save(ms, FormatType.Html);
  107. var reader = new StreamReader(ms);
  108. curvalue = Encoding.UTF8.GetString(ms.GetBuffer());
  109. curvalue = curvalue.Replace("background:#000000", "");
  110. bdirty = false;
  111. }
  112. return curvalue;
  113. }
  114. private void RichTextBoxAdv_ContentChanged(object obj, ContentChangedEventArgs args)
  115. {
  116. bdirty = true;
  117. }
  118. public void SetColor(Color color)
  119. {
  120. Editor.Background = new SolidColorBrush(color);
  121. }
  122. private static void OnBackgroundPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  123. {
  124. var source = d as RichTextEditor;
  125. source.SetColor((Color)e.NewValue);
  126. }
  127. private void RichTextBoxAdv_LostFocus(object sender, RoutedEventArgs e)
  128. {
  129. if (bReady)
  130. OnChanged?.Invoke(this);
  131. bReady = true;
  132. }
  133. private void ZoomIn_Click(object sender, RoutedEventArgs e)
  134. {
  135. Editor.ZoomFactor = Editor.ZoomFactor * 1.5F;
  136. }
  137. private void ZoomOut_Click(object sender, RoutedEventArgs e)
  138. {
  139. Editor.ZoomFactor = Editor.ZoomFactor / 1.5F;
  140. }
  141. private void Editor_RequestNavigate(object obj, RequestNavigateEventArgs args)
  142. {
  143. var processStartInfo = new ProcessStartInfo(args.Hyperlink.NavigationLink);
  144. processStartInfo.UseShellExecute = true;
  145. Process.Start(processStartInfo);
  146. }
  147. }
  148. }