@talex-touch/utils 1.0.31 → 1.0.33

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.
Files changed (116) hide show
  1. package/animation/window-node.ts +15 -12
  2. package/animation/window.ts +19 -15
  3. package/auth/clerk-types.ts +1 -1
  4. package/auth/index.ts +1 -1
  5. package/auth/useAuthState.ts +6 -5
  6. package/auth/useClerkConfig.ts +4 -4
  7. package/auth/useClerkProvider.ts +3 -2
  8. package/channel/index.ts +23 -22
  9. package/common/file-scan-constants.ts +137 -121
  10. package/common/file-scan-utils.ts +48 -27
  11. package/common/index.ts +3 -3
  12. package/common/search/gather.ts +1 -1
  13. package/common/search/index.ts +5 -6
  14. package/common/storage/constants.ts +3 -2
  15. package/common/storage/entity/app-settings.ts +5 -3
  16. package/common/storage/entity/shortcut-settings.ts +10 -10
  17. package/common/storage/shortcut-storage.ts +6 -4
  18. package/common/utils/file.ts +14 -6
  19. package/common/utils/index.ts +62 -52
  20. package/common/utils/polling.ts +88 -84
  21. package/common/utils/task-queue.ts +11 -10
  22. package/common/utils/time.ts +50 -47
  23. package/common/utils/timing.ts +41 -37
  24. package/core-box/builder/index.ts +1 -1
  25. package/core-box/builder/tuff-builder.ts +254 -229
  26. package/core-box/index.ts +4 -6
  27. package/core-box/preview/index.ts +1 -0
  28. package/core-box/preview/types.ts +43 -0
  29. package/core-box/recommendation.ts +77 -0
  30. package/core-box/tuff/index.ts +1 -1
  31. package/core-box/tuff/tuff-dsl.ts +328 -266
  32. package/electron/download-manager.ts +43 -42
  33. package/electron/env-tool.ts +19 -18
  34. package/electron/file-parsers/index.ts +2 -2
  35. package/electron/file-parsers/parsers/text-parser.ts +15 -14
  36. package/electron/file-parsers/registry.ts +9 -7
  37. package/electron/file-parsers/types.ts +4 -4
  38. package/electron/index.ts +2 -2
  39. package/eventbus/index.ts +11 -11
  40. package/index.ts +5 -4
  41. package/intelligence/client.ts +87 -0
  42. package/intelligence/index.ts +1 -0
  43. package/package.json +14 -14
  44. package/permission/index.ts +8 -8
  45. package/plugin/channel.ts +77 -68
  46. package/plugin/index.ts +96 -82
  47. package/plugin/install.ts +8 -8
  48. package/plugin/log/types.ts +5 -5
  49. package/plugin/node/index.ts +1 -1
  50. package/plugin/node/logger-manager.ts +14 -11
  51. package/plugin/node/logger.ts +8 -8
  52. package/plugin/plugin-source.ts +11 -11
  53. package/plugin/preload.ts +1 -1
  54. package/plugin/providers/registry.ts +8 -7
  55. package/plugin/providers/types.ts +6 -6
  56. package/plugin/sdk/README.md +216 -0
  57. package/plugin/sdk/box-sdk.ts +219 -0
  58. package/plugin/sdk/channel.ts +20 -20
  59. package/plugin/sdk/clipboard.ts +8 -6
  60. package/plugin/sdk/common.ts +10 -6
  61. package/plugin/sdk/core-box.ts +2 -3
  62. package/plugin/sdk/division-box.ts +266 -0
  63. package/plugin/sdk/enum/bridge-event.ts +1 -1
  64. package/plugin/sdk/examples/storage-onDidChange-example.js +1 -1
  65. package/plugin/sdk/feature-sdk.ts +235 -0
  66. package/plugin/sdk/features.ts +34 -26
  67. package/plugin/sdk/hooks/bridge.ts +3 -6
  68. package/plugin/sdk/hooks/index.ts +1 -1
  69. package/plugin/sdk/hooks/life-cycle.ts +4 -10
  70. package/plugin/sdk/index.ts +10 -7
  71. package/plugin/sdk/service/index.ts +3 -3
  72. package/plugin/sdk/storage.ts +4 -4
  73. package/plugin/sdk/system.ts +1 -1
  74. package/plugin/sdk/types.ts +165 -146
  75. package/plugin/sdk/window/index.ts +8 -5
  76. package/preload/loading.ts +6 -6
  77. package/preload/renderer.ts +4 -2
  78. package/renderer/hooks/arg-mapper.ts +1 -2
  79. package/renderer/hooks/index.ts +2 -0
  80. package/renderer/hooks/initialize.ts +10 -8
  81. package/renderer/hooks/performance.ts +4 -4
  82. package/renderer/hooks/use-channel.ts +150 -0
  83. package/renderer/hooks/use-intelligence.ts +236 -0
  84. package/renderer/index.ts +6 -2
  85. package/renderer/ref.ts +32 -36
  86. package/renderer/slots.ts +29 -26
  87. package/renderer/storage/app-settings.ts +16 -6
  88. package/renderer/storage/base-storage.ts +222 -114
  89. package/renderer/storage/index.ts +3 -0
  90. package/renderer/storage/intelligence-storage.ts +218 -0
  91. package/renderer/storage/openers.ts +13 -3
  92. package/renderer/touch-sdk/env.ts +41 -41
  93. package/renderer/touch-sdk/index.ts +1 -1
  94. package/renderer/touch-sdk/terminal.ts +5 -5
  95. package/renderer/touch-sdk/utils.ts +4 -3
  96. package/search/levenshtein-utils.ts +11 -11
  97. package/search/types.ts +102 -102
  98. package/service/index.ts +11 -11
  99. package/service/protocol/index.ts +217 -14
  100. package/types/division-box.ts +248 -0
  101. package/types/download.ts +72 -34
  102. package/types/index.ts +3 -1
  103. package/types/intelligence.ts +607 -0
  104. package/types/modules/base.ts +16 -16
  105. package/types/modules/index.ts +1 -1
  106. package/types/modules/module-lifecycle.ts +21 -21
  107. package/types/modules/module-manager.ts +11 -11
  108. package/types/modules/module.ts +16 -16
  109. package/types/storage.ts +0 -1
  110. package/types/touch-app-core.ts +32 -32
  111. package/types/update.ts +91 -21
  112. package/core-box/README.md +0 -218
  113. package/core-box/builder/tuff-builder.example.ts.bak +0 -258
  114. package/core-box/run-tests.sh +0 -7
  115. package/core-box/search.ts +0 -1
  116. package/electron/clipboard-helper.ts +0 -199
package/types/update.ts CHANGED
@@ -1,11 +1,15 @@
1
- // 更新源类型枚举
1
+ /**
2
+ * Known update provider types.
3
+ */
2
4
  export enum UpdateProviderType {
3
5
  GITHUB = 'github',
4
6
  OFFICIAL = 'official',
5
- CUSTOM = 'custom'
7
+ CUSTOM = 'custom',
6
8
  }
7
9
 
8
- // 更新源配置接口
10
+ /**
11
+ * Describes the remote endpoint that serves release metadata.
12
+ */
9
13
  export interface UpdateSourceConfig {
10
14
  type: UpdateProviderType
11
15
  name: string
@@ -14,7 +18,9 @@ export interface UpdateSourceConfig {
14
18
  priority: number
15
19
  }
16
20
 
17
- // 下载资源接口
21
+ /**
22
+ * Represents a downloadable build artifact.
23
+ */
18
24
  export interface DownloadAsset {
19
25
  name: string
20
26
  url: string
@@ -24,7 +30,9 @@ export interface DownloadAsset {
24
30
  checksum?: string
25
31
  }
26
32
 
27
- // GitHub Release接口(兼容GitHub API格式)
33
+ /**
34
+ * Minimal subset of the GitHub release payload that the updater consumes.
35
+ */
28
36
  export interface GitHubRelease {
29
37
  tag_name: string
30
38
  name: string
@@ -33,7 +41,9 @@ export interface GitHubRelease {
33
41
  assets: DownloadAsset[]
34
42
  }
35
43
 
36
- // 更新检查结果接口
44
+ /**
45
+ * Standardized shape of an update check response.
46
+ */
37
47
  export interface UpdateCheckResult {
38
48
  hasUpdate: boolean
39
49
  release?: GitHubRelease
@@ -41,7 +51,21 @@ export interface UpdateCheckResult {
41
51
  source: string
42
52
  }
43
53
 
44
- // 自定义更新源配置
54
+ export type UpdateUserAction = 'update-now' | 'skip' | 'remind-later'
55
+
56
+ export interface CachedUpdateRecord {
57
+ release: GitHubRelease
58
+ channel: AppPreviewChannel
59
+ status: 'pending' | 'skipped' | 'snoozed' | 'acknowledged'
60
+ fetchedAt: number
61
+ snoozeUntil?: number | null
62
+ tag: string
63
+ source: string
64
+ }
65
+
66
+ /**
67
+ * Custom provider definition used to extend the updater beyond the built-ins.
68
+ */
45
69
  export interface CustomUpdateConfig {
46
70
  name: string
47
71
  url: string
@@ -49,49 +73,95 @@ export interface CustomUpdateConfig {
49
73
  headers?: Record<string, string>
50
74
  }
51
75
 
52
- // 应用预览渠道枚举
76
+ /**
77
+ * Build channels supported by the desktop client.
78
+ */
53
79
  export enum AppPreviewChannel {
54
- MASTER = 'master',
55
- SNAPSHOT = 'snapshot'
80
+ RELEASE = 'RELEASE',
81
+ BETA = 'BETA',
82
+ SNAPSHOT = 'SNAPSHOT',
56
83
  }
57
84
 
58
- // 更新设置配置
85
+ /**
86
+ * Scheduler presets used by the polling worker.
87
+ */
88
+ export type UpdateFrequency = 'everyday' | '1day' | '3day' | '7day' | '1month' | 'never'
89
+
90
+ /**
91
+ * User and system configurable update preferences.
92
+ */
59
93
  export interface UpdateSettings {
60
94
  enabled: boolean
61
- frequency: 'startup' | 'daily' | 'weekly' | 'manual'
95
+ frequency: UpdateFrequency
62
96
  source: UpdateSourceConfig
63
- crossChannel: boolean
97
+ updateChannel: AppPreviewChannel
64
98
  ignoredVersions: string[]
65
99
  customSources: CustomUpdateConfig[]
100
+ /**
101
+ * Timestamp (ms) of the last successful update check.
102
+ */
103
+ lastCheckedAt?: number | null
104
+ /**
105
+ * Enable/disable local caching of remote responses.
106
+ */
107
+ cacheEnabled?: boolean
108
+ /**
109
+ * Cache TTL in minutes.
110
+ */
111
+ cacheTTL?: number
112
+ /**
113
+ * Enable retry logic for transient failures.
114
+ */
115
+ rateLimitEnabled?: boolean
116
+ /**
117
+ * Maximum retry attempts when the provider throttles requests.
118
+ */
119
+ maxRetries?: number
120
+ /**
121
+ * Base retry delay in milliseconds.
122
+ */
123
+ retryDelay?: number
66
124
  }
67
125
 
68
- // 默认更新设置
126
+ /**
127
+ * Safe defaults used when no user configuration exists yet.
128
+ */
69
129
  export const defaultUpdateSettings: UpdateSettings = {
70
130
  enabled: true,
71
- frequency: 'startup',
131
+ frequency: 'everyday',
72
132
  source: {
73
133
  type: UpdateProviderType.GITHUB,
74
134
  name: 'GitHub Releases',
75
135
  url: 'https://api.github.com/repos/talex-touch/tuff/releases',
76
136
  enabled: true,
77
- priority: 1
137
+ priority: 1,
78
138
  },
79
- crossChannel: false,
139
+ updateChannel: AppPreviewChannel.RELEASE,
80
140
  ignoredVersions: [],
81
- customSources: []
141
+ customSources: [],
142
+ lastCheckedAt: null,
143
+ cacheEnabled: true,
144
+ cacheTTL: 30,
145
+ rateLimitEnabled: true,
146
+ maxRetries: 3,
147
+ retryDelay: 2000,
82
148
  }
83
149
 
84
- // 更新错误类型
150
+ /**
151
+ * Categorized error types emitted by the updater pipeline.
152
+ */
85
153
  export enum UpdateErrorType {
86
154
  NETWORK_ERROR = 'network_error',
87
155
  TIMEOUT_ERROR = 'timeout_error',
88
156
  API_ERROR = 'api_error',
89
157
  PARSE_ERROR = 'parse_error',
90
158
  VERSION_ERROR = 'version_error',
91
- UNKNOWN_ERROR = 'unknown_error'
159
+ UNKNOWN_ERROR = 'unknown_error',
92
160
  }
93
161
 
94
- // 更新错误接口
162
+ /**
163
+ * Error type used across providers.
164
+ */
95
165
  export interface UpdateError extends Error {
96
166
  type: UpdateErrorType
97
167
  code?: string
@@ -1,218 +0,0 @@
1
- # Core Box Package
2
-
3
- The Core Box package provides unified type definitions and utility functions for the TUFF search box system. This package contains the foundational types and tools used across the entire project for search result management and plugin integration.
4
-
5
- ## Features
6
-
7
- ### 🎯 Unified Type System
8
- - **ISearchItem**: Universal search result item interface
9
- - **IRenderConfig**: Flexible rendering configuration with multiple modes
10
- - **IDataItem**: Extended interface for data processing results
11
- - **IAppItem**: Specialized interface for application results
12
- - **IFileItem**: Specialized interface for file results
13
- - **IFeatureItem**: Specialized interface for plugin feature results
14
-
15
- ### 🎨 Render Modes
16
- The package supports multiple rendering modes for search results:
17
-
18
- - **STANDARD**: Default text-based rendering
19
- - **URL**: URL rendering - loads and displays remote URL content
20
- - **HTML**: Rich HTML content rendering
21
- - **JAVASCRIPT**: JavaScript code with syntax highlighting
22
- - **JSX**: React JSX component rendering
23
- - **VUE_SFC**: Vue Single File Component rendering
24
-
25
- ### 🛠️ Utility Functions
26
- Comprehensive set of utility functions for creating and managing search results:
27
-
28
- - `SearchUtils.createSearchItem()`: Create basic search items
29
- - `SearchUtils.createDataItem()`: Create data processing results
30
- - `SearchUtils.createAppItem()`: Create application results
31
- - `SearchUtils.createFileItem()`: Create file results
32
- - `SearchUtils.createFeatureItem()`: Create plugin feature results
33
- - `SearchUtils.createErrorItem()`: Create error state items
34
- - `SearchUtils.sortByPriority()`: Sort results by relevance
35
- - `SearchUtils.removeDuplicates()`: Remove duplicate results
36
- - `SearchUtils.filterByConfidence()`: Filter by confidence score
37
- - `SearchUtils.groupByPluginType()`: Group results by type
38
-
39
- ## Usage Examples
40
-
41
- ### Basic Search Item
42
- ```typescript
43
- import { SearchUtils, RenderMode } from '@talex-touch/utils';
44
-
45
- const basicItem = SearchUtils.createSearchItem({
46
- name: "My Feature",
47
- desc: "A useful plugin feature",
48
- pluginName: "my-plugin",
49
- keyWords: ["feature", "utility"],
50
- render: {
51
- mode: RenderMode.STANDARD
52
- }
53
- });
54
- ```
55
-
56
- ### Data Processing Result
57
- ```typescript
58
- const dataItem = SearchUtils.createDataItem({
59
- name: "Translation Result",
60
- desc: "English to Chinese translation",
61
- pluginName: "translator",
62
- source: "google-translate",
63
- dataType: "translation",
64
- originalData: "Hello World",
65
- processedData: "你好世界",
66
- confidence: 95,
67
- duration: 120,
68
- render: {
69
- mode: RenderMode.STANDARD
70
- }
71
- });
72
- ```
73
-
74
- ### HTML Rendering
75
- ```typescript
76
- const htmlItem = SearchUtils.createSearchItem({
77
- name: "Rich Content",
78
- desc: "HTML formatted result",
79
- pluginName: "content-plugin",
80
- render: {
81
- mode: RenderMode.HTML,
82
- content: "<div><h3>Title</h3><p>Content</p></div>",
83
- options: {
84
- trusted: true,
85
- className: "custom-content"
86
- }
87
- }
88
- });
89
- ```
90
-
91
- ### Code Rendering
92
- ```typescript
93
- const codeItem = SearchUtils.createDataItem({
94
- name: "JavaScript Function",
95
- desc: "Generated code snippet",
96
- pluginName: "code-generator",
97
- dataType: "javascript",
98
- processedData: "function hello() { return 'world'; }",
99
- render: {
100
- mode: RenderMode.JAVASCRIPT,
101
- content: "function hello() { return 'world'; }",
102
- options: {
103
- syntaxHighlight: true,
104
- showLineNumbers: true,
105
- theme: "dark"
106
- }
107
- }
108
- });
109
- ```
110
-
111
- ### URL Rendering with Preview
112
- ```typescript
113
- const urlItem = SearchUtils.createSearchItem({
114
- name: "GitHub Repository",
115
- desc: "TUFF project",
116
- pluginName: "web-search",
117
- render: {
118
- mode: RenderMode.URL,
119
- content: "https://github.com/talex-touch/talex-touch", // The actual URL to load
120
- preview: {
121
- enabled: true,
122
- title: "Tuff", // Preview metadata
123
- description: "Modern desktop application framework", // Preview description
124
- image: "https://github.com/talex-touch.png" // Preview image
125
- }
126
- }
127
- });
128
- ```
129
-
130
- **Note**:
131
- - `content`: The remote URL that will be loaded and displayed
132
- - `preview`: Metadata shown before/alongside the loaded URL content
133
-
134
- ## Type Definitions
135
-
136
- ### ISearchItem
137
- The core interface for all search results:
138
-
139
- ```typescript
140
- interface ISearchItem {
141
- name: string; // Display name
142
- desc: string; // Description
143
- icon: ITuffIcon; // Icon configuration
144
- push: boolean; // Push mode support
145
- names: string[]; // Searchable names
146
- keyWords: string[]; // Search keywords
147
- pluginType: string; // Plugin type
148
- type: string; // General type
149
- value: string; // Associated value
150
- render?: IRenderConfig; // Render configuration
151
- // ... additional properties
152
- }
153
- ```
154
-
155
- ### IRenderConfig
156
- Configuration for custom rendering:
157
-
158
- ```typescript
159
- interface IRenderConfig {
160
- mode: RenderMode; // Rendering mode
161
- content?: string; // Content to render
162
- options?: { // Render options
163
- syntaxHighlight?: boolean;
164
- theme?: string;
165
- showLineNumbers?: boolean;
166
- className?: string;
167
- style?: Record<string, string>;
168
- trusted?: boolean;
169
- };
170
- preview?: { // URL preview config
171
- enabled?: boolean;
172
- image?: string;
173
- title?: string;
174
- description?: string;
175
- };
176
- }
177
- ```
178
-
179
- ## Integration
180
-
181
- This package is automatically exported from `@talex-touch/utils`:
182
-
183
- ```typescript
184
- import {
185
- ISearchItem,
186
- IDataItem,
187
- SearchUtils,
188
- RenderMode
189
- } from '@talex-touch/utils';
190
- ```
191
-
192
- ## Best Practices
193
-
194
- 1. **Always specify render mode**: Even for standard rendering, explicitly set the render mode
195
- 2. **Use appropriate specialized interfaces**: Use IDataItem for data processing, IAppItem for applications, etc.
196
- 3. **Include confidence scores**: For data processing results, always include confidence levels
197
- 4. **Sanitize HTML content**: When using HTML render mode, ensure content is safe
198
- 5. **Provide meaningful keywords**: Include relevant search terms in the keyWords array
199
- 6. **Use consistent naming**: Follow established naming conventions for plugin types
200
-
201
- ## Migration Guide
202
-
203
- If you're migrating from the old type system:
204
-
205
- 1. Replace `ITranslationItem` with `IDataItem`
206
- 2. Update render configurations to use the new `IRenderConfig` interface
207
- 3. Use `SearchUtils` functions instead of manual object creation
208
- 4. Update import paths to use `@talex-touch/utils`
209
-
210
- ## Contributing
211
-
212
- When adding new types or utilities:
213
-
214
- 1. Add comprehensive TSDoc comments in English
215
- 2. Include usage examples in documentation
216
- 3. Follow the established naming conventions
217
- 4. Add appropriate unit tests
218
- 5. Update this README with new features
@@ -1,258 +0,0 @@
1
- /**
2
- * TUFF Builder 示例文件
3
- * 展示如何使用 TuffBuilder 工具类创建和管理 TuffItem 对象
4
- */
5
-
6
- import { TuffItemBuilder, TuffFactory, TuffListBuilder, TuffUtils } from './tuff-builder';
7
- import type { TuffItem } from '../tuff/tuff-dsl';
8
-
9
- // ==================== 基本用法示例 ====================
10
-
11
- /**
12
- * 示例 1: 使用 TuffItemBuilder 创建单个项目
13
- */
14
- function createSingleItem(): TuffItem {
15
- // 使用 Builder 模式创建一个完整的 TuffItem
16
- const item = new TuffItemBuilder('doc-1')
17
- .setSource('plugin', 'file-explorer')
18
- .setTitle('文档.docx')
19
- .setDescription('Word 文档')
20
- .setIcon(TuffUtils.createIcon('📄'))
21
- .setKind('file')
22
- .addTag(TuffUtils.createTag('文档', '#4285F4'))
23
- .addTag(TuffUtils.createTag('最近', '#34A853'))
24
- .setAccessory('2023-06-15')
25
- .addAction(TuffUtils.createAction('open', 'open', '打开', true))
26
- .addAction(TuffUtils.createAction('copy', 'copy', '复制路径', false))
27
- .setMeta({
28
- file: {
29
- path: '/Users/documents/文档.docx',
30
- size: 1024 * 1024 * 2, // 2MB
31
- modified_at: '2023-06-15T10:30:00Z'
32
- }
33
- })
34
- .build();
35
-
36
- return item;
37
- }
38
-
39
- /**
40
- * 示例 2: 使用 TuffFactory 快速创建常见类型的项目
41
- */
42
- function createCommonItems(): TuffItem[] {
43
- const items: TuffItem[] = [];
44
-
45
- // 创建基本项目
46
- const basicItem = TuffFactory.createBasicItem(
47
- '基本项目',
48
- 'system',
49
- 'basic-example',
50
- 'text'
51
- );
52
- items.push(basicItem);
53
-
54
- // 创建文件项目
55
- const fileItem = TuffFactory.createFileItem(
56
- 'config.json',
57
- '/Users/config.json',
58
- 'plugin',
59
- 'settings-manager'
60
- );
61
- items.push(fileItem);
62
-
63
- // 创建文件夹项目
64
- const folderItem = TuffFactory.createFolderItem(
65
- '项目文件夹',
66
- '/Users/projects',
67
- 'plugin',
68
- 'file-explorer'
69
- );
70
- items.push(folderItem);
71
-
72
- // 创建链接项目
73
- const urlItem = TuffFactory.createUrlItem(
74
- 'Tuff 文档',
75
- 'https://example.com/tuff-docs',
76
- 'plugin',
77
- 'web-search'
78
- );
79
- items.push(urlItem);
80
-
81
- // 创建应用项目
82
- const appItem = TuffFactory.createAppItem(
83
- 'Visual Studio Code',
84
- '/Applications/Visual Studio Code.app',
85
- 'com.microsoft.VSCode',
86
- 'plugin',
87
- 'app-launcher'
88
- );
89
- items.push(appItem);
90
-
91
- // 创建命令项目
92
- const commandItem = TuffFactory.createCommandItem(
93
- '查看系统信息',
94
- 'system_profiler SPHardwareDataType',
95
- 'plugin',
96
- 'terminal'
97
- );
98
- items.push(commandItem);
99
-
100
- return items;
101
- }
102
-
103
- /**
104
- * 示例 3: 使用 TuffBatchBuilder 批量创建项目
105
- */
106
- function createBatchItems(): TuffItem[] {
107
- // 创建批量构建器,设置共享的来源信息
108
- const batchBuilder = new TuffListBuilder('plugin', 'file-explorer')
109
- .setSharedKind('file')
110
-
111
- .addSharedAction(TuffUtils.createAction('open', 'open', '打开', true));
112
-
113
- // 添加多个项目
114
- batchBuilder
115
- .addItem(builder => {
116
- builder
117
- .setTitle('文档1.docx')
118
- .setIcon(TuffUtils.createIcon('📄'))
119
- .setDescription('Word 文档')
120
- .addTag(TuffUtils.createTag('文档', '#4285F4'));
121
- })
122
- .addItem(builder => {
123
- builder
124
- .setTitle('图片.jpg')
125
- .setIcon(TuffUtils.createIcon('🖼️'))
126
- .setDescription('JPG 图片')
127
- .addTag(TuffUtils.createTag('图片', '#FBBC05'));
128
- })
129
- .addItem(builder => {
130
- builder
131
- .setTitle('表格.xlsx')
132
- .setIcon(TuffUtils.createIcon('📊'))
133
- .setDescription('Excel 表格')
134
- .addTag(TuffUtils.createTag('表格', '#34A853'));
135
- });
136
-
137
- // 批量添加简单项目
138
- batchBuilder.addItemsFromData([
139
- { name: '笔记1.txt' },
140
- { name: '笔记2.txt' },
141
- { name: '笔记3.txt' }
142
- ], (builder, item) => builder.setTitle(item.name));
143
-
144
- // 从数据对象批量创建项目
145
- const fileData = [
146
- { name: '报告.pdf', type: 'pdf', size: 1024 * 1024 * 3, modified: '2023-06-10' },
147
- { name: '演示.pptx', type: 'pptx', size: 1024 * 1024 * 5, modified: '2023-06-12' },
148
- { name: '数据.csv', type: 'csv', size: 1024 * 512, modified: '2023-06-14' }
149
- ];
150
-
151
- batchBuilder.addItemsFromData(fileData, (builder, file) => {
152
- let icon = '📄';
153
- if (file.type === 'pdf') icon = '📕';
154
- if (file.type === 'pptx') icon = '📊';
155
- if (file.type === 'csv') icon = '📈';
156
-
157
- builder
158
- .setTitle(file.name)
159
- .setIcon(TuffUtils.createIcon(icon))
160
- .setDescription(`${file.type.toUpperCase()} 文件`)
161
- .setAccessory(file.modified)
162
- .setMeta({
163
- file: {
164
- path: file.name,
165
- size: file.size,
166
- modified_at: file.modified
167
- }
168
- });
169
- });
170
-
171
- return batchBuilder.build();
172
- }
173
-
174
- /**
175
- * 示例 4: 使用 TuffUtils 处理项目列表
176
- */
177
- function processItems(items: TuffItem[]): void {
178
- // 过滤文件类型的项目
179
- const fileItems = TuffUtils.filterByKind(items, 'file');
180
- console.log(`文件项目数量: ${fileItems.length}`);
181
-
182
- // 按标题搜索项目
183
- const searchResults = TuffUtils.searchByTitle(items, '文档');
184
- console.log(`包含"文档"的项目数量: ${searchResults.length}`);
185
-
186
- // 按评分排序项目
187
- const sortedByScore = TuffUtils.sortByScore(items);
188
- console.log('按评分排序的前 3 个项目:');
189
- sortedByScore.slice(0, 3).forEach(item => {
190
- console.log(` - ${item.render.basic?.title} (评分: ${item.scoring?.final ?? 0})`);
191
- });
192
-
193
- // 按标题排序项目
194
- const sortedByTitle = TuffUtils.sortByTitle(items);
195
- console.log('按标题排序的前 3 个项目:');
196
- sortedByTitle.slice(0, 3).forEach(item => {
197
- console.log(` - ${item.render.basic?.title}`);
198
- });
199
- }
200
-
201
- /**
202
- * 示例 5: 从普通对象创建 TuffItem
203
- */
204
- function createFromObjects(): TuffItem[] {
205
- // 示例数据
206
- const data = [
207
- { name: '项目 A', description: '这是项目 A 的描述', type: 'project' },
208
- { name: '任务 B', description: '这是任务 B 的描述', type: 'task', priority: 'high' },
209
- { name: '笔记 C', description: '这是笔记 C 的内容', type: 'note', tags: ['重要', '工作'] }
210
- ];
211
-
212
- // 从对象数组创建 TuffItem 数组
213
- return TuffUtils.fromObjects(data, 'plugin', 'data-converter');
214
- }
215
-
216
- /**
217
- * 运行所有示例
218
- */
219
- function runAllExamples(): void {
220
- console.log('===== 示例 1: 使用 TuffItemBuilder 创建单个项目 =====');
221
- const singleItem = createSingleItem();
222
- console.log(JSON.stringify(singleItem, null, 2));
223
-
224
- console.log('\n===== 示例 2: 使用 TuffFactory 快速创建常见类型的项目 =====');
225
- const commonItems = createCommonItems();
226
- console.log(`创建了 ${commonItems.length} 个项目`);
227
-
228
- console.log('\n===== 示例 3: 使用 TuffBatchBuilder 批量创建项目 =====');
229
- const batchItems = createBatchItems();
230
- console.log(`批量创建了 ${batchItems.length} 个项目`);
231
-
232
- console.log('\n===== 示例 4: 使用 TuffUtils 处理项目列表 =====');
233
- // 合并所有创建的项目
234
- const allItems = [singleItem, ...commonItems, ...batchItems];
235
- processItems(allItems);
236
-
237
- console.log('\n===== 示例 5: 从普通对象创建 TuffItem =====');
238
- const objectItems = createFromObjects();
239
- console.log(`从对象创建了 ${objectItems.length} 个项目`);
240
- objectItems.forEach(item => {
241
- console.log(` - ${item.render.basic?.title}`);
242
- });
243
- }
244
-
245
- // 如果直接运行此文件,则执行所有示例
246
- if (require.main === module) {
247
- runAllExamples();
248
- }
249
-
250
- // 导出示例函数,以便其他模块可以使用
251
- export {
252
- createSingleItem,
253
- createCommonItems,
254
- createBatchItems,
255
- processItems,
256
- createFromObjects,
257
- runAllExamples
258
- };
@@ -1,7 +0,0 @@
1
- #!/bin/bash
2
-
3
- # 运行 TUFF Builder 测试
4
- # 测试文件位于 packages/test/src/core-box 目录下
5
- cd "$(dirname "$0")"
6
- cd ../../../
7
- pnpm --filter test test
@@ -1 +0,0 @@
1
- export {}