Skip to content

Commit 783f10c

Browse files
author
Sebi Nemeth
committed
make zwe static
1 parent b5f5d53 commit 783f10c

File tree

2 files changed

+25
-26
lines changed

2 files changed

+25
-26
lines changed

dist/index.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
var __importDefault = (this && this.__importDefault) || function (mod) {
33
return (mod && mod.__esModule) ? mod : { "default": mod };
44
};
5+
var _a;
56
Object.defineProperty(exports, "__esModule", { value: true });
67
exports.LiveTranslatorPlugin = void 0;
78
const throttle_1 = __importDefault(require("lodash/throttle"));
@@ -59,14 +60,7 @@ const css = `
5960
}
6061
`;
6162
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) {
7064
const binary = text
7165
.split('')
7266
.map((char) => char.charCodeAt(0).toString(2))
@@ -86,7 +80,7 @@ class ZeroWidthEncoder {
8680
.join('');
8781
return this.START + zeroWidth + this.END;
8882
}
89-
decode(zeroWidth) {
83+
static decode(zeroWidth) {
9084
const binary = zeroWidth
9185
.split('')
9286
.slice(1, zeroWidth.length - 1) // remove START and END
@@ -107,11 +101,17 @@ class ZeroWidthEncoder {
107101
return text;
108102
}
109103
}
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}`;
110111
class LiveTranslatorManager {
111112
constructor(options) {
112113
this._enabled = false;
113114
this._options = options;
114-
this._zwEncoder = new ZeroWidthEncoder();
115115
// handle persistance
116116
const savedRaw = localStorage.getItem('live-translator-enabled');
117117
if (this._options.persist && savedRaw) {
@@ -190,7 +190,7 @@ class LiveTranslatorManager {
190190
if (!this._enabled) {
191191
return;
192192
}
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');
194194
const queue = [document.documentElement];
195195
while (queue.length > 0) {
196196
const node = queue.pop();
@@ -199,7 +199,7 @@ class LiveTranslatorManager {
199199
if (node instanceof Text) {
200200
const matches = node.textContent.match(re);
201201
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));
203203
badges.push(createBadge(meta, this._options));
204204
}
205205
}
@@ -208,7 +208,7 @@ class LiveTranslatorManager {
208208
.filter(({ match }) => !!match);
209209
for (const { attribute, match } of attributes) {
210210
for (const m of match) {
211-
const meta = JSON.parse(this._zwEncoder.decode(m));
211+
const meta = JSON.parse(ZeroWidthEncoder.decode(m));
212212
badges.push(createBadge(meta, this._options, attribute.name));
213213
}
214214
}

src/index.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,15 @@ type LiveTranslatorPluginOptions = {
6868
persist?: boolean
6969
}
7070

71-
class ZeroWidthEncoder {
72-
START = '\u200B'
73-
ZERO = '\u200C'
74-
ONE = '\u200D'
75-
SPACE = '\u200E'
76-
END = '\u200F'
71+
abstract class ZeroWidthEncoder {
72+
static START = '\u200B'
73+
static ZERO = '\u200C'
74+
static ONE = '\u200D'
75+
static SPACE = '\u200E'
76+
static END = '\u200F'
77+
static PATTERN = `${this.START}[${this.ZERO}${this.ONE}${this.SPACE}]+${this.END}`
7778

78-
encode (text: string) {
79+
static encode (text: string) {
7980
const binary = text
8081
.split('')
8182
.map((char) => char.charCodeAt(0).toString(2))
@@ -96,7 +97,7 @@ class ZeroWidthEncoder {
9697
return this.START + zeroWidth + this.END
9798
}
9899

99-
decode (zeroWidth: string) {
100+
static decode (zeroWidth: string) {
100101
const binary = zeroWidth
101102
.split('')
102103
.slice(1, zeroWidth.length - 1) // remove START and END
@@ -122,15 +123,13 @@ class LiveTranslatorManager {
122123
_enabled: boolean
123124
_options: LiveTranslatorPluginOptions
124125
_callback: CallableFunction
125-
_zwEncoder: ZeroWidthEncoder
126126

127127
_enableButton: HTMLButtonElement
128128
_indicator: HTMLSpanElement
129129

130130
constructor (options: LiveTranslatorPluginOptions) {
131131
this._enabled = false
132132
this._options = options
133-
this._zwEncoder = new ZeroWidthEncoder()
134133

135134
// handle persistance
136135
const savedRaw = localStorage.getItem('live-translator-enabled')
@@ -225,7 +224,7 @@ class LiveTranslatorManager {
225224
return
226225
}
227226

228-
const re = new RegExp(`${this._zwEncoder.START}[${this._zwEncoder.ZERO}${this._zwEncoder.ONE}${this._zwEncoder.SPACE}]+${this._zwEncoder.END}`, 'gm')
227+
const re = new RegExp(ZeroWidthEncoder.PATTERN, 'gm')
229228

230229
const queue = [document.documentElement] as Node[]
231230
while (queue.length > 0) {
@@ -237,7 +236,7 @@ class LiveTranslatorManager {
237236
if (node instanceof Text) {
238237
const matches = (node.textContent as string).match(re)
239238
for (const match of matches ?? []) {
240-
const meta = JSON.parse(this._zwEncoder.decode(match)) as TranslationMeta
239+
const meta = JSON.parse(ZeroWidthEncoder.decode(match)) as TranslationMeta
241240
badges.push(createBadge(meta, this._options))
242241
}
243242
}
@@ -247,7 +246,7 @@ class LiveTranslatorManager {
247246
.filter(({ match }) => !!match)
248247
for (const { attribute, match } of attributes) {
249248
for (const m of (match as RegExpMatchArray)) {
250-
const meta = JSON.parse(this._zwEncoder.decode(m)) as TranslationMeta
249+
const meta = JSON.parse(ZeroWidthEncoder.decode(m)) as TranslationMeta
251250
badges.push(createBadge(meta, this._options, attribute.name))
252251
}
253252
}

0 commit comments

Comments
 (0)