123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- using KSharpEditor.Args;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading.Tasks;
- namespace KSharpEditor
- {
- [ComVisible(true)]
- public class KEditor : System.Windows.Forms.UserControl
- {
- private KBrowser kBrowserEditor;
- private string _lang = "zh-CN";
- public event EventHandler<EditorArgs> OpenButtonClick;
- public event EventHandler<EditorArgs> SaveButtonClick;
- public event EventHandler<EditorArgs> InsertImageClick;
- public event EventHandler<EditorArgs> EditorLoadComplete;
- public event EventHandler<ErrorArgs> EditorError;
- private void InitializeComponent()
- {
- this.kBrowserEditor = new KBrowser();
- this.SuspendLayout();
- //
- // kBrowserEditor
- //
- this.kBrowserEditor.Dock = System.Windows.Forms.DockStyle.Fill;
- this.kBrowserEditor.Location = new System.Drawing.Point(0, 0);
- this.kBrowserEditor.MinimumSize = new System.Drawing.Size(20, 20);
- this.kBrowserEditor.Name = "kBrowserEditor";
- this.kBrowserEditor.ScriptErrorsSuppressed = true;
- this.kBrowserEditor.Size = new System.Drawing.Size(465, 252);
- this.kBrowserEditor.TabIndex = 0;
- //
- // KEditor
- //
- this.Controls.Add(this.kBrowserEditor);
- this.Name = "KEditor";
- this.Size = new System.Drawing.Size(465, 252);
- this.Load += new System.EventHandler(this.KEditor_Load);
- this.ResumeLayout(false);
- }
- public KEditor()
- {
- InitializeComponent();
- kBrowserEditor.IsWebBrowserContextMenuEnabled = false;
- kBrowserEditor.ObjectForScripting = this;
- try
- {
- string resourcename = Assembly.GetExecutingAssembly().GetManifestResourceNames().FirstOrDefault(x => x.Contains("editor.html"));
- Stream sm = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourcename);
- byte[] bs = new byte[sm.Length];
- sm.Read(bs, 0, (int)sm.Length);
- sm.Close();
- UTF8Encoding con = new UTF8Encoding();
- string str = con.GetString(bs);
- kBrowserEditor.DocumentText = str;
- }
- catch (Exception ex)
- {
- OnError(ex);
- }
- }
- /// <summary>
- /// Sets the language of the browser. Default is zh-CN
- /// </summary>
- /// <param name="lang">The language to set. Supported are en-US, zh-CN</param>
- public KEditor(string lang): this()
- {
- _lang = lang;
- }
- /// <summary>
- /// Sets the language of the editor.
- /// </summary>
- public string Language
- {
- set
- {
- try
- {
- kBrowserEditor?.Document?.InvokeScript("setLang", new string[] { value });
- }
- catch (Exception ex)
- {
- OnError(ex);
- }
- }
- }
- /// <summary>
- /// Returns the version of the editor.
- /// </summary>
- public string Version
- {
- get
- {
- try
- {
- return kBrowserEditor.Document.InvokeScript("getVersion").ToString();
- }
- catch (Exception ex)
- {
- OnError(ex);
- return "";
- }
- }
- }
- /// <summary>
- /// Gets or sets the html in the editor
- /// </summary>
- public string Html
- {
- get
- {
- try
- {
- if (kBrowserEditor.Document == null) { this.Show(); }
- return kBrowserEditor.Document.InvokeScript("getHtml").ToString();
- }
- catch (Exception ex)
- {
- OnError(ex);
- return "";
- }
- }
- set
- {
- try
- {
- if (kBrowserEditor.Document == null) { this.Show(); }
- kBrowserEditor.Document.InvokeScript("setHtml", new string[] { value });
- }
- catch (Exception ex)
- {
- OnError(ex);
- }
- }
- }
- private void KEditor_Load(object sender, EventArgs e)
- {
- }
- private void OnError(Exception ex)
- {
- if (EditorError != null)
- {
- EditorError(this, new ErrorArgs(ex));
- }
- }
- public void OnSaveButtonClicked()
- {
- if (SaveButtonClick != null)
- {
- SaveButtonClick(this, GetEditArgs());
- }
- }
- public void OnOpenFileButtonClicked()
- {
- if (OpenButtonClick != null)
- {
- OpenButtonClick(this, GetEditArgs());
- }
- }
- private EditorArgs GetEditArgs()
- {
- return new EditorArgs()
- {
- Html = this.Html,
- Version = this.Version
- };
- }
- public void OnInsertImageButtonClicked()
- {
- if (InsertImageClick != null)
- {
- InsertImageClick(this, GetEditArgs());
- }
- }
- public void OnEditorLoadComplete()
- {
- if (EditorLoadComplete != null)
- {
- EditorLoadComplete(this, GetEditArgs());
- }
- }
- public void OnEditorLoadStart() {
- Language = _lang;
- }
- /// <summary>
- /// Clears the editor.
- /// </summary>
- public void Reset()
- {
- try
- {
- kBrowserEditor.Document.InvokeScript("reset");
- }
- catch (Exception ex)
- {
- OnError(ex);
- }
- }
- /// <summary>
- /// Inserts provided html to the editor.
- /// </summary>
- /// <param name="html">the html to insert</param>
- public void InsertNode(string html)
- {
- try
- {
- kBrowserEditor.Document.InvokeScript("insertNode", new string[] { html });
- }
- catch (Exception ex)
- {
- OnError(ex);
- }
- }
- /// <summary>
- /// Inserts a text in the editor
- /// </summary>
- /// <param name="text">the text to insert</param>
- public void InsertText(string text)
- {
- try
- {
- kBrowserEditor.Document.InvokeScript("insertText", new string[] { text });
- }
- catch (Exception ex)
- {
- OnError(ex);
- }
- }
- /// <summary>
- /// Inserts image in the editor.
- /// </summary>
- /// <param name="path">absolute path of the image file to insert</param>
- /// <param name="base64">insert image as base64 encoded string. Image is inserted in png format</param>
- public void InsertImage(string path, bool base64 = false)
- {
- try
- {
- string sImg = string.Empty;
- if (base64)
- {
- Byte[] bytes = File.ReadAllBytes(path);
- String file = Convert.ToBase64String(bytes);
- sImg = $"<p><img src=\"data:image/png;base64,{file}\" /></p>";
- }
- else
- {
- sImg = $"<p><img src='{path}' /></p>";
- }
- kBrowserEditor.Document.InvokeScript("insertNode", new string[] { sImg });
- }
- catch (Exception ex)
- {
- OnError(ex);
- }
- }
- }
- }
|