Skip to content
This repository has been archived by the owner on Dec 19, 2023. It is now read-only.

Commit

Permalink
Merge pull request #109 from yywing/master
Browse files Browse the repository at this point in the history
fix search
  • Loading branch information
zTrix authored Mar 20, 2017
2 parents 28e0ebe + 0c15bd4 commit 2e3fda8
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 21 deletions.
4 changes: 2 additions & 2 deletions server/strapdown-server.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,8 @@ func searchStr(files []string, key string, suffix string, prefix string) (search
con, _ := ioutil.ReadAll(f)
str := string(con[:])
f.Close()
if strings.Contains(str, key) {
pos := UnicodeIndex(str, key)
if strings.Contains(strings.ToLower(str), strings.ToLower(key)) {
pos := UnicodeIndex(strings.ToLower(str), strings.ToLower(key))
t := Substr(str, pos-15, 30)
searchfile := strings.TrimSuffix(files[i], suffix)
searchfile = strings.TrimPrefix(searchfile, prefix)
Expand Down
6 changes: 6 additions & 0 deletions src/strapdown.css
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,9 @@ ins.d2h-change, del.d2h-change {
height:54px;
border-bottom:1px solid #EEE;
cursor:pointer;
text-overflow:ellipsis;
overflow: hidden;
white-space: nowrap;
}
.searchlich{
font-family:PingFangSC-Regular;
Expand All @@ -861,6 +864,9 @@ ins.d2h-change, del.d2h-change {
height:54px;
border-bottom:1px solid #EEE;
cursor:pointer;
text-overflow:ellipsis;
overflow: hidden;
white-space: nowrap;
}


Expand Down
58 changes: 39 additions & 19 deletions src/strapdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ function escapeHtml(text) {
return text.replace(/[&<>"']/g, function(m) { return map[m]; });
}
function searchoff() {
var o=document.getElementById("searchul");
var o = document.getElementById("searchul");
o.innerHTML="";
var xmlhttp;
//var sendtxt;
Expand All @@ -332,11 +332,11 @@ function searchoff() {
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
contain=xmlhttp.responseText;
if (contain !="" && contain !="null"){
if (contain != "" && contain != "null"){
json=JSON.parse(contain)
for (i=0;i<json.length ;i++ ){
var obj=json[i];
var link="javascript:window.location.href='"+obj.Path+"'"
var obj = json[i];
var link = "javascript:window.location.href='"+obj.Path+"'"
li=document.createElement("li");
if (i==0){
li.className="searchlich";
Expand All @@ -363,7 +363,7 @@ function searchoff() {
document.getElementById('searchtxt').blur();
}
function mousesearch(dom){
var tar=document.getElementsByName('searchlich')[0];
var tar = document.getElementsByName('searchlich')[0];
if (tar != this ){
tar.setAttribute('class','searchli');
tar.setAttribute('name','');
Expand All @@ -378,54 +378,74 @@ function searchshow(event){
event.preventDefault();
return false;
}
if (event.keyCode==27)CloseDiv('MyDiv','fade');
if (event.keyCode==38){
var searchul=document.getElementById("searchul");
var sum=searchul.getElementsByTagName("li").length;
var res=document.getElementsByName('searchlich');
if (res.length !=0){
var index=Number(res[0].id)
if (index==0){
if (event.keyCode == 27)CloseDiv('MyDiv','fade');
if (event.keyCode == 38){
var searchul = document.getElementById("searchul");
var sum = searchul.getElementsByTagName("li").length;
var res = document.getElementsByName('searchlich');
if (res.length != 0) {
var index = Number(res[0].id)
if (index == 0) {
var next=sum-1;
document.getElementById(index.toString()).setAttribute('class','searchli');
document.getElementById(index.toString()).setAttribute('name','');
document.getElementById(next.toString()).setAttribute('class','searchlich');
document.getElementById(next.toString()).setAttribute('name','searchlich');
}else{
searchul.scrollTop=searchul.scrollHeight
} else {
var next=index-1;
document.getElementById(index.toString()).setAttribute('class','searchli');
document.getElementById(index.toString()).setAttribute('name','');
document.getElementById(next.toString()).setAttribute('class','searchlich');
document.getElementById(next.toString()).setAttribute('name','searchlich');
var lih=document.getElementById(next.toString()).offsetHeight;
var sh=next*lih
if (sh<searchul.scrollTop) {
searchul.scrollTop=next*lih;
}
if (sh>searchul.scrollTop+searchul.offsetHeight) {
searchul.scrollTop=(next+1)*lih-searchul.offsetHeight
}
}
}
event.preventDefault();

}
if (event.keyCode==40){
if (event.keyCode == 40) {
var searchul=document.getElementById("searchul");
var sum=searchul.getElementsByTagName("li").length;
var res=document.getElementsByName('searchlich');
if (res.length !=0 ){
if (res.length != 0){
var index=Number(res[0].id);
if (index==sum-1){
var next=0;
document.getElementById(index.toString()).setAttribute('class','searchli');
document.getElementById(index.toString()).setAttribute('name','');
document.getElementById(next.toString()).setAttribute('class','searchlich');
document.getElementById(next.toString()).setAttribute('name','searchlich');
}else{
searchul.scrollTop=0;

} else {
var next=index+1;
document.getElementById(index.toString()).setAttribute('class','searchli');
document.getElementById(index.toString()).setAttribute('name','');
document.getElementById(next.toString()).setAttribute('class','searchlich');
document.getElementById(next.toString()).setAttribute('name','searchlich');
var lih=document.getElementById(next.toString()).offsetHeight;
var sh=lih*(next+1);
if (sh>searchul.scrollTop+searchul.offsetHeight){
searchul.scrollTop=sh-searchul.offsetHeight;
}
if (lih*next<searchul.scrollTop) {
searchul.scrollTop=next*lih;
}
}

}
event.preventDefault();

}
if (event.keyCode==13){
if (event.keyCode == 13){
var res=document.getElementsByName('searchlich');
if (res.length != 0){
res[0].click();
Expand All @@ -436,7 +456,7 @@ function searchshow(event){
}

function enter(event){
if(event.keyCode==13){
if(event.keyCode == 13){
searchoff();
event.stopPropagation();
}
Expand Down

0 comments on commit 2e3fda8

Please sign in to comment.