sk-voice-command 1.0.0 → 1.0.2

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/README.md CHANGED
@@ -43,41 +43,7 @@ Vue.use(SkVoiceCommand, {
43
43
 
44
44
  ### 2. 注册指令
45
45
 
46
- 在页面组件中注册指令:
47
-
48
- ```javascript
49
- export default {
50
- mounted() {
51
- // 注册页面级指令
52
- this.$voice.registerCommands({
53
- scope: 'page',
54
- commands: [
55
- {
56
- id: 'submit-form',
57
- keywords: ['提交', '确认提交', '发送'],
58
- action: () => {
59
- this.submitForm()
60
- },
61
- description: '提交当前表单'
62
- },
63
- {
64
- id: 'scroll-top',
65
- keywords: ['滚动到顶部', '回到顶部'],
66
- action: () => {
67
- uni.pageScrollTo({ scrollTop: 0 })
68
- },
69
- description: '滚动到页面顶部'
70
- }
71
- ]
72
- })
73
- },
74
-
75
- beforeDestroy() {
76
- // 页面卸载时自动清除指令(插件会自动处理,也可以手动调用)
77
- // this.$voice.unregisterCommands('page')
78
- }
79
- }
80
- ```
46
+ 在 App.vue 中注册全局指令,或在页面组件中注册页面指令。详细的使用示例请参考下方的 **registerCommands** 方法文档。
81
47
 
82
48
  ### 3. 使用
83
49
 
@@ -136,7 +102,8 @@ export default {
136
102
 
137
103
  ```javascript
138
104
  this.$voice.registerCommands({
139
- scope: 'page', // 'page' 或 'global'
105
+ scope: 'page', // 'page' 或 'global'
106
+ route: 'pages/index/index', // 路由路径(scope 为 page 时可用,可选)
140
107
  commands: [
141
108
  {
142
109
  id: 'command-id', // 唯一标识(必需)
@@ -149,6 +116,113 @@ this.$voice.registerCommands({
149
116
  })
150
117
  ```
151
118
 
119
+ **参数说明**:
120
+ - `scope` (string) - 作用域:`'page'` (页面指令) | `'global'` (全局指令)
121
+ - `route` (string) - **路由路径**(仅 scope 为 page 时使用)
122
+ - 指定该指令属于哪个页面
123
+ - 如果不传,则使用当前的 `currentRoute`
124
+ - 可以在任意页面为其他页面预注册指令
125
+ - `commands` (array) - 指令数组
126
+
127
+ **使用示例**:
128
+
129
+ ```javascript
130
+ // 方式 1: 在页面中注册(推荐)
131
+ export default {
132
+ mounted() {
133
+ const pages = getCurrentPages()
134
+ const currentPage = pages[pages.length - 1]
135
+
136
+ this.$voice.registerCommands({
137
+ scope: 'page',
138
+ route: currentPage.route, // 指定当前页面路由
139
+ commands: [
140
+ {
141
+ id: 'submit-form',
142
+ keywords: ['提交', '确认提交'],
143
+ action: () => this.submitForm()
144
+ }
145
+ ]
146
+ })
147
+ }
148
+ }
149
+
150
+ // 方式 2: 在 App.vue 中为所有页面预注册指令
151
+ export default {
152
+ onLaunch() {
153
+ // 为首页注册指令
154
+ this.$voice.registerCommands({
155
+ scope: 'page',
156
+ route: 'pages/index/index',
157
+ commands: [
158
+ {
159
+ id: 'refresh-list',
160
+ keywords: ['刷新列表', '重新加载'],
161
+ action: () => {
162
+ // 首页刷新逻辑
163
+ }
164
+ }
165
+ ]
166
+ })
167
+
168
+ // 为用户页注册指令
169
+ this.$voice.registerCommands({
170
+ scope: 'page',
171
+ route: 'pages/user/user',
172
+ commands: [
173
+ {
174
+ id: 'edit-profile',
175
+ keywords: ['修改资料', '编辑信息'],
176
+ action: () => {
177
+ // 用户页编辑逻辑
178
+ }
179
+ }
180
+ ]
181
+ })
182
+
183
+ // 注册全局指令
184
+ this.$voice.registerCommands({
185
+ scope: 'global',
186
+ commands: [
187
+ {
188
+ id: 'go-home',
189
+ keywords: ['返回首页', '回到主页'],
190
+ action: () => uni.switchTab({ url: '/pages/index/index' })
191
+ }
192
+ ]
193
+ })
194
+ }
195
+ }
196
+
197
+ // 方式 3: 向后兼容(不传 route,使用 currentRoute)
198
+ export default {
199
+ mounted() {
200
+ // 先设置当前路由
201
+ const pages = getCurrentPages()
202
+ const currentPage = pages[pages.length - 1]
203
+ this.$voice._matcher.setCurrentRoute(currentPage.route)
204
+
205
+ // 然后注册指令(会自动使用 currentRoute)
206
+ this.$voice.registerCommands({
207
+ scope: 'page',
208
+ commands: [
209
+ {
210
+ id: 'submit-form',
211
+ keywords: ['提交'],
212
+ action: () => this.submitForm()
213
+ }
214
+ ]
215
+ })
216
+ }
217
+ }
218
+ ```
219
+
220
+ **指令作用域规则**:
221
+ - ✅ **页面指令** - 只在指定页面生效,优先级高于全局指令
222
+ - ✅ **全局指令** - 在所有页面生效
223
+ - ✅ **优先级** - 页面指令 > 全局指令
224
+ - ⚠️ **未设置路由** - 如果未指定路由且未设置 `currentRoute`,页面指令不会被匹配
225
+
152
226
  #### unregisterCommands(scope)
153
227
 
154
228
  注销指令
@@ -182,17 +256,61 @@ const result = await this.$voice.stop()
182
256
  // result.allMatches - 所有匹配结果(调试用)
183
257
  ```
184
258
 
185
- #### matchCommand(text)
259
+ #### matchCommand(text, options?)
186
260
 
187
261
  手动匹配指令(用于测试)
188
262
 
189
263
  ```javascript
264
+ // 默认使用相似度匹配
190
265
  const result = this.$voice.matchCommand('测试文本')
266
+
267
+ // 使用基于位置的匹配(新增)
268
+ const result = this.$voice.matchCommand('帮我提交然后返回首页', { mode: 'position' })
269
+
191
270
  if (result.matched) {
192
271
  this.$voice.executeCommand(result.command)
193
272
  }
194
273
  ```
195
274
 
275
+ **参数**:
276
+ - `text` (string) - 要匹配的文本
277
+ - `options.mode` (string) - 匹配模式:`'similarity'`(默认) | `'position'`
278
+ - `options.caseSensitive` (boolean) - 是否区分大小写(仅 position 模式)
279
+
280
+ #### matchCommandInText(text, options?)
281
+
282
+ 从文本中识别第一个匹配的指令(基于关键词位置)⚡ **高性能**
283
+
284
+ ```javascript
285
+ const result = this.$voice.matchCommandInText('帮我提交然后返回首页')
286
+ // result.matched - 是否匹配
287
+ // result.command - 匹配的指令
288
+ // result.position - 关键词在文本中的位置
289
+ // result.matchedText - 匹配的关键词文本
290
+ // result.weight - 指令权重
291
+
292
+ if (result.matched) {
293
+ console.log(`找到指令: ${result.matchedText}, 位置: ${result.position}`)
294
+ this.$voice.executeCommand(result.command)
295
+ }
296
+ ```
297
+
298
+ **参数**:
299
+ - `text` (string) - 要搜索的文本
300
+ - `options.caseSensitive` (boolean) - 是否区分大小写,默认 false
301
+
302
+ **匹配策略**:
303
+ 1. 遍历所有已注册的关键词
304
+ 2. 在文本中搜索每个关键词的首次出现位置
305
+ 3. 返回最早出现的关键词对应的指令
306
+ 4. 支持大小写不敏感匹配
307
+
308
+ **性能优势**:
309
+ - 使用 `indexOf` 字符串搜索,时间复杂度 O(n×m)
310
+ - 比相似度匹配快 2-5 倍
311
+ - 典型场景(50关键词×200字)< 0.02ms
312
+ - 极限场景(100关键词×500字)< 0.05ms
313
+
196
314
  #### executeCommand(command)
197
315
 
198
316
  执行指令
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "sk-voice-command",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "语音指令控制和自动化操作",
5
5
  "main": "src/index.js",
6
+ "type": "module",
6
7
  "scripts": {
7
8
  "test": "echo \"Error: no test specified\" && exit 1"
8
9
  },