Javascript 通过<;编辑>;无法启用元素

Javascript 通过<;编辑>;无法启用元素,javascript,xul,midas-editor,Javascript,Xul,Midas Editor,我正试图按照的说明通过XUL使用。到目前为止,我的代码如下: <window id="main" title="Anunciador Blog Editor" width="300" height="300" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" xmlns:xhtml="http://www.w3.org/1999/xhtml"> <script ty

我正试图按照的说明通过XUL使用。到目前为止,我的代码如下:

<window id="main" title="Anunciador Blog Editor" width="300" height="300"
    xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
    xmlns:xhtml="http://www.w3.org/1999/xhtml">
    <script type="application/x-javascript">
    <![CDATA[ 
    var editor = null;
    function onLoad() {
        editor = document.getElementById('editor');
        editor.contentDocument.designMode = 'on';
    }

    function onBoldButtonCommand() {
        editor.contentDocument.execCommand('bold', false, null);
    }

    window.addEventListener("load", onLoad, false);
    ]]>
    </script>
    <button label="Bold" oncommand="onBoldButtonCommand();" />
    <editor id="editor" type="content-primary" editortype="html" src="about:blank" flex="1" />
</window>
这对我来说没有意义,因为我已通过以下方式启用编辑模式:

editor.contentDocument.designMode = 'on';
另外,如果我只改变路线

<editor id="editor" type="content-primary" editortype="html" src="about:blank" flex="1" />


我可以在iframe中编辑和格式化文本(但我更喜欢使用编辑器)


我忘了什么吗?

经过长时间的研究,这个问题似乎是反复出现的,顺便说一句。显然,它终于出现了

当我们等待公共构建时(或者如果您不能使用未来较新版本的XULRunner或Firefox),您可以使用编辑器的。此对象提供一个名为
docomand()
的方法,可用于格式化文本。此方法有三个参数:一个表示命令的字符串(与
execCommand()
接受的字符串不同)、一个param对象(获取起来非常麻烦,但可以忽略一段时间)和编辑器的
contentWindow

如果您希望(例如)使选择加粗,只需按以下方式使用此方法:

function onBoldButtonCommand() {
    editor.commandManager.doCommand("cmd_bold", {}, editor.contentWindow)
}

如果你的命令需要PARAMS,但是它会变得更复杂。首先,你需要一个实例(它将是一个由JavaScript对象包装的C++对象)。获取这个对象包含一些非常深奥的代码,显然涉及XPcom或其他什么东西:

var commandParams = Components.classes['@mozilla.org/embedcomp/command-params;1'].getService(Components.interfaces.nsICommandParams);
在这个对象中,我们将命令的参数设置为键值对。我们有一个参数列表,所有的命令都接受它。不要害怕这个页面的事实是指C++代码——你可以直观地将它映射到JavaScript。而且,希望所有命令都只接收一个参数,即“代码”>“StaseOy属性”。。例如,如果要设置参数,请在param对象中按以下方式设置参数:

commandParams.setCStringValue("state_attribute", "#FF0000");
然后这次使用参数“只是”调用
docomand()

editor.commandManager.doCommand("cmd_fontColor", commandParams, editor.contentWindow);
下面的代码是使用带参数和不带参数的
docomand()
的工作示例:

<window id="main" title="Anunciador Blog Editor" width="300" height="300"
   xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
   xmlns:xhtml="http://www.w3.org/1999/xhtml">
   <script type="application/x-javascript">
   <![CDATA[
   var editor = null;
   function onLoad() {
       editor = document.getElementById('editor');
       editor.contentDocument.designMode = 'on';
   }

   function onBoldButtonCommand() {
       editor.commandManager.doCommand("cmd_bold", {}, editor.contentWindow)
   }

    function onRedTextCommand() {
        var commandParams = Components.classes['@mozilla.org/embedcomp/command-params;1'].getService(Components.interfaces.nsICommandParams);
        commandParams.setCStringValue("state_attribute", "#FF0000");
        editor.commandManager.doCommand("cmd_fontColor", commandParams, editor.contentWindow)
    }

   window.addEventListener("load", onLoad, false);
   ]]>
   </script>
   <toolbar>
       <button label="Bold" oncommand="onBoldButtonCommand();" />
       <button label="Red" oncommand="onRedTextCommand();" />
   </toolbar>
   <editor id="editor" type="content-primary" editortype="html" src="about:blank" flex="1" />
</window>

此外,这可能会有帮助,但更有用(尽管部分是用法语写的!)

editor.commandManager.doCommand("cmd_fontColor", commandParams, editor.contentWindow);
<window id="main" title="Anunciador Blog Editor" width="300" height="300"
   xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
   xmlns:xhtml="http://www.w3.org/1999/xhtml">
   <script type="application/x-javascript">
   <![CDATA[
   var editor = null;
   function onLoad() {
       editor = document.getElementById('editor');
       editor.contentDocument.designMode = 'on';
   }

   function onBoldButtonCommand() {
       editor.commandManager.doCommand("cmd_bold", {}, editor.contentWindow)
   }

    function onRedTextCommand() {
        var commandParams = Components.classes['@mozilla.org/embedcomp/command-params;1'].getService(Components.interfaces.nsICommandParams);
        commandParams.setCStringValue("state_attribute", "#FF0000");
        editor.commandManager.doCommand("cmd_fontColor", commandParams, editor.contentWindow)
    }

   window.addEventListener("load", onLoad, false);
   ]]>
   </script>
   <toolbar>
       <button label="Bold" oncommand="onBoldButtonCommand();" />
       <button label="Red" oncommand="onRedTextCommand();" />
   </toolbar>
   <editor id="editor" type="content-primary" editortype="html" src="about:blank" flex="1" />
</window>