isdata-customer-sdk 0.1.95 → 0.1.97
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/index.common.js +264 -0
- package/dist/index.common.js.map +1 -1
- package/dist/index.umd.js +264 -0
- package/dist/index.umd.js.map +1 -1
- package/dist/index.umd.min.js +3 -3
- package/dist/index.umd.min.js.map +1 -1
- package/package.json +1 -1
package/dist/index.umd.js
CHANGED
|
@@ -29615,6 +29615,7 @@ __webpack_require__.d(__webpack_exports__, {
|
|
|
29615
29615
|
getUserID: function() { return /* reexport */ getUserID; },
|
|
29616
29616
|
hasListener: function() { return /* reexport */ hasListener; },
|
|
29617
29617
|
i18n: function() { return /* reexport */ i18n; },
|
|
29618
|
+
initDomNodeI18NObserver: function() { return /* reexport */ initDomNodeI18NObserver; },
|
|
29618
29619
|
initEventCenter: function() { return /* reexport */ initEventCenter; },
|
|
29619
29620
|
initFrameWindowListener: function() { return /* reexport */ initFrameWindowListener; },
|
|
29620
29621
|
loadi18nTexts: function() { return /* reexport */ loadi18nTexts; },
|
|
@@ -33097,6 +33098,10 @@ const dify_extractFilenameFromUrl = url => {
|
|
|
33097
33098
|
};
|
|
33098
33099
|
;// ./src/api/i18n.js
|
|
33099
33100
|
|
|
33101
|
+
|
|
33102
|
+
|
|
33103
|
+
|
|
33104
|
+
|
|
33100
33105
|
const getLanguages = async appID => {
|
|
33101
33106
|
let queryData = {
|
|
33102
33107
|
param: {
|
|
@@ -33124,6 +33129,265 @@ const loadi18nTexts = async appID => {
|
|
|
33124
33129
|
window.customI18nObject.set(`${appID}-${item.key}-${item.lang}`, item.value);
|
|
33125
33130
|
}
|
|
33126
33131
|
};
|
|
33132
|
+
|
|
33133
|
+
/**
|
|
33134
|
+
* 检查元素是否有子DOM元素
|
|
33135
|
+
* @param {HTMLElement} element - 要检查的DOM元素
|
|
33136
|
+
* @returns {boolean} - 是否有子元素
|
|
33137
|
+
*/
|
|
33138
|
+
function hasChildElements(element) {
|
|
33139
|
+
return element.children.length > 0;
|
|
33140
|
+
}
|
|
33141
|
+
|
|
33142
|
+
/**
|
|
33143
|
+
* 检查文本是否匹配$L{...}格式
|
|
33144
|
+
* @param {string} text - 要检查的文本
|
|
33145
|
+
* @returns {Object|null} - 匹配结果或null
|
|
33146
|
+
*/
|
|
33147
|
+
function checkTextFormat(text) {
|
|
33148
|
+
if (!text) return null;
|
|
33149
|
+
const regex = /\$L\{([^}]+)\}/;
|
|
33150
|
+
const match = text.match(regex);
|
|
33151
|
+
if (match) {
|
|
33152
|
+
return {
|
|
33153
|
+
fullMatch: match[0],
|
|
33154
|
+
key: match[1]
|
|
33155
|
+
};
|
|
33156
|
+
}
|
|
33157
|
+
return null;
|
|
33158
|
+
}
|
|
33159
|
+
function generateUniqueId() {
|
|
33160
|
+
window.idCounter = window.idCounter || 0;
|
|
33161
|
+
return `i18n_${Date.now()}_${window.idCounter++}`;
|
|
33162
|
+
}
|
|
33163
|
+
|
|
33164
|
+
// 处理单个DOM元素的函数
|
|
33165
|
+
function processElement(element) {
|
|
33166
|
+
if (element.nodeType != 1) {
|
|
33167
|
+
return false;
|
|
33168
|
+
}
|
|
33169
|
+
// 如果元素有子元素,则不处理innerHTML
|
|
33170
|
+
const hasChildren = hasChildElements(element);
|
|
33171
|
+
if (hasChildren) {
|
|
33172
|
+
let children = element.children;
|
|
33173
|
+
for (let i = 0; i < children.length; i++) {
|
|
33174
|
+
processElement(children[i]);
|
|
33175
|
+
}
|
|
33176
|
+
// 检查是否是input元素
|
|
33177
|
+
const isButtonElement = element.tagName === "BUTTON";
|
|
33178
|
+
if (!isButtonElement) {
|
|
33179
|
+
return false;
|
|
33180
|
+
}
|
|
33181
|
+
}
|
|
33182
|
+
// element.setAttribute("i18nProcessed", "true"); // 标记元素已被处理
|
|
33183
|
+
// 检查是否是input元素
|
|
33184
|
+
const isInputElement = element.tagName === "INPUT";
|
|
33185
|
+
// 检查placeholder是否匹配
|
|
33186
|
+
const placeholder = element.getAttribute("placeholder") || element.placeholder;
|
|
33187
|
+
const placeholderMatch = isInputElement && placeholder ? checkTextFormat(placeholder) : null;
|
|
33188
|
+
|
|
33189
|
+
// 检查value是否匹配(仅对按钮类型的input)
|
|
33190
|
+
const value = element.getAttribute("value") || element.value;
|
|
33191
|
+
const valueMatch = isInputElement && value ? checkTextFormat(value) : null;
|
|
33192
|
+
|
|
33193
|
+
// 检查innerHTML是否匹配(仅对没有子元素的元素)
|
|
33194
|
+
let innerHTMLMatch = null;
|
|
33195
|
+
// if (!hasChildren) {
|
|
33196
|
+
const innerHTML = element.innerHTML;
|
|
33197
|
+
innerHTMLMatch = checkTextFormat(innerHTML);
|
|
33198
|
+
// }
|
|
33199
|
+
|
|
33200
|
+
// 如果没有匹配的内容,则不处理
|
|
33201
|
+
if (!placeholderMatch && !valueMatch && !innerHTMLMatch) {
|
|
33202
|
+
return false;
|
|
33203
|
+
}
|
|
33204
|
+
const elementId = generateUniqueId();
|
|
33205
|
+
element.setAttribute("localDomID", elementId);
|
|
33206
|
+
console.log("处理元素:", element, {
|
|
33207
|
+
placeholderMatch,
|
|
33208
|
+
valueMatch,
|
|
33209
|
+
innerHTMLMatch
|
|
33210
|
+
});
|
|
33211
|
+
// 处理placeholder
|
|
33212
|
+
if (placeholderMatch) {
|
|
33213
|
+
element.setAttribute("localPlaceholderKey", placeholderMatch.key);
|
|
33214
|
+
element.setAttribute("oldPlaceholderValue", placeholder);
|
|
33215
|
+
}
|
|
33216
|
+
|
|
33217
|
+
// 处理value
|
|
33218
|
+
if (valueMatch) {
|
|
33219
|
+
element.setAttribute("localValueKey", valueMatch.key);
|
|
33220
|
+
element.setAttribute("oldValueValue", value);
|
|
33221
|
+
}
|
|
33222
|
+
|
|
33223
|
+
// 处理innerHTML(仅对没有子元素的元素)
|
|
33224
|
+
if (innerHTMLMatch) {
|
|
33225
|
+
element.setAttribute("localKey", innerHTMLMatch.key);
|
|
33226
|
+
element.setAttribute("oldValue", element.innerHTML);
|
|
33227
|
+
}
|
|
33228
|
+
|
|
33229
|
+
// 添加到Map缓存
|
|
33230
|
+
window.i18nElementsMap.set(elementId, {
|
|
33231
|
+
dom: element,
|
|
33232
|
+
id: elementId,
|
|
33233
|
+
key: innerHTMLMatch ? innerHTMLMatch.key : null,
|
|
33234
|
+
placeholderKey: placeholderMatch ? placeholderMatch.key : null,
|
|
33235
|
+
valueKey: valueMatch ? valueMatch.key : null,
|
|
33236
|
+
oldValue: innerHTMLMatch ? element.innerHTML : null,
|
|
33237
|
+
oldPlaceholderValue: placeholderMatch ? placeholder : null,
|
|
33238
|
+
oldValueValue: valueMatch ? value : null,
|
|
33239
|
+
isInputElement: isInputElement
|
|
33240
|
+
// hasChildren: hasChildren,
|
|
33241
|
+
});
|
|
33242
|
+
applyTranslation(element);
|
|
33243
|
+
return true;
|
|
33244
|
+
}
|
|
33245
|
+
function unProcessElement(node) {
|
|
33246
|
+
if (node.nodeType !== 1) {
|
|
33247
|
+
return;
|
|
33248
|
+
}
|
|
33249
|
+
const hasChildren = hasChildElements(element);
|
|
33250
|
+
if (hasChildren) {
|
|
33251
|
+
let children = element.children;
|
|
33252
|
+
for (let i = 0; i < children.length; i++) {
|
|
33253
|
+
unProcessElement(children[i]);
|
|
33254
|
+
}
|
|
33255
|
+
}
|
|
33256
|
+
const localDomID = node.getAttribute("localDomID");
|
|
33257
|
+
if (localDomID && window.i18nElementsMap.has(localDomID)) {
|
|
33258
|
+
window.i18nElementsMap.delete(localDomID);
|
|
33259
|
+
console.log("移除元素缓存:", localDomID);
|
|
33260
|
+
}
|
|
33261
|
+
}
|
|
33262
|
+
|
|
33263
|
+
/**
|
|
33264
|
+
* 应用翻译到单个元素
|
|
33265
|
+
* @param {HTMLElement} element - 要更新的DOM元素
|
|
33266
|
+
* @param {string} lang - 语言标识符
|
|
33267
|
+
*/
|
|
33268
|
+
const applyTranslation = async element => {
|
|
33269
|
+
let lang = window.localStorage.getItem("iportal_localID") || "zh-CN";
|
|
33270
|
+
let appID = getPoratlAppID();
|
|
33271
|
+
const elementId = element.getAttribute("localDomID");
|
|
33272
|
+
const cachedItem = window.i18nElementsMap.get(elementId);
|
|
33273
|
+
if (!cachedItem) return;
|
|
33274
|
+
// 处理placeholder(如果是input元素)
|
|
33275
|
+
if (cachedItem.isInputElement && cachedItem.placeholderKey) {
|
|
33276
|
+
const placeholderKey = element.getAttribute("localPlaceholderKey");
|
|
33277
|
+
const oldPlaceholderValue = element.getAttribute("oldPlaceholderValue");
|
|
33278
|
+
if (placeholderKey && oldPlaceholderValue) {
|
|
33279
|
+
// 创建正则表达式匹配所有 $L{...} 格式
|
|
33280
|
+
const regex = /\$L\{([^}]+)\}/g;
|
|
33281
|
+
// 替换所有 $L{...} 占位符为翻译文本
|
|
33282
|
+
let newPlaceholderValue = oldPlaceholderValue;
|
|
33283
|
+
let match;
|
|
33284
|
+
while ((match = regex.exec(oldPlaceholderValue)) !== null) {
|
|
33285
|
+
const fullMatch = match[0];
|
|
33286
|
+
const placeholderKey = match[1];
|
|
33287
|
+
const translation = i18n(placeholderKey, appID, lang);
|
|
33288
|
+
newPlaceholderValue = newPlaceholderValue.replace(fullMatch, translation);
|
|
33289
|
+
}
|
|
33290
|
+
// 更新元素的placeholder属性
|
|
33291
|
+
element.setAttribute("placeholder", newPlaceholderValue);
|
|
33292
|
+
}
|
|
33293
|
+
}
|
|
33294
|
+
|
|
33295
|
+
// 处理value(如果是input元素)
|
|
33296
|
+
if (cachedItem.isInputElement && cachedItem.valueKey) {
|
|
33297
|
+
const valueKey = element.getAttribute("localValueKey");
|
|
33298
|
+
const oldValueValue = element.getAttribute("oldValueValue");
|
|
33299
|
+
if (valueKey && oldValueValue) {
|
|
33300
|
+
// 创建正则表达式匹配所有 $L{...} 格式
|
|
33301
|
+
const regex = /\$L\{([^}]+)\}/g;
|
|
33302
|
+
|
|
33303
|
+
// 替换所有 $L{...} 占位符为翻译文本
|
|
33304
|
+
let newValueValue = oldValueValue;
|
|
33305
|
+
let match;
|
|
33306
|
+
while ((match = regex.exec(oldValueValue)) !== null) {
|
|
33307
|
+
const fullMatch = match[0];
|
|
33308
|
+
const valueKey = match[1];
|
|
33309
|
+
const translation = i18n(valueKey, appID, lang);
|
|
33310
|
+
newValueValue = newValueValue.replace(fullMatch, translation);
|
|
33311
|
+
}
|
|
33312
|
+
|
|
33313
|
+
// 更新元素的value属性
|
|
33314
|
+
element.setAttribute("value", newValueValue);
|
|
33315
|
+
}
|
|
33316
|
+
}
|
|
33317
|
+
|
|
33318
|
+
// 处理innerHTML
|
|
33319
|
+
if (cachedItem.key) {
|
|
33320
|
+
const key = element.getAttribute("localKey");
|
|
33321
|
+
const oldValue = element.getAttribute("oldValue");
|
|
33322
|
+
if (key && oldValue) {
|
|
33323
|
+
// 创建正则表达式匹配所有 $L{...} 格式
|
|
33324
|
+
const regex = /\$L\{([^}]+)\}/g;
|
|
33325
|
+
|
|
33326
|
+
// 替换所有 $L{...} 占位符为翻译文本
|
|
33327
|
+
let newValue = oldValue;
|
|
33328
|
+
let match;
|
|
33329
|
+
while ((match = regex.exec(oldValue)) !== null) {
|
|
33330
|
+
const fullMatch = match[0];
|
|
33331
|
+
const placeholderKey = match[1];
|
|
33332
|
+
const translation = i18n(placeholderKey, appID, lang);
|
|
33333
|
+
newValue = newValue.replace(fullMatch, translation);
|
|
33334
|
+
}
|
|
33335
|
+
|
|
33336
|
+
// 更新元素的innerHTML
|
|
33337
|
+
element.innerHTML = newValue;
|
|
33338
|
+
}
|
|
33339
|
+
}
|
|
33340
|
+
};
|
|
33341
|
+
const initDomNodeI18NObserver = () => {
|
|
33342
|
+
if (!window.i18nElementsMap) {
|
|
33343
|
+
window.i18nElementsMap = new Map();
|
|
33344
|
+
} else {
|
|
33345
|
+
window.i18nElementsMap.clear();
|
|
33346
|
+
}
|
|
33347
|
+
registerEventListener("IPORTAL_LANGUAGE_CHANGE_EVENT", async lang => {
|
|
33348
|
+
console.log("语言切换事件触发,更新已处理元素的翻译:", lang);
|
|
33349
|
+
// 遍历Map,更新每个已处理元素的翻译
|
|
33350
|
+
for (const [elementId, item] of window.i18nElementsMap.entries()) {
|
|
33351
|
+
applyTranslation(item.dom);
|
|
33352
|
+
}
|
|
33353
|
+
});
|
|
33354
|
+
// 创建观察器实例
|
|
33355
|
+
const observer = new MutationObserver(mutations => {
|
|
33356
|
+
mutations.forEach(mutation => {
|
|
33357
|
+
// 节点添加
|
|
33358
|
+
if (mutation.addedNodes.length > 0) {
|
|
33359
|
+
// console.log("检测到新增节点:", mutation.addedNodes);
|
|
33360
|
+
for (let i = 0; i < mutation.addedNodes.length; i++) {
|
|
33361
|
+
const node = mutation.addedNodes[i];
|
|
33362
|
+
processElement(node);
|
|
33363
|
+
}
|
|
33364
|
+
}
|
|
33365
|
+
|
|
33366
|
+
// 节点移除
|
|
33367
|
+
if (mutation.removedNodes.length > 0) {
|
|
33368
|
+
for (let i = 0; i < mutation.removedNodes.length; i++) {
|
|
33369
|
+
const node = mutation.removedNodes[i];
|
|
33370
|
+
unProcessElement(node);
|
|
33371
|
+
}
|
|
33372
|
+
}
|
|
33373
|
+
});
|
|
33374
|
+
});
|
|
33375
|
+
|
|
33376
|
+
// 配置观察选项
|
|
33377
|
+
const config = {
|
|
33378
|
+
childList: true,
|
|
33379
|
+
// 观察子节点变化
|
|
33380
|
+
subtree: true,
|
|
33381
|
+
// 观察所有后代节点
|
|
33382
|
+
attributes: false,
|
|
33383
|
+
// 不观察属性变化
|
|
33384
|
+
characterData: false // 不观察文本内容变化
|
|
33385
|
+
};
|
|
33386
|
+
// 开始观察目标节点
|
|
33387
|
+
const targetNode = document.body;
|
|
33388
|
+
processElement(targetNode);
|
|
33389
|
+
observer.observe(targetNode, config);
|
|
33390
|
+
};
|
|
33127
33391
|
;// ./src/main.js
|
|
33128
33392
|
|
|
33129
33393
|
|