zx-editor
Version:
ZxEditor is a HTML5 rich text editor
1 lines • 3.04 kB
JavaScript
require(["gitbook","jquery"],function(r,s){var i,l,u,c,h,p=500,e=r.state,o={},n=void 0!==history.pushState,t=s("body");function d(e){if(null!=e&&""!==e.trim()){var t,n,a=[];for(n in o)-1!==(t=o[n].body.toLowerCase().indexOf(e.toLowerCase()))&&a.push({url:n,title:o[n].title,body:o[n].body.substr(Math.max(0,t-50),p).replace(new RegExp("("+String(e).replace(/([\*\.\?\+\$\^\[\]\(\)\{\}\|\/\\])/g,"\\$1")+")","gi"),'<span class="search-highlight-keyword">$1</span>')});!function(o){i=s("#book-search-results"),l=i.find(".search-results-list"),u=i.find(".search-results-title"),c=u.find(".search-results-count"),h=u.find(".search-query"),i.addClass("open");var e=0==o.count;i.toggleClass("no-results",e),l.empty(),c.text(o.count),h.text(o.query),o.results.forEach(function(e){var t=s("<li>",{class:"search-results-item"}),n=s("<h3>"),a=s("<a>",{href:r.state.basePath+"/"+e.url+"?h="+encodeURIComponent(o.query),text:e.title,"data-is-search":1});a[0].href.split("?")[0]===location.href.split("?")[0]&&a[0].setAttribute("data-need-reload",1);e=e.body.trim();e.length>p&&(e+="...");e=s("<p>").html(e);a.appendTo(n),n.appendTo(t),e.appendTo(t),t.appendTo(l)}),s(".body-inner").scrollTop(0)}({count:a.length,query:e,results:a})}}function a(e){var n,a,o;t.addClass("with-search"),t.addClass("search-loading"),n=function(){d(e),t.removeClass("search-loading")},function(){var e=this,t=arguments;o=o||setTimeout(function(){o=null,n.apply(e,t)},a)}()}function f(){t.removeClass("with-search"),s("#book-search-results").removeClass("open")}function g(){var e=s("body");e.on("keyup","#book-search-input input",function(e){var t;13===e.keyCode&&n&&(t=m("q",s(this).val()),history.pushState({path:t},null,t)),0==(t=s("#book-search-input input").val()).length?f():a(t)}),e.on("blur","#book-search-input input",function(e){var t;n&&(t=m("q",s(this).val()),history.pushState({path:t},null,t))})}r.events.on("start",function(){g(),s.getJSON(e.basePath+"/search_plus_index.json").then(function(e){o=e,b(),f()})});var v=function(e){s(".page-inner").mark(e,{ignoreJoiners:!0,acrossElements:!0,separateWordSearch:!1}),setTimeout(function(){var e=s('mark[data-markjs="true"]');e.length&&e[0].scrollIntoView()},100)};function b(){var e,t;/\b(q|h)=([^&]+)/.test(location.search)&&(t=RegExp.$1,e=decodeURIComponent(RegExp.$2),("q"===t?a:v)(e),s("#book-search-input input").val(e))}function m(e,t){t=encodeURIComponent(t);var n=window.location.href.replace(/([?&])(?:q|h)=([^&]+)(&|$)/,function(e,t,n,a){return"&"===a?t:""}),a=new RegExp("([?&])"+e+"=.*?(&|#|$)(.*)","gi");if(a.test(n))return null!=t?n.replace(a,"$1"+e+"="+t+"$2$3"):(n=(o=n.split("#"))[0].replace(a,"$1$3").replace(/(&|\?)$/,""),void 0!==o[1]&&null!==o[1]&&(n+="#"+o[1]),n);if(null==t)return n;var o,a=-1!==n.indexOf("?")?"&":"?",n=(o=n.split("#"))[0]+a+e+"="+t;return void 0!==o[1]&&null!==o[1]&&(n+="#"+o[1]),n}r.events.on("page.change",b),window.addEventListener("click",function(e){"A"===e.target.tagName&&e.target.getAttribute("data-need-reload")&&setTimeout(function(){location.reload()},100)},!0)});