diff --git a/mention/plugin.js b/mention/plugin.js index 38dbb13..9fa4a89 100644 --- a/mention/plugin.js +++ b/mention/plugin.js @@ -184,7 +184,18 @@ }, lookup: function () { - this.query = $.trim($(this.editor.getBody()).find('#autocomplete-searchtext').text()).replace('\ufeff', ''); + var body = $(this.editor.getBody()); + var autoCompleteSearchText = body.find('#autocomplete-searchtext'); + this.query = $.trim(autoCompleteSearchText.text()).replace('\ufeff', ''); + if (this.query === '') { + var autoComplete = body.find('#autocomplete'); + if (autoComplete.length) { + autoCompleteSearchText.appendTo(autoComplete); + this.editor.selection.select(autoCompleteSearchText.find('span')[0]); + } else { + this.cleanUp(true); + } + } if (this.$dropdown === undefined) { this.show(); @@ -253,11 +264,21 @@ items = _this.sorter(items); - items = items.slice(0, this.options.items); + if (this.options.items === -1) { + items = items.slice(); + } else { + items = items.slice(0, this.options.items); + } $.each(items, function (i, item) { var $element = $(_this.render(item, i)); + var textNodes = $element.find('*').andSelf().contents().filter(function() { + return this.nodeType === 3; //Node.TEXT_NODE + }).each(function(index, element) { + $(element).parent().html(_this.highlighter(element.textContent)); + }); + $element.html($element.html().replace($element.text(), _this.highlighter($element.text()))); $.each(items[i], function (key, val) { @@ -269,6 +290,7 @@ if (result.length) { this.$dropdown.html(result.join('')).show(); + this.highlightNextResult(); } else { this.$dropdown.hide(); this.$dropdown.find('li').removeClass('active'); diff --git a/mention/plugin.min.js b/mention/plugin.min.js index 9ab7437..9706857 100644 --- a/mention/plugin.min.js +++ b/mention/plugin.min.js @@ -1 +1 @@ -!function(t){"use strict";if("object"==typeof exports&&"undefined"!=typeof module)module.exports=t(require("jquery"));else if("function"==typeof define&&define.amd)define(["jquery"],t);else{t(("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).jQuery)}}(function(t){"use strict";var e=function(e,i){this.editor=e,this.options=t.extend({},{source:[],delay:500,queryBy:"name",items:10},i),this.options.insertFrom=this.options.insertFrom||this.options.queryBy,this.matcher=this.options.matcher||this.matcher,this.sorter=this.options.sorter||this.sorter,this.renderDropdown=this.options.renderDropdown||this.renderDropdown,this.render=this.options.render||this.render,this.insert=this.options.insert||this.insert,this.highlighter=this.options.highlighter||this.highlighter,this.query="",this.hasFocus=!0,this.renderInput(),this.bindEvents()};e.prototype={constructor:e,renderInput:function(){var t=''+this.options.delimiter+'\ufeff';this.editor.execCommand("mceInsertContent",!1,t),this.editor.focus(),this.editor.selection.select(this.editor.selection.dom.select("span#autocomplete-searchtext span")[0]),this.editor.selection.collapse(0)},bindEvents:function(){this.editor.on("keyup",this.editorKeyUpProxy=t.proxy(this.rteKeyUp,this)),this.editor.on("keydown",this.editorKeyDownProxy=t.proxy(this.rteKeyDown,this),!0),this.editor.on("click",this.editorClickProxy=t.proxy(this.rteClicked,this)),t("body").on("click",this.bodyClickProxy=t.proxy(this.rteLostFocus,this)),t(this.editor.getWin()).on("scroll",this.rteScroll=t.proxy(function(){this.cleanUp(!0)},this))},unbindEvents:function(){this.editor.off("keyup",this.editorKeyUpProxy),this.editor.off("keydown",this.editorKeyDownProxy),this.editor.off("click",this.editorClickProxy),t("body").off("click",this.bodyClickProxy),t(this.editor.getWin()).off("scroll",this.rteScroll)},rteKeyUp:function(t){switch(t.which||t.keyCode){case 40:case 38:case 16:case 17:case 18:break;case 8:""===this.query?this.cleanUp(!0):this.lookup();break;case 9:case 13:var e=void 0!==this.$dropdown?this.$dropdown.find("li.active"):[];e.length?(this.select(e.data()),this.cleanUp(!1)):this.cleanUp(!0);break;case 27:this.cleanUp(!0);break;default:this.lookup()}},rteKeyDown:function(t){switch(t.which||t.keyCode){case 9:case 13:case 27:t.preventDefault();break;case 38:t.preventDefault(),void 0!==this.$dropdown&&this.highlightPreviousResult();break;case 40:t.preventDefault(),void 0!==this.$dropdown&&this.highlightNextResult()}t.stopPropagation()},rteClicked:function(e){var i=t(e.target);this.hasFocus&&"autocomplete-searchtext"!==i.parent().attr("id")&&this.cleanUp(!0)},rteLostFocus:function(){this.hasFocus&&this.cleanUp(!0)},lookup:function(){this.query=t.trim(t(this.editor.getBody()).find("#autocomplete-searchtext").text()).replace("\ufeff",""),void 0===this.$dropdown&&this.show(),clearTimeout(this.searchTimeout),this.searchTimeout=setTimeout(t.proxy(function(){var e=t.isFunction(this.options.source)?this.options.source(this.query,t.proxy(this.process,this),this.options.delimiter):this.options.source;e&&this.process(e)},this),this.options.delay)},matcher:function(t){return~t[this.options.queryBy].toLowerCase().indexOf(this.query.toLowerCase())},sorter:function(t){for(var e,i=[],o=[],s=[];void 0!==(e=t.shift());)e[this.options.queryBy].toLowerCase().indexOf(this.query.toLowerCase())?~e[this.options.queryBy].indexOf(this.query)?o.push(e):s.push(e):i.push(e);return i.concat(o,s)},highlighter:function(t){return t.replace(new RegExp("("+this.query.replace(/([.?*+^$[\]\\(){}|-])/g,"\\$1")+")","ig"),function(t,e){return""+e+""})},show:function(){var e=this.editor.inline?this.offsetInline():this.offset();this.$dropdown=t(this.renderDropdown()).css({top:e.top,left:e.left}),t("body").append(this.$dropdown),this.$dropdown.on("click",t.proxy(this.autoCompleteClick,this))},process:function(e){if(this.hasFocus){var i=this,o=[],s=t.grep(e,function(t){return i.matcher(t)});s=i.sorter(s),s=s.slice(0,this.options.items),t.each(s,function(e,n){var r=t(i.render(n,e));r.html(r.html().replace(r.text(),i.highlighter(r.text()))),t.each(s[e],function(t,e){r.attr("data-"+t,e)}),o.push(r[0].outerHTML)}),o.length?this.$dropdown.html(o.join("")).show():this.$dropdown.hide()}},renderDropdown:function(){return''},render:function(t,e){return'
  • '+t[this.options.queryBy]+"
  • "},autoCompleteClick:function(e){var i=t(e.target).closest("li").data();t.isEmptyObject(i)||(this.select(i),this.cleanUp(!1)),e.stopPropagation(),e.preventDefault()},highlightPreviousResult:function(){var t=this.$dropdown.find("li.active").index(),e=0===t?this.$dropdown.find("li").length-1:--t;this.$dropdown.find("li").removeClass("active").eq(e).addClass("active")},highlightNextResult:function(){var t=this.$dropdown.find("li.active").index(),e=t===this.$dropdown.find("li").length-1?0:++t;this.$dropdown.find("li").removeClass("active").eq(e).addClass("active")},select:function(t){this.editor.focus();var e=this.editor.dom.select("span#autocomplete")[0];this.editor.dom.remove(e),this.editor.execCommand("mceInsertContent",!1,this.insert(t))},insert:function(t){return""+t[this.options.insertFrom]+" "},cleanUp:function(e){if(this.unbindEvents(),this.hasFocus=!1,void 0!==this.$dropdown&&(this.$dropdown.remove(),delete this.$dropdown),e){var i=this.query,o=t(this.editor.dom.select("span#autocomplete"));if(!o.length)return;var s=t("

    "+this.options.delimiter+i+"

    ")[0].firstChild,n=t(this.editor.selection.getNode()).offset().top===o.offset().top+(o.outerHeight()-o.height())/2;this.editor.dom.replace(s,o[0]),n&&(this.editor.selection.select(s),this.editor.selection.collapse())}},offset:function(){var e=t(this.editor.getContainer()).offset(),i=t(this.editor.getContentAreaContainer()).position(),o=t(this.editor.dom.select("span#autocomplete")).position();return{top:e.top+i.top+o.top+t(this.editor.selection.getNode()).innerHeight()-t(this.editor.getDoc()).scrollTop()+5,left:e.left+i.left+o.left}},offsetInline:function(){var e=t(this.editor.dom.select("span#autocomplete")).offset();return{top:e.top+t(this.editor.selection.getNode()).innerHeight()+5,left:e.left}}},tinymce.create("tinymce.plugins.Mention",{init:function(i){function o(){var e=i.selection.getRng(!0).startOffset,o=(i.selection.getRng(!0).startContainer.data||"").substr(e>0?e-1:0,1);return!t.trim(o).length}var s,n=i.getParam("mentions");n.delimiter=void 0!==n.delimiter?t.isArray(n.delimiter)?n.delimiter:[n.delimiter]:["@"],i.on("keypress",function(r){var h=t.inArray(String.fromCharCode(r.which||r.keyCode),n.delimiter);h>-1&&o()&&(void 0===s||void 0!==s.hasFocus&&!s.hasFocus)&&(r.preventDefault(),s=new e(i,t.extend({},n,{delimiter:n.delimiter[h]})))})},getInfo:function(){return{longname:"mention",author:"Steven Devooght",version:tinymce.majorVersion+"."+tinymce.minorVersion}}}),tinymce.PluginManager.add("mention",tinymce.plugins.Mention)}); \ No newline at end of file +(function(f){"use strict";if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f(require("jquery"))}else if(typeof define==="function"&&define.amd){define(["jquery"],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}f(g.jQuery)}})(function($){"use strict";var AutoComplete=function(ed,options){this.editor=ed;this.options=$.extend({},{source:[],delay:500,queryBy:"name",items:10},options);this.options.insertFrom=this.options.insertFrom||this.options.queryBy;this.matcher=this.options.matcher||this.matcher;this.sorter=this.options.sorter||this.sorter;this.renderDropdown=this.options.renderDropdown||this.renderDropdown;this.render=this.options.render||this.render;this.insert=this.options.insert||this.insert;this.highlighter=this.options.highlighter||this.highlighter;this.query="";this.hasFocus=true;this.renderInput();this.bindEvents()};AutoComplete.prototype={constructor:AutoComplete,renderInput:function(){var rawHtml=''+''+this.options.delimiter+""+'\ufeff'+"";this.editor.execCommand("mceInsertContent",false,rawHtml);this.editor.focus();this.editor.selection.select(this.editor.selection.dom.select("span#autocomplete-searchtext span")[0]);this.editor.selection.collapse(0)},bindEvents:function(){this.editor.on("keyup",this.editorKeyUpProxy=$.proxy(this.rteKeyUp,this));this.editor.on("keydown",this.editorKeyDownProxy=$.proxy(this.rteKeyDown,this),true);this.editor.on("click",this.editorClickProxy=$.proxy(this.rteClicked,this));$("body").on("click",this.bodyClickProxy=$.proxy(this.rteLostFocus,this));$(this.editor.getWin()).on("scroll",this.rteScroll=$.proxy(function(){this.cleanUp(true)},this))},unbindEvents:function(){this.editor.off("keyup",this.editorKeyUpProxy);this.editor.off("keydown",this.editorKeyDownProxy);this.editor.off("click",this.editorClickProxy);$("body").off("click",this.bodyClickProxy);$(this.editor.getWin()).off("scroll",this.rteScroll)},rteKeyUp:function(e){switch(e.which||e.keyCode){case 40:case 38:case 16:case 17:case 18:break;case 8:if(this.query===""){this.cleanUp(true)}else{this.lookup()}break;case 9:case 13:var item=this.$dropdown!==undefined?this.$dropdown.find("li.active"):[];if(item.length){this.select(item.data());this.cleanUp(false)}else{this.cleanUp(true)}break;case 27:this.cleanUp(true);break;default:this.lookup()}},rteKeyDown:function(e){switch(e.which||e.keyCode){case 9:case 13:case 27:e.preventDefault();break;case 38:e.preventDefault();if(this.$dropdown!==undefined){this.highlightPreviousResult()}break;case 40:e.preventDefault();if(this.$dropdown!==undefined){this.highlightNextResult()}break}e.stopPropagation()},rteClicked:function(e){var $target=$(e.target);if(this.hasFocus&&$target.parent().attr("id")!=="autocomplete-searchtext"){this.cleanUp(true)}},rteLostFocus:function(){if(this.hasFocus){this.cleanUp(true)}},lookup:function(){var body=$(this.editor.getBody());var autoCompleteSearchText=body.find("#autocomplete-searchtext");this.query=$.trim(autoCompleteSearchText.text()).replace("\ufeff","");if(this.query===""){var autoComplete=body.find("#autocomplete");if(autoComplete.length){autoCompleteSearchText.appendTo(autoComplete);this.editor.selection.select(autoCompleteSearchText.find("span")[0])}else{this.cleanUp(true)}}if(this.$dropdown===undefined){this.show()}clearTimeout(this.searchTimeout);this.searchTimeout=setTimeout($.proxy(function(){var items=$.isFunction(this.options.source)?this.options.source(this.query,$.proxy(this.process,this),this.options.delimiter):this.options.source;if(items){this.process(items)}},this),this.options.delay)},matcher:function(item){return~item[this.options.queryBy].toLowerCase().indexOf(this.query.toLowerCase())},sorter:function(items){var beginswith=[],caseSensitive=[],caseInsensitive=[],item;while((item=items.shift())!==undefined){if(!item[this.options.queryBy].toLowerCase().indexOf(this.query.toLowerCase())){beginswith.push(item)}else if(~item[this.options.queryBy].indexOf(this.query)){caseSensitive.push(item)}else{caseInsensitive.push(item)}}return beginswith.concat(caseSensitive,caseInsensitive)},highlighter:function(text){return text.replace(new RegExp("("+this.query.replace(/([.?*+^$[\]\\(){}|-])/g,"\\$1")+")","ig"),function($1,match){return""+match+""})},show:function(){var offset=this.editor.inline?this.offsetInline():this.offset();this.$dropdown=$(this.renderDropdown()).css({top:offset.top,left:offset.left});$("body").append(this.$dropdown);this.$dropdown.on("click",$.proxy(this.autoCompleteClick,this))},process:function(data){if(!this.hasFocus){return}var _this=this,result=[],items=$.grep(data,function(item){return _this.matcher(item)});items=_this.sorter(items);if(this.options.items===-1){items=items.slice()}else{items=items.slice(0,this.options.items)}$.each(items,function(i,item){var $element=$(_this.render(item,i));var textNodes=$element.find("*").andSelf().contents().filter(function(){return this.nodeType===3}).each(function(index,element){$(element).parent().html(_this.highlighter(element.textContent))});$element.html($element.html().replace($element.text(),_this.highlighter($element.text())));$.each(items[i],function(key,val){$element.attr("data-"+key,val)});result.push($element[0].outerHTML)});if(result.length){this.$dropdown.html(result.join("")).show();this.highlightNextResult()}else{this.$dropdown.hide();this.$dropdown.find("li").removeClass("active")}},renderDropdown:function(){return''},render:function(item,index){return"
  • "+''+item[this.options.queryBy]+""+"
  • "},autoCompleteClick:function(e){var item=$(e.target).closest("li").data();if(!$.isEmptyObject(item)){this.select(item);this.cleanUp(false)}e.stopPropagation();e.preventDefault()},highlightPreviousResult:function(){var currentIndex=this.$dropdown.find("li.active").index(),index=currentIndex===0?this.$dropdown.find("li").length-1:--currentIndex;this.$dropdown.find("li").removeClass("active").eq(index).addClass("active")},highlightNextResult:function(){var currentIndex=this.$dropdown.find("li.active").index(),index=currentIndex===this.$dropdown.find("li").length-1?0:++currentIndex;this.$dropdown.find("li").removeClass("active").eq(index).addClass("active")},select:function(item){this.editor.focus();var selection=this.editor.dom.select("span#autocomplete")[0];this.editor.dom.remove(selection);this.editor.execCommand("mceInsertContent",false,this.insert(item))},insert:function(item){return""+item[this.options.insertFrom]+" "},cleanUp:function(rollback){this.unbindEvents();this.hasFocus=false;if(this.$dropdown!==undefined){this.$dropdown.remove();delete this.$dropdown}if(rollback){var text=this.query,$selection=$(this.editor.dom.select("span#autocomplete"));if(!$selection.length){return}var replacement=$("

    "+this.options.delimiter+text+"

    ")[0].firstChild,focus=$(this.editor.selection.getNode()).offset().top===$selection.offset().top+($selection.outerHeight()-$selection.height())/2;this.editor.dom.replace(replacement,$selection[0]);if(focus){this.editor.selection.select(replacement);this.editor.selection.collapse()}}},offset:function(){var rtePosition=$(this.editor.getContainer()).offset(),contentAreaPosition=$(this.editor.getContentAreaContainer()).position(),nodePosition=$(this.editor.dom.select("span#autocomplete")).position();return{top:rtePosition.top+contentAreaPosition.top+nodePosition.top+$(this.editor.selection.getNode()).innerHeight()-$(this.editor.getDoc()).scrollTop()+5,left:rtePosition.left+contentAreaPosition.left+nodePosition.left}},offsetInline:function(){var nodePosition=$(this.editor.dom.select("span#autocomplete")).offset();return{top:nodePosition.top+$(this.editor.selection.getNode()).innerHeight()+5,left:nodePosition.left}}};tinymce.create("tinymce.plugins.Mention",{init:function(ed){var autoComplete,autoCompleteData=ed.getParam("mentions");autoCompleteData.delimiter=autoCompleteData.delimiter!==undefined?!$.isArray(autoCompleteData.delimiter)?[autoCompleteData.delimiter]:autoCompleteData.delimiter:["@"];function prevCharIsSpace(){var start=ed.selection.getRng(true).startOffset,text=ed.selection.getRng(true).startContainer.data||"",charachter=text.substr(start>0?start-1:0,1);return!!$.trim(charachter).length?false:true}ed.on("keypress",function(e){var delimiterIndex=$.inArray(String.fromCharCode(e.which||e.keyCode),autoCompleteData.delimiter);if(delimiterIndex>-1&&prevCharIsSpace()){if(autoComplete===undefined||autoComplete.hasFocus!==undefined&&!autoComplete.hasFocus){e.preventDefault();autoComplete=new AutoComplete(ed,$.extend({},autoCompleteData,{delimiter:autoCompleteData.delimiter[delimiterIndex]}))}}})},getInfo:function(){return{longname:"mention",author:"Steven Devooght",version:tinymce.majorVersion+"."+tinymce.minorVersion}}});tinymce.PluginManager.add("mention",tinymce.plugins.Mention)}); \ No newline at end of file