11<script setup>
2- import { onMounted , ref , h } from ' vue' ;
2+ import { onMounted , ref , h , watch } from ' vue' ;
33import { ConfigProvider , Layout , Menu } from ' ant-design-vue' ;
44import { FileTextOutlined , SettingOutlined } from ' @ant-design/icons-vue' ;
55import HomeView from ' ./components/HomeView.vue' ;
66import ConfigView from ' ./components/ConfigView.vue' ;
7+ import { useCommitTypesStore } from ' ./stores/commitTypes' ;
78
89const { Sider , Content } = Layout;
910
1011const currentView = ref ([' home' ]);
1112const enterAction = ref ({});
13+ const commitTypesStore = useCommitTypesStore ();
1214
1315const 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+
2670onMounted (() => {
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+
37111const handleMenuClick = ({ key }) => {
38112 currentView .value = [key];
39113};
0 commit comments