Jquery在Redactor文本编辑器中获得突出显示的文本

Cyb*_*kie 4 jquery wysiwyg text-editor redactor

我正在使用名为Redactor的精彩jquery文本编辑器.我正在尝试添加一个新按钮,单击该按钮可获得文本编辑器中突出显示的文本.

该脚本允许通过添加以下设置添加新按钮:

buttonsCustom: {
        button1: {
            title: 'Button', 
            callback: testButton //executes callback on button click
        } 
}  
Run Code Online (Sandbox Code Playgroud)

然后在回调中我想获得突出显示的文本

function testButton(obj, event, key)
{
     alert(highlighted_text);
}
Run Code Online (Sandbox Code Playgroud)

我彻底查看了文档,没有办法获得突出显示的文本.我试过像......这样的其他功能

function getSelText() {
  var txt = '';
  if (window.getSelection) {
    txt = window.getSelection();
  } else if (document.getSelection) {
    txt = document.getSelection();
  } else if (document.selection) {
    txt = document.selection.createRange().text;
  } else return;
  return txt;
}
Run Code Online (Sandbox Code Playgroud)

...但是文本编辑器脚本已经有了一种方法,最好使用它.

在脚本中我找到了文本选择功能在1719行的位置,但无法弄清楚如何将它用于自定义按钮.

任何有Redactor经验的人,请帮忙!

Gen*_*man 8

选择你的毒药(两种方法适用于Firefox和IE):

方法1:未记录的内部函数

有一个名为的内部函数getSelection,但它不是公共API的一部分.

你可以用它来调用它$('#redactor_content').data('redactor').getSelection().

方法2:复制功能

现在,如果您不想依赖于访问Redactor的内部,您可以将实现复制到您自己的函数中,通过调用以下内容来替换对内部变量的访问getDoc():

function getRedactorSelection(elem)
{
    var doc = elem.getDoc().get(0);
    if (doc.getSelection)
    {
        return doc.getSelection();
    }
    else if (doc.selection)
    {
        return doc.selection.createRange();
    }
};
Run Code Online (Sandbox Code Playgroud)

用法: getRedactorSelection($('#redactor_content'))

好处是保护您免受Redactor内部函数命名和调用的变化的影响,但缺点是您的代码不再与浏览器无关.