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

私はそれが私が探しているものではない。私は引用符を挿入することができますが、カーソルの位置は[/引用符]の後ろではなく、そのタグの直前です。タイプすると、 – Ben

答えて

2

おそらくあるはずですが、挿入されたコンテンツの後にカーソルを置くのは簡単ではありません。私は1つを追加する問題を作成します。

今のところ、範囲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

Perfectの後に引用符が追加されます。どうもありがとう! – Ben

関連する問題