1
- "use strict" ;
2
- var __importDefault = ( this && this . __importDefault ) || function ( mod ) {
3
- return ( mod && mod . __esModule ) ? mod : { "default" : mod } ;
4
- } ;
5
- var _a ;
6
- Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
7
- exports . LiveTranslatorPlugin = void 0 ;
8
- const throttle_1 = __importDefault ( require ( "lodash/throttle" ) ) ;
1
+ import throttle from 'lodash/throttle' ;
9
2
const css = `
10
3
.live-translator-enable-button {
11
4
position: fixed !important;
@@ -29,6 +22,11 @@ const css = `
29
22
border-radius: 100%;
30
23
background-color: red;
31
24
}
25
+ .live-translator-badge-wrapper {
26
+ position: relative !important;
27
+ width: 0px;
28
+ height: 0px;
29
+ }
32
30
.live-translator-badge-container {
33
31
position: absolute !important;
34
32
display: flex;
@@ -60,6 +58,12 @@ const css = `
60
58
}
61
59
` ;
62
60
class ZeroWidthEncoder {
61
+ static START = '\u200B' ;
62
+ static ZERO = '\u200C' ;
63
+ static ONE = '\u200D' ;
64
+ static SPACE = '\u200E' ;
65
+ static END = '\u200F' ;
66
+ static PATTERN = `${ this . START } [${ this . ZERO } ${ this . ONE } ${ this . SPACE } ]+${ this . END } ` ;
63
67
static encode ( text ) {
64
68
const binary = text
65
69
. split ( '' )
@@ -101,14 +105,11 @@ class ZeroWidthEncoder {
101
105
return text ;
102
106
}
103
107
}
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 } ` ;
111
108
class LiveTranslatorManager {
109
+ _enabled ;
110
+ _options ;
111
+ _enableButton ;
112
+ _indicator ;
112
113
constructor ( options ) {
113
114
this . _enabled = false ;
114
115
this . _options = options ;
@@ -142,18 +143,24 @@ class LiveTranslatorManager {
142
143
const self = this ;
143
144
this . _options . i18n . formatter = {
144
145
interpolate ( message , values , path ) {
145
- const meta = ZeroWidthEncoder . encode ( JSON . stringify ( {
146
- message,
147
- values,
148
- path,
149
- locale : self . _options . i18n . locale ,
150
- } ) ) ;
151
146
const original = originalFormatter . interpolate ( message , values , path ) ;
152
- return ( original && self . _enabled ) ? [ meta , ...original ] : original ;
147
+ let meta = '' ;
148
+ try {
149
+ meta = ZeroWidthEncoder . encode ( JSON . stringify ( {
150
+ message,
151
+ values,
152
+ path,
153
+ locale : self . _options . i18n . locale ,
154
+ } ) ) ;
155
+ }
156
+ catch ( exception ) {
157
+ console . warn ( path , exception ) ;
158
+ }
159
+ return ( original && meta && self . _enabled ) ? [ meta , ...original ] : original ;
153
160
} ,
154
161
} ;
155
162
// initialize decode & render
156
- const throttler = ( 0 , throttle_1 . default ) ( ( ) => this . render ( ) , 800 ) ;
163
+ const throttler = throttle ( ( ) => this . render ( ) , 800 ) ;
157
164
const observer = new MutationObserver ( throttler ) ;
158
165
observer . observe ( document . documentElement , {
159
166
subtree : true ,
@@ -184,9 +191,9 @@ class LiveTranslatorManager {
184
191
console . log ( `%c Live Translator ${ this . _enabled ? 'ON' : 'OFF' } ` , 'background: #222; color: #bada55' ) ;
185
192
}
186
193
render ( ) {
187
- const badges = document . querySelectorAll ( '.live-translator-badge' ) ;
188
- badges . forEach ( ( badge ) => {
189
- badge . remove ( ) ;
194
+ const badgeWrappers = document . querySelectorAll ( '.live-translator-badge-wrapper ' ) ;
195
+ badgeWrappers . forEach ( ( wrapper ) => {
196
+ wrapper . remove ( ) ;
190
197
} ) ;
191
198
this . _indicator . style . background = this . _enabled ? 'lightgreen' : 'red' ;
192
199
if ( ! this . _enabled ) {
@@ -200,7 +207,7 @@ class LiveTranslatorManager {
200
207
const parent = node . parentElement ;
201
208
if ( node instanceof Text ) {
202
209
const matches = node . textContent . match ( re ) ;
203
- for ( const match of matches !== null && matches !== void 0 ? matches : [ ] ) {
210
+ for ( const match of matches ?? [ ] ) {
204
211
const meta = JSON . parse ( ZeroWidthEncoder . decode ( match ) ) ;
205
212
badges . push ( createBadge ( meta , this . _options ) ) ;
206
213
}
@@ -222,7 +229,10 @@ class LiveTranslatorManager {
222
229
else {
223
230
container = document . createElement ( 'span' ) ;
224
231
container . classList . add ( 'live-translator-badge-container' ) ;
225
- parent . insertBefore ( container , node ) ;
232
+ const relativeWrapper = document . createElement ( 'span' ) ;
233
+ relativeWrapper . classList . add ( 'live-translator-badge-wrapper' ) ;
234
+ relativeWrapper . appendChild ( container ) ;
235
+ parent . insertBefore ( relativeWrapper , node ) ;
226
236
}
227
237
for ( const badge of badges ) {
228
238
container . appendChild ( badge ) ;
@@ -255,7 +265,7 @@ const createBadge = (meta, options, attribute) => {
255
265
} ) ;
256
266
return badge ;
257
267
} ;
258
- exports . LiveTranslatorPlugin = {
268
+ export const LiveTranslatorPlugin = {
259
269
install ( app , options ) {
260
270
console . log ( 'LiveTranslator is installed' ) ;
261
271
new LiveTranslatorManager ( options ) ;
0 commit comments