RichTextEditor.xaml.cs 5.8 KB

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