@gringow/gringow-react 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/gringow-component.d.mts +17 -0
- package/dist/gringow-component.mjs +124 -0
- package/dist/gringow-event.d.mts +11 -0
- package/dist/gringow-event.mjs +19 -0
- package/dist/gringow-store.d.mts +20 -0
- package/dist/gringow-store.mjs +68 -0
- package/dist/index.d.mts +11 -0
- package/dist/index.mjs +161 -0
- package/package.json +5 -1
- package/src/g.tsx +0 -51
- package/src/gringow-component.ts +0 -58
- package/src/gringow-event.ts +0 -23
- package/src/gringow-store.ts +0 -83
- package/src/index.ts +0 -8
- package/tsconfig.json +0 -10
- package/tsup.config.ts +0 -21
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
declare class GringowComponent extends HTMLElement {
|
|
2
|
+
shadowRoot: ShadowRoot;
|
|
3
|
+
private cacheId;
|
|
4
|
+
private cache;
|
|
5
|
+
private flatten;
|
|
6
|
+
private message;
|
|
7
|
+
private gringowElement;
|
|
8
|
+
private _lang;
|
|
9
|
+
get lang(): string;
|
|
10
|
+
set lang(value: string);
|
|
11
|
+
constructor();
|
|
12
|
+
private languageChangedListener;
|
|
13
|
+
private updateMessage;
|
|
14
|
+
private initializeShadowRoot;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export { GringowComponent };
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { GRINGOW_IO_URL } from '@gringow/gringow/browser';
|
|
2
|
+
|
|
3
|
+
// src/gringow-event.ts
|
|
4
|
+
var LanguageChangeEvent = class _LanguageChangeEvent extends CustomEvent {
|
|
5
|
+
static EVENT_NAME = "language-change";
|
|
6
|
+
constructor(detail) {
|
|
7
|
+
super(_LanguageChangeEvent.EVENT_NAME, {
|
|
8
|
+
detail: {
|
|
9
|
+
lang: detail.lang ?? "en-US",
|
|
10
|
+
timestamp: detail.timestamp || Date.now()
|
|
11
|
+
},
|
|
12
|
+
bubbles: true,
|
|
13
|
+
cancelable: true
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
static create(lang) {
|
|
17
|
+
return new _LanguageChangeEvent({ lang });
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
var GringowStoreSingleton = class _GringowStoreSingleton {
|
|
21
|
+
static instance = null;
|
|
22
|
+
#cache = null;
|
|
23
|
+
#cacheUrl;
|
|
24
|
+
#gringowIoToken;
|
|
25
|
+
#defaultLanguage;
|
|
26
|
+
constructor() {
|
|
27
|
+
}
|
|
28
|
+
static getInstance() {
|
|
29
|
+
if (!_GringowStoreSingleton.instance) {
|
|
30
|
+
_GringowStoreSingleton.instance = new _GringowStoreSingleton();
|
|
31
|
+
}
|
|
32
|
+
return _GringowStoreSingleton.instance;
|
|
33
|
+
}
|
|
34
|
+
set defaultLanguage(value) {
|
|
35
|
+
this.#defaultLanguage = value ?? "en-US";
|
|
36
|
+
}
|
|
37
|
+
get defaultLanguage() {
|
|
38
|
+
return this.#defaultLanguage;
|
|
39
|
+
}
|
|
40
|
+
set cacheUrl(value) {
|
|
41
|
+
this.#cacheUrl = value;
|
|
42
|
+
this.fetchCache();
|
|
43
|
+
}
|
|
44
|
+
get cacheUrl() {
|
|
45
|
+
if (!this.#cacheUrl) {
|
|
46
|
+
throw new Error("Cache URL is not set");
|
|
47
|
+
}
|
|
48
|
+
return this.#cacheUrl;
|
|
49
|
+
}
|
|
50
|
+
set gringowIoToken(value) {
|
|
51
|
+
this.#gringowIoToken = value;
|
|
52
|
+
this.#cacheUrl = `${GRINGOW_IO_URL}?token=${this.#gringowIoToken}`;
|
|
53
|
+
this.fetchCache();
|
|
54
|
+
}
|
|
55
|
+
get gringowIoToken() {
|
|
56
|
+
return this.#gringowIoToken;
|
|
57
|
+
}
|
|
58
|
+
get cache() {
|
|
59
|
+
return this.#cache;
|
|
60
|
+
}
|
|
61
|
+
set cache(value) {
|
|
62
|
+
this.#cache = value;
|
|
63
|
+
}
|
|
64
|
+
async fetchCache(force = false) {
|
|
65
|
+
if (!this.#cacheUrl) {
|
|
66
|
+
throw new Error("Cache URL is not set");
|
|
67
|
+
}
|
|
68
|
+
if (!this.cache || force) {
|
|
69
|
+
try {
|
|
70
|
+
const response = await fetch(this.#cacheUrl);
|
|
71
|
+
if (!response.ok) {
|
|
72
|
+
throw new Error("Failed to fetch gringow cache");
|
|
73
|
+
}
|
|
74
|
+
this.cache = await response.json();
|
|
75
|
+
} catch (error) {
|
|
76
|
+
throw new Error("Failed to fetch gringow cache", { cause: error });
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return this.cache;
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
var GringowStore = GringowStoreSingleton.getInstance();
|
|
83
|
+
|
|
84
|
+
// src/gringow-component.ts
|
|
85
|
+
var GringowComponent = class extends HTMLElement {
|
|
86
|
+
shadowRoot;
|
|
87
|
+
cacheId;
|
|
88
|
+
cache;
|
|
89
|
+
flatten;
|
|
90
|
+
message;
|
|
91
|
+
gringowElement;
|
|
92
|
+
_lang = GringowStore.defaultLanguage;
|
|
93
|
+
get lang() {
|
|
94
|
+
return this._lang;
|
|
95
|
+
}
|
|
96
|
+
set lang(value) {
|
|
97
|
+
this._lang = value ?? GringowStore.defaultLanguage;
|
|
98
|
+
this.updateMessage(this._lang);
|
|
99
|
+
}
|
|
100
|
+
constructor() {
|
|
101
|
+
super();
|
|
102
|
+
this.initializeShadowRoot();
|
|
103
|
+
window.addEventListener(LanguageChangeEvent.EVENT_NAME, this.languageChangedListener.bind(this));
|
|
104
|
+
}
|
|
105
|
+
languageChangedListener(event) {
|
|
106
|
+
if (this.lang !== event.detail.lang) {
|
|
107
|
+
this.lang = event.detail.lang;
|
|
108
|
+
}
|
|
109
|
+
this.updateMessage(event.detail.lang);
|
|
110
|
+
}
|
|
111
|
+
updateMessage(lang) {
|
|
112
|
+
this.gringowElement = this.shadowRoot.host;
|
|
113
|
+
this.cacheId = this.gringowElement.cacheId;
|
|
114
|
+
this.cache = this.gringowElement.cache;
|
|
115
|
+
this.message = this.cache?.translation?.[lang] ?? this.cache?.original ?? this.flatten;
|
|
116
|
+
this.shadowRoot.innerHTML = `<slot>${this.message}</slot>`;
|
|
117
|
+
}
|
|
118
|
+
initializeShadowRoot() {
|
|
119
|
+
this.shadowRoot = this.attachShadow({ mode: "open" });
|
|
120
|
+
this.updateMessage(this.lang);
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
export { GringowComponent };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
interface LanguageChangeEventDetail {
|
|
2
|
+
lang: string;
|
|
3
|
+
timestamp?: number;
|
|
4
|
+
}
|
|
5
|
+
declare class LanguageChangeEvent extends CustomEvent<LanguageChangeEventDetail> {
|
|
6
|
+
static readonly EVENT_NAME = "language-change";
|
|
7
|
+
constructor(detail: LanguageChangeEventDetail);
|
|
8
|
+
static create(lang: string): LanguageChangeEvent;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export { LanguageChangeEvent, type LanguageChangeEventDetail };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// src/gringow-event.ts
|
|
2
|
+
var LanguageChangeEvent = class _LanguageChangeEvent extends CustomEvent {
|
|
3
|
+
static EVENT_NAME = "language-change";
|
|
4
|
+
constructor(detail) {
|
|
5
|
+
super(_LanguageChangeEvent.EVENT_NAME, {
|
|
6
|
+
detail: {
|
|
7
|
+
lang: detail.lang ?? "en-US",
|
|
8
|
+
timestamp: detail.timestamp || Date.now()
|
|
9
|
+
},
|
|
10
|
+
bubbles: true,
|
|
11
|
+
cancelable: true
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
static create(lang) {
|
|
15
|
+
return new _LanguageChangeEvent({ lang });
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export { LanguageChangeEvent };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { GringowCache } from '@gringow/gringow';
|
|
2
|
+
|
|
3
|
+
declare class GringowStoreSingleton {
|
|
4
|
+
#private;
|
|
5
|
+
private static instance;
|
|
6
|
+
private constructor();
|
|
7
|
+
static getInstance(): GringowStoreSingleton;
|
|
8
|
+
set defaultLanguage(value: string);
|
|
9
|
+
get defaultLanguage(): string;
|
|
10
|
+
set cacheUrl(value: string);
|
|
11
|
+
get cacheUrl(): string;
|
|
12
|
+
set gringowIoToken(value: string);
|
|
13
|
+
get gringowIoToken(): string;
|
|
14
|
+
get cache(): GringowCache;
|
|
15
|
+
set cache(value: GringowCache);
|
|
16
|
+
fetchCache(force?: boolean): Promise<GringowCache>;
|
|
17
|
+
}
|
|
18
|
+
declare const GringowStore: GringowStoreSingleton;
|
|
19
|
+
|
|
20
|
+
export { GringowStore };
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { GRINGOW_IO_URL } from '@gringow/gringow/browser';
|
|
2
|
+
|
|
3
|
+
// src/gringow-store.ts
|
|
4
|
+
var GringowStoreSingleton = class _GringowStoreSingleton {
|
|
5
|
+
static instance = null;
|
|
6
|
+
#cache = null;
|
|
7
|
+
#cacheUrl;
|
|
8
|
+
#gringowIoToken;
|
|
9
|
+
#defaultLanguage;
|
|
10
|
+
constructor() {
|
|
11
|
+
}
|
|
12
|
+
static getInstance() {
|
|
13
|
+
if (!_GringowStoreSingleton.instance) {
|
|
14
|
+
_GringowStoreSingleton.instance = new _GringowStoreSingleton();
|
|
15
|
+
}
|
|
16
|
+
return _GringowStoreSingleton.instance;
|
|
17
|
+
}
|
|
18
|
+
set defaultLanguage(value) {
|
|
19
|
+
this.#defaultLanguage = value ?? "en-US";
|
|
20
|
+
}
|
|
21
|
+
get defaultLanguage() {
|
|
22
|
+
return this.#defaultLanguage;
|
|
23
|
+
}
|
|
24
|
+
set cacheUrl(value) {
|
|
25
|
+
this.#cacheUrl = value;
|
|
26
|
+
this.fetchCache();
|
|
27
|
+
}
|
|
28
|
+
get cacheUrl() {
|
|
29
|
+
if (!this.#cacheUrl) {
|
|
30
|
+
throw new Error("Cache URL is not set");
|
|
31
|
+
}
|
|
32
|
+
return this.#cacheUrl;
|
|
33
|
+
}
|
|
34
|
+
set gringowIoToken(value) {
|
|
35
|
+
this.#gringowIoToken = value;
|
|
36
|
+
this.#cacheUrl = `${GRINGOW_IO_URL}?token=${this.#gringowIoToken}`;
|
|
37
|
+
this.fetchCache();
|
|
38
|
+
}
|
|
39
|
+
get gringowIoToken() {
|
|
40
|
+
return this.#gringowIoToken;
|
|
41
|
+
}
|
|
42
|
+
get cache() {
|
|
43
|
+
return this.#cache;
|
|
44
|
+
}
|
|
45
|
+
set cache(value) {
|
|
46
|
+
this.#cache = value;
|
|
47
|
+
}
|
|
48
|
+
async fetchCache(force = false) {
|
|
49
|
+
if (!this.#cacheUrl) {
|
|
50
|
+
throw new Error("Cache URL is not set");
|
|
51
|
+
}
|
|
52
|
+
if (!this.cache || force) {
|
|
53
|
+
try {
|
|
54
|
+
const response = await fetch(this.#cacheUrl);
|
|
55
|
+
if (!response.ok) {
|
|
56
|
+
throw new Error("Failed to fetch gringow cache");
|
|
57
|
+
}
|
|
58
|
+
this.cache = await response.json();
|
|
59
|
+
} catch (error) {
|
|
60
|
+
throw new Error("Failed to fetch gringow cache", { cause: error });
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return this.cache;
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
var GringowStore = GringowStoreSingleton.getInstance();
|
|
67
|
+
|
|
68
|
+
export { GringowStore };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
export { GringowComponent } from './gringow-component.mjs';
|
|
3
|
+
export { LanguageChangeEvent, LanguageChangeEventDetail } from './gringow-event.mjs';
|
|
4
|
+
export { GringowStore } from './gringow-store.mjs';
|
|
5
|
+
import '@gringow/gringow';
|
|
6
|
+
|
|
7
|
+
declare function g(strings: TemplateStringsArray, ...values: Array<unknown>): React.ReactNode;
|
|
8
|
+
|
|
9
|
+
declare const gringowReact: {};
|
|
10
|
+
|
|
11
|
+
export { gringowReact as default, g };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { GRINGOW_IO_URL, flatTemplateString, createCacheId } from '@gringow/gringow/browser';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import { jsx } from 'react/jsx-runtime';
|
|
4
|
+
|
|
5
|
+
// src/g.tsx
|
|
6
|
+
|
|
7
|
+
// src/gringow-event.ts
|
|
8
|
+
var LanguageChangeEvent = class _LanguageChangeEvent extends CustomEvent {
|
|
9
|
+
static EVENT_NAME = "language-change";
|
|
10
|
+
constructor(detail) {
|
|
11
|
+
super(_LanguageChangeEvent.EVENT_NAME, {
|
|
12
|
+
detail: {
|
|
13
|
+
lang: detail.lang ?? "en-US",
|
|
14
|
+
timestamp: detail.timestamp || Date.now()
|
|
15
|
+
},
|
|
16
|
+
bubbles: true,
|
|
17
|
+
cancelable: true
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
static create(lang) {
|
|
21
|
+
return new _LanguageChangeEvent({ lang });
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
var GringowStoreSingleton = class _GringowStoreSingleton {
|
|
25
|
+
static instance = null;
|
|
26
|
+
#cache = null;
|
|
27
|
+
#cacheUrl;
|
|
28
|
+
#gringowIoToken;
|
|
29
|
+
#defaultLanguage;
|
|
30
|
+
constructor() {
|
|
31
|
+
}
|
|
32
|
+
static getInstance() {
|
|
33
|
+
if (!_GringowStoreSingleton.instance) {
|
|
34
|
+
_GringowStoreSingleton.instance = new _GringowStoreSingleton();
|
|
35
|
+
}
|
|
36
|
+
return _GringowStoreSingleton.instance;
|
|
37
|
+
}
|
|
38
|
+
set defaultLanguage(value) {
|
|
39
|
+
this.#defaultLanguage = value ?? "en-US";
|
|
40
|
+
}
|
|
41
|
+
get defaultLanguage() {
|
|
42
|
+
return this.#defaultLanguage;
|
|
43
|
+
}
|
|
44
|
+
set cacheUrl(value) {
|
|
45
|
+
this.#cacheUrl = value;
|
|
46
|
+
this.fetchCache();
|
|
47
|
+
}
|
|
48
|
+
get cacheUrl() {
|
|
49
|
+
if (!this.#cacheUrl) {
|
|
50
|
+
throw new Error("Cache URL is not set");
|
|
51
|
+
}
|
|
52
|
+
return this.#cacheUrl;
|
|
53
|
+
}
|
|
54
|
+
set gringowIoToken(value) {
|
|
55
|
+
this.#gringowIoToken = value;
|
|
56
|
+
this.#cacheUrl = `${GRINGOW_IO_URL}?token=${this.#gringowIoToken}`;
|
|
57
|
+
this.fetchCache();
|
|
58
|
+
}
|
|
59
|
+
get gringowIoToken() {
|
|
60
|
+
return this.#gringowIoToken;
|
|
61
|
+
}
|
|
62
|
+
get cache() {
|
|
63
|
+
return this.#cache;
|
|
64
|
+
}
|
|
65
|
+
set cache(value) {
|
|
66
|
+
this.#cache = value;
|
|
67
|
+
}
|
|
68
|
+
async fetchCache(force = false) {
|
|
69
|
+
if (!this.#cacheUrl) {
|
|
70
|
+
throw new Error("Cache URL is not set");
|
|
71
|
+
}
|
|
72
|
+
if (!this.cache || force) {
|
|
73
|
+
try {
|
|
74
|
+
const response = await fetch(this.#cacheUrl);
|
|
75
|
+
if (!response.ok) {
|
|
76
|
+
throw new Error("Failed to fetch gringow cache");
|
|
77
|
+
}
|
|
78
|
+
this.cache = await response.json();
|
|
79
|
+
} catch (error) {
|
|
80
|
+
throw new Error("Failed to fetch gringow cache", { cause: error });
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return this.cache;
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
var GringowStore = GringowStoreSingleton.getInstance();
|
|
87
|
+
|
|
88
|
+
// src/gringow-component.ts
|
|
89
|
+
var GringowComponent = class extends HTMLElement {
|
|
90
|
+
shadowRoot;
|
|
91
|
+
cacheId;
|
|
92
|
+
cache;
|
|
93
|
+
flatten;
|
|
94
|
+
message;
|
|
95
|
+
gringowElement;
|
|
96
|
+
_lang = GringowStore.defaultLanguage;
|
|
97
|
+
get lang() {
|
|
98
|
+
return this._lang;
|
|
99
|
+
}
|
|
100
|
+
set lang(value) {
|
|
101
|
+
this._lang = value ?? GringowStore.defaultLanguage;
|
|
102
|
+
this.updateMessage(this._lang);
|
|
103
|
+
}
|
|
104
|
+
constructor() {
|
|
105
|
+
super();
|
|
106
|
+
this.initializeShadowRoot();
|
|
107
|
+
window.addEventListener(LanguageChangeEvent.EVENT_NAME, this.languageChangedListener.bind(this));
|
|
108
|
+
}
|
|
109
|
+
languageChangedListener(event) {
|
|
110
|
+
if (this.lang !== event.detail.lang) {
|
|
111
|
+
this.lang = event.detail.lang;
|
|
112
|
+
}
|
|
113
|
+
this.updateMessage(event.detail.lang);
|
|
114
|
+
}
|
|
115
|
+
updateMessage(lang) {
|
|
116
|
+
this.gringowElement = this.shadowRoot.host;
|
|
117
|
+
this.cacheId = this.gringowElement.cacheId;
|
|
118
|
+
this.cache = this.gringowElement.cache;
|
|
119
|
+
this.message = this.cache?.translation?.[lang] ?? this.cache?.original ?? this.flatten;
|
|
120
|
+
this.shadowRoot.innerHTML = `<slot>${this.message}</slot>`;
|
|
121
|
+
}
|
|
122
|
+
initializeShadowRoot() {
|
|
123
|
+
this.shadowRoot = this.attachShadow({ mode: "open" });
|
|
124
|
+
this.updateMessage(this.lang);
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
var ELEMENT_NAME = "g-gringow";
|
|
128
|
+
function g(strings, ...values) {
|
|
129
|
+
const [elementShow, setElementShow] = React.useState(false);
|
|
130
|
+
const [cache, setCache] = React.useState();
|
|
131
|
+
const ref = React.useRef(null);
|
|
132
|
+
const flatten = flatTemplateString(strings, values);
|
|
133
|
+
const cacheId = createCacheId(flatten);
|
|
134
|
+
React.useEffect(() => {
|
|
135
|
+
React.startTransition(async () => {
|
|
136
|
+
await GringowStore.fetchCache();
|
|
137
|
+
setCache(GringowStore.cache);
|
|
138
|
+
});
|
|
139
|
+
}, []);
|
|
140
|
+
React.useEffect(() => {
|
|
141
|
+
if (!window.customElements.get(ELEMENT_NAME)) {
|
|
142
|
+
window.customElements.define(ELEMENT_NAME, GringowComponent);
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
React.useEffect(() => {
|
|
146
|
+
if (ref.current) {
|
|
147
|
+
ref.current.lang = GringowStore.defaultLanguage;
|
|
148
|
+
ref.current.cacheId = cacheId;
|
|
149
|
+
ref.current.cache = cache[cacheId];
|
|
150
|
+
ref.current.flatten = flatten;
|
|
151
|
+
if (!elementShow) setElementShow(true);
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
return /* @__PURE__ */ jsx(React.Suspense, { fallback: flatten, children: Boolean(cache) && /* @__PURE__ */ jsx("g-gringow", { ref, id: cacheId, children: /* @__PURE__ */ jsx("g-outlet", { slot: cacheId, children: flatten }) }) });
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// src/index.ts
|
|
158
|
+
var gringowReact = {};
|
|
159
|
+
var index_default = gringowReact;
|
|
160
|
+
|
|
161
|
+
export { GringowComponent, GringowStore, LanguageChangeEvent, index_default as default, g };
|
package/package.json
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gringow/gringow-react",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"description": "React bindings for Gringow AI-powered translation tool",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist",
|
|
10
|
+
"README.md"
|
|
11
|
+
],
|
|
8
12
|
"exports": {
|
|
9
13
|
".": {
|
|
10
14
|
"import": "./dist/index.mjs",
|
package/src/g.tsx
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { createCacheId, flatTemplateString } from '@gringow/gringow/browser'
|
|
2
|
-
import React from 'react'
|
|
3
|
-
|
|
4
|
-
import { GringowComponent } from './gringow-component'
|
|
5
|
-
import { GringowStore } from './gringow-store'
|
|
6
|
-
|
|
7
|
-
import type { GringowCache } from '@gringow/gringow'
|
|
8
|
-
|
|
9
|
-
const ELEMENT_NAME = 'g-gringow'
|
|
10
|
-
|
|
11
|
-
export function g(strings: TemplateStringsArray, ...values: Array<unknown>): React.ReactNode {
|
|
12
|
-
const [elementShow, setElementShow] = React.useState(false)
|
|
13
|
-
const [cache, setCache] = React.useState<GringowCache | undefined>()
|
|
14
|
-
const ref = React.useRef<GringowComponent>(null)
|
|
15
|
-
|
|
16
|
-
const flatten = flatTemplateString(strings, values)
|
|
17
|
-
const cacheId = createCacheId(flatten)
|
|
18
|
-
|
|
19
|
-
React.useEffect(() => {
|
|
20
|
-
React.startTransition(async () => {
|
|
21
|
-
await GringowStore.fetchCache()
|
|
22
|
-
setCache(GringowStore.cache)
|
|
23
|
-
})
|
|
24
|
-
}, [])
|
|
25
|
-
|
|
26
|
-
React.useEffect(() => {
|
|
27
|
-
if (!window.customElements.get(ELEMENT_NAME)) {
|
|
28
|
-
window.customElements.define(ELEMENT_NAME, GringowComponent)
|
|
29
|
-
}
|
|
30
|
-
})
|
|
31
|
-
|
|
32
|
-
React.useEffect(() => {
|
|
33
|
-
if (ref.current) {
|
|
34
|
-
ref.current.lang = GringowStore.defaultLanguage
|
|
35
|
-
ref.current.cacheId = cacheId
|
|
36
|
-
ref.current.cache = cache[cacheId]
|
|
37
|
-
ref.current.flatten = flatten
|
|
38
|
-
if (!elementShow) setElementShow(true)
|
|
39
|
-
}
|
|
40
|
-
})
|
|
41
|
-
|
|
42
|
-
return (
|
|
43
|
-
<React.Suspense fallback={flatten}>
|
|
44
|
-
{Boolean(cache) && (
|
|
45
|
-
<g-gringow ref={ref} id={cacheId}>
|
|
46
|
-
<g-outlet slot={cacheId}>{flatten}</g-outlet>
|
|
47
|
-
</g-gringow>
|
|
48
|
-
)}
|
|
49
|
-
</React.Suspense>
|
|
50
|
-
)
|
|
51
|
-
}
|
package/src/gringow-component.ts
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import { LanguageChangeEvent } from './gringow-event'
|
|
2
|
-
import { GringowStore } from './gringow-store'
|
|
3
|
-
|
|
4
|
-
import type { GringowCacheItem } from '@gringow/gringow'
|
|
5
|
-
|
|
6
|
-
type GringowElement = Element & { cache: GringowCacheItem; cacheId: string }
|
|
7
|
-
|
|
8
|
-
class GringowComponent extends HTMLElement {
|
|
9
|
-
public shadowRoot: ShadowRoot
|
|
10
|
-
private cacheId: string
|
|
11
|
-
private cache: GringowCacheItem
|
|
12
|
-
private flatten: string
|
|
13
|
-
private message: string
|
|
14
|
-
private gringowElement: GringowElement
|
|
15
|
-
|
|
16
|
-
private _lang: string = GringowStore.defaultLanguage
|
|
17
|
-
|
|
18
|
-
get lang() {
|
|
19
|
-
return this._lang
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
set lang(value: string) {
|
|
23
|
-
this._lang = value ?? GringowStore.defaultLanguage
|
|
24
|
-
this.updateMessage(this._lang)
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
constructor() {
|
|
28
|
-
super()
|
|
29
|
-
this.initializeShadowRoot()
|
|
30
|
-
|
|
31
|
-
window.addEventListener(LanguageChangeEvent.EVENT_NAME, this.languageChangedListener.bind(this))
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
private languageChangedListener(event: LanguageChangeEvent) {
|
|
35
|
-
if (this.lang !== event.detail.lang) {
|
|
36
|
-
this.lang = event.detail.lang
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
this.updateMessage(event.detail.lang)
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
private updateMessage(lang: string) {
|
|
43
|
-
this.gringowElement = this.shadowRoot.host as GringowElement
|
|
44
|
-
this.cacheId = this.gringowElement.cacheId
|
|
45
|
-
this.cache = this.gringowElement.cache
|
|
46
|
-
|
|
47
|
-
this.message = this.cache?.translation?.[lang] ?? this.cache?.original ?? this.flatten
|
|
48
|
-
|
|
49
|
-
this.shadowRoot.innerHTML = `<slot>${this.message}</slot>`
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
private initializeShadowRoot() {
|
|
53
|
-
this.shadowRoot = this.attachShadow({ mode: 'open' })
|
|
54
|
-
this.updateMessage(this.lang)
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export { GringowComponent }
|
package/src/gringow-event.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
export interface LanguageChangeEventDetail {
|
|
2
|
-
lang: string
|
|
3
|
-
timestamp?: number
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
export class LanguageChangeEvent extends CustomEvent<LanguageChangeEventDetail> {
|
|
7
|
-
static readonly EVENT_NAME = 'language-change'
|
|
8
|
-
|
|
9
|
-
constructor(detail: LanguageChangeEventDetail) {
|
|
10
|
-
super(LanguageChangeEvent.EVENT_NAME, {
|
|
11
|
-
detail: {
|
|
12
|
-
lang: detail.lang ?? 'en-US',
|
|
13
|
-
timestamp: detail.timestamp || Date.now(),
|
|
14
|
-
},
|
|
15
|
-
bubbles: true,
|
|
16
|
-
cancelable: true,
|
|
17
|
-
})
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
static create(lang: string): LanguageChangeEvent {
|
|
21
|
-
return new LanguageChangeEvent({ lang })
|
|
22
|
-
}
|
|
23
|
-
}
|
package/src/gringow-store.ts
DELETED
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
import { GRINGOW_IO_URL } from '@gringow/gringow/browser'
|
|
2
|
-
|
|
3
|
-
import type { GringowCache } from '@gringow/gringow'
|
|
4
|
-
|
|
5
|
-
class GringowStoreSingleton {
|
|
6
|
-
private static instance: GringowStoreSingleton | null = null
|
|
7
|
-
#cache: GringowCache | null = null
|
|
8
|
-
#cacheUrl: string
|
|
9
|
-
#gringowIoToken: string
|
|
10
|
-
#defaultLanguage: string
|
|
11
|
-
|
|
12
|
-
private constructor() {}
|
|
13
|
-
|
|
14
|
-
public static getInstance(): GringowStoreSingleton {
|
|
15
|
-
if (!GringowStoreSingleton.instance) {
|
|
16
|
-
GringowStoreSingleton.instance = new GringowStoreSingleton()
|
|
17
|
-
}
|
|
18
|
-
return GringowStoreSingleton.instance
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
public set defaultLanguage(value: string) {
|
|
22
|
-
this.#defaultLanguage = value ?? 'en-US'
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
public get defaultLanguage() {
|
|
26
|
-
return this.#defaultLanguage
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
public set cacheUrl(value: string) {
|
|
30
|
-
this.#cacheUrl = value
|
|
31
|
-
this.fetchCache()
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
public get cacheUrl() {
|
|
35
|
-
if (!this.#cacheUrl) {
|
|
36
|
-
throw new Error('Cache URL is not set')
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
return this.#cacheUrl
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
public set gringowIoToken(value: string) {
|
|
43
|
-
this.#gringowIoToken = value
|
|
44
|
-
this.#cacheUrl = `${GRINGOW_IO_URL}?token=${this.#gringowIoToken}`
|
|
45
|
-
this.fetchCache()
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
public get gringowIoToken() {
|
|
49
|
-
return this.#gringowIoToken
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
public get cache() {
|
|
53
|
-
return this.#cache
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
public set cache(value: GringowCache) {
|
|
57
|
-
this.#cache = value
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
public async fetchCache(force: boolean = false): Promise<GringowCache> {
|
|
61
|
-
if (!this.#cacheUrl) {
|
|
62
|
-
throw new Error('Cache URL is not set')
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
if (!this.cache || force) {
|
|
66
|
-
try {
|
|
67
|
-
const response = await fetch(this.#cacheUrl)
|
|
68
|
-
if (!response.ok) {
|
|
69
|
-
throw new Error('Failed to fetch gringow cache')
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
this.cache = await response.json()
|
|
73
|
-
} catch (error) {
|
|
74
|
-
throw new Error('Failed to fetch gringow cache', { cause: error })
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
return this.cache
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
const GringowStore = GringowStoreSingleton.getInstance()
|
|
83
|
-
export { GringowStore }
|
package/src/index.ts
DELETED
package/tsconfig.json
DELETED
package/tsup.config.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { defineConfig } from 'tsup'
|
|
2
|
-
|
|
3
|
-
export default defineConfig({
|
|
4
|
-
entry: ['src/**/*.ts'],
|
|
5
|
-
// format: ["cjs", "esm"],
|
|
6
|
-
format: ['esm'],
|
|
7
|
-
dts: true,
|
|
8
|
-
splitting: false,
|
|
9
|
-
sourcemap: false,
|
|
10
|
-
clean: true,
|
|
11
|
-
treeshake: 'safest',
|
|
12
|
-
minify: false,
|
|
13
|
-
outDir: 'dist',
|
|
14
|
-
target: 'esnext',
|
|
15
|
-
outExtension({ format }) {
|
|
16
|
-
return {
|
|
17
|
-
js: format === 'cjs' ? '.js' : '.mjs',
|
|
18
|
-
}
|
|
19
|
-
},
|
|
20
|
-
onSuccess: 'tsc-alias --verbose',
|
|
21
|
-
})
|