在匹配标签之间获取价值? (代码镜像)
Get value between matching tags? (CodeMirror)
我正在使用 Codemirror 5.3。
我在混合模式文档中匹配 html 标签以突出显示开始和结束标签 - 这很有效。 (https://codemirror.net/demo/matchtags.html)
我正在尝试捕获标记之间的内容(在我的例子中使用右键单击标签的上下文菜单操作),因此我可以将其发送到外部进程。
我使用 var tm = doc.getAllMarks();
,因为我是标签匹配而不是书签,所以我很清楚数组中只会有两个项目。但是,这个 returns 不包含的 TextMarker 数组(据我所知)包含标记的 {line, ch}
游标。
是否有正确的方法来获取标记的开始和结束位置 - 直接或作为行和字符位置?我能想到的最好的方法是迭代每个:
[].lines[0].parent.lines
并查看 CodeMirror.Line 的每个实例是否都有一个 markedSpans
对象,这会给我行索引,然后使用 [].lines[0].markedSpans[0].from
和 [].lines[0].markedSpans[0].to
找到字符在标记中的位置。然后使用 doc.getRange
抓取内容并将其随机播放以进行处理......像这样:
var tm = doc.getAllMarks(),
lines = tm[0].lines[0].parent.lines,
range = {
from: { line: 0, ch: 0},
to: { line: 0, ch: 0 }
},
hack = 0,
textContent = "";
for (var i=0,j=lines.length;i<j;i++) {
if (lines[i].hasOwnProperty("markedSpans")) {
if (hack==0) { // sorry, i'm in a hurry
range.from.line = i;
range.from.ch = lines[i].markedSpans.from;
hack=1;
} else {
range.to.line = i;
range.to.ch = lines[i].markedSpans.to;
}
}
}
textContent = doc.getRange(range.from,range.to);
所有这一切听起来很麻烦,我正在寻找更好的方法。
您可以在 return 由 markText
编辑的对象上调用 .find()
,它将 return 标记的 {from, to}
位置(或 null
如果标记被清除)。
我正在使用 Codemirror 5.3。
我在混合模式文档中匹配 html 标签以突出显示开始和结束标签 - 这很有效。 (https://codemirror.net/demo/matchtags.html)
我正在尝试捕获标记之间的内容(在我的例子中使用右键单击标签的上下文菜单操作),因此我可以将其发送到外部进程。
我使用 var tm = doc.getAllMarks();
,因为我是标签匹配而不是书签,所以我很清楚数组中只会有两个项目。但是,这个 returns 不包含的 TextMarker 数组(据我所知)包含标记的 {line, ch}
游标。
是否有正确的方法来获取标记的开始和结束位置 - 直接或作为行和字符位置?我能想到的最好的方法是迭代每个:
[].lines[0].parent.lines
并查看 CodeMirror.Line 的每个实例是否都有一个 markedSpans
对象,这会给我行索引,然后使用 [].lines[0].markedSpans[0].from
和 [].lines[0].markedSpans[0].to
找到字符在标记中的位置。然后使用 doc.getRange
抓取内容并将其随机播放以进行处理......像这样:
var tm = doc.getAllMarks(),
lines = tm[0].lines[0].parent.lines,
range = {
from: { line: 0, ch: 0},
to: { line: 0, ch: 0 }
},
hack = 0,
textContent = "";
for (var i=0,j=lines.length;i<j;i++) {
if (lines[i].hasOwnProperty("markedSpans")) {
if (hack==0) { // sorry, i'm in a hurry
range.from.line = i;
range.from.ch = lines[i].markedSpans.from;
hack=1;
} else {
range.to.line = i;
range.to.ch = lines[i].markedSpans.to;
}
}
}
textContent = doc.getRange(range.from,range.to);
所有这一切听起来很麻烦,我正在寻找更好的方法。
您可以在 return 由 markText
编辑的对象上调用 .find()
,它将 return 标记的 {from, to}
位置(或 null
如果标记被清除)。