我想在tinyMce编辑器中进行简单的实时语法高亮显示。
我想为文本中的每个#{keyword}
和#{more keywords}
突出显示(更改文本的背景或颜色)。关键字只能包含字母,数字和点(。)。
我不知道我的想法是否合适:
<span class="code">#{keyword}</span>
(正则表达式)(css class code
将背景设置为某种颜色)
任何想法如何解决这个问题?
答案 0 :(得分:2)
绑定到onChange事件似乎没问题,但你也应该考虑使用onKey ---事件。我希望以下代码段对您有所帮助:
css(将在content_css中定义,即:)
[current] {
background-color:yellow;
}
[changed] {
background-color:green;
}
js helpfunctions:
var select_current = function(node){
unselect_current();
node.setAttribute('current','true');
};
var unselect_current = function(){
var n = ed.getBody().firstChild;
while (n){
if (n.nodeType == 1 && n.getAttribute('current'))
{
n.removeAttribute('current');
}
n = n.nextSibling;
}
};
p_of = function(node) // returns p-Element of node
{
while (node){
if (node.nodeName == 'BODY') { return null; }
if (node.nodeName == 'P') { return node; }
else { node = node.parentNode; }
}
return null;
}
事件调用:
var _node_changing = false;
console.log('onNodeChange: ', arguments);
if (!_node_changing && element.getAttribute('_mce_type') != 'bookmark'){
_node_changing = true;
var p = p_of(element);
if (p){
if (!p.getAttribute('current')){
select_current(p);
}
}
_node_changing = false;
}