quill-placeholder-autocomplete-module
Advanced tools
Comparing version 1.0.5 to 1.0.6
@@ -1,1 +0,1 @@ | ||
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.QuillPlaceholderAutocomplete=e():t.QuillPlaceholderAutocomplete=e()}("undefined"!=typeof self?self:this,function(){return function(t){var e={};function n(r){if(e[r])return e[r].exports;var i=e[r]={i:r,l:!1,exports:{}};return t[r].call(i.exports,i,i.exports,n),i.l=!0,i.exports}return n.m=t,n.c=e,n.d=function(t,e,r){n.o(t,e)||Object.defineProperty(t,e,{configurable:!1,enumerable:!0,get:r})},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="",n(n.s=0)}([function(t,e,n){n(1),t.exports=n(2)},function(t,e){},function(t,e,n){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var r=function(){return function(t,e){if(Array.isArray(t))return t;if(Symbol.iterator in Object(t))return function(t,e){var n=[],r=!0,i=!1,o=void 0;try{for(var s,a=t[Symbol.iterator]();!(r=(s=a.next()).done)&&(n.push(s.value),!e||n.length!==e);r=!0);}catch(t){i=!0,o=t}finally{try{!r&&a.return&&a.return()}finally{if(i)throw o}}return n}(t,e);throw new TypeError("Invalid attempt to destructure non-iterable instance")}}(),i=function(){function t(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),o=u(n(3)),s=n(5),a=u(n(6));function u(t){return t&&t.__esModule?t:{default:t}}e.default=function(t){var e=t.import("delta");return t.register("formats/suggest",(0,a.default)(t)),function(){function n(t,e){var r=this,i=e.onClose,o=e.onOpen,a=e.getPlaceholders,u=e.container;!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,n),this.quill=t,this.onClose=i,this.onOpen=o,this.getPlaceholders=a,"string"==typeof u?this.container=this.quill.container.parentNode.querySelector(u):void 0===u?(this.container=(0,s.h)("ul",{}),this.quill.container.parentNode.appendChild(this.container)):this.container=u,this.container.classList.add("ql-autocomplete-menu","completions"),this.container.style.position="absolute",this.container.style.display="none",this.onSelectionChange=this.maybeUnfocus.bind(this),this.onTextChange=this.update.bind(this),this.open=!1,this.quill.suggestsDialogOpen=!1,this.hashIndex=null,this.focusedButton=null,this.buttons=[],this.matchedPlaceholders=[],this.placeholders=[],this.toolbarHeight=0,t.root.addEventListener("keydown",function(t){t.defaultPrevented||"#"===t.key&&(r.toolbarHeight||(r.toolbarHeight=r.quill.getModule("toolbar").container.offsetHeight),r.onHashKey(r.quill.getSelection()),t.preventDefault())},!0),t.keyboard.addBinding({key:40,collapsed:!0,format:["suggest"]},this.handleArrow.bind(this)),t.keyboard.addBinding({key:27,collapsed:null,format:["suggest"]},this.handleEscape.bind(this))}return i(n,[{key:"onHashKey",value:function(e){var n=this;if(this.open)return!0;e.length>0&&this.quill.deleteText(e.index,e.length,t.sources.USER),this.quill.insertText(e.index,"#","suggest","@@placeholder",t.sources.USER);var r=this.quill.getBounds(e.index),i=this.toolbarHeight+2;this.quill.setSelection(e.index+1,t.sources.SILENT),this.hashIndex=e.index,this.container.style.left=r.left+"px",this.container.style.top=r.top+r.height+i+"px",this.open=!0,this.quill.suggestsDialogOpen=!0,this.quill.on("text-change",this.onTextChange),this.quill.once("selection-change",this.onSelectionChange),this.quill.root.addEventListener("keydown",function(t){"Enter"===t.key&&n.handleEnterTab()},{once:!0}),this.update(),this.onOpen&&this.onOpen()}},{key:"handleArrow",value:function(){if(!this.open)return!0;this.buttons[0].focus()}},{key:"handleEnterTab",value:function(){if(!this.open)return!0;this.close(this.matchedPlaceholders[0])}},{key:"handleEscape",value:function(){if(!this.open)return!0;this.close()}},{key:"emptyCompletionsContainer",value:function(){for(;this.container.firstChild;)this.container.removeChild(this.container.firstChild)}},{key:"update",value:function(){var t=this.quill.getSelection().index,e=this.getPlaceholders(),n=e.map(function(t){return t.label.toLowerCase()}),i=(0,o.default)(n,!1),s=null;if(this.hashIndex>=t)return this.close(null);this.originalQuery=this.quill.getText(this.hashIndex+1,t-this.hashIndex-1),this.query=this.originalQuery.toLowerCase(),this.query.length&&(s=i.get(this.query)),s=s?s.map(function(t){return r(t,2)[1]}):n,this.matchedPlaceholders=e.filter(function(t){var e=t.label;return-1!==s.indexOf(e)}),this.renderCompletions(this.matchedPlaceholders)}},{key:"maybeUnfocus",value:function(){this.container.querySelector("*:focus")||this.close(null)}},{key:"renderCompletions",value:function(t){var e=this;this.emptyCompletionsContainer();var n=Array(t.length);this.buttons=n;var r=new RegExp("^(.*)("+this.query+")(.*)$");t.forEach(function(t,i){var o=t.label,a=(o.match(r)||[null,o]).slice(1).map(function(t,e){return t.length?(0,s.h)("span",{className:1===e?"matched":"unmatched"},(0===e?"#":"")+t):null}).filter(function(t){return t}),u=(0,s.h)("li",{},s.h.apply(void 0,["button",{type:"button"}].concat(function(t){if(Array.isArray(t)){for(var e=0,n=Array(t.length);e<t.length;e++)n[e]=t[e];return n}return Array.from(t)}(a))));e.container.appendChild(u),n[i]=u.firstChild,n[i].addEventListener("keydown",function(t,r){return function(i){"ArrowDown"===i.key||40===i.keyCode?(i.preventDefault(),n[Math.min(n.length-1,t+1)].focus()):"ArrowUp"===i.key||38===i.keyCode?(i.preventDefault(),n[Math.max(0,t-1)].focus()):"Enter"===i.key||13===i.keyCode||" "===i.key||32===i.keyCode||"Tab"===i.key||9===i.keyCode?(i.preventDefault(),e.close(r)):"Escape"!==i.key&&27!==i.keyCode||(i.preventDefault(),e.close())}}(i,t)),n[i].addEventListener("mousedown",function(){return e.close(t)}),n[i].addEventListener("focus",function(){return e.focusedButton=i}),n[i].addEventListener("unfocus",function(){return e.focusedButton=null})}),this.container.style.display="block"}},{key:"close",value:function(n){this.container.style.display="none",this.emptyCompletionsContainer(),this.quill.off("selection-change",this.onSelectionChange),this.quill.off("text-change",this.onTextChange);var r=(new e).retain(this.hashIndex).delete(this.query.length+1);this.quill.updateContents(r,t.sources.USER),n&&(this.quill.insertEmbed(this.hashIndex,"placeholder",n,t.sources.USER),this.quill.setSelection(this.hashIndex+1,0,t.sources.SILENT)),this.quill.focus(),this.open=!1,this.quill.suggestsDialogOpen=!1,this.onClose&&this.onClose(n||null)}}]),n}()}},function(t,e,n){t.exports=n(4)},function(t,e){!function(){var e=function(t,e,n,r){var i={};t=t||[],i.gramSizeLower=n||2,i.gramSizeUpper=r||3,i.useLevenshtein="boolean"!=typeof e||e,i.exactSet={},i.matchDict={},i.items={};var o=function(t,e){if(null===t&&null===e)throw"Trying to compare two null values";if(null===t||null===e)return 0;var n=function(t,e){for(var n,r,i=[],o=0;o<=e.length;o++)for(var s=0;s<=t.length;s++)r=o&&s?t.charAt(s-1)===e.charAt(o-1)?n:Math.min(i[s],i[s-1],n)+1:o+s,n=i[s],i[s]=r;return i.pop()}(t=String(t),e=String(e));return t.length>e.length?1-n/t.length:1-n/e.length},s=/[^a-zA-Z0-9\u00C0-\u00FF, ]+/g,a=function(t,e){for(var n={},r=function(t,e){e=e||2;var n="-"+t.toLowerCase().replace(s,"")+"-",r=e-n.length,i=[];if(r>0)for(var o=0;o<r;++o)t+="-";for(o=0;o<n.length-e+1;++o)i.push(n.slice(o,o+e));return i}(t,e=e||2),i=0;i<r.length;++i)r[i]in n?n[r[i]]+=1:n[r[i]]=1;return n};i.get=function(t,e,n){void 0===n&&(n=.33);var r=this._get(t,n);return r||void 0===e?r:e},i._get=function(t,e){var n=this._normalizeStr(t),r=this.exactSet[n];if(r)return[[1,r]];for(var i=[],o=this.gramSizeUpper;o>=this.gramSizeLower;--o)if((i=this.__get(t,o,e))&&i.length>0)return i;return null},i.__get=function(t,e,n){var r,i,s,u,l=this._normalizeStr(t),h={},c=a(l,e),f=this.items[e],p=0;for(r in c)if(i=c[r],p+=Math.pow(i,2),r in this.matchDict)for(S=0;S<this.matchDict[r].length;++S)s=this.matchDict[r][S][0],u=this.matchDict[r][S][1],s in h?h[s]+=i*u:h[s]=i*u;if(function(t){for(var e in t)if(t.hasOwnProperty(e))return!1;return!0}(h))return null;var d,y=Math.sqrt(p),g=[];for(var v in h)d=h[v],g.push([d/(y*f[v][0]),f[v][1]]);var m=function(t,e){return t[0]<e[0]?1:t[0]>e[0]?-1:0};if(g.sort(m),this.useLevenshtein){for(var b=[],x=Math.min(50,g.length),S=0;S<x;++S)b.push([o(g[S][1],l),g[S][1]]);(g=b).sort(m)}b=[];return g.forEach(function(t){t[0]>=n&&b.push([t[0],this.exactSet[t[1]]])}.bind(this)),b},i.add=function(t){if(this._normalizeStr(t)in this.exactSet)return!1;for(var e=this.gramSizeLower;e<this.gramSizeUpper+1;++e)this._add(t,e)},i._add=function(t,e){var n=this._normalizeStr(t),r=this.items[e]||[],i=r.length;r.push(0);var o,s,u=a(n,e),l=0;for(o in u)s=u[o],l+=Math.pow(s,2),o in this.matchDict?this.matchDict[o].push([i,s]):this.matchDict[o]=[[i,s]];var h=Math.sqrt(l);r[i]=[h,n],this.items[e]=r,this.exactSet[n]=t},i._normalizeStr=function(t){if("[object String]"!==Object.prototype.toString.call(t))throw"Must use a string as argument to FuzzySet functions";return t.toLowerCase()},i.length=function(){var t,e=0;for(t in this.exactSet)this.exactSet.hasOwnProperty(t)&&(e+=1);return e},i.isEmpty=function(){for(var t in this.exactSet)if(this.exactSet.hasOwnProperty(t))return!1;return!0},i.values=function(){var t,e=[];for(t in this.exactSet)this.exactSet.hasOwnProperty(t)&&e.push(this.exactSet[t]);return e};for(var u=i.gramSizeLower;u<i.gramSizeUpper+1;++u)i.items[u]=[];for(u=0;u<t.length;++u)i.add(t[u]);return i};void 0!==t&&t.exports?(t.exports=e,this.FuzzySet=e):this.FuzzySet=e}()},function(t,e,n){"use strict";Object.defineProperty(e,"__esModule",{value:!0});e.h=function(t,e){for(var n=arguments.length,r=Array(n>2?n-2:0),i=2;i<n;i++)r[i-2]=arguments[i];var o=document.createElement(t);return Object.keys(e).forEach(function(t){return o[t]=e[t]}),r.forEach(function(t){"string"==typeof t&&(t=document.createTextNode(t)),o.appendChild(t)}),o}},function(t,e,n){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var r=function(){function t(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),i=function t(e,n,r){null===e&&(e=Function.prototype);var i=Object.getOwnPropertyDescriptor(e,n);if(void 0===i){var o=Object.getPrototypeOf(e);return null===o?void 0:t(o,n,r)}if("value"in i)return i.value;var s=i.get;return void 0!==s?s.call(r):void 0};e.default=function(t){var e=t.import("blots/inline"),n=function(t){function n(){return function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,n),function(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e}(this,(n.__proto__||Object.getPrototypeOf(n)).apply(this,arguments))}return function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)}(n,e),r(n,[{key:"format",value:function(t,e){"suggest"===t&&e?this.domNode.setAttribute("data-id",e):i(n.prototype.__proto__||Object.getPrototypeOf(n.prototype),"format",this).call(this,t,e)}},{key:"formats",value:function(){var t=i(n.prototype.__proto__||Object.getPrototypeOf(n.prototype),"formats",this).call(this);return t.suggest=n.formats(this.domNode),t}}],[{key:"create",value:function(t){var e=i(n.__proto__||Object.getPrototypeOf(n),"create",this).call(this);return e.dataset.id=t,e}},{key:"formats",value:function(t){return t.dataset.id}}]),n}();return n.blotName="suggest",n.tagName="SPAN",n.className="suggest",n}}]).default}); | ||
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.QuillPlaceholderAutocomplete=e():t.QuillPlaceholderAutocomplete=e()}("undefined"!=typeof self?self:this,function(){return function(t){var e={};function n(r){if(e[r])return e[r].exports;var i=e[r]={i:r,l:!1,exports:{}};return t[r].call(i.exports,i,i.exports,n),i.l=!0,i.exports}return n.m=t,n.c=e,n.d=function(t,e,r){n.o(t,e)||Object.defineProperty(t,e,{configurable:!1,enumerable:!0,get:r})},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="",n(n.s=0)}([function(t,e,n){n(1),t.exports=n(2)},function(t,e){},function(t,e,n){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var r=function(){return function(t,e){if(Array.isArray(t))return t;if(Symbol.iterator in Object(t))return function(t,e){var n=[],r=!0,i=!1,o=void 0;try{for(var s,a=t[Symbol.iterator]();!(r=(s=a.next()).done)&&(n.push(s.value),!e||n.length!==e);r=!0);}catch(t){i=!0,o=t}finally{try{!r&&a.return&&a.return()}finally{if(i)throw o}}return n}(t,e);throw new TypeError("Invalid attempt to destructure non-iterable instance")}}(),i=function(){function t(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),o=u(n(3)),s=n(5),a=u(n(6));function u(t){return t&&t.__esModule?t:{default:t}}e.default=function(t){var e=t.import("delta");return t.register("formats/suggest",(0,a.default)(t)),function(){function n(t,e){var r=this,i=e.onClose,o=e.onOpen,a=e.getPlaceholders,u=e.fetchPlaceholders,l=e.container;!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,n),this.quill=t,this.onClose=i,this.onOpen=o,this.getPlaceholders=a,this.fetchPlaceholders=u,"string"==typeof l?this.container=this.quill.container.parentNode.querySelector(l):void 0===l?(this.container=(0,s.h)("ul",{}),this.quill.container.parentNode.appendChild(this.container)):this.container=l,this.container.classList.add("ql-autocomplete-menu","completions"),this.container.style.position="absolute",this.container.style.display="none",this.onSelectionChange=this.maybeUnfocus.bind(this),this.onTextChange=this.update.bind(this),this.open=!1,this.quill.suggestsDialogOpen=!1,this.hashIndex=null,this.focusedButton=null,this.buttons=[],this.matchedPlaceholders=[],this.toolbarHeight=0,this.suggestKeydownHandler=function(t){"Enter"===t.key&&this.handleEnterTab()}.bind(this),t.root.addEventListener("keydown",function(t){t.defaultPrevented||"#"===t.key&&(r.toolbarHeight||(r.toolbarHeight=r.quill.getModule("toolbar").container.offsetHeight),r.onHashKey(r.quill.getSelection()),t.preventDefault())},!0),t.keyboard.addBinding({key:40,collapsed:!0,format:["suggest"]},this.handleArrow.bind(this)),t.keyboard.addBinding({key:27,collapsed:null,format:["suggest"]},this.handleEscape.bind(this)),t.suggestHandler=this.onHashKey.bind(this)}return i(n,[{key:"onHashKey",value:function(e){if(this.open)return!0;e.length>0&&this.quill.deleteText(e.index,e.length,t.sources.USER),this.quill.insertText(e.index,"#","suggest","@@placeholder",t.sources.USER);var n=this.quill.getBounds(e.index),r=this.toolbarHeight+2;this.quill.setSelection(e.index+1,t.sources.SILENT),this.hashIndex=e.index,this.container.style.left=n.left+"px",this.container.style.top=n.top+n.height+r+"px",this.open=!0,this.quill.suggestsDialogOpen=!0,this.quill.on("text-change",this.onTextChange),this.quill.once("selection-change",this.onSelectionChange),this.quill.root.addEventListener("keydown",this.suggestKeydownHandler),this.update(),this.onOpen&&this.onOpen()}},{key:"handleArrow",value:function(){if(!this.open)return!0;this.buttons[0].focus()}},{key:"handleEnterTab",value:function(){if(!this.open)return!0;this.close(this.matchedPlaceholders[0])}},{key:"handleEscape",value:function(){if(!this.open)return!0;this.close()}},{key:"emptyCompletionsContainer",value:function(){for(;this.container.firstChild;)this.container.removeChild(this.container.firstChild)}},{key:"update",value:function(){var t,e=(t=regeneratorRuntime.mark(function t(){var e,n,i,s,a,u;return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:if(e=this.quill.getSelection().index,n=this.getPlaceholders(),i=n.map(function(t){return t.label.toLowerCase()}),s=(0,o.default)(i,!1),a=null,!(this.hashIndex>=e)){t.next=7;break}return t.abrupt("return",this.close(null));case 7:if(this.originalQuery=this.quill.getText(this.hashIndex+1,e-this.hashIndex-1),this.query=this.originalQuery.toLowerCase(),!this.query.length){t.next=16;break}if(!(this.fetchPlaceholders&&this.query.length>4)){t.next=15;break}return t.next=13,this.fetchPlaceholders(this.query);case 13:(u=t.sent)&&u.length&&u.forEach(function(t){-1===i.indexOf(t.label)&&(s.add(t.label),n.push(t),i.push(t.label))});case 15:a=s.get(this.query);case 16:a=a?a.map(function(t){return r(t,2)[1]}):i,this.matchedPlaceholders=n.filter(function(t){var e=t.label;return-1!==a.indexOf(e)}),this.renderCompletions(this.matchedPlaceholders);case 19:case"end":return t.stop()}},t,this)}),function(){var e=t.apply(this,arguments);return new Promise(function(t,n){return function r(i,o){try{var s=e[i](o),a=s.value}catch(t){return void n(t)}if(!s.done)return Promise.resolve(a).then(function(t){r("next",t)},function(t){r("throw",t)});t(a)}("next")})});return function(){return e.apply(this,arguments)}}()},{key:"maybeUnfocus",value:function(){this.container.querySelector("*:focus")||this.close(null)}},{key:"renderCompletions",value:function(t){var e=this;this.emptyCompletionsContainer();var n=Array(t.length);this.buttons=n;var r=new RegExp("^(.*)("+this.query+")(.*)$");t.forEach(function(t,i){var o=t.label,a=(o.match(r)||[null,o]).slice(1).map(function(t,e){return t.length?(0,s.h)("span",{className:1===e?"matched":"unmatched"},(0===e?"#":"")+t):null}).filter(function(t){return t}),u=(0,s.h)("li",{},s.h.apply(void 0,["button",{type:"button"}].concat(function(t){if(Array.isArray(t)){for(var e=0,n=Array(t.length);e<t.length;e++)n[e]=t[e];return n}return Array.from(t)}(a))));e.container.appendChild(u),n[i]=u.firstChild,n[i].addEventListener("keydown",function(t,r){return function(i){"ArrowDown"===i.key||40===i.keyCode?(i.preventDefault(),n[Math.min(n.length-1,t+1)].focus()):"ArrowUp"===i.key||38===i.keyCode?(i.preventDefault(),n[Math.max(0,t-1)].focus()):"Enter"===i.key||13===i.keyCode||" "===i.key||32===i.keyCode||"Tab"===i.key||9===i.keyCode?(i.preventDefault(),e.close(r)):"Escape"!==i.key&&27!==i.keyCode||(i.preventDefault(),e.close())}}(i,t)),n[i].addEventListener("mousedown",function(){return e.close(t)}),n[i].addEventListener("focus",function(){return e.focusedButton=i}),n[i].addEventListener("unfocus",function(){return e.focusedButton=null})}),this.container.style.display="block"}},{key:"close",value:function(n){this.container.style.display="none",this.emptyCompletionsContainer(),this.quill.off("selection-change",this.onSelectionChange),this.quill.off("text-change",this.onTextChange),this.quill.root.removeEventListener("keydown",this.suggestKeydownHandler);var r=(new e).retain(this.hashIndex).delete(this.query.length+1);this.quill.updateContents(r,t.sources.USER),n&&(this.quill.insertEmbed(this.hashIndex,"placeholder",n,t.sources.USER),this.quill.setSelection(this.hashIndex+1,0,t.sources.SILENT)),this.quill.focus(),this.open=!1,this.quill.suggestsDialogOpen=!1,this.onClose&&this.onClose(n||null)}}]),n}()}},function(t,e,n){t.exports=n(4)},function(t,e){!function(){var e=function(t,e,n,r){var i={};t=t||[],i.gramSizeLower=n||2,i.gramSizeUpper=r||3,i.useLevenshtein="boolean"!=typeof e||e,i.exactSet={},i.matchDict={},i.items={};var o=function(t,e){if(null===t&&null===e)throw"Trying to compare two null values";if(null===t||null===e)return 0;var n=function(t,e){for(var n,r,i=[],o=0;o<=e.length;o++)for(var s=0;s<=t.length;s++)r=o&&s?t.charAt(s-1)===e.charAt(o-1)?n:Math.min(i[s],i[s-1],n)+1:o+s,n=i[s],i[s]=r;return i.pop()}(t=String(t),e=String(e));return t.length>e.length?1-n/t.length:1-n/e.length},s=/[^a-zA-Z0-9\u00C0-\u00FF, ]+/g,a=function(t,e){for(var n={},r=function(t,e){e=e||2;var n="-"+t.toLowerCase().replace(s,"")+"-",r=e-n.length,i=[];if(r>0)for(var o=0;o<r;++o)t+="-";for(o=0;o<n.length-e+1;++o)i.push(n.slice(o,o+e));return i}(t,e=e||2),i=0;i<r.length;++i)r[i]in n?n[r[i]]+=1:n[r[i]]=1;return n};i.get=function(t,e,n){void 0===n&&(n=.33);var r=this._get(t,n);return r||void 0===e?r:e},i._get=function(t,e){var n=this._normalizeStr(t),r=this.exactSet[n];if(r)return[[1,r]];for(var i=[],o=this.gramSizeUpper;o>=this.gramSizeLower;--o)if((i=this.__get(t,o,e))&&i.length>0)return i;return null},i.__get=function(t,e,n){var r,i,s,u,l=this._normalizeStr(t),h={},c=a(l,e),f=this.items[e],d=0;for(r in c)if(i=c[r],d+=Math.pow(i,2),r in this.matchDict)for(w=0;w<this.matchDict[r].length;++w)s=this.matchDict[r][w][0],u=this.matchDict[r][w][1],s in h?h[s]+=i*u:h[s]=i*u;if(function(t){for(var e in t)if(t.hasOwnProperty(e))return!1;return!0}(h))return null;var p,y=Math.sqrt(d),g=[];for(var v in h)p=h[v],g.push([p/(y*f[v][0]),f[v][1]]);var m=function(t,e){return t[0]<e[0]?1:t[0]>e[0]?-1:0};if(g.sort(m),this.useLevenshtein){for(var b=[],x=Math.min(50,g.length),w=0;w<x;++w)b.push([o(g[w][1],l),g[w][1]]);(g=b).sort(m)}b=[];return g.forEach(function(t){t[0]>=n&&b.push([t[0],this.exactSet[t[1]]])}.bind(this)),b},i.add=function(t){if(this._normalizeStr(t)in this.exactSet)return!1;for(var e=this.gramSizeLower;e<this.gramSizeUpper+1;++e)this._add(t,e)},i._add=function(t,e){var n=this._normalizeStr(t),r=this.items[e]||[],i=r.length;r.push(0);var o,s,u=a(n,e),l=0;for(o in u)s=u[o],l+=Math.pow(s,2),o in this.matchDict?this.matchDict[o].push([i,s]):this.matchDict[o]=[[i,s]];var h=Math.sqrt(l);r[i]=[h,n],this.items[e]=r,this.exactSet[n]=t},i._normalizeStr=function(t){if("[object String]"!==Object.prototype.toString.call(t))throw"Must use a string as argument to FuzzySet functions";return t.toLowerCase()},i.length=function(){var t,e=0;for(t in this.exactSet)this.exactSet.hasOwnProperty(t)&&(e+=1);return e},i.isEmpty=function(){for(var t in this.exactSet)if(this.exactSet.hasOwnProperty(t))return!1;return!0},i.values=function(){var t,e=[];for(t in this.exactSet)this.exactSet.hasOwnProperty(t)&&e.push(this.exactSet[t]);return e};for(var u=i.gramSizeLower;u<i.gramSizeUpper+1;++u)i.items[u]=[];for(u=0;u<t.length;++u)i.add(t[u]);return i};void 0!==t&&t.exports?(t.exports=e,this.FuzzySet=e):this.FuzzySet=e}()},function(t,e,n){"use strict";Object.defineProperty(e,"__esModule",{value:!0});e.h=function(t,e){for(var n=arguments.length,r=Array(n>2?n-2:0),i=2;i<n;i++)r[i-2]=arguments[i];var o=document.createElement(t);return Object.keys(e).forEach(function(t){return o[t]=e[t]}),r.forEach(function(t){"string"==typeof t&&(t=document.createTextNode(t)),o.appendChild(t)}),o}},function(t,e,n){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var r=function(){function t(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),i=function t(e,n,r){null===e&&(e=Function.prototype);var i=Object.getOwnPropertyDescriptor(e,n);if(void 0===i){var o=Object.getPrototypeOf(e);return null===o?void 0:t(o,n,r)}if("value"in i)return i.value;var s=i.get;return void 0!==s?s.call(r):void 0};e.default=function(t){var e=t.import("blots/inline"),n=function(t){function n(){return function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,n),function(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e}(this,(n.__proto__||Object.getPrototypeOf(n)).apply(this,arguments))}return function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)}(n,e),r(n,[{key:"format",value:function(t,e){"suggest"===t&&e?this.domNode.setAttribute("data-id",e):i(n.prototype.__proto__||Object.getPrototypeOf(n.prototype),"format",this).call(this,t,e)}},{key:"formats",value:function(){var t=i(n.prototype.__proto__||Object.getPrototypeOf(n.prototype),"formats",this).call(this);return t.suggest=n.formats(this.domNode),t}}],[{key:"create",value:function(t){var e=i(n.__proto__||Object.getPrototypeOf(n),"create",this).call(this);return e.dataset.id=t,e}},{key:"formats",value:function(t){return t.dataset.id}}]),n}();return n.blotName="suggest",n.tagName="SPAN",n.className="suggest",n}}]).default}); |
{ | ||
"name": "quill-placeholder-autocomplete-module", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"description": "brings autocomplete to Quill placeholder module", | ||
@@ -29,5 +29,5 @@ "main": "dist/quill-placeholder-autocomplete-module.min.js", | ||
"babel-loader": "~7.1.2", | ||
"babel-preset-env": "^1.7.0", | ||
"css-loader": "^0.28.8", | ||
"eslint": "^5.1.0", | ||
"babel-preset-env": "~1.7.0", | ||
"css-loader": "~0.28.8", | ||
"eslint": "~5.1.0", | ||
"extract-text-webpack-plugin": "~3.0.2", | ||
@@ -34,0 +34,0 @@ "fuzzyset.js": "~0.0.5", |
# quill-placeholder-autocomplete | ||
brings autocomplete to [quill-placeholder-module](https://github.com/jspaine/quill-placeholder-module) | ||
## CI status | ||
[ ![Codeship Status for Datananas/quill-placeholder-autocomplete](https://app.codeship.com/projects/19a30040-d83b-0135-4786-2a3c011fd6eb/status?branch=master)](https://app.codeship.com/projects/263594) | ||
## Install | ||
@@ -38,3 +41,3 @@ + using NPM: | ||
getPlaceholders: () => placeholders // factory | ||
container: '#completions', // can also be return of `document.querySelector` or keeped to `undefined` | ||
container: '#completions', // can also be return of `document.querySelector` or keeped `undefined` | ||
onOpen: () => console.log('opened'), // optional | ||
@@ -41,0 +44,0 @@ onClose: (placeholder) => console.log('user choosed:', placeholder), //optional |
@@ -22,3 +22,3 @@ import FuzzySet from 'fuzzyset.js'; | ||
*/ | ||
constructor(quill, { onClose, onOpen, getPlaceholders, container }) { | ||
constructor(quill, { onClose, onOpen, getPlaceholders, fetchPlaceholders, container }) { | ||
this.quill = quill; | ||
@@ -28,2 +28,3 @@ this.onClose = onClose; | ||
this.getPlaceholders = getPlaceholders; | ||
this.fetchPlaceholders = fetchPlaceholders; | ||
if (typeof container === 'string') { | ||
@@ -50,5 +51,9 @@ this.container = this.quill.container.parentNode.querySelector(container); | ||
this.matchedPlaceholders = []; | ||
this.placeholders = []; | ||
this.toolbarHeight = 0; | ||
this.suggestKeydownHandler = function(event) { | ||
if (event.key === 'Enter') | ||
this.handleEnterTab(); | ||
}.bind(this); | ||
// TODO: Once Quill supports using event.key (issue #1091) use that instead of alt-3 | ||
@@ -88,3 +93,3 @@ // quill.keyboard.addBinding({ | ||
// quill.suggestHandler = this.onHashKey.bind(this); | ||
quill.suggestHandler = this.onHashKey.bind(this); | ||
} | ||
@@ -125,6 +130,3 @@ | ||
// when autocomplete UI is in default state | ||
this.quill.root.addEventListener('keydown', (event) => { | ||
if (event.key === 'Enter') | ||
this.handleEnterTab(); | ||
}, { once: true }); | ||
this.quill.root.addEventListener('keydown', this.suggestKeydownHandler); | ||
this.update(); | ||
@@ -182,3 +184,3 @@ this.onOpen && this.onOpen(); | ||
*/ | ||
update() { | ||
async update() { | ||
const sel = this.quill.getSelection().index; | ||
@@ -189,3 +191,3 @@ const placeholders = this.getPlaceholders(); | ||
let labelResults = null; | ||
// Deleted the at character | ||
// user deleted the '#' character | ||
if (this.hashIndex >= sel) { | ||
@@ -196,5 +198,21 @@ return this.close(null); | ||
this.query = this.originalQuery.toLowerCase(); | ||
if (this.query.length) | ||
if (this.query.length) { | ||
// handle promise fetching custom placeholders | ||
if (this.fetchPlaceholders && this.query.length > 4) { | ||
const results = await this.fetchPlaceholders(this.query); | ||
if(results && results.length) | ||
results.forEach((ph) => { | ||
const notExisting = labels.indexOf(ph.label) === -1; | ||
if (notExisting) { | ||
fs.add(ph.label); | ||
placeholders.push(ph); | ||
labels.push(ph.label); | ||
} | ||
}); | ||
} | ||
labelResults = fs.get(this.query); | ||
labelResults = labelResults | ||
} | ||
labelResults = labelResults | ||
? labelResults.map(([ , label ]) => label) | ||
@@ -289,2 +307,3 @@ : labels; | ||
this.quill.off('text-change', this.onTextChange); | ||
this.quill.root.removeEventListener('keydown', this.suggestKeydownHandler); | ||
const delta = new Delta() | ||
@@ -291,0 +310,0 @@ .retain(this.hashIndex) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
6859
1
50
418467
16