Optimize the Tag input box.

Former-commit-id: baafa404eb74901a2a1af98c0f64fb83310b6685
This commit is contained in:
Physton
2023-05-10 13:05:12 +08:00
parent 115f5ee9fa
commit a910a9fbe0
4 changed files with 88 additions and 8 deletions

View File

@@ -1 +1 @@
3b17c5383640d6f5ceb2797ae24583a03f801d0b
7bfac2db6858359f55e66e9858cdf9d2d313f6d3

View File

@@ -1 +1 @@
b71166182a10d2d00f63e8c2dcc264cb05767fff
176c4d30de42288fe6b821095dc99ade99240d4e

View File

@@ -162,7 +162,7 @@
</div>
</div>
<div class="btn-tag-extend">
<input v-if="tag.weightNum > 0" type="number" min="0.1" step="0.1"
<input type="number" min="0" step="0.1"
:value="tag.weightNum"
@change="onTagWeightNumChange(index, $event)">
<button type="button" v-tooltip="getLang('increase_weight_add_parentheses')"
@@ -574,7 +574,44 @@ export default {
},
onTagWeightNumChange(index, e) {
if (this.tags[index].weightNum === e.target.value) return
this.tags[index].weightNum = e.target.value
let weightNum = e.target.value
let value = this.tags[index].value
let localValue = this.tags[index].localValue
if (weightNum > 0) {
// 如果原来没有权重数,那么就加上权重数
if (!common.weightNumRegex.test(value)) {
// 如果原来有括号,就要加到括号内
let bracket = common.hasBrackets(value)
if (bracket) {
value = common.setLayers(value, 1, bracket[0], bracket[1], ':' + weightNum)
if (localValue !== '') localValue = common.setLayers(localValue, 1, bracket[0], bracket[1], ':' + weightNum)
} else {
value = value + ':' + weightNum
if (localValue !== '') localValue = localValue + ':' + weightNum
}
}
// 如果原来没有括号() [] {} <>,那么就加上括号
if (!common.hasBrackets(value)) {
value = common.setLayers(value, 1, '(', ')')
if (localValue !== '') localValue = common.setLayers(localValue, 1, '(', ')')
}
if (value !== this.tags[index].value) {
this.tags[index].value = value
if (localValue !== '') this.tags[index].localValue = localValue
this._setTag(this.tags[index])
}
} else {
// 如果原来的括号是<>那么最小权重数只能是0.1
const bracket = common.hasBrackets(value)
if (bracket[0] === '<' && bracket[1] === '>') {
weightNum = 0.1
} else {
// 移除权重数
this.tags[index].value = value.replace(common.weightNumRegex, '')
if (localValue !== '') this.tags[index].localValue = this.tags[index].localValue.replace(common.weightNumRegex, '')
}
}
this.tags[index].weightNum = weightNum
this.updateTags()
},
onDeleteTagClick(index) {
@@ -592,26 +629,34 @@ export default {
},
onIncWeightClick(index, num) {
let value = this.tags[index].value
let localValue = this.tags[index].localValue
value = common.setLayers(value, 0, '[', ']')
if (localValue !== '') localValue = common.setLayers(localValue, 0, '[', ']')
let incWeight = this.tags[index].incWeight
incWeight += num
if (incWeight < 0) incWeight = 0
this.tags[index].incWeight = incWeight
this.tags[index].decWeight = 0
value = common.setLayers(value, incWeight, '(', ')')
if (localValue !== '') localValue = common.setLayers(localValue, incWeight, '(', ')')
this.tags[index].value = value
if (localValue !== '') this.tags[index].localValue = localValue
this.updateTags()
},
onDecWeightClick(index, num) {
let value = this.tags[index].value
let localValue = this.tags[index].localValue
value = common.setLayers(value, 0, '(', ')')
if (localValue !== '') localValue = common.setLayers(localValue, 0, '(', ')')
let decWeight = this.tags[index].decWeight
decWeight += num
if (decWeight < 0) decWeight = 0
this.tags[index].incWeight = 0
this.tags[index].decWeight = decWeight
value = common.setLayers(value, decWeight, '[', ']')
if (localValue !== '') localValue = common.setLayers(localValue, decWeight, '[', ']')
this.tags[index].value = value
if (localValue !== '') this.tags[index].localValue = localValue
this.updateTags()
},
onTranslateToLocalClick(index) {

View File

@@ -3,6 +3,11 @@ export default {
weightNumRegexEN: /:\s*([0-9\.]+)/,
weightNumRegexCN: /\s*([0-9\.]+)/,
/**
* 替换标签
* @param text {string}
* @returns {*|string}
*/
replaceTag(text) {
if (typeof text !== "string") return text
if (text === "") return text
@@ -12,6 +17,11 @@ export default {
return text
},
/**
* 替换括号
* @param text
* @returns {*}
*/
replaceBrackets(text) {
const length = text.length
if (length === 0) return text
@@ -38,14 +48,38 @@ export default {
let start = text[0]
let end = text[length - 1]
if (typeof replaces[start] !== "undefined") {
text[0] = replaces[start]
text = replaces[start] + text.substring(1)
}
if (typeof replaces[end] !== "undefined") {
text[length - 1] = replaces[end]
text = text.substring(0, length - 1) + replaces[end]
}
return text
},
/**
* 是否有括号
* @param text {string}
* @returns {Array|boolean}
*/
hasBrackets(text) {
const length = text.length
if (length === 0) return false
const brackets = [[
'(', ')'],
['[', ']'],
['{', '}'],
['<', '>']
]
let start = text[0]
let end = text[length - 1]
for (const bracket of brackets) {
if (bracket[0] === start && bracket[1] === end) {
return bracket
}
}
return false
},
/**
* 分割标签
* @param tags {string}
@@ -225,9 +259,10 @@ export default {
* @param num {number}
* @param start {string}
* @param end {string}
* @param joinStr {string}
* @returns {string}
*/
setLayers(str, num = 0, start = '(', end = ')') {
setLayers(str, num = 0, start = '(', end = ')', joinStr = '') {
// 先去除所有的括号
while (true) {
let first = str[0]
@@ -243,7 +278,7 @@ export default {
// 如果层数为0那么直接返回
if (num === 0) return str
// 如果层数大于0那么在字符串的前面加上num个start后面加上num个end
return start.repeat(num) + str + end.repeat(num)
return start.repeat(num) + str + joinStr + end.repeat(num)
},
/**