From d218fdbfe5e7b118e6fda75700e71d0c4c813f0e Mon Sep 17 00:00:00 2001 From: Sean <1026025056@qq.com> Date: Sat, 9 May 2020 16:20:57 +0800 Subject: [PATCH] optimization search function --- docs/assets/js/custom.js | 36 ++++++++++++++++++++++++++---------- docs/assets/js/custom.min.js | 2 +- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/docs/assets/js/custom.js b/docs/assets/js/custom.js index 2ad57c52..079c86c8 100644 --- a/docs/assets/js/custom.js +++ b/docs/assets/js/custom.js @@ -140,24 +140,40 @@ function product_search(onEnter_content){ $(".item").parent().css("display","inline-block"); $(".search_point")[0].textContent = ""; var p = $(".item .item-title"); - var sku = $(".item .mask_sku"); + var sku_list = $(".item .mask_sku"); var textEnter = onEnter_content.toLocaleLowerCase(); for (var i=0; i${textEnter.toUpperCase()}${kw_part2}` - }else{ - var kw = ""; + kw_content = p[i].dataset.kw; + var kw = ""; + var sku = sku = `--SKU:${sku_list[i].textContent}`; + if(kw_content.toLocaleLowerCase().indexOf(textEnter) != -1){ + var kw_index = kw_content.toLocaleLowerCase().indexOf(textEnter); + var kw_index_start = (function(){ + for(var index = kw_index; index>=0; index--){ + if((kw_content[index]==" ") || (index == 0)){ + return index; + } + } + })(); + var kw_index_end = (function(){ + for(var index = kw_index; index<=kw_content.length; index++){ + if((kw_content[index]==" ") || (index == kw_content.length)){ + return index; + } + } + })(); + var kw_part1 = kw_content.substring(0,kw_index_start); + var kw_part2 = kw_content.substring(kw_index_end,); + var kw_part3 = kw_content.substring(kw_index_start,kw_index_end); + kw = `--${kw_part3} ${kw_part1}${kw_part2}` } if(textEnter != ""){ - $(".search_point").append(`${p[i].textContent+kw}`); + $(".search_point").append(`${p[i].textContent+kw+sku}`); }else{ $(".item").parent().css("display","block"); } diff --git a/docs/assets/js/custom.min.js b/docs/assets/js/custom.min.js index 849495d7..9decbf86 100644 --- a/docs/assets/js/custom.min.js +++ b/docs/assets/js/custom.min.js @@ -1 +1 @@ -const description_icon='',purchase_icon='',example_icon='',video_icon='',quickstart_icon='',schematic_icon='',icon_list={EASYLOADER:"https://m5stack.oss-cn-shenzhen.aliyuncs.com/image/m5-docs_logo/EASYLOADER_ICON.png",CORE:"https://m5stack.oss-cn-shenzhen.aliyuncs.com/image/m5-docs_logo/CORE_ICON.png",MODULE:"https://m5stack.oss-cn-shenzhen.aliyuncs.com/image/m5-docs_logo/MODULE_ICON.png",BASE:"https://m5stack.oss-cn-shenzhen.aliyuncs.com/image/m5-docs_logo/BASE_ICON.png",UNIT:"https://m5stack.oss-cn-shenzhen.aliyuncs.com/image/m5-docs_logo/UNIT_ICON.png",HAT:"https://m5stack.oss-cn-shenzhen.aliyuncs.com/image/m5-docs_logo/HAT_ICON.png",ATOM:"https://m5stack.oss-cn-shenzhen.aliyuncs.com/image/m5-docs_logo/ATOM_BASE_ICON.png",APPLICATION:"https://m5stack.oss-cn-shenzhen.aliyuncs.com/image/m5-docs_logo/APPLICATION_ICON.png",ACCESSORY:"https://m5stack.oss-cn-shenzhen.aliyuncs.com/image/m5-docs_logo/ACCESSORY_ICON.png",ALUMINIUM:"https://m5stack.oss-cn-shenzhen.aliyuncs.com/image/m5-docs_logo/ALUMINIUM_ICON.png"};var Label1="",Label2='here is the anchor_name';function anchor_create(e,t){var c=window.location.href,n=Label1.replace(/here is the anchor_id/,t),o=Label2.replace(/here is the anchor_name/,e.toUpperCase());"DESCRIPTION"==e||"描述"==e?$(".anchor-box").append(n+description_icon+o):"EXAMPLE"==e||"案例程序"==e?$(".anchor-box").append(n+example_icon+o):"SCHEMATIC"==e||"原理图"==e?$(".anchor-box").append(n+schematic_icon+o):"VIDEO"==e||"相关视频"==e?$(".anchor-box").append(n+video_icon+o):icon_list.hasOwnProperty(e)?($(".anchor-box").append(n+""+o),"EASYLOADER"==e&&("#example_video".length>0&&$("#play-btn").on("click",function(e){e.stopPropagation(),$(".easyloader-mask").toggleClass("video_play"),$("#example_video")[0].play()}),$("#example_video").on("click",function(e){e.preventDefault(),$(".easyloader-mask").toggleClass("video_play"),$("#example_video")[0].pause()}))):-1==c.indexOf(/uiflow/)&&-1==c.indexOf(/arduino/)&&-1==c.indexOf(/quick_start/)&&-1==c.indexOf(/related_documents/)||$(".anchor-box").append(n+quickstart_icon+'"+e+"")}function anchor_search(e="none",t="none"){var c=window.location.href;if("/en/"==c.slice(-4)||"/zh_CN/"==c.slice(-7)||"/#/"==c.slice(-3)){$("#search_note").eq(0).after("
");var n=$(".product_page > div:gt(1)")}else{$("h2").eq(0).before("
");n=$("#main h2")}for(var o=0;o'+purchase_icon+''+a+""),"none"!=t&&$(".anchor-box").append("'+quickstart_icon+''+s+"")}function anchor_scroll(e,t,c){var n=window.location.href,o=$("#"+t).offset().top+$("#"+t).height()-$(window).scrollTop();if(o>100&&o<$("#"+t).height()+700)for(var a=0;a div:gt(1)");else c=$("#main h2");for(var n=0;n${n.toUpperCase()}${i}`;else l="";""!=n?$(".search_point").append(`${t[o].textContent+l}`):$(".item").parent().css("display","block")}else $(".item").eq(o).css("display","none"),$(".item").parent().children("p").css("display","none"),$(".item").eq(o).parent().next("hr").css("display","none")}function faq_search(e){$(".search-tips").css("display","block"),$(".faq-class").css("display","none");for(var t=$(".faq-item h5"),c=0;c0;)l.addImage(i,"JPEG",0,a,595.28,s),a-=841.89,(o-=n)>0&&l.addPage();var r=window.location.href,h=r.indexOf("#"),p=r.slice(h+2);l.save(p+".pdf")}}),$(".anchor-box").removeAttr("style"),$(".related_links").removeAttr("style"),$(".easyloader-box").removeAttr("style")}Input.focusin(function(){Input.animate({width:300}),$(".search_point").show(240)}),Input.focusout(function(){Input.animate({width:240}),setTimeout(function(){$(".search_point").hide(140)},160)}),$(document).ready(function(){$(".btn-toggle").on("click",function(){$(".btn-toggle").toggleClass("open"),$(".d-flex").toggleClass("open")})}),window.onhashchange=page_loading,page_loading(); \ No newline at end of file +const description_icon='',purchase_icon='',example_icon='',video_icon='',quickstart_icon='',schematic_icon='',icon_list={EASYLOADER:"https://m5stack.oss-cn-shenzhen.aliyuncs.com/image/m5-docs_logo/EASYLOADER_ICON.png",CORE:"https://m5stack.oss-cn-shenzhen.aliyuncs.com/image/m5-docs_logo/CORE_ICON.png",MODULE:"https://m5stack.oss-cn-shenzhen.aliyuncs.com/image/m5-docs_logo/MODULE_ICON.png",BASE:"https://m5stack.oss-cn-shenzhen.aliyuncs.com/image/m5-docs_logo/BASE_ICON.png",UNIT:"https://m5stack.oss-cn-shenzhen.aliyuncs.com/image/m5-docs_logo/UNIT_ICON.png",HAT:"https://m5stack.oss-cn-shenzhen.aliyuncs.com/image/m5-docs_logo/HAT_ICON.png",ATOM:"https://m5stack.oss-cn-shenzhen.aliyuncs.com/image/m5-docs_logo/ATOM_BASE_ICON.png",APPLICATION:"https://m5stack.oss-cn-shenzhen.aliyuncs.com/image/m5-docs_logo/APPLICATION_ICON.png",ACCESSORY:"https://m5stack.oss-cn-shenzhen.aliyuncs.com/image/m5-docs_logo/ACCESSORY_ICON.png",ALUMINIUM:"https://m5stack.oss-cn-shenzhen.aliyuncs.com/image/m5-docs_logo/ALUMINIUM_ICON.png"};var Label1="",Label2='here is the anchor_name';function anchor_create(e,t){var c=window.location.href,n=Label1.replace(/here is the anchor_id/,t),o=Label2.replace(/here is the anchor_name/,e.toUpperCase());"DESCRIPTION"==e||"描述"==e?$(".anchor-box").append(n+description_icon+o):"EXAMPLE"==e||"案例程序"==e?$(".anchor-box").append(n+example_icon+o):"SCHEMATIC"==e||"原理图"==e?$(".anchor-box").append(n+schematic_icon+o):"VIDEO"==e||"相关视频"==e?$(".anchor-box").append(n+video_icon+o):icon_list.hasOwnProperty(e)?($(".anchor-box").append(n+""+o),"EASYLOADER"==e&&("#example_video".length>0&&$("#play-btn").on("click",function(e){e.stopPropagation(),$(".easyloader-mask").toggleClass("video_play"),$("#example_video")[0].play()}),$("#example_video").on("click",function(e){e.preventDefault(),$(".easyloader-mask").toggleClass("video_play"),$("#example_video")[0].pause()}))):-1==c.indexOf(/uiflow/)&&-1==c.indexOf(/arduino/)&&-1==c.indexOf(/quick_start/)&&-1==c.indexOf(/related_documents/)||$(".anchor-box").append(n+quickstart_icon+'"+e+"")}function anchor_search(e="none",t="none"){var c=window.location.href;if("/en/"==c.slice(-4)||"/zh_CN/"==c.slice(-7)||"/#/"==c.slice(-3)){$("#search_note").eq(0).after("
");var n=$(".product_page > div:gt(1)")}else{$("h2").eq(0).before("
");n=$("#main h2")}for(var o=0;o'+purchase_icon+''+a+""),"none"!=t&&$(".anchor-box").append("'+quickstart_icon+''+s+"")}function anchor_scroll(e,t,c){var n=window.location.href,o=$("#"+t).offset().top+$("#"+t).height()-$(window).scrollTop();if(o>100&&o<$("#"+t).height()+700)for(var a=0;a div:gt(1)");else c=$("#main h2");for(var n=0;n${c[o].textContent}`;if(-1!=kw_content.toLocaleLowerCase().indexOf(n)){var i=kw_content.toLocaleLowerCase().indexOf(n),l=function(){for(var e=i;e>=0;e--)if(" "==kw_content[e]||0==e)return e}(),r=function(){for(var e=i;e<=kw_content.length;e++)if(" "==kw_content[e]||e==kw_content.length)return e}(),h=kw_content.substring(0,l),p=kw_content.substring(r);a=`--${kw_content.substring(l,r)} ${h}${p}`}""!=n?$(".search_point").append(`${t[o].textContent+a+s}`):$(".item").parent().css("display","block")}else $(".item").eq(o).css("display","none"),$(".item").parent().children("p").css("display","none"),$(".item").eq(o).parent().next("hr").css("display","none")}function faq_search(e){$(".search-tips").css("display","block"),$(".faq-class").css("display","none");for(var t=$(".faq-item h5"),c=0;c0;)l.addImage(i,"JPEG",0,a,595.28,s),a-=841.89,(o-=n)>0&&l.addPage();var r=window.location.href,h=r.indexOf("#"),p=r.slice(h+2);l.save(p+".pdf")}}),$(".anchor-box").removeAttr("style"),$(".related_links").removeAttr("style"),$(".easyloader-box").removeAttr("style")}Input.focusin(function(){Input.animate({width:300}),$(".search_point").show(240)}),Input.focusout(function(){Input.animate({width:240}),setTimeout(function(){$(".search_point").hide(140)},160)}),$(document).ready(function(){$(".btn-toggle").on("click",function(){$(".btn-toggle").toggleClass("open"),$(".d-flex").toggleClass("open")})}),window.onhashchange=page_loading,page_loading(); \ No newline at end of file