Javascript 如何根据子字符串位置突出显示CodeMirror中的子字符串
我试图使用codemirr()作为一个基本的文本编辑器,并提供一些额外的功能。其中之一是突出显示由其在原始字符串中的位置指定的特定单词或单词组。我有一个外部结构,存储我要突出显示的子字符串位置列表。此结构是一个数组,其中每个元素表示一个文本行,并包含一个对象数组,其中子字符串的位置将高亮显示。例如,我有以下文本字符串:Javascript 如何根据子字符串位置突出显示CodeMirror中的子字符串,javascript,codemirror,codemirror-modes,Javascript,Codemirror,Codemirror Modes,我试图使用codemirr()作为一个基本的文本编辑器,并提供一些额外的功能。其中之一是突出显示由其在原始字符串中的位置指定的特定单词或单词组。我有一个外部结构,存储我要突出显示的子字符串位置列表。此结构是一个数组,其中每个元素表示一个文本行,并包含一个对象数组,其中子字符串的位置将高亮显示。例如,我有以下文本字符串: The moon is pale and round as is also the sun 突出显示的词语有“月亮”、“苍白”、“圆”和“太阳”。因此,突出显示结构如下所示:
The moon
is pale and round
as is
also the sun
突出显示的词语有“月亮”、“苍白”、“圆”和“太阳”。因此,突出显示结构如下所示:
[
[ { iStart:4, iEnd:7 } ], // "moon"
[ { iStart:3, iEnd:6 }, { iStart:12, iEnd:16 } ], // "pale" and "round"
[],
[ { iStart:9, iEnd:11 } ] // "sun"
]
function highlightText()
{
console.log( "highlightText()" );
// Get a reference to the text lines in the code editor
var codeLines = $("#editorContainer .CodeMirror-code pre>span" );
for( var i=0; i<colorSegments.length; i++ ){
// If there's text to be highlighted in this line...
if( colorSegments[i] && colorSegments[i].length > 0 ){
// Get the right element and do so
var lineElement = codeLines[i];
highlightWordsInLine( lineElement, colorSegments[i] );
}
}
}
function highlightWordsInLine(element, positions) {
// Get the raw text
var str = $( element ).text();
// Build a new string with highlighting tags.
// Start
var out = str.substr(0, positions[0].iStart);
for( var j=0; j<positions.length; j++ ){
var position = positions[j];
// Apply the highlighting tag
out += '<span class="cm-s-ambiance cm-relation">';
out += str.substr( position.iStart, position.iEnd - position.iStart + 1);
out += '</span>';
// Do not forget to incluide unhighlighted text in between
if( j < positions.length-1 ){
out += str.substr( position.iEnd - position.iStart + 1, positions[j+1].iStart );
}
}
// Wrap up to end of line
out += str.substr( position.iEnd + 1);
// Reset the html element value including applied highlight tags
element.innerHTML = out;
}
为了实现这一点,我首先尝试编写一个自定义语言模式,但没有成功(主要是因为我不知道如何管理CodeMirror似乎使用标记而不是行,我显然需要知道当前标记所在的行,以便从突出显示结构中检索正确的数据)
然后我尝试编写一个外部函数,通过手动添加SPAN标记来应用高亮显示,如下所示:
[
[ { iStart:4, iEnd:7 } ], // "moon"
[ { iStart:3, iEnd:6 }, { iStart:12, iEnd:16 } ], // "pale" and "round"
[],
[ { iStart:9, iEnd:11 } ] // "sun"
]
function highlightText()
{
console.log( "highlightText()" );
// Get a reference to the text lines in the code editor
var codeLines = $("#editorContainer .CodeMirror-code pre>span" );
for( var i=0; i<colorSegments.length; i++ ){
// If there's text to be highlighted in this line...
if( colorSegments[i] && colorSegments[i].length > 0 ){
// Get the right element and do so
var lineElement = codeLines[i];
highlightWordsInLine( lineElement, colorSegments[i] );
}
}
}
function highlightWordsInLine(element, positions) {
// Get the raw text
var str = $( element ).text();
// Build a new string with highlighting tags.
// Start
var out = str.substr(0, positions[0].iStart);
for( var j=0; j<positions.length; j++ ){
var position = positions[j];
// Apply the highlighting tag
out += '<span class="cm-s-ambiance cm-relation">';
out += str.substr( position.iStart, position.iEnd - position.iStart + 1);
out += '</span>';
// Do not forget to incluide unhighlighted text in between
if( j < positions.length-1 ){
out += str.substr( position.iEnd - position.iStart + 1, positions[j+1].iStart );
}
}
// Wrap up to end of line
out += str.substr( position.iEnd + 1);
// Reset the html element value including applied highlight tags
element.innerHTML = out;
}
函数highlightText()
{
log(“highlightText()”);
//获取对代码编辑器中文本行的引用
var codeLines=$(“#editorContainer.CodeMirror code pre>span”);
对于(var i=0;i此方法旨在简化此类操作。这可能会有所帮助。此提琴使用数组标记加载时的文本。感谢@aljordan82,我不知道markText()函数。