@talex-touch/utils 1.0.23 → 1.0.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,324 @@
1
+ /**
2
+ * Plugin Features SDK
3
+ *
4
+ * @description
5
+ * 提供插件features管理的SDK功能,包括动态添加、删除、查询和优先级管理
6
+ *
7
+ * @version 1.0.0
8
+ * @module plugin/sdk/features
9
+ */
10
+
11
+ import { IPluginFeature } from '../index'
12
+
13
+ /**
14
+ * Features管理器接口
15
+ *
16
+ * @description
17
+ * 提供完整的features管理功能,包括CRUD操作和优先级管理
18
+ */
19
+ export interface IFeaturesManager {
20
+ /**
21
+ * 动态添加功能到插件
22
+ * @param feature - 功能定义
23
+ * @returns 是否添加成功
24
+ */
25
+ addFeature(feature: IPluginFeature): boolean
26
+
27
+ /**
28
+ * 删除功能
29
+ * @param featureId - 功能ID
30
+ * @returns 是否删除成功
31
+ */
32
+ removeFeature(featureId: string): boolean
33
+
34
+ /**
35
+ * 获取所有功能
36
+ * @returns 所有功能列表
37
+ */
38
+ getFeatures(): IPluginFeature[]
39
+
40
+ /**
41
+ * 获取指定功能
42
+ * @param featureId - 功能ID
43
+ * @returns 功能对象,如果不存在返回null
44
+ */
45
+ getFeature(featureId: string): IPluginFeature | null
46
+
47
+ /**
48
+ * 设置功能优先级
49
+ * @param featureId - 功能ID
50
+ * @param priority - 优先级值(数字越大优先级越高)
51
+ * @returns 是否设置成功
52
+ */
53
+ setPriority(featureId: string, priority: number): boolean
54
+
55
+ /**
56
+ * 获取功能优先级
57
+ * @param featureId - 功能ID
58
+ * @returns 优先级值,如果功能不存在返回null
59
+ */
60
+ getPriority(featureId: string): number | null
61
+
62
+ /**
63
+ * 按优先级排序获取所有功能
64
+ * @returns 按优先级排序的功能列表(高优先级在前)
65
+ */
66
+ getFeaturesByPriority(): IPluginFeature[]
67
+
68
+ /**
69
+ * 批量设置功能优先级
70
+ * @param priorities - 优先级映射对象 {featureId: priority}
71
+ * @returns 设置成功的功能数量
72
+ */
73
+ setPriorities(priorities: Record<string, number>): number
74
+
75
+ /**
76
+ * 重置功能优先级为默认值(0)
77
+ * @param featureId - 功能ID
78
+ * @returns 是否重置成功
79
+ */
80
+ resetPriority(featureId: string): boolean
81
+
82
+ /**
83
+ * 获取功能统计信息
84
+ * @returns 功能统计对象
85
+ */
86
+ getStats(): {
87
+ total: number
88
+ byPriority: Record<number, number>
89
+ averagePriority: number
90
+ }
91
+ }
92
+
93
+ /**
94
+ * 创建Features管理器
95
+ *
96
+ * @description
97
+ * 为插件创建features管理器实例
98
+ *
99
+ * @param pluginName - 插件名称
100
+ * @param utils - 插件工具对象
101
+ * @returns Features管理器实例
102
+ *
103
+ * @example
104
+ * ```typescript
105
+ * const featuresManager = createFeaturesManager('my-plugin', utils)
106
+ *
107
+ * // 添加功能
108
+ * featuresManager.addFeature({
109
+ * id: 'search',
110
+ * name: '搜索功能',
111
+ * desc: '提供搜索能力',
112
+ * commands: [{ type: 'over', value: ['search'] }],
113
+ * priority: 100
114
+ * })
115
+ *
116
+ * // 设置优先级
117
+ * featuresManager.setPriority('search', 200)
118
+ *
119
+ * // 获取按优先级排序的功能
120
+ * const sortedFeatures = featuresManager.getFeaturesByPriority()
121
+ * ```
122
+ */
123
+ export function createFeaturesManager(
124
+ pluginName: string,
125
+ utils: any
126
+ ): IFeaturesManager {
127
+ return {
128
+ /**
129
+ * 动态添加功能到插件
130
+ */
131
+ addFeature(feature: IPluginFeature): boolean {
132
+ try {
133
+ return utils.features.addFeature(feature)
134
+ } catch (error) {
135
+ console.error(`[FeaturesManager] Failed to add feature ${feature.id}:`, error)
136
+ return false
137
+ }
138
+ },
139
+
140
+ /**
141
+ * 删除功能
142
+ */
143
+ removeFeature(featureId: string): boolean {
144
+ try {
145
+ return utils.features.removeFeature(featureId)
146
+ } catch (error) {
147
+ console.error(`[FeaturesManager] Failed to remove feature ${featureId}:`, error)
148
+ return false
149
+ }
150
+ },
151
+
152
+ /**
153
+ * 获取所有功能
154
+ */
155
+ getFeatures(): IPluginFeature[] {
156
+ try {
157
+ return utils.features.getFeatures()
158
+ } catch (error) {
159
+ console.error(`[FeaturesManager] Failed to get features:`, error)
160
+ return []
161
+ }
162
+ },
163
+
164
+ /**
165
+ * 获取指定功能
166
+ */
167
+ getFeature(featureId: string): IPluginFeature | null {
168
+ try {
169
+ return utils.features.getFeature(featureId)
170
+ } catch (error) {
171
+ console.error(`[FeaturesManager] Failed to get feature ${featureId}:`, error)
172
+ return null
173
+ }
174
+ },
175
+
176
+ /**
177
+ * 设置功能优先级
178
+ */
179
+ setPriority(featureId: string, priority: number): boolean {
180
+ try {
181
+ return utils.features.setPriority(featureId, priority)
182
+ } catch (error) {
183
+ console.error(`[FeaturesManager] Failed to set priority for ${featureId}:`, error)
184
+ return false
185
+ }
186
+ },
187
+
188
+ /**
189
+ * 获取功能优先级
190
+ */
191
+ getPriority(featureId: string): number | null {
192
+ try {
193
+ return utils.features.getPriority(featureId)
194
+ } catch (error) {
195
+ console.error(`[FeaturesManager] Failed to get priority for ${featureId}:`, error)
196
+ return null
197
+ }
198
+ },
199
+
200
+ /**
201
+ * 按优先级排序获取所有功能
202
+ */
203
+ getFeaturesByPriority(): IPluginFeature[] {
204
+ try {
205
+ return utils.features.getFeaturesByPriority()
206
+ } catch (error) {
207
+ console.error(`[FeaturesManager] Failed to get features by priority:`, error)
208
+ return []
209
+ }
210
+ },
211
+
212
+ /**
213
+ * 批量设置功能优先级
214
+ */
215
+ setPriorities(priorities: Record<string, number>): number {
216
+ let successCount = 0
217
+ for (const [featureId, priority] of Object.entries(priorities)) {
218
+ if (this.setPriority(featureId, priority)) {
219
+ successCount++
220
+ }
221
+ }
222
+ return successCount
223
+ },
224
+
225
+ /**
226
+ * 重置功能优先级为默认值
227
+ */
228
+ resetPriority(featureId: string): boolean {
229
+ return this.setPriority(featureId, 0)
230
+ },
231
+
232
+ /**
233
+ * 获取功能统计信息
234
+ */
235
+ getStats(): {
236
+ total: number
237
+ byPriority: Record<number, number>
238
+ averagePriority: number
239
+ } {
240
+ try {
241
+ const features = this.getFeatures()
242
+ const total = features.length
243
+
244
+ const byPriority: Record<number, number> = {}
245
+ let totalPriority = 0
246
+
247
+ features.forEach(feature => {
248
+ const priority = feature.priority ?? 0
249
+ byPriority[priority] = (byPriority[priority] || 0) + 1
250
+ totalPriority += priority
251
+ })
252
+
253
+ const averagePriority = total > 0 ? totalPriority / total : 0
254
+
255
+ return {
256
+ total,
257
+ byPriority,
258
+ averagePriority
259
+ }
260
+ } catch (error) {
261
+ console.error(`[FeaturesManager] Failed to get stats:`, error)
262
+ return {
263
+ total: 0,
264
+ byPriority: {},
265
+ averagePriority: 0
266
+ }
267
+ }
268
+ }
269
+ }
270
+ }
271
+
272
+ /**
273
+ * Features管理器工厂函数
274
+ *
275
+ * @description
276
+ * 为插件创建features管理器的便捷工厂函数
277
+ *
278
+ * @param utils - 插件工具对象
279
+ * @returns Features管理器实例
280
+ */
281
+ export function useFeatures(utils: any): IFeaturesManager {
282
+ const pluginName = utils.plugin?.getInfo()?.name || 'unknown'
283
+ return createFeaturesManager(pluginName, utils)
284
+ }
285
+
286
+ /**
287
+ * 功能优先级常量
288
+ *
289
+ * @description
290
+ * 提供常用的优先级值,便于插件开发者使用
291
+ */
292
+ export const FEATURE_PRIORITIES = {
293
+ /** 最高优先级 */
294
+ HIGHEST: 1000,
295
+ /** 高优先级 */
296
+ HIGH: 500,
297
+ /** 默认优先级 */
298
+ NORMAL: 0,
299
+ /** 低优先级 */
300
+ LOW: -500,
301
+ /** 最低优先级 */
302
+ LOWEST: -1000
303
+ } as const
304
+
305
+ /**
306
+ * 功能类型枚举
307
+ *
308
+ * @description
309
+ * 预定义的功能类型,便于分类管理
310
+ */
311
+ export const FEATURE_TYPES = {
312
+ /** 搜索功能 */
313
+ SEARCH: 'search',
314
+ /** 工具功能 */
315
+ TOOL: 'tool',
316
+ /** 系统功能 */
317
+ SYSTEM: 'system',
318
+ /** 网络功能 */
319
+ NETWORK: 'network',
320
+ /** 文件功能 */
321
+ FILE: 'file',
322
+ /** 应用功能 */
323
+ APP: 'app'
324
+ } as const
@@ -19,5 +19,7 @@ export * from './service/index'
19
19
 
20
20
  export * from './channel'
21
21
  export * from './clipboard'
22
+ export * from './core-box'
22
23
  export * from './storage'
23
24
  export * from './system'
25
+ export * from './features'
@@ -5,6 +5,7 @@
5
5
  */
6
6
 
7
7
  import type { ITouchChannel, ITouchClientChannel, StandardChannelData } from '@talex-touch/utils/channel';
8
+ import type { IPluginFeature } from '../index';
8
9
 
9
10
  /**
10
11
  * Handler signature for plugin channel events.
@@ -198,6 +199,18 @@ export interface IPluginUtils {
198
199
  * @returns Array of current search result items
199
200
  */
200
201
  getItems: () => any[];
202
+
203
+ /**
204
+ * Features manager for dynamic feature management
205
+ * @see {@link IFeaturesManager}
206
+ */
207
+ features: IFeaturesManager;
208
+
209
+ /**
210
+ * Plugin information manager
211
+ * @see {@link IPluginInfoManager}
212
+ */
213
+ plugin: IPluginInfoManager;
201
214
  }
202
215
 
203
216
  /**
@@ -700,3 +713,132 @@ export function createSearchManager(): ISearchManager {
700
713
  }
701
714
  };
702
715
  }
716
+
717
+ /**
718
+ * Features管理器接口
719
+ *
720
+ * @description
721
+ * 提供完整的features管理功能,包括CRUD操作和优先级管理
722
+ */
723
+ export interface IFeaturesManager {
724
+ /**
725
+ * 动态添加功能到插件
726
+ * @param feature - 功能定义
727
+ * @returns 是否添加成功
728
+ */
729
+ addFeature(feature: IPluginFeature): boolean
730
+
731
+ /**
732
+ * 删除功能
733
+ * @param featureId - 功能ID
734
+ * @returns 是否删除成功
735
+ */
736
+ removeFeature(featureId: string): boolean
737
+
738
+ /**
739
+ * 获取所有功能
740
+ * @returns 所有功能列表
741
+ */
742
+ getFeatures(): IPluginFeature[]
743
+
744
+ /**
745
+ * 获取指定功能
746
+ * @param featureId - 功能ID
747
+ * @returns 功能对象,如果不存在返回null
748
+ */
749
+ getFeature(featureId: string): IPluginFeature | null
750
+
751
+ /**
752
+ * 设置功能优先级
753
+ * @param featureId - 功能ID
754
+ * @param priority - 优先级值(数字越大优先级越高)
755
+ * @returns 是否设置成功
756
+ */
757
+ setPriority(featureId: string, priority: number): boolean
758
+
759
+ /**
760
+ * 获取功能优先级
761
+ * @param featureId - 功能ID
762
+ * @returns 优先级值,如果功能不存在返回null
763
+ */
764
+ getPriority(featureId: string): number | null
765
+
766
+ /**
767
+ * 按优先级排序获取所有功能
768
+ * @returns 按优先级排序的功能列表(高优先级在前)
769
+ */
770
+ getFeaturesByPriority(): IPluginFeature[]
771
+
772
+ /**
773
+ * 批量设置功能优先级
774
+ * @param priorities - 优先级映射对象 {featureId: priority}
775
+ * @returns 设置成功的功能数量
776
+ */
777
+ setPriorities(priorities: Record<string, number>): number
778
+
779
+ /**
780
+ * 重置功能优先级为默认值(0)
781
+ * @param featureId - 功能ID
782
+ * @returns 是否重置成功
783
+ */
784
+ resetPriority(featureId: string): boolean
785
+
786
+ /**
787
+ * 获取功能统计信息
788
+ * @returns 功能统计对象
789
+ */
790
+ getStats(): {
791
+ total: number
792
+ byPriority: Record<number, number>
793
+ averagePriority: number
794
+ }
795
+ }
796
+
797
+ /**
798
+ * 插件信息管理器接口
799
+ *
800
+ * @description
801
+ * 提供插件信息查询功能
802
+ */
803
+ export interface IPluginInfoManager {
804
+ /**
805
+ * 获取完整插件信息
806
+ * @returns 包含所有插件信息的对象
807
+ */
808
+ getInfo(): {
809
+ name: string
810
+ version: string
811
+ desc: string
812
+ readme: string
813
+ dev: any
814
+ status: number
815
+ platforms: any
816
+ pluginPath: string
817
+ features: any[]
818
+ issues: any[]
819
+ }
820
+
821
+ /**
822
+ * 获取插件路径
823
+ * @returns 插件文件系统路径
824
+ */
825
+ getPath(): string
826
+
827
+ /**
828
+ * 获取插件状态
829
+ * @returns 当前插件状态
830
+ */
831
+ getStatus(): number
832
+
833
+ /**
834
+ * 获取开发信息
835
+ * @returns 开发配置信息
836
+ */
837
+ getDevInfo(): any
838
+
839
+ /**
840
+ * 获取平台支持信息
841
+ * @returns 平台兼容性信息
842
+ */
843
+ getPlatforms(): any
844
+ }
@@ -85,6 +85,15 @@ export class TouchSDK {
85
85
  return this.channel.send('plugin:explorer', pluginName)
86
86
  }
87
87
 
88
+ /**
89
+ * Reloads a plugin by its name
90
+ * @param pluginName - The name of the plugin to reload
91
+ * @returns Promise that resolves when the reload operation completes
92
+ */
93
+ async reloadPlugin(pluginName: string): Promise<void> {
94
+ return this.channel.send('reload-plugin', { name: pluginName })
95
+ }
96
+
88
97
  /**
89
98
  * Module Operations
90
99
  */