@talex-touch/utils 1.0.24 → 1.0.25
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.
package/package.json
CHANGED
package/plugin/index.ts
CHANGED
|
@@ -59,7 +59,7 @@ export interface IPluginBaseInfo {
|
|
|
59
59
|
export interface IPluginDev {
|
|
60
60
|
enable: boolean
|
|
61
61
|
address: string
|
|
62
|
-
source?:
|
|
62
|
+
source?: boolean
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
export interface ITouchPlugin extends IPluginBaseInfo {
|
|
@@ -81,6 +81,47 @@ export interface ITouchPlugin extends IPluginBaseInfo {
|
|
|
81
81
|
|
|
82
82
|
enable(): Promise<boolean>
|
|
83
83
|
disable(): Promise<boolean>
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Get the plugin file.
|
|
87
|
+
* @param fileName The name of the file.
|
|
88
|
+
* @returns The content of the file.
|
|
89
|
+
*/
|
|
90
|
+
getPluginFile(fileName: string): object
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Save the plugin file.
|
|
94
|
+
* @param fileName The name of the file.
|
|
95
|
+
* @param content The content of the file.
|
|
96
|
+
* @returns The result of the save operation.
|
|
97
|
+
*/
|
|
98
|
+
savePluginFile(fileName: string, content: object): { success: boolean; error?: string }
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Delete the plugin file.
|
|
102
|
+
* @param fileName The name of the file.
|
|
103
|
+
* @returns The result of the delete operation.
|
|
104
|
+
*/
|
|
105
|
+
deletePluginFile(fileName: string): { success: boolean; error?: string }
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* List all files in the plugin.
|
|
109
|
+
* @returns The list of files.
|
|
110
|
+
*/
|
|
111
|
+
listPluginFiles(): string[]
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Get the plugin configuration.
|
|
115
|
+
* @returns The configuration content.
|
|
116
|
+
*/
|
|
117
|
+
getPluginConfig(): object
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Save the plugin configuration.
|
|
121
|
+
* @param content The configuration content.
|
|
122
|
+
* @returns The result of the save operation.
|
|
123
|
+
*/
|
|
124
|
+
savePluginConfig(content: object): { success: boolean; error?: string }
|
|
84
125
|
}
|
|
85
126
|
|
|
86
127
|
export interface IFeatureCommand {
|
|
@@ -124,6 +165,13 @@ export interface IFeatureLifeCycle {
|
|
|
124
165
|
* Can be used to prepare data or UI specific to this session.
|
|
125
166
|
*/
|
|
126
167
|
onInit?(): void
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Called when a message is received from the main application.
|
|
171
|
+
* @param key - The key of the message
|
|
172
|
+
* @param info - The information of the message
|
|
173
|
+
*/
|
|
174
|
+
onMessage?(key: string, info: any): void
|
|
127
175
|
/**
|
|
128
176
|
* Called when a feature is actively launched from the launcher.
|
|
129
177
|
* Can be used to prepare data or UI specific to this session.
|
package/plugin/sdk/common.ts
CHANGED
|
@@ -1,28 +1,77 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Plugin SDK Common Utilities
|
|
3
|
+
*
|
|
4
|
+
* @description
|
|
5
|
+
* 提供插件SDK的通用功能,包括通信、快捷键等
|
|
6
|
+
*/
|
|
3
7
|
|
|
4
|
-
|
|
5
|
-
const channel = genChannel()
|
|
8
|
+
import { genChannel } from '../channel'
|
|
6
9
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
+
/**
|
|
11
|
+
* Register a shortcut
|
|
12
|
+
* @param key - The shortcut combination
|
|
13
|
+
* @param func - The trigger function
|
|
14
|
+
* @returns Whether the shortcut is registered successfully
|
|
15
|
+
*/
|
|
16
|
+
export function regShortcut(key: string, func: Function): boolean {
|
|
17
|
+
const channel = genChannel()
|
|
18
|
+
|
|
19
|
+
const res = channel.sendSync('shortcon:reg', { key })
|
|
20
|
+
if (res instanceof String) throw new Error(String(res))
|
|
21
|
+
if (res === false) return false
|
|
10
22
|
|
|
11
|
-
|
|
23
|
+
channel.regChannel('shortcon:trigger', ({ data }) => key === data.key && func())
|
|
12
24
|
|
|
13
|
-
|
|
25
|
+
return true
|
|
14
26
|
}
|
|
15
27
|
|
|
16
|
-
|
|
28
|
+
/**
|
|
29
|
+
* Communicate with other plugins via the index:communicate channel
|
|
30
|
+
* @param key - The message key
|
|
31
|
+
* @param info - The message data
|
|
32
|
+
* @returns Promise<any> The communication result
|
|
33
|
+
*/
|
|
34
|
+
export async function communicateWithPlugin(
|
|
35
|
+
key: string,
|
|
36
|
+
info: any = {}
|
|
37
|
+
): Promise<any> {
|
|
17
38
|
const channel = genChannel()
|
|
18
39
|
|
|
19
|
-
|
|
40
|
+
try {
|
|
41
|
+
return await channel.send('index:communicate', {
|
|
42
|
+
key,
|
|
43
|
+
info
|
|
44
|
+
})
|
|
45
|
+
} catch (error) {
|
|
46
|
+
console.error(`[Plugin SDK] Failed to communicate`, error)
|
|
47
|
+
throw error
|
|
48
|
+
}
|
|
20
49
|
}
|
|
21
50
|
|
|
22
|
-
|
|
51
|
+
/**
|
|
52
|
+
* Send a message to the main application
|
|
53
|
+
* @param message - The message type
|
|
54
|
+
* @param data - The message data
|
|
55
|
+
* @returns Promise<any> The message result
|
|
56
|
+
*/
|
|
57
|
+
export async function sendMessage(message: string, data: any = {}): Promise<any> {
|
|
23
58
|
const channel = genChannel()
|
|
24
59
|
|
|
25
|
-
|
|
60
|
+
try {
|
|
61
|
+
return await channel.send(`plugin:${message}`, data)
|
|
62
|
+
} catch (error) {
|
|
63
|
+
console.error(`[Plugin SDK] Failed to send message: ${message}`, error)
|
|
64
|
+
throw error
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Get the channel object for the plugin
|
|
70
|
+
* Attention: Using this function make sure you know what you are doing.
|
|
71
|
+
* @returns The channel object for the plugin
|
|
72
|
+
*/
|
|
73
|
+
export function getChannel() {
|
|
74
|
+
return genChannel()
|
|
26
75
|
}
|
|
27
76
|
|
|
28
|
-
export * from './window'
|
|
77
|
+
export * from './window'
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Storage onDidChange 示例
|
|
3
|
+
* 展示如何使用 storage.onDidChange 监听存储文件变化
|
|
4
|
+
* 注意:这里只能获取新值,旧值无法获取
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
// 获取SDK实例
|
|
8
|
+
const sdk = window.$touchSDK
|
|
9
|
+
|
|
10
|
+
// 示例1: 监听整个配置文件的变化
|
|
11
|
+
function setupConfigListener() {
|
|
12
|
+
console.log('[Storage] Setting up listener for entire config changes')
|
|
13
|
+
|
|
14
|
+
const unsubscribe = sdk.storage.onDidChange((newConfig) => {
|
|
15
|
+
console.log('[Storage] Config changed:')
|
|
16
|
+
console.log(' New config:', newConfig)
|
|
17
|
+
|
|
18
|
+
// 处理特定键的变化
|
|
19
|
+
if (newConfig && newConfig.user_preference && newConfig.user_preference.theme) {
|
|
20
|
+
console.log(`[Storage] Theme changed to: ${newConfig.user_preference.theme}`)
|
|
21
|
+
}
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
// 返回取消监听的函数
|
|
25
|
+
return unsubscribe
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// 示例2: 监听配置变化并处理特定键
|
|
29
|
+
function setupConfigChangeHandler() {
|
|
30
|
+
console.log('[Storage] Setting up config change handler')
|
|
31
|
+
|
|
32
|
+
const unsubscribe = sdk.storage.onDidChange((newConfig) => {
|
|
33
|
+
console.log('[Storage] Config changed:')
|
|
34
|
+
console.log(' New config:', newConfig)
|
|
35
|
+
|
|
36
|
+
// 处理特定键的变化
|
|
37
|
+
if (newConfig.user_preference) {
|
|
38
|
+
handleUserPreferenceChange(newConfig.user_preference)
|
|
39
|
+
}
|
|
40
|
+
if (newConfig.settings) {
|
|
41
|
+
handleSettingsChange(newConfig.settings)
|
|
42
|
+
}
|
|
43
|
+
if (newConfig.cache) {
|
|
44
|
+
handleCacheChange(newConfig.cache)
|
|
45
|
+
}
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
return unsubscribe
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// 示例3: 动态监听
|
|
52
|
+
function setupDynamicListener() {
|
|
53
|
+
let currentListener = null
|
|
54
|
+
|
|
55
|
+
// 开始监听
|
|
56
|
+
function startListening() {
|
|
57
|
+
console.log('[Storage] Starting to listen for config changes')
|
|
58
|
+
|
|
59
|
+
// 如果已有监听器,先取消
|
|
60
|
+
if (currentListener) {
|
|
61
|
+
currentListener()
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
currentListener = sdk.storage.onDidChange((newConfig) => {
|
|
65
|
+
console.log('[Storage] Dynamic listener for config changes:')
|
|
66
|
+
console.log(' New config:', newConfig)
|
|
67
|
+
})
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// 停止监听
|
|
71
|
+
function stopListening() {
|
|
72
|
+
if (currentListener) {
|
|
73
|
+
console.log('[Storage] Stopping current listener')
|
|
74
|
+
currentListener()
|
|
75
|
+
currentListener = null
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return { startListening, stopListening }
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// 示例4: 条件监听
|
|
83
|
+
function setupConditionalListener() {
|
|
84
|
+
console.log('[Storage] Setting up conditional listener')
|
|
85
|
+
|
|
86
|
+
const unsubscribe = sdk.storage.onDidChange((newConfig) => {
|
|
87
|
+
// 处理用户偏好变化
|
|
88
|
+
if (newConfig.user_preference) {
|
|
89
|
+
const pref = newConfig.user_preference
|
|
90
|
+
|
|
91
|
+
// 处理主题变化
|
|
92
|
+
if (pref.theme) {
|
|
93
|
+
console.log(`[Storage] Theme: ${pref.theme}`)
|
|
94
|
+
applyTheme(pref.theme)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// 处理语言变化
|
|
98
|
+
if (pref.language) {
|
|
99
|
+
console.log(`[Storage] Language: ${pref.language}`)
|
|
100
|
+
reloadInterface(pref.language)
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
return unsubscribe
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// 示例5: 批量操作监听
|
|
109
|
+
function setupBatchOperationListener() {
|
|
110
|
+
console.log('[Storage] Setting up batch operation listener')
|
|
111
|
+
|
|
112
|
+
const unsubscribe = sdk.storage.onDidChange((newConfig) => {
|
|
113
|
+
console.log('[Storage] Config changed:')
|
|
114
|
+
console.log(' New config keys:', Object.keys(newConfig))
|
|
115
|
+
|
|
116
|
+
// 处理批量数据变化
|
|
117
|
+
if (newConfig.batch_data) {
|
|
118
|
+
console.log('[Storage] Processing batch data changes')
|
|
119
|
+
console.log(' Batch data count:', Object.keys(newConfig.batch_data).length)
|
|
120
|
+
|
|
121
|
+
// 应用变化
|
|
122
|
+
applyChanges(newConfig.batch_data)
|
|
123
|
+
}
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
return unsubscribe
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// 辅助函数
|
|
130
|
+
function handleUserPreferenceChange(newValue) {
|
|
131
|
+
console.log('[Storage] Handling user preference change')
|
|
132
|
+
// 处理用户偏好变化
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function handleSettingsChange(newValue) {
|
|
136
|
+
console.log('[Storage] Handling settings change')
|
|
137
|
+
// 处理设置变化
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function handleCacheChange(newValue) {
|
|
141
|
+
console.log('[Storage] Handling cache change')
|
|
142
|
+
// 处理缓存变化
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function applyTheme(theme) {
|
|
146
|
+
console.log(`[Storage] Applying theme: ${theme}`)
|
|
147
|
+
// 应用主题逻辑
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function reloadInterface(language) {
|
|
151
|
+
console.log(`[Storage] Reloading interface for language: ${language}`)
|
|
152
|
+
// 重新加载界面逻辑
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function findChanges(oldValue, newValue) {
|
|
156
|
+
const changes = []
|
|
157
|
+
// 比较两个对象并找出变化
|
|
158
|
+
return changes
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function applyChanges(changes) {
|
|
162
|
+
console.log('[Storage] Applying changes:', changes)
|
|
163
|
+
// 应用变化逻辑
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// 导出函数供外部调用
|
|
167
|
+
window.storageOnDidChangeExample = {
|
|
168
|
+
setupConfigListener,
|
|
169
|
+
setupConfigChangeHandler,
|
|
170
|
+
setupDynamicListener,
|
|
171
|
+
setupConditionalListener,
|
|
172
|
+
setupBatchOperationListener
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// 自动运行示例(可选)
|
|
176
|
+
if (typeof window !== 'undefined' && window.location.hostname === 'localhost') {
|
|
177
|
+
console.log('[Auto] Setting up storage listeners...')
|
|
178
|
+
|
|
179
|
+
// 设置配置监听
|
|
180
|
+
const unsubscribe1 = setupConfigListener()
|
|
181
|
+
|
|
182
|
+
// 设置配置变化处理器
|
|
183
|
+
const unsubscribeAll = setupConfigChangeHandler()
|
|
184
|
+
|
|
185
|
+
// 设置动态监听
|
|
186
|
+
const { startListening, stopListening } = setupDynamicListener()
|
|
187
|
+
|
|
188
|
+
// 5秒后切换到监听其他键
|
|
189
|
+
setTimeout(() => {
|
|
190
|
+
stopListening()
|
|
191
|
+
startListening('settings')
|
|
192
|
+
}, 5000)
|
|
193
|
+
|
|
194
|
+
// 10秒后停止所有监听
|
|
195
|
+
setTimeout(() => {
|
|
196
|
+
console.log('[Auto] Stopping all listeners...')
|
|
197
|
+
unsubscribe1()
|
|
198
|
+
unsubscribeAll()
|
|
199
|
+
stopListening()
|
|
200
|
+
}, 10000)
|
|
201
|
+
}
|