繁体   English   中英

如何拖放到文本中

[英]How to drag and drop into text

在尝试构建之前,我想确定它是否可行。

从文本区域开始,可以预先填充文本,用户可以添加/删除文本。 现在,这方面有一些小元素。 它们可以是图像或HTML元素,如按钮或锚链接,更容易。 用户可以将元素拖动到文本区域,并将其插入鼠标光标位置,并通过推动其周围的现有文本占用文本空间(附近的元素也将保留,以便用户可以丢弃一秒)。 元素将保留为一个元素,以后可以将其拖动到文档中的其他位置或将被移除的视图端口之外。 当元素根据需要定位时,可以通过某种方式(正则表达式,dom等)识别元素的位置,以便可以用不同的内容替换它们。

将需要换行。 理想情况下,它可以与jQuery和IE7 +一起使用,但是,IE7的愿望可能需要改变。

我发现以下几个很接近但不完全的。

如果您认为它可以构建并且您有我应该开始的建议,请告知。

昨天我做了一些非常相似的事情,为什么不分享:)

我的目标是将文本元素放在我的文本区域中,在两行文本的中间,同时显示一个指示符,指示它将被丢弃的位置。 我相信这对你有用! ;)

$(document).ready(function() {

    var lineHeight = parseInt($('#textarea').css('line-height').replace('px',''));
    var previousLineNo = 0;
    var content = $('#textarea').val();
    var linesArray = content.length > 0 ? content.split('\n') : [];
    var lineNo = 0;
    var emptyLineAdded = false;

    $('.draggable').draggable({
        revert: function(is_valid_drop) {
            if (!is_valid_drop) {
                $('#textarea').val(content);

                return true;
            }
        },
        drag: function(event, ui) {
            lineNo = getLineNo(ui, lineHeight);

            if (linesArray.length > 0 && previousLineNo != lineNo) {
                insertWhiteLine(lineNo, linesArray);
            }

            previousLineNo = lineNo;
        }
    });

    $("#textarea").droppable({
        accept: ".draggable",
        drop: function( event, ui ) {
            appendAtLine(lineNo, linesArray, ui.draggable.text());
            $(ui.draggable).remove();
            content = $('#textarea').val();
            linesArray = content.split('\n');

            if (linesArray[linesArray.length - 1] == '')
                linesArray.pop(); //remove empty line
        }
    });

    $('#textarea').on('input', function() {
        if (!emptyLineAdded) {
            console.log('input !');
            console.log($('#textarea').val());
            content = $('#textarea').val();
            linesArray = content.split('\n');

            if (linesArray[linesArray.length - 1] == '')
                linesArray.pop(); //remove empty line
        }
    });

});


//Returns the top position of a draggable element,
//relative to the textarea. (0 means at the very top of the textarea)
function getYPosition(element, lineHeight) {
    var participantIndex = $(element.helper.context).index();
    var initPos = participantIndex * lineHeight;
    var actualPos = initPos + element.position.top;

    return actualPos;
}


//Returns the line number corresponding to where the element
//would be dropped
function getLineNo(element, lineHeight) {
    return Math.round(getYPosition(element, lineHeight) / lineHeight);
}


//Inserts a white line at the given line number,
//to show where the element would be dropped in the textarea
function insertWhiteLine(lineNo, linesArray) {
    $('#textarea').val('');
    $(linesArray).each(function(index, value) {
        if (index < lineNo)
            $('#textarea').val($('#textarea').val() + value + '\n');
    });

    emptyLineAdded = true;
    $('#textarea').val($('#textarea').val() + '_____________\n'); //white line
    emptyLineAdded = false;

    $(linesArray).each(function(index, value) {
        if (index >= lineNo)
            $('#textarea').val($('#textarea').val() + value + '\n');
    });
}


//Inserts content of draggable at the given line number
function appendAtLine(lineNo, linesArray, content) {
    $('#textarea').val('');
    $(linesArray).each(function(index, value) {
        if (index < lineNo)
            $('#textarea').val($('#textarea').val() + value + '\n');
    });

    $('#textarea').val($('#textarea').val() + content + '\n'); //content to be added

    $(linesArray).each(function(index, value) {
        if (index >= lineNo)
            $('#textarea').val($('#textarea').val() + value + '\n');
    });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM