You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
information-system/wysiwyg/plugins/sourcelocation/plugin.js

54 lines
2.1 KiB
JavaScript

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

CKEDITOR.plugins.add("sourcelocation", {
init: function (editor) {
var selectText = '';//选中的文字
// var selectRange;//可视化编辑器选中的区域用于source返回wysiwyg后的定位
//var bookmark;
editor.on("mode", function () {
if (selectText !== '') {
if (editor.mode === "wysiwyg") {
//console.log(bookmark);
//editor.getSelection().selectBookmarks( bookmark).scrollIntoView();
//editor.getSelection().scrollIntoView();
} else {
if (selectText !== "") {
var codemirror = window["codemirror_" + editor.id]; //编辑器的codemirror实例
var find_start = -1;
var find_str = selectText; //需要查找的字符串
for (i = 0; i < codemirror.doc.lastLine(); i++) {
line = codemirror.doc.getLineHandle(i);
find_start = line.text.indexOf(find_str);
if (find_start !== -1) {
line_number = codemirror.doc.getLineNumber(line);
//console.log(find_start,codemirror.doc.getLineNumber(line),line.text);
codemirror.doc.setCursor(line_number + 10); //滚动到稍微远一点的行,选中的文本才可以展示在编辑器中间
codemirror.doc.setSelection(
{ line: line_number, ch: find_start },
{ line: line_number, ch: find_start + find_str.length }
);
break;
}
}
}
}
}
});
//监控source按钮按下之前的事件获取当前选中的文字
editor.on('beforeCommandExec', function (event) {
var commandName = event.data.name;
if (commandName == 'source') {
if (editor.mode === "wysiwyg") {
selectText = editor.getSelectedHtml(true);
//selectRange = editor.getSelection().getRanges();
// bookmark = editor.getSelection().createBookmarks();
//console.log(bookmark);
} else {
selectText = window["codemirror_" + editor.id].doc.getSelection();
}
}
});
}
});