From 7ebec03052c6be203f67aa5a9a01ba0b449cc59d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=B9=E8=AF=9A=E8=AF=9A?= Date: Wed, 23 Oct 2019 16:42:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=BC=96=E8=BE=91=E5=99=A8ht?= =?UTF-8?q?ml=E6=BA=90=E7=A0=81=E5=AE=9A=E4=BD=8D=E6=8F=92=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- wysiwyg/config.js | 2 +- wysiwyg/plugins/sourcelocation/plugin.js | 53 ++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 wysiwyg/plugins/sourcelocation/plugin.js diff --git a/wysiwyg/config.js b/wysiwyg/config.js index e7ba59d2..46affd19 100644 --- a/wysiwyg/config.js +++ b/wysiwyg/config.js @@ -45,7 +45,7 @@ CKEDITOR.editorConfig = function( config ) { - config.extraPlugins= 'tableresize,uploadimage,image2,codemirror,hianamedia'; + config.extraPlugins= 'tableresize,uploadimage,image2,codemirror,hianamedia,sourcelocation'; config.codemirror = { showFormatButton: false, showCommentButton: false, diff --git a/wysiwyg/plugins/sourcelocation/plugin.js b/wysiwyg/plugins/sourcelocation/plugin.js new file mode 100644 index 00000000..639fd626 --- /dev/null +++ b/wysiwyg/plugins/sourcelocation/plugin.js @@ -0,0 +1,53 @@ +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(); + } + } + }); + + } +});