KEditor.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. using KSharpEditor.Args;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Runtime.InteropServices;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace KSharpEditor
  11. {
  12. [ComVisible(true)]
  13. public class KEditor : System.Windows.Forms.UserControl
  14. {
  15. private KBrowser kBrowserEditor;
  16. private string _lang = "zh-CN";
  17. public event EventHandler<EditorArgs> OpenButtonClick;
  18. public event EventHandler<EditorArgs> SaveButtonClick;
  19. public event EventHandler<EditorArgs> InsertImageClick;
  20. public event EventHandler<EditorArgs> EditorLoadComplete;
  21. public event EventHandler<ErrorArgs> EditorError;
  22. private void InitializeComponent()
  23. {
  24. this.kBrowserEditor = new KBrowser();
  25. this.SuspendLayout();
  26. //
  27. // kBrowserEditor
  28. //
  29. this.kBrowserEditor.Dock = System.Windows.Forms.DockStyle.Fill;
  30. this.kBrowserEditor.Location = new System.Drawing.Point(0, 0);
  31. this.kBrowserEditor.MinimumSize = new System.Drawing.Size(20, 20);
  32. this.kBrowserEditor.Name = "kBrowserEditor";
  33. this.kBrowserEditor.ScriptErrorsSuppressed = true;
  34. this.kBrowserEditor.Size = new System.Drawing.Size(465, 252);
  35. this.kBrowserEditor.TabIndex = 0;
  36. //
  37. // KEditor
  38. //
  39. this.Controls.Add(this.kBrowserEditor);
  40. this.Name = "KEditor";
  41. this.Size = new System.Drawing.Size(465, 252);
  42. this.Load += new System.EventHandler(this.KEditor_Load);
  43. this.ResumeLayout(false);
  44. }
  45. public KEditor()
  46. {
  47. InitializeComponent();
  48. kBrowserEditor.IsWebBrowserContextMenuEnabled = false;
  49. kBrowserEditor.ObjectForScripting = this;
  50. try
  51. {
  52. string resourcename = Assembly.GetExecutingAssembly().GetManifestResourceNames().FirstOrDefault(x => x.Contains("editor.html"));
  53. Stream sm = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourcename);
  54. byte[] bs = new byte[sm.Length];
  55. sm.Read(bs, 0, (int)sm.Length);
  56. sm.Close();
  57. UTF8Encoding con = new UTF8Encoding();
  58. string str = con.GetString(bs);
  59. kBrowserEditor.DocumentText = str;
  60. }
  61. catch (Exception ex)
  62. {
  63. OnError(ex);
  64. }
  65. }
  66. /// <summary>
  67. /// Sets the language of the browser. Default is zh-CN
  68. /// </summary>
  69. /// <param name="lang">The language to set. Supported are en-US, zh-CN</param>
  70. public KEditor(string lang): this()
  71. {
  72. _lang = lang;
  73. }
  74. /// <summary>
  75. /// Sets the language of the editor.
  76. /// </summary>
  77. public string Language
  78. {
  79. set
  80. {
  81. try
  82. {
  83. kBrowserEditor?.Document?.InvokeScript("setLang", new string[] { value });
  84. }
  85. catch (Exception ex)
  86. {
  87. OnError(ex);
  88. }
  89. }
  90. }
  91. /// <summary>
  92. /// Returns the version of the editor.
  93. /// </summary>
  94. public string Version
  95. {
  96. get
  97. {
  98. try
  99. {
  100. return kBrowserEditor.Document.InvokeScript("getVersion").ToString();
  101. }
  102. catch (Exception ex)
  103. {
  104. OnError(ex);
  105. return "";
  106. }
  107. }
  108. }
  109. /// <summary>
  110. /// Gets or sets the html in the editor
  111. /// </summary>
  112. public string Html
  113. {
  114. get
  115. {
  116. try
  117. {
  118. if (kBrowserEditor.Document == null) { this.Show(); }
  119. return kBrowserEditor.Document.InvokeScript("getHtml").ToString();
  120. }
  121. catch (Exception ex)
  122. {
  123. OnError(ex);
  124. return "";
  125. }
  126. }
  127. set
  128. {
  129. try
  130. {
  131. if (kBrowserEditor.Document == null) { this.Show(); }
  132. kBrowserEditor.Document.InvokeScript("setHtml", new string[] { value });
  133. }
  134. catch (Exception ex)
  135. {
  136. OnError(ex);
  137. }
  138. }
  139. }
  140. private void KEditor_Load(object sender, EventArgs e)
  141. {
  142. }
  143. private void OnError(Exception ex)
  144. {
  145. if (EditorError != null)
  146. {
  147. EditorError(this, new ErrorArgs(ex));
  148. }
  149. }
  150. public void OnSaveButtonClicked()
  151. {
  152. if (SaveButtonClick != null)
  153. {
  154. SaveButtonClick(this, GetEditArgs());
  155. }
  156. }
  157. public void OnOpenFileButtonClicked()
  158. {
  159. if (OpenButtonClick != null)
  160. {
  161. OpenButtonClick(this, GetEditArgs());
  162. }
  163. }
  164. private EditorArgs GetEditArgs()
  165. {
  166. return new EditorArgs()
  167. {
  168. Html = this.Html,
  169. Version = this.Version
  170. };
  171. }
  172. public void OnInsertImageButtonClicked()
  173. {
  174. if (InsertImageClick != null)
  175. {
  176. InsertImageClick(this, GetEditArgs());
  177. }
  178. }
  179. public void OnEditorLoadComplete()
  180. {
  181. if (EditorLoadComplete != null)
  182. {
  183. EditorLoadComplete(this, GetEditArgs());
  184. }
  185. }
  186. public void OnEditorLoadStart() {
  187. Language = _lang;
  188. }
  189. /// <summary>
  190. /// Clears the editor.
  191. /// </summary>
  192. public void Reset()
  193. {
  194. try
  195. {
  196. kBrowserEditor.Document.InvokeScript("reset");
  197. }
  198. catch (Exception ex)
  199. {
  200. OnError(ex);
  201. }
  202. }
  203. /// <summary>
  204. /// Inserts provided html to the editor.
  205. /// </summary>
  206. /// <param name="html">the html to insert</param>
  207. public void InsertNode(string html)
  208. {
  209. try
  210. {
  211. kBrowserEditor.Document.InvokeScript("insertNode", new string[] { html });
  212. }
  213. catch (Exception ex)
  214. {
  215. OnError(ex);
  216. }
  217. }
  218. /// <summary>
  219. /// Inserts a text in the editor
  220. /// </summary>
  221. /// <param name="text">the text to insert</param>
  222. public void InsertText(string text)
  223. {
  224. try
  225. {
  226. kBrowserEditor.Document.InvokeScript("insertText", new string[] { text });
  227. }
  228. catch (Exception ex)
  229. {
  230. OnError(ex);
  231. }
  232. }
  233. /// <summary>
  234. /// Inserts image in the editor.
  235. /// </summary>
  236. /// <param name="path">absolute path of the image file to insert</param>
  237. /// <param name="base64">insert image as base64 encoded string. Image is inserted in png format</param>
  238. public void InsertImage(string path, bool base64 = false)
  239. {
  240. try
  241. {
  242. string sImg = string.Empty;
  243. if (base64)
  244. {
  245. Byte[] bytes = File.ReadAllBytes(path);
  246. String file = Convert.ToBase64String(bytes);
  247. sImg = $"<p><img src=\"data:image/png;base64,{file}\" /></p>";
  248. }
  249. else
  250. {
  251. sImg = $"<p><img src='{path}' /></p>";
  252. }
  253. kBrowserEditor.Document.InvokeScript("insertNode", new string[] { sImg });
  254. }
  255. catch (Exception ex)
  256. {
  257. OnError(ex);
  258. }
  259. }
  260. }
  261. }