Skip to content

Commit 34bcd51

Browse files
committed
✨ feat: 动态注册命令
1 parent 12cde6e commit 34bcd51

File tree

5 files changed

+116
-22
lines changed

5 files changed

+116
-22
lines changed

public/plugin.json

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,8 @@
1111
"code": "commit",
1212
"explain": "生成提交信息",
1313
"cmds": [
14-
"feat",
1514
"commit",
16-
"提交信息",
17-
"fix",
18-
"docs",
19-
"init",
20-
"style",
21-
"refactor",
22-
"perf",
23-
"test",
24-
"build",
25-
"chore",
26-
"revert"
15+
"提交信息"
2716
]
2817
}
2918
]

src/App.vue

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
<script setup>
2-
import { onMounted, ref, h } from 'vue';
2+
import { onMounted, ref, h, watch } from 'vue';
33
import { ConfigProvider, Layout, Menu } from 'ant-design-vue';
44
import { FileTextOutlined, SettingOutlined } from '@ant-design/icons-vue';
55
import HomeView from './components/HomeView.vue';
66
import ConfigView from './components/ConfigView.vue';
7+
import { useCommitTypesStore } from './stores/commitTypes';
78
89
const { Sider, Content } = Layout;
910
1011
const currentView = ref(['home']);
1112
const enterAction = ref({});
13+
const commitTypesStore = useCommitTypesStore();
1214
1315
const menuItems = [
1416
{
@@ -23,9 +25,65 @@ const menuItems = [
2325
},
2426
];
2527
28+
// 当前已注册的动态功能 codes
29+
const registeredFeatureCodes = ref(new Set());
30+
let registerTimer = null;
31+
32+
// 动态注册 uTools 命令 - 为每个提交类型创建独立功能
33+
const registerCommands = () => {
34+
// 清除之前的定时器,防止短时间内多次调用
35+
if (registerTimer) {
36+
clearTimeout(registerTimer);
37+
}
38+
39+
registerTimer = setTimeout(() => {
40+
const currentTypes = new Set(commitTypesStore.allCommitTypes.map(type => type.value));
41+
42+
// 添加新类型的功能
43+
commitTypesStore.allCommitTypes.forEach(type => {
44+
const featureCode = `commit-${type.value}`;
45+
46+
// 如果还未注册,则注册
47+
if (!registeredFeatureCodes.value.has(featureCode)) {
48+
window.utools.setFeature({
49+
code: featureCode,
50+
explain: `生成 ${type.label || type.value} 类型的提交信息`,
51+
cmds: [type.value]
52+
});
53+
registeredFeatureCodes.value.add(featureCode);
54+
}
55+
});
56+
57+
// 删除已移除类型的功能
58+
registeredFeatureCodes.value.forEach(featureCode => {
59+
const typeValue = featureCode.replace('commit-', '');
60+
if (!currentTypes.has(typeValue)) {
61+
window.utools.removeFeature(featureCode);
62+
registeredFeatureCodes.value.delete(featureCode);
63+
}
64+
});
65+
66+
registerTimer = null;
67+
}, 100); // 100ms 防抖
68+
};
69+
2670
onMounted(() => {
71+
// 清理可能存在的旧动态功能
72+
try {
73+
const existingFeatures = window.utools.getFeatures();
74+
existingFeatures.forEach(feature => {
75+
if (feature.code.startsWith('commit-')) {
76+
window.utools.removeFeature(feature.code);
77+
}
78+
});
79+
} catch (e) {
80+
// ignore
81+
}
82+
83+
// 初始化时注册命令
84+
registerCommands();
85+
2786
window.utools.onPluginEnter((action) => {
28-
console.log("Plugin entered", action);
2987
enterAction.value = action;
3088
});
3189
@@ -34,6 +92,22 @@ onMounted(() => {
3492
});
3593
});
3694
95+
// 监听提交类型数组的变化
96+
watch(
97+
() => commitTypesStore.commitTypes.length,
98+
() => {
99+
registerCommands();
100+
}
101+
);
102+
103+
// 同时监听类型值的变化
104+
watch(
105+
() => commitTypesStore.commitTypes.map(t => t.value).join(','),
106+
() => {
107+
registerCommands();
108+
}
109+
);
110+
37111
const handleMenuClick = ({ key }) => {
38112
currentView.value = [key];
39113
};

src/components/ConfigView.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ const resetTypesToDefault = () => {
309309
<div class="config-row">
310310
<a-button type="primary" @click="saveData">保存</a-button>
311311
<a-popconfirm title="确定吗?" ok-text="Yes" cancel-text="我再想想" @confirm="resetConfig">
312-
<a-button type="dashed" danger>重置</a-button>
312+
<a-button danger>重置</a-button>
313313
</a-popconfirm>
314314
</div>
315315
</div>

src/components/HomeView.vue

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,44 @@ const selectedIcon = computed(() => {
2727
return type?.icon || '';
2828
});
2929
30+
// 从 enterAction 中提取并设置类型
31+
const selectTypeFromAction = (action) => {
32+
if (!action || !action.code) {
33+
selectedType.value = 'feat';
34+
return;
35+
}
36+
37+
// 动态功能的 code 格式为 "commit-{type}"
38+
if (action.code.startsWith('commit-')) {
39+
const typeValue = action.code.replace('commit-', '');
40+
if (commitTypesStore.hasCommitType(typeValue)) {
41+
selectedType.value = typeValue;
42+
return;
43+
}
44+
}
45+
// 静态 commit 功能,使用 payload
46+
else if (action.code === 'commit' && action.payload) {
47+
if (commitTypesStore.hasCommitType(action.payload)) {
48+
selectedType.value = action.payload;
49+
return;
50+
}
51+
}
52+
53+
// 默认值
54+
selectedType.value = 'feat';
55+
};
56+
57+
// 监听 enterAction 的变化
58+
watch(
59+
() => props.enterAction,
60+
(newAction) => {
61+
selectTypeFromAction(newAction);
62+
},
63+
{ immediate: true, deep: true }
64+
);
65+
3066
onMounted(() => {
31-
selectedType.value = props.enterAction?.payload || 'feat';
67+
selectTypeFromAction(props.enterAction);
3268
});
3369
3470
const autoClassifyCommitType = (description) => {

src/main.css

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
@font-face {
2-
font-family: '喵字果汁体';
3-
src: url(https://cdn.jsdelivr.net/gh/caolib/cdn@main/fonts/MiaoZi-GuoZhiTi.ttf);
4-
}
5-
61
:root {
72
--blue: rgb(88, 164, 246);
83
--light: #fff;
@@ -16,7 +11,7 @@ body {
1611
}
1712

1813
* {
19-
font-family: 'Cascadia Code', Segoe Fluent Icons, '喵字果汁体', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif !important;
14+
font-family: 'Cascadia Code', Segoe Fluent Icons, '喵字果汁体', 黑体, 微软雅黑, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif !important;
2015
}
2116

2217
button {

0 commit comments

Comments
 (0)