@arcblock/ux 2.12.51 → 2.12.53
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/lib/Locale/context.js +14 -2
- package/lib/Util/index.d.ts +4 -0
- package/lib/Util/index.js +46 -0
- package/package.json +5 -5
- package/src/Locale/context.tsx +12 -2
- package/src/Util/index.ts +53 -0
package/lib/Locale/context.js
CHANGED
@@ -5,7 +5,7 @@ import get from 'lodash/get';
|
|
5
5
|
import Cookie from 'js-cookie';
|
6
6
|
import browserLang from './browser-lang';
|
7
7
|
import { translate } from './util';
|
8
|
-
import { getCookieOptions } from '../Util';
|
8
|
+
import { getCookieOptions, resolveRootDomain } from '../Util';
|
9
9
|
const cookieName = 'nf_lang';
|
10
10
|
|
11
11
|
// 跨应用传递多语言选择的方式是在 query string 中添加 locale 参数,LocaleSelector 要高优先级遵守这个参数
|
@@ -33,7 +33,19 @@ const getLocale = (languages = []) => {
|
|
33
33
|
return getLocaleFromSearchParams(languages) || Cookie.get(cookieName) || browserLang(langParams);
|
34
34
|
};
|
35
35
|
const setLocale = locale => {
|
36
|
-
|
36
|
+
const opts = getCookieOptions();
|
37
|
+
|
38
|
+
// @compatibility 移除历史上使用根域名的旧 cookie
|
39
|
+
if (!opts.domain) {
|
40
|
+
const rootDomain = resolveRootDomain();
|
41
|
+
if (rootDomain) {
|
42
|
+
Cookie.remove(cookieName, {
|
43
|
+
path: opts.path || '/',
|
44
|
+
domain: rootDomain
|
45
|
+
});
|
46
|
+
}
|
47
|
+
}
|
48
|
+
Cookie.set(cookieName, locale, opts);
|
37
49
|
setLocaleParam(locale);
|
38
50
|
};
|
39
51
|
const getLanguages = arg => {
|
package/lib/Util/index.d.ts
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
import type { $TSFixMe, Locale } from '../type';
|
2
2
|
declare let dateTool: $TSFixMe | null;
|
3
|
+
/** 是否常见二段式顶级域名 */
|
4
|
+
export declare function isTwoSegmentTLD(domain: string): boolean;
|
5
|
+
/** 获取根域名 */
|
6
|
+
export declare function resolveRootDomain(): string;
|
3
7
|
export declare function parseQuery(str: string): Record<string, string | boolean>;
|
4
8
|
export declare function stringifyQuery(params?: {}): string;
|
5
9
|
export declare function getCookieOptions(expireInDays?: number | {
|
package/lib/Util/index.js
CHANGED
@@ -7,6 +7,52 @@ import omitBy from 'lodash/omitBy';
|
|
7
7
|
import pRetry from 'p-retry';
|
8
8
|
import { DID_PREFIX, BLOCKLET_SERVICE_PATH_PREFIX } from './constant';
|
9
9
|
let dateTool = null;
|
10
|
+
const IP_V4_REGEX = /^(\d{1,3}\.){3}\d{1,3}(:\d+)?$/;
|
11
|
+
// 常见顶级域名
|
12
|
+
const commonSLDs = new Set(['co', 'com', 'net', 'org', 'gov', 'edu', 'ac']);
|
13
|
+
// 常见国家域名
|
14
|
+
const commonCcTLDs = new Set(['uk', 'au', 'cn', 'nz', 'za', 'in', 'br', 'mx', 'fr', 'it', 'ca']);
|
15
|
+
|
16
|
+
/** 是否常见二段式顶级域名 */
|
17
|
+
export function isTwoSegmentTLD(domain) {
|
18
|
+
if (!domain) {
|
19
|
+
return false;
|
20
|
+
}
|
21
|
+
const parts = domain.split('.');
|
22
|
+
if (parts.length < 2) return false;
|
23
|
+
const sld = parts[parts.length - 2];
|
24
|
+
const tld = parts[parts.length - 1];
|
25
|
+
return commonSLDs.has(sld) && commonCcTLDs.has(tld);
|
26
|
+
}
|
27
|
+
|
28
|
+
/** 获取根域名 */
|
29
|
+
export function resolveRootDomain() {
|
30
|
+
const {
|
31
|
+
host
|
32
|
+
} = window.location;
|
33
|
+
if (!host) {
|
34
|
+
return '';
|
35
|
+
}
|
36
|
+
if (IP_V4_REGEX.test(host)) {
|
37
|
+
return '';
|
38
|
+
}
|
39
|
+
|
40
|
+
// 移除可能存在的端口号
|
41
|
+
const hostWithoutPort = host.split(':')[0];
|
42
|
+
const parts = hostWithoutPort.split('.');
|
43
|
+
if (parts.length === 1) {
|
44
|
+
return '';
|
45
|
+
}
|
46
|
+
|
47
|
+
// 二段式顶级域名
|
48
|
+
if (parts.length > 2) {
|
49
|
+
const tld = parts.slice(-2).join('.');
|
50
|
+
if (isTwoSegmentTLD(tld)) {
|
51
|
+
return `.${parts.slice(-3).join('.')}`;
|
52
|
+
}
|
53
|
+
}
|
54
|
+
return `.${parts.slice(-2).join('.')}`;
|
55
|
+
}
|
10
56
|
export function parseQuery(str) {
|
11
57
|
return str.replace(/^\?/, '').split('&').map(x => x.split('=')).filter(([key]) => !!key).reduce((memo, x) => {
|
12
58
|
const key = x[0];
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@arcblock/ux",
|
3
|
-
"version": "2.12.
|
3
|
+
"version": "2.12.53",
|
4
4
|
"description": "Common used react components for arcblock products",
|
5
5
|
"keywords": [
|
6
6
|
"react",
|
@@ -69,12 +69,12 @@
|
|
69
69
|
"react": ">=18.2.0",
|
70
70
|
"react-router-dom": ">=6.22.3"
|
71
71
|
},
|
72
|
-
"gitHead": "
|
72
|
+
"gitHead": "454c63f6d9b58724fa0176ac74de2f306b53207c",
|
73
73
|
"dependencies": {
|
74
74
|
"@arcblock/did-motif": "^1.1.13",
|
75
|
-
"@arcblock/icons": "^2.12.
|
76
|
-
"@arcblock/nft-display": "^2.12.
|
77
|
-
"@arcblock/react-hooks": "^2.12.
|
75
|
+
"@arcblock/icons": "^2.12.53",
|
76
|
+
"@arcblock/nft-display": "^2.12.53",
|
77
|
+
"@arcblock/react-hooks": "^2.12.53",
|
78
78
|
"@babel/plugin-syntax-dynamic-import": "^7.8.3",
|
79
79
|
"@fontsource/inter": "^5.0.16",
|
80
80
|
"@fontsource/ubuntu-mono": "^5.0.18",
|
package/src/Locale/context.tsx
CHANGED
@@ -6,7 +6,7 @@ import Cookie from 'js-cookie';
|
|
6
6
|
import browserLang from './browser-lang';
|
7
7
|
import { translate } from './util';
|
8
8
|
|
9
|
-
import { getCookieOptions } from '../Util';
|
9
|
+
import { getCookieOptions, resolveRootDomain } from '../Util';
|
10
10
|
import type { Locale } from '../type';
|
11
11
|
|
12
12
|
const cookieName = 'nf_lang';
|
@@ -42,7 +42,17 @@ const getLocale = (languages: { code: string }[] = []): string => {
|
|
42
42
|
};
|
43
43
|
|
44
44
|
const setLocale = (locale: Locale) => {
|
45
|
-
|
45
|
+
const opts = getCookieOptions();
|
46
|
+
|
47
|
+
// @compatibility 移除历史上使用根域名的旧 cookie
|
48
|
+
if (!opts.domain) {
|
49
|
+
const rootDomain = resolveRootDomain();
|
50
|
+
if (rootDomain) {
|
51
|
+
Cookie.remove(cookieName, { path: opts.path || '/', domain: rootDomain });
|
52
|
+
}
|
53
|
+
}
|
54
|
+
|
55
|
+
Cookie.set(cookieName, locale, opts);
|
46
56
|
setLocaleParam(locale);
|
47
57
|
};
|
48
58
|
|
package/src/Util/index.ts
CHANGED
@@ -9,6 +9,59 @@ import { DID_PREFIX, BLOCKLET_SERVICE_PATH_PREFIX } from './constant';
|
|
9
9
|
import type { $TSFixMe, Locale } from '../type';
|
10
10
|
|
11
11
|
let dateTool: $TSFixMe | null = null;
|
12
|
+
const IP_V4_REGEX = /^(\d{1,3}\.){3}\d{1,3}(:\d+)?$/;
|
13
|
+
// 常见顶级域名
|
14
|
+
const commonSLDs = new Set(['co', 'com', 'net', 'org', 'gov', 'edu', 'ac']);
|
15
|
+
// 常见国家域名
|
16
|
+
const commonCcTLDs = new Set(['uk', 'au', 'cn', 'nz', 'za', 'in', 'br', 'mx', 'fr', 'it', 'ca']);
|
17
|
+
|
18
|
+
/** 是否常见二段式顶级域名 */
|
19
|
+
export function isTwoSegmentTLD(domain: string) {
|
20
|
+
if (!domain) {
|
21
|
+
return false;
|
22
|
+
}
|
23
|
+
|
24
|
+
const parts = domain.split('.');
|
25
|
+
|
26
|
+
if (parts.length < 2) return false;
|
27
|
+
|
28
|
+
const sld = parts[parts.length - 2];
|
29
|
+
const tld = parts[parts.length - 1];
|
30
|
+
|
31
|
+
return commonSLDs.has(sld) && commonCcTLDs.has(tld);
|
32
|
+
}
|
33
|
+
|
34
|
+
/** 获取根域名 */
|
35
|
+
export function resolveRootDomain() {
|
36
|
+
const { host } = window.location;
|
37
|
+
|
38
|
+
if (!host) {
|
39
|
+
return '';
|
40
|
+
}
|
41
|
+
|
42
|
+
if (IP_V4_REGEX.test(host)) {
|
43
|
+
return '';
|
44
|
+
}
|
45
|
+
|
46
|
+
// 移除可能存在的端口号
|
47
|
+
const hostWithoutPort = host.split(':')[0];
|
48
|
+
const parts = hostWithoutPort.split('.');
|
49
|
+
|
50
|
+
if (parts.length === 1) {
|
51
|
+
return '';
|
52
|
+
}
|
53
|
+
|
54
|
+
// 二段式顶级域名
|
55
|
+
if (parts.length > 2) {
|
56
|
+
const tld = parts.slice(-2).join('.');
|
57
|
+
|
58
|
+
if (isTwoSegmentTLD(tld)) {
|
59
|
+
return `.${parts.slice(-3).join('.')}`;
|
60
|
+
}
|
61
|
+
}
|
62
|
+
|
63
|
+
return `.${parts.slice(-2).join('.')}`;
|
64
|
+
}
|
12
65
|
|
13
66
|
export function parseQuery(str: string) {
|
14
67
|
return str
|