|
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;
|
@@ -65,6 +58,12 @@ const css = `
|
65 | 58 | }
|
66 | 59 | `;
|
67 | 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}`; |
68 | 67 | static encode(text) {
|
69 | 68 | const binary = text
|
70 | 69 | .split('')
|
@@ -106,14 +105,11 @@ class ZeroWidthEncoder {
|
106 | 105 | return text;
|
107 | 106 | }
|
108 | 107 | }
|
109 |
| -_a = ZeroWidthEncoder; |
110 |
| -ZeroWidthEncoder.START = '\u200B'; |
111 |
| -ZeroWidthEncoder.ZERO = '\u200C'; |
112 |
| -ZeroWidthEncoder.ONE = '\u200D'; |
113 |
| -ZeroWidthEncoder.SPACE = '\u200E'; |
114 |
| -ZeroWidthEncoder.END = '\u200F'; |
115 |
| -ZeroWidthEncoder.PATTERN = `${_a.START}[${_a.ZERO}${_a.ONE}${_a.SPACE}]+${_a.END}`; |
116 | 108 | class LiveTranslatorManager {
|
| 109 | + _enabled; |
| 110 | + _options; |
| 111 | + _enableButton; |
| 112 | + _indicator; |
117 | 113 | constructor(options) {
|
118 | 114 | this._enabled = false;
|
119 | 115 | this._options = options;
|
@@ -147,18 +143,24 @@ class LiveTranslatorManager {
|
147 | 143 | const self = this;
|
148 | 144 | this._options.i18n.formatter = {
|
149 | 145 | interpolate(message, values, path) {
|
150 |
| - const meta = ZeroWidthEncoder.encode(JSON.stringify({ |
151 |
| - message, |
152 |
| - values, |
153 |
| - path, |
154 |
| - locale: self._options.i18n.locale, |
155 |
| - })); |
156 | 146 | const original = originalFormatter.interpolate(message, values, path);
|
157 |
| - 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; |
158 | 160 | },
|
159 | 161 | };
|
160 | 162 | // initialize decode & render
|
161 |
| - const throttler = (0, throttle_1.default)(() => this.render(), 800); |
| 163 | + const throttler = throttle(() => this.render(), 800); |
162 | 164 | const observer = new MutationObserver(throttler);
|
163 | 165 | observer.observe(document.documentElement, {
|
164 | 166 | subtree: true,
|
@@ -205,7 +207,7 @@ class LiveTranslatorManager {
|
205 | 207 | const parent = node.parentElement;
|
206 | 208 | if (node instanceof Text) {
|
207 | 209 | const matches = node.textContent.match(re);
|
208 |
| - for (const match of matches !== null && matches !== void 0 ? matches : []) { |
| 210 | + for (const match of matches ?? []) { |
209 | 211 | const meta = JSON.parse(ZeroWidthEncoder.decode(match));
|
210 | 212 | badges.push(createBadge(meta, this._options));
|
211 | 213 | }
|
@@ -263,7 +265,7 @@ const createBadge = (meta, options, attribute) => {
|
263 | 265 | });
|
264 | 266 | return badge;
|
265 | 267 | };
|
266 |
| -exports.LiveTranslatorPlugin = { |
| 268 | +export const LiveTranslatorPlugin = { |
267 | 269 | install(app, options) {
|
268 | 270 | console.log('LiveTranslator is installed');
|
269 | 271 | new LiveTranslatorManager(options);
|
|
0 commit comments