RichTextEditor.xaml.cs 5.5 KB

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