4

私はWYSIHTML5( https://github.com/xing/wysihtml5)に基づいたWYSIHTML5 Bootstrap( http://jhollingworth.github.com/bootstrap-wysihtml5 )を使用しています。ウェブサイト。

コードをエディターで処理してから、HighlightJSで構文を強調表示できるようにしたいと思います。

新しいボタンを作成し、wysihtml5.jsで使用されているメソッドを複製して、代わりに次<b>を使用して太字のオンとオフを切り替えました。<pre>

(function(wysihtml5) {
var undef;

wysihtml5.commands.pre = {
    exec: function(composer, command) {

        return wysihtml5.commands.formatInline.exec(composer, command, "pre");
    },

    state: function(composer, command, color) {

        return wysihtml5.commands.formatInline.state(composer, command, "pre");
    },

    value: function() {
        return undef;
    }
};
})(wysihtml5)

しかし、それだけでは十分ではありません。エディターは、編集時にタグを非表示にします。<pre>コンテンツをとの両方でラップできる必要があります<code><pre><code></code></pre>

これは、wysihtml5で使用されているものとは異なる関数を作成することを意味しますが、方法がわかりません...誰かがそれを手伝ってくれませんか?

wysihtml5のformatInline関数のコードは次のとおりです。

 wysihtml5.commands.formatInline = {
exec: function(composer, command, tagName, className, classRegExp) {
  var range = composer.selection.getRange();
  if (!range) {
    return false;
  }
  _getApplier(tagName, className, classRegExp).toggleRange(range);
  composer.selection.setSelection(range);
},

state: function(composer, command, tagName, className, classRegExp) {
  var doc           = composer.doc,
      aliasTagName  = ALIAS_MAPPING[tagName] || tagName,
      range;

  // Check whether the document contains a node with the desired tagName
  if (!wysihtml5.dom.hasElementWithTagName(doc, tagName) &&
      !wysihtml5.dom.hasElementWithTagName(doc, aliasTagName)) {
    return false;
  }

   // Check whether the document contains a node with the desired className
  if (className && !wysihtml5.dom.hasElementWithClassName(doc, className)) {
     return false;
  }

  range = composer.selection.getRange();
  if (!range) {
    return false;
  }

  return _getApplier(tagName, className, classRegExp).isAppliedToRange(range);
},

value: function() {
  return undef;
}
};
})(wysihtml5);
4

2 に答える 2

5

wysihtml5 の開発者である Christopher から回答を得ました。

wysihtml5.commands.formatCode = function() {
exec: function(composer) {
var pre = this.state(composer);
if (pre) {
  // caret is already within a <pre><code>...</code></pre>
  composer.selection.executeAndRestore(function() {
    var code = pre.querySelector("code");
    wysihtml5.dom.replaceWithChildNodes(pre);
    if (code) {
      wysihtml5.dom.replaceWithChildNodes(pre);
    }
  });
} else {
  // Wrap in <pre><code>...</code></pre>
  var range = composer.selection.getRange(),
      selectedNodes = range.extractContents(),
      pre = composer.doc.createElement("pre"),
      code = composer.doc.createElement("code");
  pre.appendChild(code);
  code.appendChild(selectedNodes);
  range.insertNode(pre);
  composer.selection.selectNode(pre);
}
},
state: function(composer) {
var selectedNode = composer.selection.getSelectedNode();
return wysihtml5.dom.getParentElement(selectedNode, { nodeName: "CODE" }) && wysihtml5.dom.getParentElement(selectedNode, { nodeName: "PRE" });
}
};

...そしてこれをツールバーに追加します:

<a data-wysihtml5-command="formatCode">highlight code</a>

クリストファーに感謝します!!

于 2012-09-27T23:03:59.837 に答える
2

今日、bootstrap-wysihtml5 プロジェクトをフォークし、Highlight.js を使用してコードの強調表示のサポートを追加します。

http://evereq.github.com/bootstrap-wysihtml5でデモを確認し、ソース コードを確認できますhttps://github.com/evereq/bootstrap-wysihtml5。これは基本的に Christopher のコードとほとんど同じで、UI の変更とエディタ自体のブートストラップ バージョンの内部に埋め込まれています。

于 2012-12-03T16:41:50.063 に答える