CKEditor可以在WinForms应用程序中用于(X)HTML编辑吗?

6

我已经根据winforms html editor的代码进行了适应,转换到了C#(请见下文)。是否可以使用CKEditor替代?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WebEditorTest
{
/// <summary>
/// https://dev59.com/l3VC5IYBdhLWcg3wsTRi
/// </summary>
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        webBrowser1.Navigate("about:blank");
        Application.DoEvents();
        webBrowser1.Document.OpenNew(false).Write("<html><body><div id=\"editable\">Edit this text</div></body></html>");
        foreach (HtmlElement el in webBrowser1.Document.All)
        {
            el.SetAttribute("unselectable", "on");
            el.SetAttribute("contenteditable", "false");
        }
        foreach (HtmlElement el in webBrowser1.Document.All.GetElementsByName("editable"))
        {
            el.SetAttribute("width", webBrowser1.Width + "px");
            el.SetAttribute("height", "100%");
            el.SetAttribute("contenteditable", "true");
        }
        webBrowser1.Document.DomDocument.GetType().GetProperty("designMode").SetValue(webBrowser1.Document.DomDocument, "on", null);
        webBrowser1.IsWebBrowserContextMenuEnabled = false;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        textBoxMarkup.Text = webBrowser1.DocumentText;
    }
}
}
2个回答

4

是的。由于您已经在使用WebBrowser控件,所以没有任何阻止您加载包含CKEditor的HTML页面并通过DOM和InvokeScript与其交互。

更新 - 这里是交互的工作示例:

form.cs

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        webBrowser1.Navigate("C:\\blank.htm");
        Application.DoEvents();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        webBrowser1.Document.InvokeScript("InitEditor");
    }
}

blank.htm

<html>
    <head>
        <script src='http://ckeditor.com/apps/ckeditor/4.2/ckeditor.js?mriyyd'></script>
        <script type='text/javascript'>
            function InitEditor() { CKEDITOR.replace('editor1'); }
        </script>
    </head>
    <body>
        <textarea cols='80' id='editor1' name='editor1' rows='10'>
            <span>Lorem Ipsum</span>
        </textarea>
    </body>
</html>

实际上,权限被拒绝可能会阻止您将脚本加载到Web浏览器控件中。 - Constantin
怎么会有跨域或同源问题呢?因为他是动态创建页面。在 Stack Overflow 上有几个例子:https://dev59.com/9XVC5IYBdhLWcg3w4Vb6。 - LouD
没必要注入整个 CKEditor,你可以注入一个脚本标签来加载它的所有内容。这和从外部 CDN 拉取 jQuery 等库没有什么区别。同样的原始策略不适用于脚本加载:https://dev59.com/f2kv5IYBdhLWcg3wiRQJ - LouD
你是根据自己的经验说话还是一直在搜索?因为我曾经遇到过在动态生成的HTML内容中加载外部JS文件的问题,我告诉你,IE浏览器不会让你加载该脚本,会抛出权限被拒绝的错误。 - Constantin
我明白了,我当时是通过“about:blank”进行导航并添加元素和脚本。现在清楚了为什么会因为该权限而失控。 - Constantin
显示剩余2条评论

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接