Skip to content

Commit 02646f1

Browse files
committed
utools 关键字配置
1 parent 0c4924d commit 02646f1

File tree

8 files changed

+208
-22
lines changed

8 files changed

+208
-22
lines changed

packages/ctool-adapter/utools/src/index.ts

Lines changed: 103 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
1-
import {PlatformRuntime, StorageInterface, Storage, toolExists, getTool} from "ctool-config";
1+
import {PlatformRuntime, StorageInterface, Storage, toolExists, getTool, FeatureInterface, tools} from "ctool-config";
22
import storageUtools from "./storage"
33

4+
type FeatureItem = { feature: FeatureInterface, cmds: string[] }
5+
6+
const $t = (key: string): string => {
7+
// @ts-ignore
8+
return window['$t'](key)
9+
}
10+
11+
const setFeatures = (items: any) => {
12+
try {
13+
window.utools.setFeature(items)
14+
} catch (e) {
15+
16+
}
17+
}
18+
419
export const runtime = new (class implements PlatformRuntime {
520
name = "utools"
621

@@ -23,11 +38,18 @@ export const runtime = new (class implements PlatformRuntime {
2338
entry(storage: Storage) {
2439
return new Promise<void>((resolve) => {
2540
window.utools.onPluginEnter(({code, type, payload}) => {
41+
try {
42+
if (window.utools.getFeatures().length === 0) {
43+
// utools 动态关键字初始化设置
44+
this.resetFeatures()
45+
}
46+
} catch (e) {
47+
}
2648
window.utools.showMainWindow()
2749
if (!code.includes("ctool-")) {
2850
return resolve()
2951
}
30-
const [_, _tool, _feature] = code.split('-')
52+
const [, _tool, _feature] = code.split('-')
3153
if (!toolExists(_tool)) {
3254
return resolve()
3355
}
@@ -52,4 +74,83 @@ export const runtime = new (class implements PlatformRuntime {
5274
})
5375
})
5476
}
77+
78+
getFeatures() {
79+
const result = new Map<FeatureInterface, string[]>()
80+
tools.forEach(tool => {
81+
tool.features.forEach(feature => result.set(feature, []))
82+
})
83+
window.utools.getFeatures()
84+
.filter(item => item.code.includes('ctool-') && item.code.includes('-customize'))
85+
.forEach(item => {
86+
const [, _tool, _feature] = item.code.split('-')
87+
if (!toolExists(_tool)) {
88+
return null
89+
}
90+
91+
const tool = getTool(_tool)
92+
if (!tool.existFeature(_feature)) {
93+
return null
94+
}
95+
const feature = tool.getFeature(_feature)
96+
result.set(feature, item.cmds as any)
97+
})
98+
99+
return result
100+
}
101+
102+
resetFeatures() {
103+
let features: FeatureItem[] = []
104+
tools.forEach(tool => {
105+
tool.features.forEach(feature => {
106+
features.push({
107+
feature: feature,
108+
cmds: [
109+
...(
110+
new Set([
111+
tool.name,
112+
feature.name,
113+
tool.isSimple() ? `ctool-${tool.name}` : `ctool-${tool.name}-${feature.name}`,
114+
$t(`tool_${tool.name}`),
115+
$t(`tool_${tool.name}_${feature.name}`),
116+
...$t(`tool_${tool.name}_${feature.name}_keywords`).split(","),
117+
`${tool.isSimple() ? "" : $t(`tool_${tool.name}`) + " - "}${$t(`tool_${tool.name}_${feature.name}`)}`,
118+
].map(item => item.trim().toLowerCase()).filter(item => item !== "")
119+
)
120+
)
121+
]
122+
})
123+
})
124+
});
125+
this.setFeatures(features)
126+
}
127+
128+
setFeatures(features: FeatureItem[]) {
129+
// 默认添加
130+
setFeatures({
131+
"code": "Ctool",
132+
"explain": "ctool - 程序开发常用工具",
133+
"cmds": [
134+
"Ctool"
135+
]
136+
})
137+
138+
// 移除已有项目
139+
window.utools.getFeatures().forEach(item => {
140+
if (item.code.includes('ctool-') && item.code.includes('-customize')) {
141+
window.utools.removeFeature(item.code)
142+
}
143+
})
144+
145+
// 添加项目
146+
features.forEach(({feature, cmds}) => {
147+
if (cmds.length > 0) {
148+
setFeatures({
149+
"code": `ctool-${feature.getKey()}-customize`,
150+
"explain": `${feature.tool.isSimple() ? "" : $t(`tool_${feature.tool.name}`) + " - "}${$t(`tool_${feature.tool.name}_${feature.name}`)}`,
151+
"cmds": cmds
152+
});
153+
}
154+
})
155+
}
55156
})

packages/ctool-adapter/utools/src/release.ts

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {copyCoreDist, release, replaceFileContent, version, getAdditionData} from "ctool-adapter-base";
2-
import {tools, ToolInterface, FeatureInterface,AllLocaleStructure} from "ctool-config";
2+
import {tools, ToolInterface, FeatureInterface, AllLocaleStructure} from "ctool-config";
33
import {CustomCmd, customCmds} from "./config";
44
import {join} from "path";
55
import {cpSync, mkdirSync, rmSync} from "fs";
@@ -21,9 +21,6 @@ const getToolTitle = (tool: ToolInterface) => {
2121
const getToolFeatureTitle = (feature: FeatureInterface) => {
2222
return i18n.detail.zh_CN[`tool_${feature.tool.name}_${feature.name}`]?.message || ""
2323
}
24-
const getToolFeatureKeywords = (feature: FeatureInterface) => {
25-
return i18n.detail.zh_CN[`tool_${feature.tool.name}_${feature.name}_keywords`]?.message || ""
26-
}
2724

2825
type UtoolsFeature = {
2926
code: string,
@@ -40,28 +37,16 @@ tools.forEach(tool => {
4037
tool.features.forEach(feature => {
4138
const code: UtoolsFeature['code'] = `ctool-${tool.name}-${feature.name}`
4239
const explain: UtoolsFeature['explain'] = `${tool.isSimple() ? "" : getToolTitle(tool) + " - "}${getToolFeatureTitle(feature)}`
43-
const cmds: UtoolsFeature['cmds'] = [
44-
...(
45-
new Set([
46-
tool.name,
47-
feature.name,
48-
tool.isSimple() ? `ctool-${tool.name}` : `ctool-${tool.name}-${feature.name}`,
49-
getToolTitle(tool),
50-
getToolFeatureTitle(feature),
51-
explain,
52-
...getToolFeatureKeywords(feature).split(",")
53-
].map(item => item.trim().toLowerCase()).filter(item => item !== "")
54-
)
55-
)
56-
]
57-
40+
const cmds: UtoolsFeature['cmds'] = []
5841
if (customCmds.has(feature)) {
5942
cmds.push(...(customCmds.get(feature) as CustomCmd[]).map((item => {
6043
item.label = explain
6144
return item
6245
})))
6346
}
64-
utoolsFeature.push({code, explain, cmds})
47+
if (cmds.length > 0) {
48+
utoolsFeature.push({code, explain, cmds})
49+
}
6550
})
6651
});
6752

packages/ctool-config/src/config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,8 @@ export interface FeatureInterface<T extends ToolType = ToolType> {
260260
tool: ToolInterface<T>
261261

262262
getRouter(): string
263+
264+
getKey(): string
263265

264266
getComponentPath(): string
265267

packages/ctool-config/src/feature.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,8 @@ export default class Feature<T extends ToolType = ToolType> implements FeatureIn
3636
...other
3737
}
3838
}
39+
40+
getKey() {
41+
return `${this.tool.name}-${this.name}`
42+
}
3943
}

packages/ctool-core/src/block/Setting.vue

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,17 @@
5555
@change="(value)=>storeSetting.save('auto_read_copy_filter',value)"
5656
/>
5757
</div>
58+
<template v-if="platform.isUtools()">
59+
<span>uTools</span>
60+
<div>
61+
<Button :size="'small'" @click="openUtoolsKeyword = true" :text="`${$t(`main_ui_keyword`)}${$t(`main_ui_config`)}`" />
62+
</div>
63+
</template>
5864
</div>
5965
</Card>
66+
<ExtendPage v-model="openUtoolsKeyword" disable-replace>
67+
<UtoolsKeyword v-if="platform.isUtools()"/>
68+
</ExtendPage>
6069
</template>
6170

6271
<script setup lang="ts">
@@ -65,8 +74,10 @@ import {useClipboardPermission} from "@/helper/clipboard"
6574
import {locales, themes} from "@/types"
6675
import platform from "@/helper/platform"
6776
import {getLocaleName} from "@/i18n"
77+
import UtoolsKeyword from "./utools/Keyword.vue"
6878
6979
const storeSetting = useSetting()
80+
let openUtoolsKeyword = $ref(false)
7081
7182
const localeOptions = locales.map((item) => {
7283
return {value: item, label: getLocaleName(item) || ""}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<template>
2+
<Card :title="`uTools - ${$t(`main_ui_keyword`)}${$t(`main_ui_config`)}`" height="100%">
3+
<div v-row="`1-1-1-1`">
4+
<Textarea
5+
:height="180"
6+
v-for="key in itemsKey"
7+
:key="key"
8+
v-model="items[key].cmds"
9+
:float-text="items[key].title"
10+
float-position="bottom-right"
11+
/>
12+
</div>
13+
<template #extra>
14+
<Align>
15+
<Button :size="'small'" @click="save" :text="$t('main_ui_save')" type="primary"/>
16+
<Button :size="'small'" @click="reset" :text="$t('main_ui_reset')"/>
17+
<Button :size="'small'" @click="clear" :text="$t('main_ui_clear')" type="danger"/>
18+
</Align>
19+
</template>
20+
</Card>
21+
</template>
22+
23+
<script setup lang="ts">
24+
import {runtime} from 'ctool-adapter-utools'
25+
import {FeatureInterface} from '@/config'
26+
import Message from "@/helper/message";
27+
28+
type Item = {
29+
feature: FeatureInterface,
30+
title: string
31+
cmds: string
32+
}
33+
34+
const getFeatures = () => {
35+
let lists: Record<string, Item> = {}
36+
runtime.getFeatures().forEach((value, feature) => {
37+
lists[`${feature.getKey()}`] = {
38+
feature,
39+
title: feature.tool.isSimple() ? $t(`tool_${feature.tool.name}`) : `${$t(`tool_${feature.tool.name}`)} - ${$t(`tool_${feature.tool.name}_${feature.name}`)}`,
40+
cmds: value.join("\n")
41+
}
42+
})
43+
return lists
44+
}
45+
46+
let items = $ref(getFeatures())
47+
48+
const itemsKey = Object.keys(items)
49+
50+
const save = () => {
51+
const features: { feature: FeatureInterface, cmds: string[] }[] = []
52+
itemsKey.forEach(key => {
53+
let cmds = [...(new Set(items[key].cmds.split("\n").map(item => item.trim()).filter(item => item !== "")))]
54+
if (cmds.length > 0) {
55+
features.push({
56+
feature: items[key].feature,
57+
cmds
58+
})
59+
}
60+
})
61+
runtime.setFeatures(features)
62+
items = getFeatures()
63+
Message.success($t('main_ui_success'))
64+
}
65+
const reset = () => {
66+
runtime.resetFeatures()
67+
items = getFeatures()
68+
Message.success($t('main_ui_success'))
69+
}
70+
71+
const clear = () => {
72+
runtime.setFeatures([])
73+
items = getFeatures()
74+
Message.success($t('main_ui_success'))
75+
}
76+
</script>
77+

packages/ctool-core/src/i18n/locales/en/main.i18n.json5

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
"ui_setting": "Settings",
1616
"ui_config": "Config",
1717
"ui_reset": "Reset",
18+
"ui_save": "Save",
19+
"ui_submit": "Submit",
1820
"ui_views": "View",
1921
"ui_clear": "Clear",
2022
"ui_more": "More",
@@ -40,6 +42,7 @@
4042
"ui_advanced": "Advanced",
4143
"ui_simple": "Simple",
4244
"ui_reference": "Reference",
45+
"ui_keyword": "Keyword",
4346
// views
4447
"setting_language": "Language",
4548
"tools_lists": "Tool Lists",

packages/ctool-core/src/i18n/locales/zh_CN/main.i18n.json5

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
"ui_setting": "设置",
1616
"ui_config": "配置",
1717
"ui_reset": "重置",
18+
"ui_save": "保存",
19+
"ui_submit": "提交",
1820
"ui_views": "查看",
1921
"ui_clear": "清空",
2022
"ui_more": "更多",
@@ -40,6 +42,7 @@
4042
"ui_advanced": "高级",
4143
"ui_simple": "简单",
4244
"ui_reference": "参考",
45+
"ui_keyword": "关键字",
4346
// 界面
4447
"setting_language": "语言",
4548
"tools_lists": "工具列表",

0 commit comments

Comments
 (0)