@@ -57,17 +57,7 @@ class DOMFilterFactory extends BaseFilterFactory {
57
57
58
58
#document;
59
59
60
- #hcmFilter;
61
-
62
- #hcmKey;
63
-
64
- #hcmUrl;
65
-
66
- #hcmHighlightFilter;
67
-
68
- #hcmHighlightKey;
69
-
70
- #hcmHighlightUrl;
60
+ #_hcmCache;
71
61
72
62
#id = 0 ;
73
63
@@ -81,6 +71,10 @@ class DOMFilterFactory extends BaseFilterFactory {
81
71
return ( this . #_cache ||= new Map ( ) ) ;
82
72
}
83
73
74
+ get #hcmCache( ) {
75
+ return ( this . #_hcmCache ||= new Map ( ) ) ;
76
+ }
77
+
84
78
get #defs( ) {
85
79
if ( ! this . #_defs) {
86
80
const div = this . #document. createElement ( "div" ) ;
@@ -161,16 +155,28 @@ class DOMFilterFactory extends BaseFilterFactory {
161
155
162
156
addHCMFilter ( fgColor , bgColor ) {
163
157
const key = `${ fgColor } -${ bgColor } ` ;
164
- if ( this . #hcmKey === key ) {
165
- return this . #hcmUrl;
158
+ const filterName = "base" ;
159
+ let info = this . #hcmCache. get ( filterName ) ;
160
+ if ( info ?. key === key ) {
161
+ return info . url ;
166
162
}
167
163
168
- this . #hcmKey = key ;
169
- this . #hcmUrl = "none" ;
170
- this . #hcmFilter?. remove ( ) ;
164
+ if ( info ) {
165
+ info . filter ?. remove ( ) ;
166
+ info . key = key ;
167
+ info . url = "none" ;
168
+ info . filter = null ;
169
+ } else {
170
+ info = {
171
+ key,
172
+ url : "none" ,
173
+ filter : null ,
174
+ } ;
175
+ this . #hcmCache. set ( filterName , info ) ;
176
+ }
171
177
172
178
if ( ! fgColor || ! bgColor ) {
173
- return this . #hcmUrl ;
179
+ return info . url ;
174
180
}
175
181
176
182
const fgRGB = this . #getRGB( fgColor ) ;
@@ -183,7 +189,7 @@ class DOMFilterFactory extends BaseFilterFactory {
183
189
( fgColor === "#000000" && bgColor === "#ffffff" ) ||
184
190
fgColor === bgColor
185
191
) {
186
- return this . #hcmUrl ;
192
+ return info . url ;
187
193
}
188
194
189
195
// https://developer.mozilla.org/en-US/docs/Web/Accessibility/Understanding_Colors_and_Luminance
@@ -203,7 +209,7 @@ class DOMFilterFactory extends BaseFilterFactory {
203
209
const table = map . join ( "," ) ;
204
210
205
211
const id = `g_${ this . #docId} _hcm_filter` ;
206
- const filter = ( this . #hcmHighlightFilter = this . #createFilter( id ) ) ;
212
+ const filter = ( info . filter = this . #createFilter( id ) ) ;
207
213
this . #addTransferMapConversion( table , table , table , filter ) ;
208
214
this . #addGrayConversion( filter ) ;
209
215
@@ -223,22 +229,33 @@ class DOMFilterFactory extends BaseFilterFactory {
223
229
filter
224
230
) ;
225
231
226
- this . #hcmUrl = `url(#${ id } )` ;
227
- return this . #hcmUrl ;
232
+ info . url = `url(#${ id } )` ;
233
+ return info . url ;
228
234
}
229
235
230
- addHighlightHCMFilter ( fgColor , bgColor , newFgColor , newBgColor ) {
236
+ addHighlightHCMFilter ( filterName , fgColor , bgColor , newFgColor , newBgColor ) {
231
237
const key = `${ fgColor } -${ bgColor } -${ newFgColor } -${ newBgColor } ` ;
232
- if ( this . #hcmHighlightKey === key ) {
233
- return this . #hcmHighlightUrl;
238
+ let info = this . #hcmCache. get ( filterName ) ;
239
+ if ( info ?. key === key ) {
240
+ return info . url ;
234
241
}
235
242
236
- this . #hcmHighlightKey = key ;
237
- this . #hcmHighlightUrl = "none" ;
238
- this . #hcmHighlightFilter?. remove ( ) ;
243
+ if ( info ) {
244
+ info . filter ?. remove ( ) ;
245
+ info . key = key ;
246
+ info . url = "none" ;
247
+ info . filter = null ;
248
+ } else {
249
+ info = {
250
+ key,
251
+ url : "none" ,
252
+ filter : null ,
253
+ } ;
254
+ this . #hcmCache. set ( filterName , info ) ;
255
+ }
239
256
240
257
if ( ! fgColor || ! bgColor ) {
241
- return this . #hcmHighlightUrl ;
258
+ return info . url ;
242
259
}
243
260
244
261
const [ fgRGB , bgRGB ] = [ fgColor , bgColor ] . map ( this . #getRGB. bind ( this ) ) ;
@@ -294,8 +311,8 @@ class DOMFilterFactory extends BaseFilterFactory {
294
311
return arr . join ( "," ) ;
295
312
} ;
296
313
297
- const id = `g_${ this . #docId} _hcm_highlight_filter ` ;
298
- const filter = ( this . #hcmHighlightFilter = this . #createFilter( id ) ) ;
314
+ const id = `g_${ this . #docId} _hcm_ ${ filterName } _filter ` ;
315
+ const filter = ( info . filter = this . #createFilter( id ) ) ;
299
316
300
317
this . #addGrayConversion( filter ) ;
301
318
this . #addTransferMapConversion(
@@ -305,12 +322,12 @@ class DOMFilterFactory extends BaseFilterFactory {
305
322
filter
306
323
) ;
307
324
308
- this . #hcmHighlightUrl = `url(#${ id } )` ;
309
- return this . #hcmHighlightUrl ;
325
+ info . url = `url(#${ id } )` ;
326
+ return info . url ;
310
327
}
311
328
312
329
destroy ( keepHCM = false ) {
313
- if ( keepHCM && ( this . #hcmUrl || this . #hcmHighlightUrl ) ) {
330
+ if ( keepHCM && this . #hcmCache . size !== 0 ) {
314
331
return ;
315
332
}
316
333
if ( this . #_defs) {
0 commit comments