答案 0 :(得分:0)
我举了两个例子。
首先,同步选择:
只要此事件触发器通过cursorActivity
方法获取选择详细信息,然后通过getCursor
方法将其设置为第二个编辑器,则为setSelection
事件添加事件监听器。
firstEditor.on("cursorActivity", () => {
let head = this.ca.getCursor("head");
let anchor = this.ca.getCursor("anchor");
secondEditor.setSelection(anchor, head);
});
第二行,突出显示选择行:
再次,只要此事件触发器通过cursorActivity
方法获得选择的开始和结束行,然后为getCursor
事件添加事件侦听器,然后在第二个编辑器中突出显示这些行。
要突出显示行,CodeMirror具有active-line
选项启用的插件调用styleActiveLine
。但不幸的是,我无法在多行中使用此功能,因此我针对这种情况进行了修改。
const WRAP_CLASS = "CodeMirror-activeline";
const BACK_CLASS = "CodeMirror-activeline-background";
const GUTT_CLASS = "CodeMirror-activeline-gutter";
const clearActiveLines = function() {
for (let i = 0; i < this.lineCount(); i++) {
this.removeLineClass(i, "wrap", WRAP_CLASS);
this.removeLineClass(i, "background", BACK_CLASS);
this.removeLineClass(i, "gutter", GUTT_CLASS);
}
};
const setActiveLines = function(from, to) {
clearActiveLines.call(this);
for (let i = from; i <= to; i++) {
this.addLineClass(i, "wrap", WRAP_CLASS);
this.addLineClass(i, "background", BACK_CLASS);
this.addLineClass(i, "gutter", GUTT_CLASS);
}
};
CodeMirror.defineExtension("setActiveLines", setActiveLines);
CodeMirror.defineExtension("clearActiveLines", clearActiveLines);
然后使用它。
firstEditor.on("cursorActivity", () => {
if (firstEditor.getSelection()) {
let from = this.ca.getCursor("from");
let to = this.ca.getCursor("to");
secondEditor.setActiveLines(from.line, to.line);
} else {
secondEditor.clearActiveLines();
}
});
我对CodeMirror不太熟悉,但希望能有所帮助。