在CKeditor中实现JQuery UI自动完成功能

时间:2017-04-03 11:31:56

标签: javascript jquery jquery-ui autocomplete ckeditor4.x

我正在尝试在CKEditor中实现JQuery UI自动完成功能,但它无法正常工作。我看到了一些示例和解决方案,如果用户键入“#”,则下拉列表会显示选项。我想要的是用户在键入时从文本编辑器中获取自动填充建议,从单词列表中获取。我想要像CKeditor中的代码,而不是textarea。

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>jQuery UI Autocomplete - Multiple values</title>
    <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    <script src="https://cdn.ckeditor.com/4.6.2/standard/ckeditor.js"></script>
</head>
<body>

<div class="ui-widget">
    <!--<input id="tags" size="50">-->
    <textarea id="tags"></textarea>
</div>
<script>
//    CKEDITOR.replace( 'tags' );
    $( function() {
        var availableTags = [
            "ActionScript",
            "AppleScript",
            "Asp",
            "BASIC",
            "C",
            "C++",
            "Clojure",
            "COBOL",
            "ColdFusion",
            "Erlang",
            "Fortran",
            "Groovy",
            "Haskell",
            "Java",
            "JavaScript",
            "Lisp",
            "Perl",
            "PHP",
            "Python",
            "Ruby",
            "Scala",
            "Scheme"
        ];
        function split( val ) {
            return val.split( / \s*/ );
        }
        function extractLast( term ) {
            return split( term ).pop();
        }

        $( "#tags" )
        // don't navigate away from the field on tab when selecting an item
            .on( "keydown", function( event ) {
                if ( event.keyCode === $.ui.keyCode.TAB &&
                    $( this ).autocomplete( "instance" ).menu.active ) {
                    event.preventDefault();
                }
            })
            .autocomplete({
                minLength: 0,
                source: function( request, response ) {
                    // delegate back to autocomplete, but extract the last term
                    response( $.ui.autocomplete.filter(
                        availableTags, extractLast( request.term ) ) );
                },
                focus: function() {
                    // prevent value inserted on focus
                    return false;
                },
                select: function( event, ui ) {
                    var terms = split( this.value );
                    // remove the current input
                    terms.pop();
                    // add the selected item
                    terms.push( ui.item.value );
                    // add placeholder to get the comma-and-space at the end
                    terms.push( "" );
                    this.value = terms.join( " " );
                    return false;
                }
            });
    } );
</script>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

如果您不受自动填充库的限制,则会编写At.js以使用特定标记 - 使用@进行用户引用,或使用:进行表情符号。关于如何将其与CKEditor集成,还有一个wiki page