|
@@ -1,6 +1,7 @@
|
|
|
import axios from 'axios'
|
|
|
-import { delayPerform } from 'js-perform-lock'
|
|
|
+import { delayPerform, queuePerform } from 'js-perform-lock'
|
|
|
|
|
|
+// 数据过滤校验
|
|
|
function isNumberRegex(str) {
|
|
|
const pureNumberRegex = /^\d+$/
|
|
|
const floatNumberRegex = /^\d*\.\d+$/
|
|
@@ -34,51 +35,62 @@ function isNumberRegex(str) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+// 文本转译请求
|
|
|
+function pushNewText(data) {
|
|
|
+ return new Promise(function (r, j) {
|
|
|
+ axios
|
|
|
+ .post('https://jiasm.zfire.top/translate/api/v1/common/translationOfText', data)
|
|
|
+ .then(response => {
|
|
|
+ if (response.data.code === 0) {
|
|
|
+ var obj = {}
|
|
|
+ response.data.data.map(val => {
|
|
|
+ obj[val[0]] = val[1]
|
|
|
+ })
|
|
|
+ window.Vue_Translation_Of_Text_Data = { ...(window.Vue_Translation_Of_Text_Data || {}), ...obj }
|
|
|
+ r({ ...obj })
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .catch(j)
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
var deferredReplacement = (function () {
|
|
|
+ var allKeywords = []
|
|
|
var keywords = []
|
|
|
var zhixingfun = []
|
|
|
var index = 0
|
|
|
var index2 = 0
|
|
|
|
|
|
- function pushNewText(data) {
|
|
|
- return new Promise(function (r, j) {
|
|
|
- axios
|
|
|
- .post('https://jiasm.zfire.top/translate/api/v1/common/translationOfText', data)
|
|
|
- .then(response => {
|
|
|
- if (response.data.code === 0) {
|
|
|
- var obj = {}
|
|
|
- response.data.data.map(val => {
|
|
|
- obj[val[0]] = val[1]
|
|
|
- })
|
|
|
- window.Vue_Translation_Of_Text_Data = { ...(window.Vue_Translation_Of_Text_Data || {}), ...obj }
|
|
|
- r({ ...obj })
|
|
|
- }
|
|
|
- })
|
|
|
- .catch(j)
|
|
|
- })
|
|
|
- }
|
|
|
-
|
|
|
- const d = new delayPerform(250).refactor(function (/**可接收参数**/) {
|
|
|
+ const dInit = new delayPerform(250).refactor(function () {
|
|
|
let len = index
|
|
|
let len2 = index2
|
|
|
- pushNewText({
|
|
|
- keywords: [...keywords],
|
|
|
- targetLanguage: window?.Vue_Translation_Of_Text_Type
|
|
|
- }).then(data => {
|
|
|
- for (var i = 0; i < len2; i++) {
|
|
|
- zhixingfun[i]()
|
|
|
- }
|
|
|
- keywords.splice(0, len)
|
|
|
- zhixingfun.splice(0, len2)
|
|
|
- index -= len
|
|
|
- index2 -= len2
|
|
|
- })
|
|
|
+ let keywords_ = keywords.splice(0, len)
|
|
|
+ let zhixingfun_ = zhixingfun.splice(0, len2)
|
|
|
+ index -= len
|
|
|
+ index2 -= len2
|
|
|
+ if (keywords_.length > 0) {
|
|
|
+ pushNewText({
|
|
|
+ keywords: keywords_,
|
|
|
+ targetLanguage: window?.Vue_Translation_Of_Text_Type
|
|
|
+ }).then(data => {
|
|
|
+ for (var i = 0; i < len2; i++) {
|
|
|
+ zhixingfun_?.[i]?.()
|
|
|
+ }
|
|
|
+ })
|
|
|
+ } else if (zhixingfun_.length > 0) {
|
|
|
+ setTimeout(function () {
|
|
|
+ for (var i = 0; i < len2; i++) {
|
|
|
+ zhixingfun_?.[i]?.()
|
|
|
+ }
|
|
|
+ }, 250)
|
|
|
+ }
|
|
|
})
|
|
|
|
|
|
return function (text, cb) {
|
|
|
- d()
|
|
|
- if (!keywords.includes(text)) {
|
|
|
+ dInit()
|
|
|
+ if (!allKeywords.includes(text)) {
|
|
|
index++
|
|
|
+ allKeywords.push(text)
|
|
|
keywords.push(text)
|
|
|
}
|
|
|
index2++
|
|
@@ -139,12 +151,43 @@ export default {
|
|
|
}
|
|
|
} catch (error) {}
|
|
|
}
|
|
|
+ } else if (child?.classList?.contains?.('el-select')) {
|
|
|
+ // 处理 class 为 el-select 的组件
|
|
|
+ this.processSelect(child)
|
|
|
} else {
|
|
|
// 递归处理子节点
|
|
|
this.processTextNodes(child)
|
|
|
}
|
|
|
})
|
|
|
}
|
|
|
+ },
|
|
|
+ processSelect(el) {
|
|
|
+ // 遍历子节点并处理文本内容
|
|
|
+ if (el.nodeType === Node.ELEMENT_NODE) {
|
|
|
+ Array.from(el.childNodes).forEach(child => {
|
|
|
+ if (child.nodeName === 'INPUT') {
|
|
|
+ // 修改输入框和下拉框的 placeholder 内容
|
|
|
+ let value = child.value
|
|
|
+ let _child = child
|
|
|
+ if (value && !isNumberRegex(value.trim())) {
|
|
|
+ try {
|
|
|
+ if (window?.Vue_Translation_Of_Text_Data?.[value.trim() + '']) {
|
|
|
+ child.value = window?.Vue_Translation_Of_Text_Data?.[value.trim() + '']
|
|
|
+ } else if (value.trim() + '') {
|
|
|
+ deferredReplacement(value.trim() + '', function () {
|
|
|
+ if (window?.Vue_Translation_Of_Text_Data?.[value.trim() + '']) {
|
|
|
+ _child.value = window?.Vue_Translation_Of_Text_Data?.[value.trim() + '']
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+ } catch (error) {}
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 递归处理子节点
|
|
|
+ this.processSelect(child)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
})
|