1

この記事の指示に従って、XULの要素を介してMidasを使用しようとしています。これまでのところ、以下のコードがあります。

<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>、テキストは変更されず、JS コンソールに次のエラーが表示されます。

Error: uncaught exception: [Exception... "Component returned failure code: 0x80004005
(NS_ERROR_FAILURE) [nsIDOMNSHTMLDocument.execCommand]"  nsresult: "0x80004005
(NS_ERROR_FAILURE)"  location: "JS frame :: chrome://anunciador/content/main.xul :: 
onBoldButtonCommand :: line 14"  data: no]

次のように編集モードを有効にしているので、それは私には意味がありません。

editor.contentDocument.designMode = 'on';

また、行のみを変更すると

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

<xhtml:iframe id="editor" src="about:blank"></xhtml:iframe>

iframe 内のテキストを編集および書式設定できます (ただし、エディターを使用することを本当に好みます)。

私は何かを忘れましたか?

4

1 に答える 1

3

長い調査の結果、問題はGeckoのバグであるように思われます。どうやら、それは最終的に 解決されます。

パブリックビルドを待つ間(または、XULRunnerまたはFirefoxの将来の新しいバージョンを使用できない場合)、回避策としてエディターのcommandManagerプロパティを使用できます。このオブジェクトは、doCommand()テキストのフォーマットに使用できると呼ばれるメソッドを提供します。このメソッドには3つのパラメーターがあります。コマンドを表す文字列(によって受け入れられるものとは異なります)、パラメーターexecCommand()オブジェクト(取得するのは非常に面倒ですが、しばらくの間無視できます)、およびcontentWindowエディターのパラメーターです。

たとえば、選択範囲を太字にしたい場合は、次のようにこの方法を使用します。

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

ただし、コマンドにパラメータが必要な場合は、もう少し複雑になる可能性があります。まず、nsICommandParamsインターフェイスのインスタンス(JavaScriptオブジェクトでラップされたC ++オブジェクトになります)が必要になります。このオブジェクトの取得には、XPCOMなどのような非常に難解なコードが含まれているようです。

var commandParams = Components.classes['@mozilla.org/embedcomp/command-params;1'].getService(Components.interfaces.nsICommandParams);

このオブジェクトでは、コマンドのパラメーターをキーと値のペアとして設定します。ここに、すべてのコマンドで受け入れられるパラメーターのリストがあります。このページがC++コードを参照しているという事実を恐れないでください-直感的にJavaScriptにマップできます。また、うまくいけば、すべてのコマンドが1つのパラメータ、を受け取るだけのよう"state_attribute"です。たとえば、テキストの色を設定する場合は、paramオブジェクトで次のようにパラメータを設定します。

commandParams.setCStringValue("state_attribute", "#FF0000");

doCommand()次に、今回はパラメータを使用して「ただ」呼び出します。

editor.commandManager.doCommand("cmd_fontColor", commandParams, editor.contentWindow);

doCommand()以下のコードは、パラメーターありとパラメーターなしの両方を使用する実際の例です。

<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>

また、このページは役立つ可能性があり、このページはさらに役立ちます(一部はフランス語で書かれていますが!)。

于 2011-07-18T13:23:53.690 に答える