@@ -28,16 +28,15 @@ const app = {
28
28
con . addEventListener ( 'click' , ( ) => {
29
29
// el.focus(() => {});
30
30
el . focus ( ) ;
31
+ let selection = window . getSelection ( ) ;
32
+ if ( selection . anchorOffset != 0 ) {
33
+ return ;
34
+ } ;
31
35
let range = document . createRange ( ) ;
32
36
range . selectNodeContents ( el ) ;
33
37
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 ) ;
41
40
} ) ;
42
41
43
42
// 可编辑div里的内容变化事件,这是为了重写linenum
@@ -88,44 +87,11 @@ const app = {
88
87
} ) ;
89
88
90
89
// 在搜索关键字输入框里enter/shift+enter时触发向后找/向前找
91
- // 此时第一步是需要进行关键字着色,这里改了好多遍,注意下面的那个正则的使用,下来后要继续多深入学习下正则了 -_-!
90
+ // 此时第一步是需要进行关键字着色,这里改了好多遍,注意search()里的正则的使用,下来后要继续多深入学习正则 -_-!
92
91
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
- }
101
92
if ( e . key == 'Enter' ) {
102
93
// 向后找
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 ( ) ;
129
95
}
130
96
if ( e . shiftKey && e . key == 'Enter' ) {
131
97
// 向前找
@@ -163,6 +129,45 @@ const app = {
163
129
let el = document . getElementById ( 'ta' ) , content = document . getElementById ( 'ta' ) . innerHTML ;
164
130
el . innerHTML = content . replaceAll ( m , this . keyword . last ) ;
165
131
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 ;
166
171
}
167
172
}
168
173
}
0 commit comments