-
Notifications
You must be signed in to change notification settings - Fork 745
Expand file tree
/
Copy pathPromptItem.ts
More file actions
110 lines (99 loc) · 3.24 KB
/
PromptItem.ts
File metadata and controls
110 lines (99 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { IPromptWord, PromptWordType } from "../Lib/parsePrompts/parsePrompts"
import { useDatabaseServer } from "../Lib/DatabaseServer/DatabaseServer"
import { chinesePercentage } from "../Lib/chinesePercentage"
import { translatePrompts } from "../Lib/translatePrompts"
export interface IPromptItemData {
word: IPromptWord
// 被禁用的
disabled?: boolean
}
export class PromptItem {
static fromWord(word: IPromptWord) {
return new PromptItem({
word: Object.assign(emptyWord(), word),
})
}
static createEmpty(data: { text?: string; group?: string; subType?: string }) {
return new PromptItem({
word: <any>{
...emptyWord(),
id: `${data.text},${Date.now()}`,
text: data.text ?? "",
rawText: data.text ?? "",
type: PromptWordType.Word,
group: data.group,
subType: data.subType,
desc: "",
args: [],
lv: 1,
isEg: false,
},
})
}
data!: IPromptItemData
state = {
isEdit: <false | "text" | "lang">false,
isDict: false,
}
constructor(data?: IPromptItemData) {
this.data = Object.assign({ disabled: false }, data)
}
/** 更新内容(检查分类、翻译) */
async updateContent(text: string) {
let dataserver = useDatabaseServer()
let rawText = text
// 命令
let isCommand
if (text.startsWith("--") || text.startsWith("—")) {
text = text.split(" ")[0]
isCommand = true
}
// 重置旧数据
this.data.word.desc = undefined
this.data.word.langText = undefined
this.data.word.rawText = ""
// 「中翻英」中文翻译成英文
let cp = chinesePercentage(text)
let isZhToEn
if (!isCommand && cp > 50) {
let re = await translatePrompts([text], { to: "en" })
if (re?.[0]) {
isZhToEn = true
this.data.word.langText = text
text = re[0]
rawText = text
}
}
this.data.word.text = text
this.data.word.rawText = rawText
let pDesc = (await dataserver.queryPromptsDefine([text]))?.[0]
if (pDesc) {
if (pDesc["lang_zh"]) this.data.word.langText = pDesc["lang_zh"]
if (pDesc.desc) this.data.word.desc = pDesc.desc
if (pDesc.subType) this.data.word.subType = pDesc.subType
}
console.log("[updateContent]", this.data.word.langText, cp)
// 如果且没有进行中翻英,且文本是英文而且没有译文,进行「英翻中」
if (!isZhToEn && !isCommand && !this.data.word.langText && cp < 5) {
let re = await translatePrompts([text])
if (re && re[0]) {
this.data.word.langText = re[0]
}
}
}
}
function emptyWord() {
return {
id: null,
text: null,
rawText: null,
langText: null,
type: PromptWordType.Word,
group: null,
subType: null,
desc: null,
args: [],
lv: null,
isEg: null,
}
}