@jiyr0119/dsh-service-console 0.7.0 → 0.7.1
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/dsh.plugin.json +1 -1
- package/lib/client/i18n.js +59 -0
- package/lib/client.js +122 -71
- package/lib/index.js +40 -28
- package/lib/types/client/i18n.d.ts +30 -0
- package/manifest.json +1 -1
- package/package.json +1 -1
package/dsh.plugin.json
CHANGED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
//#region src/client/i18n.ts
|
|
2
|
+
/**
|
|
3
|
+
* 幂等注册词典:逐语种注册;单条失败(如 already has locale)跳过,
|
|
4
|
+
* 不影响其余语种与后续绑定。返回 disposer(整体可安全清理)。
|
|
5
|
+
*/
|
|
6
|
+
function registerDictionaries(locale, ns, dicts) {
|
|
7
|
+
const offs = [];
|
|
8
|
+
for (const pair of dicts) {
|
|
9
|
+
const code = pair[0];
|
|
10
|
+
const dict = pair[1];
|
|
11
|
+
try {
|
|
12
|
+
const off = locale.register(ns, code, dict);
|
|
13
|
+
if (typeof off === "function") offs.push(off);
|
|
14
|
+
} catch {}
|
|
15
|
+
}
|
|
16
|
+
return () => {
|
|
17
|
+
for (const off of offs) try {
|
|
18
|
+
off();
|
|
19
|
+
} catch {}
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* 绑定翻译函数:稳定外层 + 每次取最新 bound(旧版 bind 可能缓存语种)。
|
|
24
|
+
* 变量插值手工兜底(旧版 bound 可能不支持 params)。
|
|
25
|
+
*/
|
|
26
|
+
function bindTranslator(locale, ns) {
|
|
27
|
+
const make = () => {
|
|
28
|
+
let bound;
|
|
29
|
+
try {
|
|
30
|
+
bound = locale.bind(ns);
|
|
31
|
+
} catch {
|
|
32
|
+
bound = (key) => key;
|
|
33
|
+
}
|
|
34
|
+
return (key, params) => {
|
|
35
|
+
let s;
|
|
36
|
+
try {
|
|
37
|
+
s = bound(key);
|
|
38
|
+
} catch {
|
|
39
|
+
s = key;
|
|
40
|
+
}
|
|
41
|
+
if (typeof s !== "string" || s === "") s = key;
|
|
42
|
+
if (params) for (const k of Object.keys(params)) s = s.split("{" + k + "}").join(String(params[k]));
|
|
43
|
+
return s;
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
let t = make();
|
|
47
|
+
return {
|
|
48
|
+
t: (key, params) => t(key, params),
|
|
49
|
+
subscribe: (fn) => {
|
|
50
|
+
const off = locale.subscribe(() => {
|
|
51
|
+
t = make();
|
|
52
|
+
fn();
|
|
53
|
+
});
|
|
54
|
+
return typeof off === "function" ? off : () => {};
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
//#endregion
|
|
59
|
+
export { bindTranslator, registerDictionaries };
|
package/lib/client.js
CHANGED
|
@@ -40,76 +40,76 @@ window.__ModuleLoader__.load({
|
|
|
40
40
|
document.head.appendChild(tag);
|
|
41
41
|
}
|
|
42
42
|
var service_console_module_css_default = {
|
|
43
|
-
"
|
|
44
|
-
"
|
|
45
|
-
"
|
|
43
|
+
"tabBody": "PuSt5a_tabBody",
|
|
44
|
+
"rowName": "PuSt5a_rowName",
|
|
45
|
+
"noteWarn": "PuSt5a_noteWarn",
|
|
46
46
|
"headerBtnOn": "PuSt5a_headerBtnOn",
|
|
47
|
-
"
|
|
48
|
-
"
|
|
49
|
-
"
|
|
50
|
-
"
|
|
51
|
-
"
|
|
52
|
-
"
|
|
53
|
-
"
|
|
54
|
-
"empty": "PuSt5a_empty",
|
|
55
|
-
"tabOn": "PuSt5a_tabOn",
|
|
56
|
-
"starLabel": "PuSt5a_starLabel",
|
|
57
|
-
"sc-spin": "PuSt5a_sc-spin",
|
|
58
|
-
"hintWarn": "PuSt5a_hintWarn",
|
|
47
|
+
"filter": "PuSt5a_filter",
|
|
48
|
+
"confirmRow": "PuSt5a_confirmRow",
|
|
49
|
+
"detailAccent": "PuSt5a_detailAccent",
|
|
50
|
+
"filterRow": "PuSt5a_filterRow",
|
|
51
|
+
"segGroup": "PuSt5a_segGroup",
|
|
52
|
+
"starLink": "PuSt5a_starLink",
|
|
53
|
+
"setCap": "PuSt5a_setCap",
|
|
59
54
|
"tabInd": "PuSt5a_tabInd",
|
|
60
|
-
"
|
|
55
|
+
"hintWarn": "PuSt5a_hintWarn",
|
|
61
56
|
"confirmTxt": "PuSt5a_confirmTxt",
|
|
62
57
|
"detailHead": "PuSt5a_detailHead",
|
|
63
|
-
"
|
|
64
|
-
"
|
|
65
|
-
"setRow": "PuSt5a_setRow",
|
|
66
|
-
"starInfo": "PuSt5a_starInfo",
|
|
67
|
-
"segBtnOn": "PuSt5a_segBtnOn",
|
|
68
|
-
"layer": "PuSt5a_layer",
|
|
58
|
+
"rowDot": "PuSt5a_rowDot",
|
|
59
|
+
"setSection": "PuSt5a_setSection",
|
|
69
60
|
"popup": "PuSt5a_popup",
|
|
70
|
-
"
|
|
71
|
-
"tabBody": "PuSt5a_tabBody",
|
|
72
|
-
"tabs": "PuSt5a_tabs",
|
|
73
|
-
"badge": "PuSt5a_badge",
|
|
74
|
-
"noteWarn": "PuSt5a_noteWarn",
|
|
75
|
-
"icoBtn": "PuSt5a_icoBtn",
|
|
76
|
-
"evidenceArrow": "PuSt5a_evidenceArrow",
|
|
61
|
+
"setInput": "PuSt5a_setInput",
|
|
77
62
|
"segBtn": "PuSt5a_segBtn",
|
|
78
|
-
"starLink": "PuSt5a_starLink",
|
|
79
|
-
"rowMain": "PuSt5a_rowMain",
|
|
80
63
|
"headIco": "PuSt5a_headIco",
|
|
81
|
-
"
|
|
82
|
-
"
|
|
83
|
-
"
|
|
64
|
+
"detailLabel": "PuSt5a_detailLabel",
|
|
65
|
+
"icoBtn": "PuSt5a_icoBtn",
|
|
66
|
+
"tab": "PuSt5a_tab",
|
|
67
|
+
"rowMain": "PuSt5a_rowMain",
|
|
68
|
+
"setPage": "PuSt5a_setPage",
|
|
84
69
|
"switch": "PuSt5a_switch",
|
|
85
|
-
"
|
|
70
|
+
"rowSub": "PuSt5a_rowSub",
|
|
71
|
+
"badge": "PuSt5a_badge",
|
|
72
|
+
"spin": "PuSt5a_spin",
|
|
73
|
+
"confirmCancel": "PuSt5a_confirmCancel",
|
|
74
|
+
"empty": "PuSt5a_empty",
|
|
75
|
+
"setSelect": "PuSt5a_setSelect",
|
|
76
|
+
"segBtnOn": "PuSt5a_segBtnOn",
|
|
77
|
+
"tabs": "PuSt5a_tabs",
|
|
78
|
+
"setInfo": "PuSt5a_setInfo",
|
|
79
|
+
"popupOn": "PuSt5a_popupOn",
|
|
80
|
+
"tabOn": "PuSt5a_tabOn",
|
|
81
|
+
"confirmOk": "PuSt5a_confirmOk",
|
|
82
|
+
"spacer": "PuSt5a_spacer",
|
|
83
|
+
"setNote": "PuSt5a_setNote",
|
|
84
|
+
"detailGrid": "PuSt5a_detailGrid",
|
|
85
|
+
"detailBox": "PuSt5a_detailBox",
|
|
86
86
|
"rowBtn": "PuSt5a_rowBtn",
|
|
87
|
+
"starInfo": "PuSt5a_starInfo",
|
|
88
|
+
"row": "PuSt5a_row",
|
|
89
|
+
"hintLine": "PuSt5a_hintLine",
|
|
90
|
+
"okText": "PuSt5a_okText",
|
|
91
|
+
"sc-spin": "PuSt5a_sc-spin",
|
|
92
|
+
"prevBtn": "PuSt5a_prevBtn",
|
|
93
|
+
"setRow": "PuSt5a_setRow",
|
|
94
|
+
"layer": "PuSt5a_layer",
|
|
95
|
+
"rowMeta": "PuSt5a_rowMeta",
|
|
87
96
|
"rowAct": "PuSt5a_rowAct",
|
|
97
|
+
"starSection": "PuSt5a_starSection",
|
|
98
|
+
"evidenceBlock": "PuSt5a_evidenceBlock",
|
|
88
99
|
"evidenceItem": "PuSt5a_evidenceItem",
|
|
89
|
-
"rowSub": "PuSt5a_rowSub",
|
|
90
|
-
"row": "PuSt5a_row",
|
|
91
|
-
"setCap": "PuSt5a_setCap",
|
|
92
|
-
"confirmOk": "PuSt5a_confirmOk",
|
|
93
100
|
"liveDot": "PuSt5a_liveDot",
|
|
94
|
-
"filterRow": "PuSt5a_filterRow",
|
|
95
|
-
"detailAccent": "PuSt5a_detailAccent",
|
|
96
|
-
"detailBox": "PuSt5a_detailBox",
|
|
97
|
-
"headTitle": "PuSt5a_headTitle",
|
|
98
|
-
"popupOn": "PuSt5a_popupOn",
|
|
99
101
|
"setLabel": "PuSt5a_setLabel",
|
|
100
|
-
"
|
|
101
|
-
"
|
|
102
|
-
"
|
|
103
|
-
"
|
|
102
|
+
"evidenceArrow": "PuSt5a_evidenceArrow",
|
|
103
|
+
"starRow": "PuSt5a_starRow",
|
|
104
|
+
"note": "PuSt5a_note",
|
|
105
|
+
"setFooter": "PuSt5a_setFooter",
|
|
106
|
+
"headerBtn": "PuSt5a_headerBtn",
|
|
107
|
+
"noteErr": "PuSt5a_noteErr",
|
|
108
|
+
"starLabel": "PuSt5a_starLabel",
|
|
109
|
+
"headTitle": "PuSt5a_headTitle",
|
|
104
110
|
"head": "PuSt5a_head",
|
|
105
|
-
"
|
|
106
|
-
"
|
|
107
|
-
"spacer": "PuSt5a_spacer",
|
|
108
|
-
"tab": "PuSt5a_tab",
|
|
109
|
-
"setNote": "PuSt5a_setNote",
|
|
110
|
-
"hintLine": "PuSt5a_hintLine",
|
|
111
|
-
"spin": "PuSt5a_spin",
|
|
112
|
-
"filterClear": "PuSt5a_filterClear"
|
|
111
|
+
"filterClear": "PuSt5a_filterClear",
|
|
112
|
+
"errText": "PuSt5a_errText"
|
|
113
113
|
};
|
|
114
114
|
//#endregion
|
|
115
115
|
//#region src/client/ServiceConsole.tsx
|
|
@@ -1030,6 +1030,64 @@ window.__ModuleLoader__.load({
|
|
|
1030
1030
|
});
|
|
1031
1031
|
}
|
|
1032
1032
|
//#endregion
|
|
1033
|
+
//#region src/client/i18n.ts
|
|
1034
|
+
/**
|
|
1035
|
+
* 幂等注册词典:逐语种注册;单条失败(如 already has locale)跳过,
|
|
1036
|
+
* 不影响其余语种与后续绑定。返回 disposer(整体可安全清理)。
|
|
1037
|
+
*/
|
|
1038
|
+
function registerDictionaries(locale, ns, dicts) {
|
|
1039
|
+
const offs = [];
|
|
1040
|
+
for (const pair of dicts) {
|
|
1041
|
+
const code = pair[0];
|
|
1042
|
+
const dict = pair[1];
|
|
1043
|
+
try {
|
|
1044
|
+
const off = locale.register(ns, code, dict);
|
|
1045
|
+
if (typeof off === "function") offs.push(off);
|
|
1046
|
+
} catch {}
|
|
1047
|
+
}
|
|
1048
|
+
return () => {
|
|
1049
|
+
for (const off of offs) try {
|
|
1050
|
+
off();
|
|
1051
|
+
} catch {}
|
|
1052
|
+
};
|
|
1053
|
+
}
|
|
1054
|
+
/**
|
|
1055
|
+
* 绑定翻译函数:稳定外层 + 每次取最新 bound(旧版 bind 可能缓存语种)。
|
|
1056
|
+
* 变量插值手工兜底(旧版 bound 可能不支持 params)。
|
|
1057
|
+
*/
|
|
1058
|
+
function bindTranslator(locale, ns) {
|
|
1059
|
+
const make = () => {
|
|
1060
|
+
let bound;
|
|
1061
|
+
try {
|
|
1062
|
+
bound = locale.bind(ns);
|
|
1063
|
+
} catch {
|
|
1064
|
+
bound = (key) => key;
|
|
1065
|
+
}
|
|
1066
|
+
return (key, params) => {
|
|
1067
|
+
let s;
|
|
1068
|
+
try {
|
|
1069
|
+
s = bound(key);
|
|
1070
|
+
} catch {
|
|
1071
|
+
s = key;
|
|
1072
|
+
}
|
|
1073
|
+
if (typeof s !== "string" || s === "") s = key;
|
|
1074
|
+
if (params) for (const k of Object.keys(params)) s = s.split("{" + k + "}").join(String(params[k]));
|
|
1075
|
+
return s;
|
|
1076
|
+
};
|
|
1077
|
+
};
|
|
1078
|
+
let t = make();
|
|
1079
|
+
return {
|
|
1080
|
+
t: (key, params) => t(key, params),
|
|
1081
|
+
subscribe: (fn) => {
|
|
1082
|
+
const off = locale.subscribe(() => {
|
|
1083
|
+
t = make();
|
|
1084
|
+
fn();
|
|
1085
|
+
});
|
|
1086
|
+
return typeof off === "function" ? off : () => {};
|
|
1087
|
+
}
|
|
1088
|
+
};
|
|
1089
|
+
}
|
|
1090
|
+
//#endregion
|
|
1033
1091
|
//#region src/client/dictionaries.ts
|
|
1034
1092
|
/**
|
|
1035
1093
|
* dsh-service-console — i18n 词典(对齐 DSH locale.register 机制)
|
|
@@ -1195,23 +1253,16 @@ window.__ModuleLoader__.load({
|
|
|
1195
1253
|
}
|
|
1196
1254
|
//#endregion
|
|
1197
1255
|
module.exports = {
|
|
1198
|
-
inject: [
|
|
1199
|
-
"slots",
|
|
1200
|
-
"timer",
|
|
1201
|
-
"locale"
|
|
1202
|
-
],
|
|
1256
|
+
inject: ["slots", "timer"],
|
|
1203
1257
|
apply(ctx) {
|
|
1204
1258
|
bindTimer(ctx.timer);
|
|
1205
|
-
const locale = ctx.locale;
|
|
1206
|
-
|
|
1207
|
-
zh
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
t: labelT,
|
|
1213
|
-
subscribe: (fn) => locale.subscribe(fn)
|
|
1214
|
-
});
|
|
1259
|
+
const locale = ctx.get("locale");
|
|
1260
|
+
if (locale !== void 0) {
|
|
1261
|
+
ctx.effect(() => registerDictionaries(locale, NS, [["zh", ZH_DICT], ["en", EN_DICT]]));
|
|
1262
|
+
const bound = bindTranslator(locale, NS);
|
|
1263
|
+
labelT = bound.t;
|
|
1264
|
+
bindLocale(bound);
|
|
1265
|
+
}
|
|
1215
1266
|
ctx.effect(() => ctx.slots.inject("conversation.session.header.utilities", () => ctx.slots.register({
|
|
1216
1267
|
name: "conversation.session.header.utilities",
|
|
1217
1268
|
id: "dsh-service-console",
|
package/lib/index.js
CHANGED
|
@@ -145,40 +145,52 @@ var src_default = {
|
|
|
145
145
|
})
|
|
146
146
|
}
|
|
147
147
|
];
|
|
148
|
-
for (const route of routes) ctx.
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
148
|
+
for (const route of routes) ctx.effect(() => {
|
|
149
|
+
try {
|
|
150
|
+
return ctx.webServer.register({
|
|
151
|
+
kind: "exact",
|
|
152
|
+
path: route.path,
|
|
153
|
+
handler: async (req, res) => {
|
|
154
|
+
const body = await readJsonBody(req);
|
|
155
|
+
if (body === null) {
|
|
156
|
+
sendJson(res, 400, {
|
|
157
|
+
ok: false,
|
|
158
|
+
error: {
|
|
159
|
+
code: "BAD_REQUEST",
|
|
160
|
+
message: "invalid JSON body"
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
return;
|
|
159
164
|
}
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
message: String(e?.message || e)
|
|
165
|
+
try {
|
|
166
|
+
const result = await route.handler(body);
|
|
167
|
+
sendJson(res, result.ok ? 200 : 400, result);
|
|
168
|
+
} catch (e) {
|
|
169
|
+
sendJson(res, 500, {
|
|
170
|
+
ok: false,
|
|
171
|
+
error: {
|
|
172
|
+
code: "INTERNAL",
|
|
173
|
+
message: String(e?.message || e)
|
|
174
|
+
}
|
|
175
|
+
});
|
|
172
176
|
}
|
|
173
|
-
}
|
|
174
|
-
}
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
} catch (e) {
|
|
180
|
+
if (process.env.DSH_SC_DEBUG === "1") console.warn(`[dsh-sc] webServer route skip ${route.path}:`, String(e));
|
|
181
|
+
return () => {};
|
|
175
182
|
}
|
|
176
183
|
});
|
|
177
184
|
const tools = ctx.get("tools");
|
|
178
185
|
if (tools) ctx.effect(() => {
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
186
|
+
try {
|
|
187
|
+
const off = tools.register(buildServiceTool({ snapshot }));
|
|
188
|
+
if (process.env.DSH_SC_DEBUG === "1") console.log(`[dsh-sc] service_list registered: ${String(Boolean(tools.get("service_list")))}`);
|
|
189
|
+
return off;
|
|
190
|
+
} catch (e) {
|
|
191
|
+
if (process.env.DSH_SC_DEBUG === "1") console.warn("[dsh-sc] tools.register skip:", String(e));
|
|
192
|
+
return () => {};
|
|
193
|
+
}
|
|
182
194
|
});
|
|
183
195
|
else if (process.env.DSH_SC_DEBUG === "1") console.log("[dsh-sc] tools service absent — service_list not registered");
|
|
184
196
|
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* i18n 注册/绑定(热挂载与双装载幂等,0.7.1)
|
|
3
|
+
*
|
|
4
|
+
* 桌面 dshmarket 热更新会把插件装载两次:第二次 locale.register 抛
|
|
5
|
+
* "already has locale",webServer.register 抛 "duplicate exact route"。
|
|
6
|
+
* 0.5–0.7.0 的初始化在异常后直接放弃绑定,导致 UI 退回原始键。
|
|
7
|
+
* 这里把注册做成幂等(已注册即跳过),并保证无论注册是否抛错都完成绑定。
|
|
8
|
+
*/
|
|
9
|
+
import type { ConsoleKey } from './dictionaries';
|
|
10
|
+
/** 平台 locale 服务的最小兼容面(3 参式注册,各版本通用;对象式为 0.2.0+ 专属) */
|
|
11
|
+
export interface LocaleCompat {
|
|
12
|
+
register(ns: string, locale: string, dict: Record<string, string>): (() => void) | unknown;
|
|
13
|
+
bind(ns: string): (key: string, params?: Record<string, unknown>) => string;
|
|
14
|
+
subscribe(fn: () => void): (() => void) | void;
|
|
15
|
+
}
|
|
16
|
+
export type Translate = (key: ConsoleKey, params?: Record<string, unknown>) => string;
|
|
17
|
+
export interface BoundLocale {
|
|
18
|
+
t: Translate;
|
|
19
|
+
subscribe: (fn: () => void) => () => void;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* 幂等注册词典:逐语种注册;单条失败(如 already has locale)跳过,
|
|
23
|
+
* 不影响其余语种与后续绑定。返回 disposer(整体可安全清理)。
|
|
24
|
+
*/
|
|
25
|
+
export declare function registerDictionaries(locale: LocaleCompat, ns: string, dicts: ReadonlyArray<readonly [string, Record<string, string>]>): () => void;
|
|
26
|
+
/**
|
|
27
|
+
* 绑定翻译函数:稳定外层 + 每次取最新 bound(旧版 bind 可能缓存语种)。
|
|
28
|
+
* 变量插值手工兜底(旧版 bound 可能不支持 params)。
|
|
29
|
+
*/
|
|
30
|
+
export declare function bindTranslator(locale: LocaleCompat, ns: string): BoundLocale;
|
package/manifest.json
CHANGED
package/package.json
CHANGED