2
2
var __importDefault = ( this && this . __importDefault ) || function ( mod ) {
3
3
return ( mod && mod . __esModule ) ? mod : { "default" : mod } ;
4
4
} ;
5
+ var _a ;
5
6
Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
6
7
exports . LiveTranslatorPlugin = void 0 ;
7
8
const throttle_1 = __importDefault ( require ( "lodash/throttle" ) ) ;
@@ -59,14 +60,7 @@ const css = `
59
60
}
60
61
` ;
61
62
class ZeroWidthEncoder {
62
- constructor ( ) {
63
- this . START = '\u200B' ;
64
- this . ZERO = '\u200C' ;
65
- this . ONE = '\u200D' ;
66
- this . SPACE = '\u200E' ;
67
- this . END = '\u200F' ;
68
- }
69
- encode ( text ) {
63
+ static encode ( text ) {
70
64
const binary = text
71
65
. split ( '' )
72
66
. map ( ( char ) => char . charCodeAt ( 0 ) . toString ( 2 ) )
@@ -86,7 +80,7 @@ class ZeroWidthEncoder {
86
80
. join ( '' ) ;
87
81
return this . START + zeroWidth + this . END ;
88
82
}
89
- decode ( zeroWidth ) {
83
+ static decode ( zeroWidth ) {
90
84
const binary = zeroWidth
91
85
. split ( '' )
92
86
. slice ( 1 , zeroWidth . length - 1 ) // remove START and END
@@ -107,11 +101,17 @@ class ZeroWidthEncoder {
107
101
return text ;
108
102
}
109
103
}
104
+ _a = ZeroWidthEncoder ;
105
+ ZeroWidthEncoder . START = '\u200B' ;
106
+ ZeroWidthEncoder . ZERO = '\u200C' ;
107
+ ZeroWidthEncoder . ONE = '\u200D' ;
108
+ ZeroWidthEncoder . SPACE = '\u200E' ;
109
+ ZeroWidthEncoder . END = '\u200F' ;
110
+ ZeroWidthEncoder . PATTERN = `${ _a . START } [${ _a . ZERO } ${ _a . ONE } ${ _a . SPACE } ]+${ _a . END } ` ;
110
111
class LiveTranslatorManager {
111
112
constructor ( options ) {
112
113
this . _enabled = false ;
113
114
this . _options = options ;
114
- this . _zwEncoder = new ZeroWidthEncoder ( ) ;
115
115
// handle persistance
116
116
const savedRaw = localStorage . getItem ( 'live-translator-enabled' ) ;
117
117
if ( this . _options . persist && savedRaw ) {
@@ -190,7 +190,7 @@ class LiveTranslatorManager {
190
190
if ( ! this . _enabled ) {
191
191
return ;
192
192
}
193
- const re = new RegExp ( ` ${ this . _zwEncoder . START } [ ${ this . _zwEncoder . ZERO } ${ this . _zwEncoder . ONE } ${ this . _zwEncoder . SPACE } ]+ ${ this . _zwEncoder . END } ` , 'gm' ) ;
193
+ const re = new RegExp ( ZeroWidthEncoder . PATTERN , 'gm' ) ;
194
194
const queue = [ document . documentElement ] ;
195
195
while ( queue . length > 0 ) {
196
196
const node = queue . pop ( ) ;
@@ -199,7 +199,7 @@ class LiveTranslatorManager {
199
199
if ( node instanceof Text ) {
200
200
const matches = node . textContent . match ( re ) ;
201
201
for ( const match of matches !== null && matches !== void 0 ? matches : [ ] ) {
202
- const meta = JSON . parse ( this . _zwEncoder . decode ( match ) ) ;
202
+ const meta = JSON . parse ( ZeroWidthEncoder . decode ( match ) ) ;
203
203
badges . push ( createBadge ( meta , this . _options ) ) ;
204
204
}
205
205
}
@@ -208,7 +208,7 @@ class LiveTranslatorManager {
208
208
. filter ( ( { match } ) => ! ! match ) ;
209
209
for ( const { attribute, match } of attributes ) {
210
210
for ( const m of match ) {
211
- const meta = JSON . parse ( this . _zwEncoder . decode ( m ) ) ;
211
+ const meta = JSON . parse ( ZeroWidthEncoder . decode ( m ) ) ;
212
212
badges . push ( createBadge ( meta , this . _options , attribute . name ) ) ;
213
213
}
214
214
}
0 commit comments