用于jQuery文本编辑器样式选择的插件?

时间:2010-02-26 20:04:08

标签: jquery jquery-ui select text-editor selectable

我想要实现的是使用jQuery来模仿您在典型的文本编辑器中看到的文本选择功能的行为,除了不选择文本,我想选择多行{{1} }秒。然而,到目前为止,我发现jQuery的唯一“选择”插件基于矩形套索模型运行。特别是,我正在使用jQueryUI可选插件。要了解我在说什么,请考虑以下2张图片:

默认的jQueryUI“可选”插件行为 http://img685.imageshack.us/img685/2344/selectabledefaultthumb.png

理想的插件行为(没有套索) http://img709.imageshack.us/img709/5664/selectableidealthumb.png

你也可以去here玩这个确切的例子。有人知道一个实现这个目标的插件吗?这样可以避免我继续攻击或破解这个插件来获得我想要的东西......

P / S:在我的应用程序中,每行最多包含150个左右的div,每个div中都有几个div。我试过手动滚动自己的可选择但是即使只处理一行也很慢。我目前正在使用这个插件,因为它比我写的更高效。

3 个答案:

答案 0 :(得分:2)

也许这可能会以某种方式进行优化但我只在Chrome中进行了测试,但我认为它也适用于其他浏览器。这不需要jQuery UI,它是手工制作的;)

$(function() {
    var selectableLi = $('#selectable li');
    selectableLi.mousedown(function(){
        var startIndex, endIndex, mouseUpOnLi = false;

        // When dragging starts, remove classes active and hover
        selectableLi.removeClass('active hover');

        // Give the element where dragging starts a class
        $(this).addClass('active');

        // Save the start index
        startIndex = $(this).index();

        // Bind mouse up event 
        selectableLi.bind('mouseup', function(){

            // Mouse up is on a li-element
            mouseUpOnLi = true;
            $(this).addClass('active');

            // Remove the events for mouseup, mouseover and mouseout
            selectableLi.unbind('mouseup mouseover mouseout');

            // Store the end index
            endIndex = $(this).index();

            // Swap values if endIndex < startindex
            if(endIndex < startIndex){
                var tmp = startIndex;
                startIndex = endIndex;
                endIndex = tmp;                 
            }

            // Give the selected elements a colour
            for(i=startIndex; i<=endIndex; i++){
                $(selectableLi[i]).addClass('active');
            }

        }).bind('mouseover', function(){
            // Give elements a hover class when hovering
            $(this).addClass('hover');
        }).bind('mouseout', function(){
            // Remove the hover class when mouse moves out the li
            $(this).removeClass('hover');
        });

        $(document).bind('mouseup', function(e){
            // When mouse up is outside a li-element
            if(!mouseUpOnLi){
                selectableLi.removeClass('active');
            }
            $(this).unbind('mouseup');
        });
    }).attr("unselectable","on").css("MozUserSelect","none").bind("selectstart",function(){return false});
});

我有 example online 。请注意,选择时项目没有背景颜色;我认为这会带来更好的表现。


更新 - Example 2

我更新了它,因此在选择时可以看到选择:

var selectableLi;

function colourSelected(a, b, Class){
    selectableLi.removeClass(Class);
    // Swap values if a > b
    if(a > b){
        var tmp = a;
        a = b;
        b = tmp;                    
    }

    // Give the selected elements a colour
    for(i=a; i<=b; i++){
        $(selectableLi[i]).addClass(Class);
    }       
}

$(function() {
    selectableLi = $('#selectable li');
    selectableLi.mousedown(function(){
        var startIndex, endIndex, mouseUpOnLi = false;

        // When dragging starts, remove classes active and hover
        selectableLi.removeClass('active hover');

        // Give the element where dragging starts a class
        $(this).addClass('active');

        // Save the start index
        startIndex = $(this).index();

        // Bind mouse up event 
        selectableLi.bind('mouseup', function(){

            // Mouse up is on a li-element
            mouseUpOnLi = true;
            $(this).addClass('active');

            // Remove the events for mouseup, mouseover and mouseout
            selectableLi.unbind('mouseup mouseover mouseout');

            colourSelected(startIndex, $(this).index(), 'active');

        }).bind('mouseover mouseout', function(){
            // Give elements a hover class when hovering
            colourSelected(startIndex, $(this).index(), 'hover');
        });

        $(document).bind('mouseup', function(e){
            // When mouse up is outside a li-element
            if(!mouseUpOnLi){
                selectableLi.removeClass('active hover');
            }
            $(this).unbind('mouseup');
            selectableLi.unbind('mouseover mouseout');
        });
    }).attr("unselectable","on").css("MozUserSelect","none").bind("selectstart",function(){return false});
});

同样,也许这段代码可以以某种方式进行优化以提高性能。

答案 1 :(得分:0)

也许你已经为此制作了自己的剧本,但我对我进行了优化和改进。它仅在需要时添加或删除类,这对性能很有帮助。

它也有一些可能有用的方法:

var sR = $('#selectable').selectableRange({
    /* Alternatively, you could overwrite default options
    classname: 'active',
    log: false,
    logElement: $('#log'),
    nodename: 'LI'*/
});

// Initialize the selectable so it works
sR.init();

// You can always change options like this:
$('#logOnOff').click(function(){
    // Toggle log
    sR.options.log = (sR.options.log) ? false : true;
});

// Also you can use this methods:
// sR.deselect()
// sR.destroy()
// sR.getSelectedItems()

Give it a try ,代码 also availabe

答案 2 :(得分:0)

我会使用jQuery功能创建自己的版本。

首先,将事件与“stop:”接口(也许就像序列化http://jqueryui.com/demos/selectable/#serialize

然后看一下我回来的ID,最低和最高会给我足够的一个简单的“for ... next”循环剩下的对象。

我知道它是一个修复/破解解决方案,但从我的角度来看似乎解决了这个问题,它对你有用还是你也需要代码?只想先提供算法思想。 :O)

相关问题