有没有人找到一种方法来愉快地处理WYSIHTML5中的代码?

时间:2012-09-26 20:18:38

标签: javascript parsing syntax wysihtml5

我正在使用基于WYSIHTML5(http://jhollingworth.github.com/bootstrap-wysihtml5)的WYSIHTML5 Bootstrap(https://github.com/xing/wysihtml5),这在从网站复制粘贴时清理HTML非常棒。

我希望能够将代码处理到编辑器中,然后使用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);

2 个答案:

答案 0 :(得分: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>

非常感谢克里斯托弗!!

答案 1 :(得分:2)

我今天fork了bootstrap-wysihtml5项目,并使用Highlight.js添加突出显示支持的代码。

您可以在http://evereq.github.com/bootstrap-wysihtml5查看演示并查看源代码https://github.com/evereq/bootstrap-wysihtml5。它与Christopher的代码基本相同,并且UI更改并嵌入编辑器本身的bootstrap版本中。