Skip to content

Commit

Permalink
utools 关键字配置
Browse files Browse the repository at this point in the history
  • Loading branch information
baiy committed Jan 8, 2023
1 parent 0c4924d commit 02646f1
Show file tree
Hide file tree
Showing 8 changed files with 208 additions and 22 deletions.
105 changes: 103 additions & 2 deletions packages/ctool-adapter/utools/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
import {PlatformRuntime, StorageInterface, Storage, toolExists, getTool} from "ctool-config";
import {PlatformRuntime, StorageInterface, Storage, toolExists, getTool, FeatureInterface, tools} from "ctool-config";
import storageUtools from "./storage"

type FeatureItem = { feature: FeatureInterface, cmds: string[] }

const $t = (key: string): string => {
// @ts-ignore
return window['$t'](key)
}

const setFeatures = (items: any) => {
try {
window.utools.setFeature(items)
} catch (e) {

}
}

export const runtime = new (class implements PlatformRuntime {
name = "utools"

Expand All @@ -23,11 +38,18 @@ export const runtime = new (class implements PlatformRuntime {
entry(storage: Storage) {
return new Promise<void>((resolve) => {
window.utools.onPluginEnter(({code, type, payload}) => {
try {
if (window.utools.getFeatures().length === 0) {
// utools 动态关键字初始化设置
this.resetFeatures()
}
} catch (e) {
}
window.utools.showMainWindow()
if (!code.includes("ctool-")) {
return resolve()
}
const [_, _tool, _feature] = code.split('-')
const [, _tool, _feature] = code.split('-')
if (!toolExists(_tool)) {
return resolve()
}
Expand All @@ -52,4 +74,83 @@ export const runtime = new (class implements PlatformRuntime {
})
})
}

getFeatures() {
const result = new Map<FeatureInterface, string[]>()
tools.forEach(tool => {
tool.features.forEach(feature => result.set(feature, []))
})
window.utools.getFeatures()
.filter(item => item.code.includes('ctool-') && item.code.includes('-customize'))
.forEach(item => {
const [, _tool, _feature] = item.code.split('-')
if (!toolExists(_tool)) {
return null
}

const tool = getTool(_tool)
if (!tool.existFeature(_feature)) {
return null
}
const feature = tool.getFeature(_feature)
result.set(feature, item.cmds as any)
})

return result
}

resetFeatures() {
let features: FeatureItem[] = []
tools.forEach(tool => {
tool.features.forEach(feature => {
features.push({
feature: feature,
cmds: [
...(
new Set([
tool.name,
feature.name,
tool.isSimple() ? `ctool-${tool.name}` : `ctool-${tool.name}-${feature.name}`,
$t(`tool_${tool.name}`),
$t(`tool_${tool.name}_${feature.name}`),
...$t(`tool_${tool.name}_${feature.name}_keywords`).split(","),
`${tool.isSimple() ? "" : $t(`tool_${tool.name}`) + " - "}${$t(`tool_${tool.name}_${feature.name}`)}`,
].map(item => item.trim().toLowerCase()).filter(item => item !== "")
)
)
]
})
})
});
this.setFeatures(features)
}

setFeatures(features: FeatureItem[]) {
// 默认添加
setFeatures({
"code": "Ctool",
"explain": "ctool - 程序开发常用工具",
"cmds": [
"Ctool"
]
})

// 移除已有项目
window.utools.getFeatures().forEach(item => {
if (item.code.includes('ctool-') && item.code.includes('-customize')) {
window.utools.removeFeature(item.code)
}
})

// 添加项目
features.forEach(({feature, cmds}) => {
if (cmds.length > 0) {
setFeatures({
"code": `ctool-${feature.getKey()}-customize`,
"explain": `${feature.tool.isSimple() ? "" : $t(`tool_${feature.tool.name}`) + " - "}${$t(`tool_${feature.tool.name}_${feature.name}`)}`,
"cmds": cmds
});
}
})
}
})
25 changes: 5 additions & 20 deletions packages/ctool-adapter/utools/src/release.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {copyCoreDist, release, replaceFileContent, version, getAdditionData} from "ctool-adapter-base";
import {tools, ToolInterface, FeatureInterface,AllLocaleStructure} from "ctool-config";
import {tools, ToolInterface, FeatureInterface, AllLocaleStructure} from "ctool-config";
import {CustomCmd, customCmds} from "./config";
import {join} from "path";
import {cpSync, mkdirSync, rmSync} from "fs";
Expand All @@ -21,9 +21,6 @@ const getToolTitle = (tool: ToolInterface) => {
const getToolFeatureTitle = (feature: FeatureInterface) => {
return i18n.detail.zh_CN[`tool_${feature.tool.name}_${feature.name}`]?.message || ""
}
const getToolFeatureKeywords = (feature: FeatureInterface) => {
return i18n.detail.zh_CN[`tool_${feature.tool.name}_${feature.name}_keywords`]?.message || ""
}

type UtoolsFeature = {
code: string,
Expand All @@ -40,28 +37,16 @@ tools.forEach(tool => {
tool.features.forEach(feature => {
const code: UtoolsFeature['code'] = `ctool-${tool.name}-${feature.name}`
const explain: UtoolsFeature['explain'] = `${tool.isSimple() ? "" : getToolTitle(tool) + " - "}${getToolFeatureTitle(feature)}`
const cmds: UtoolsFeature['cmds'] = [
...(
new Set([
tool.name,
feature.name,
tool.isSimple() ? `ctool-${tool.name}` : `ctool-${tool.name}-${feature.name}`,
getToolTitle(tool),
getToolFeatureTitle(feature),
explain,
...getToolFeatureKeywords(feature).split(",")
].map(item => item.trim().toLowerCase()).filter(item => item !== "")
)
)
]

const cmds: UtoolsFeature['cmds'] = []
if (customCmds.has(feature)) {
cmds.push(...(customCmds.get(feature) as CustomCmd[]).map((item => {
item.label = explain
return item
})))
}
utoolsFeature.push({code, explain, cmds})
if (cmds.length > 0) {
utoolsFeature.push({code, explain, cmds})
}
})
});

Expand Down
2 changes: 2 additions & 0 deletions packages/ctool-config/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ export interface FeatureInterface<T extends ToolType = ToolType> {
tool: ToolInterface<T>

getRouter(): string

getKey(): string

getComponentPath(): string

Expand Down
4 changes: 4 additions & 0 deletions packages/ctool-config/src/feature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ export default class Feature<T extends ToolType = ToolType> implements FeatureIn
...other
}
}

getKey() {
return `${this.tool.name}-${this.name}`
}
}
11 changes: 11 additions & 0 deletions packages/ctool-core/src/block/Setting.vue
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,17 @@
@change="(value)=>storeSetting.save('auto_read_copy_filter',value)"
/>
</div>
<template v-if="platform.isUtools()">
<span>uTools</span>
<div>
<Button :size="'small'" @click="openUtoolsKeyword = true" :text="`${$t(`main_ui_keyword`)}${$t(`main_ui_config`)}`" />
</div>
</template>
</div>
</Card>
<ExtendPage v-model="openUtoolsKeyword" disable-replace>
<UtoolsKeyword v-if="platform.isUtools()"/>
</ExtendPage>
</template>

<script setup lang="ts">
Expand All @@ -65,8 +74,10 @@ import {useClipboardPermission} from "@/helper/clipboard"
import {locales, themes} from "@/types"
import platform from "@/helper/platform"
import {getLocaleName} from "@/i18n"
import UtoolsKeyword from "./utools/Keyword.vue"
const storeSetting = useSetting()
let openUtoolsKeyword = $ref(false)
const localeOptions = locales.map((item) => {
return {value: item, label: getLocaleName(item) || ""}
Expand Down
77 changes: 77 additions & 0 deletions packages/ctool-core/src/block/utools/Keyword.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<template>
<Card :title="`uTools - ${$t(`main_ui_keyword`)}${$t(`main_ui_config`)}`" height="100%">
<div v-row="`1-1-1-1`">
<Textarea
:height="180"
v-for="key in itemsKey"
:key="key"
v-model="items[key].cmds"
:float-text="items[key].title"
float-position="bottom-right"
/>
</div>
<template #extra>
<Align>
<Button :size="'small'" @click="save" :text="$t('main_ui_save')" type="primary"/>
<Button :size="'small'" @click="reset" :text="$t('main_ui_reset')"/>
<Button :size="'small'" @click="clear" :text="$t('main_ui_clear')" type="danger"/>
</Align>
</template>
</Card>
</template>

<script setup lang="ts">
import {runtime} from 'ctool-adapter-utools'
import {FeatureInterface} from '@/config'
import Message from "@/helper/message";
type Item = {
feature: FeatureInterface,
title: string
cmds: string
}
const getFeatures = () => {
let lists: Record<string, Item> = {}
runtime.getFeatures().forEach((value, feature) => {
lists[`${feature.getKey()}`] = {
feature,
title: feature.tool.isSimple() ? $t(`tool_${feature.tool.name}`) : `${$t(`tool_${feature.tool.name}`)} - ${$t(`tool_${feature.tool.name}_${feature.name}`)}`,
cmds: value.join("\n")
}
})
return lists
}
let items = $ref(getFeatures())
const itemsKey = Object.keys(items)
const save = () => {
const features: { feature: FeatureInterface, cmds: string[] }[] = []
itemsKey.forEach(key => {
let cmds = [...(new Set(items[key].cmds.split("\n").map(item => item.trim()).filter(item => item !== "")))]
if (cmds.length > 0) {
features.push({
feature: items[key].feature,
cmds
})
}
})
runtime.setFeatures(features)
items = getFeatures()
Message.success($t('main_ui_success'))
}
const reset = () => {
runtime.resetFeatures()
items = getFeatures()
Message.success($t('main_ui_success'))
}
const clear = () => {
runtime.setFeatures([])
items = getFeatures()
Message.success($t('main_ui_success'))
}
</script>

3 changes: 3 additions & 0 deletions packages/ctool-core/src/i18n/locales/en/main.i18n.json5
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
"ui_setting": "Settings",
"ui_config": "Config",
"ui_reset": "Reset",
"ui_save": "Save",
"ui_submit": "Submit",
"ui_views": "View",
"ui_clear": "Clear",
"ui_more": "More",
Expand All @@ -40,6 +42,7 @@
"ui_advanced": "Advanced",
"ui_simple": "Simple",
"ui_reference": "Reference",
"ui_keyword": "Keyword",
// views
"setting_language": "Language",
"tools_lists": "Tool Lists",
Expand Down
3 changes: 3 additions & 0 deletions packages/ctool-core/src/i18n/locales/zh_CN/main.i18n.json5
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
"ui_setting": "设置",
"ui_config": "配置",
"ui_reset": "重置",
"ui_save": "保存",
"ui_submit": "提交",
"ui_views": "查看",
"ui_clear": "清空",
"ui_more": "更多",
Expand All @@ -40,6 +42,7 @@
"ui_advanced": "高级",
"ui_simple": "简单",
"ui_reference": "参考",
"ui_keyword": "关键字",
// 界面
"setting_language": "语言",
"tools_lists": "工具列表",
Expand Down

0 comments on commit 02646f1

Please sign in to comment.