Skip to content

Commit 3ca20ac

Browse files
author
lizl6
committed
scrap
1 parent fe62595 commit 3ca20ac

File tree

1 file changed

+47
-42
lines changed

1 file changed

+47
-42
lines changed

src/index.js

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,15 @@ const app = {
2828
con.addEventListener('click', () => {
2929
// el.focus(() => {});
3030
el.focus();
31+
let selection = window.getSelection();
32+
if(selection.anchorOffset != 0) {
33+
return;
34+
};
3135
let range = document.createRange();
3236
range.selectNodeContents(el);
3337
range.collapse(false);
34-
let sel = window.getSelection();
35-
//判断光标位置,如不需要可删除
36-
if(sel.anchorOffset!=0){
37-
return;
38-
};
39-
sel.removeAllRanges();
40-
sel.addRange(range);
38+
selection.removeAllRanges();
39+
selection.addRange(range);
4140
});
4241

4342
// 可编辑div里的内容变化事件,这是为了重写linenum
@@ -88,44 +87,11 @@ const app = {
8887
});
8988

9089
// 在搜索关键字输入框里enter/shift+enter时触发向后找/向前找
91-
// 此时第一步是需要进行关键字着色,这里改了好多遍,注意下面的那个正则的使用,下来后要继续多深入学习下正则了-_-!
90+
// 此时第一步是需要进行关键字着色,这里改了好多遍,注意search()里的正则的使用,下来后要继续多深入学习正则-_-!
9291
txtSearch.addEventListener('keyup', (e) => {
93-
if(e.key == 'Enter' || (e.shiftKey && e.key == 'Enter')) {
94-
let kw = txtSearch.value.trim();
95-
if(this.keyword.now == kw) {
96-
return;
97-
}
98-
this.keyword.last = this.keyword.now;
99-
this.keyword.now = kw;
100-
}
10192
if(e.key == 'Enter') {
10293
// 向后找
103-
let searchText = txtSearch.value.trim();
104-
if(undefined == searchText || '' == searchText) {
105-
console.info('clear hilight');
106-
this.clearHilight();
107-
return;
108-
}
109-
let textContent = el.textContent.trim();
110-
if(undefined == textContent || 'undefined' == textContent || '' == textContent || 0 === textContent.length) {
111-
return;
112-
}
113-
this.clearHilight();
114-
let content = el.innerHTML;//, reg = new RegExp(searchText, 'gi');
115-
let reg = new RegExp('(<span class="[^"]+">)((?:(?!<\/span>).)*?)(' + searchText + ')', 'gi');
116-
let arr = content.match(reg), i = -1;
117-
if(undefined == arr || null == arr) {
118-
console.info('there is no match');
119-
this.match = 'match:0'
120-
return;
121-
}
122-
content = content.replace(reg, '$1$2<span id="result" class="hilight">$3</span>');
123-
// content = content.replace(reg, (e) => {
124-
// i++;
125-
// return '<span id="result" class="hilight">' + arr[i] + '</span>';
126-
// });
127-
el.innerHTML = '<pre>' + content + '</pre>';
128-
this.match = 'match:' + arr.length;
94+
this.search();
12995
}
13096
if(e.shiftKey && e.key == 'Enter') {
13197
// 向前找
@@ -163,6 +129,45 @@ const app = {
163129
let el = document.getElementById('ta'), content = document.getElementById('ta').innerHTML;
164130
el.innerHTML = content.replaceAll(m, this.keyword.last);
165131
this.match = '';
132+
},
133+
recordKeyword() {
134+
let kw = txtSearch.value.trim();
135+
if(this.keyword.now == kw) {
136+
return;
137+
}
138+
this.keyword.last = this.keyword.now;
139+
this.keyword.now = kw;
140+
},
141+
search() {
142+
this.recordKeyword();
143+
this.clearHilight();
144+
let el = document.getElementById('ta');
145+
let searchText = txtSearch.value.trim();
146+
if(undefined == searchText || '' == searchText) {
147+
console.info('clear hilight');
148+
this.clearHilight();
149+
return;
150+
}
151+
let textContent = el.textContent.trim();
152+
if(undefined == textContent || 'undefined' == textContent || '' == textContent || 0 === textContent.length) {
153+
return;
154+
}
155+
this.clearHilight();
156+
let content = el.innerHTML;//, reg = new RegExp(searchText, 'gi');
157+
let reg = new RegExp('(<span class="[^"]+">)((?:(?!<\/span>).)*?)(' + searchText + ')', 'gi');
158+
let arr = content.match(reg), i = -1;
159+
if(undefined == arr || null == arr) {
160+
console.info('there is no match');
161+
this.match = 'match:0'
162+
return;
163+
}
164+
content = content.replace(reg, '$1$2<span id="result" class="hilight">$3</span>');
165+
// content = content.replace(reg, (e) => {
166+
// i++;
167+
// return '<span id="result" class="hilight">' + arr[i] + '</span>';
168+
// });
169+
el.innerHTML = '<pre>' + content + '</pre>';
170+
this.match = 'match:' + arr.length;
166171
}
167172
}
168173
}

0 commit comments

Comments
 (0)