2017-04-20 108 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

相關問題