2017-04-20 61 views
2

我试图插入一个引号,然后将光标移动到编辑器的末尾。目前,我插入报价,光标结束了报价,所以如果你要开始输入,用户将添加到报价这不是我想要发生的报价使用SCEditor将光标设置到文本的末尾

我试过了documentation弄清楚如何做到这一点,但我不认为我引用的理解

这里是我当前的代码:

$(document.body).on('click', '.action.quote', function() { 

     var $target = $($(this).data('target')); 
     var editor = $('#reply-body').sceditor('instance'); 
     var bodyText = $target.html(); 

     editor.insert('[quote=author]' + bodyText + '[/quote]'); 
     editor.focus(); 

     smoothScroll('#new-comment'); 
    }); 

是否有一个快速的方法来做到这一点?我对SCEditor非常陌生,所以对我来说都是新的

+0

这是否帮助呢? https://www.sceditor.com/documentation/custom-bbcodes/#format + https://www.sceditor.com/documentation/custom-commands/#exec – brunoais

+0

我不确定这就是我要找的。我可以插入报价,但光标的位置并不在[/ quote]之后,而是位于该标签之前,因此当您打字时,不会在 – Ben

回答

2

目前还没有简单的方法将光标置于插入的内容之后,尽管可能应该有。我会创建一个问题来添加一个。

现在,您需要使用范围API手动移动光标。像这样的东西应该工作:

$(document.body).on('click', '.action.quote', function() { 
    var $target = $($(this).data('target')); 
    var editor = $('#reply-body').sceditor('instance'); 
    var bodyText = 'Dummy text for example'; 

    editor.insert('[quote=author]' + bodyText + '[/quote]'); 
    editor.focus(); 

    var rangeHelper = editor.getRangeHelper(); 
    var range = rangeHelper.cloneSelected(); 

    // Handle DOM ranges, if you casre about IE < 9 
    // you'll need to handle TextRanges too 
    if ('selectNodeContents' in range) { 
     var bodyChildren = editor.getBody()[0].children; 

     // Select the last child of the body 
     range.selectNodeContents(bodyChildren[bodyChildren.length - 1]); 

     // Move cursor to after it 
     range.collapse(false); 
     rangeHelper.selectRange(range); 
    } 

    smoothScroll('#new-comment'); 
}); 

活生生的例子:https://jsfiddle.net/3z8cg8z1/

+0

之后添加报价。非常感谢! – Ben

相关问题