vue-element-ui-x 0.1.0

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 (91) hide show
  1. package/README.md +47 -0
  2. package/lib/attachments.js +3046 -0
  3. package/lib/bubble-list.js +13845 -0
  4. package/lib/bubble.js +13130 -0
  5. package/lib/conversations.js +18817 -0
  6. package/lib/files-card.js +2457 -0
  7. package/lib/index.common.js +19 -0
  8. package/lib/index.esm.js +19 -0
  9. package/lib/index.js +38282 -0
  10. package/lib/index.umd.js +19 -0
  11. package/lib/mixins.js +1016 -0
  12. package/lib/prompts.js +832 -0
  13. package/lib/sender.js +1901 -0
  14. package/lib/think.js +799 -0
  15. package/lib/thinking.js +809 -0
  16. package/lib/thought-chain.js +30396 -0
  17. package/lib/typewriter.js +12793 -0
  18. package/lib/welcome.js +755 -0
  19. package/package.json +42 -0
  20. package/src/components/Attachments/index.js +8 -0
  21. package/src/components/Attachments/src/main.vue +537 -0
  22. package/src/components/Bubble/index.js +6 -0
  23. package/src/components/Bubble/src/main.vue +299 -0
  24. package/src/components/BubbleList/index.js +8 -0
  25. package/src/components/BubbleList/src/loading.vue +75 -0
  26. package/src/components/BubbleList/src/main.vue +466 -0
  27. package/src/components/Conversations/index.js +8 -0
  28. package/src/components/Conversations/src/components/item.vue +371 -0
  29. package/src/components/Conversations/src/main.vue +635 -0
  30. package/src/components/FilesCard/index.js +8 -0
  31. package/src/components/FilesCard/src/fileSvg/audio.vue +38 -0
  32. package/src/components/FilesCard/src/fileSvg/changeFileName.bat +18 -0
  33. package/src/components/FilesCard/src/fileSvg/code.vue +35 -0
  34. package/src/components/FilesCard/src/fileSvg/database.vue +94 -0
  35. package/src/components/FilesCard/src/fileSvg/excel.vue +38 -0
  36. package/src/components/FilesCard/src/fileSvg/file.vue +40 -0
  37. package/src/components/FilesCard/src/fileSvg/image.vue +40 -0
  38. package/src/components/FilesCard/src/fileSvg/index.js +46 -0
  39. package/src/components/FilesCard/src/fileSvg/link.vue +54 -0
  40. package/src/components/FilesCard/src/fileSvg/mark.vue +38 -0
  41. package/src/components/FilesCard/src/fileSvg/pdf.vue +38 -0
  42. package/src/components/FilesCard/src/fileSvg/ppt.vue +38 -0
  43. package/src/components/FilesCard/src/fileSvg/three.vue +38 -0
  44. package/src/components/FilesCard/src/fileSvg/txt.vue +38 -0
  45. package/src/components/FilesCard/src/fileSvg/unknown.vue +54 -0
  46. package/src/components/FilesCard/src/fileSvg/video.vue +38 -0
  47. package/src/components/FilesCard/src/fileSvg/word.vue +38 -0
  48. package/src/components/FilesCard/src/fileSvg/zip.vue +38 -0
  49. package/src/components/FilesCard/src/main.vue +405 -0
  50. package/src/components/FilesCard/src/options.js +18 -0
  51. package/src/components/Prompts/index.js +8 -0
  52. package/src/components/Prompts/src/main.vue +248 -0
  53. package/src/components/Sender/index.js +8 -0
  54. package/src/components/Sender/src/components/ClearButton.vue +28 -0
  55. package/src/components/Sender/src/components/Loading.vue +53 -0
  56. package/src/components/Sender/src/components/LoadingButton.vue +39 -0
  57. package/src/components/Sender/src/components/SendButton.vue +26 -0
  58. package/src/components/Sender/src/components/SpeechButton.vue +24 -0
  59. package/src/components/Sender/src/components/SpeechLoading.vue +87 -0
  60. package/src/components/Sender/src/components/SpeechLoadingButton.vue +43 -0
  61. package/src/components/Sender/src/main.vue +758 -0
  62. package/src/components/Think/index.js +8 -0
  63. package/src/components/Think/src/main.vue +190 -0
  64. package/src/components/Thinking/index.js +8 -0
  65. package/src/components/Thinking/src/main.vue +195 -0
  66. package/src/components/ThoughtChain/index.js +8 -0
  67. package/src/components/ThoughtChain/src/main.vue +293 -0
  68. package/src/components/Typewriter/index.js +8 -0
  69. package/src/components/Typewriter/src/main.vue +251 -0
  70. package/src/components/Welcome/index.js +8 -0
  71. package/src/components/Welcome/src/main.vue +151 -0
  72. package/src/index.js +63 -0
  73. package/src/mixins/index.js +49 -0
  74. package/src/mixins/recordMixin.js +118 -0
  75. package/src/mixins/sendMixin.js +357 -0
  76. package/src/mixins/streamMixin.js +499 -0
  77. package/src/styles/Attachments.scss +236 -0
  78. package/src/styles/Bubble.scss +157 -0
  79. package/src/styles/BubbleList.scss +148 -0
  80. package/src/styles/Conversations.scss +260 -0
  81. package/src/styles/FilesCard.scss +221 -0
  82. package/src/styles/Prompts.scss +195 -0
  83. package/src/styles/Sender.scss +199 -0
  84. package/src/styles/Think.scss +134 -0
  85. package/src/styles/Thinking.scss +112 -0
  86. package/src/styles/ThoughtChain.scss +113 -0
  87. package/src/styles/Typewriter.scss +66 -0
  88. package/src/styles/Welcome.scss +283 -0
  89. package/src/theme/var.scss +72 -0
  90. package/src/utils/index.js +199 -0
  91. package/src/utils/scrollDetector.js +34 -0
@@ -0,0 +1,758 @@
1
+ <template>
2
+ <div
3
+ class="el-x-sender-wrap"
4
+ :style="{
5
+ cursor: disabled ? 'not-allowed' : 'default',
6
+ '--el-x-sender-trigger-popover-width': triggerPopoverWidth,
7
+ '--el-x-sender-trigger-popover-left': triggerPopoverLeft,
8
+ }"
9
+ >
10
+ <div
11
+ ref="senderRef"
12
+ class="el-x-sender"
13
+ :style="{
14
+ '--el-x-sender-box-shadow-tertiary':
15
+ '0 1px 2px 0 rgba(0, 0, 0, 0.03), 0 1px 6px -1px rgba(0, 0, 0, 0.02), 0 2px 4px 0 rgba(0, 0, 0, 0.02)',
16
+ '--el-x-sender-input-font-size': '14px',
17
+ '--el-x-sender-header-animation-duration': `${headerAnimationTimer}ms`,
18
+ }"
19
+ :class="{
20
+ 'el-x-sender-disabled': disabled,
21
+ }"
22
+ >
23
+ <!-- 头部容器 -->
24
+ <transition name="slide">
25
+ <div
26
+ v-if="visiableHeader"
27
+ class="el-x-sender-header-wrap"
28
+ >
29
+ <div
30
+ v-if="$slots.header"
31
+ class="el-x-sender-header"
32
+ >
33
+ <slot name="header"></slot>
34
+ </div>
35
+ </div>
36
+ </transition>
37
+ <!-- 内容容器 内置变体逻辑 -->
38
+ <div
39
+ class="el-x-sender-content"
40
+ :class="{ 'content-variant-updown': variant === 'updown' }"
41
+ @mousedown="onContentMouseDown"
42
+ >
43
+ <!-- Prefix 前缀 -->
44
+ <div
45
+ v-if="$slots.prefix && variant === 'default'"
46
+ class="el-x-sender-prefix"
47
+ >
48
+ <slot name="prefix"></slot>
49
+ </div>
50
+ <!-- 输入框 -->
51
+ <el-input
52
+ ref="inputRef"
53
+ v-model="internalValue"
54
+ class="el-x-sender-input"
55
+ :rows="1"
56
+ :autosize="computedAutoSize"
57
+ type="textarea"
58
+ :validate-event="false"
59
+ :placeholder="placeholder"
60
+ :readonly="readOnly || disabled"
61
+ :disabled="disabled"
62
+ @keydown.native="handleKeyDown"
63
+ @compositionstart="handleCompositionStart"
64
+ @compositionend="handleCompositionEnd"
65
+ />
66
+ <!-- 操作列表 -->
67
+ <div
68
+ v-if="variant === 'default'"
69
+ class="el-x-sender-action-list"
70
+ >
71
+ <slot name="action-list">
72
+ <div class="el-x-sender-action-list-presets">
73
+ <send-button
74
+ v-if="!loading"
75
+ :disabled="isSubmitDisabled"
76
+ @submit="submit"
77
+ ></send-button>
78
+
79
+ <loading-button
80
+ v-if="loading"
81
+ @cancel="cancel"
82
+ ></loading-button>
83
+
84
+ <speech-button
85
+ v-if="!speechLoading && allowSpeech"
86
+ @click="startRecognition"
87
+ ></speech-button>
88
+
89
+ <speech-loading-button
90
+ v-if="speechLoading && allowSpeech"
91
+ @click="stopRecognition"
92
+ ></speech-loading-button>
93
+
94
+ <clear-button
95
+ v-if="clearable"
96
+ @clear="clear"
97
+ ></clear-button>
98
+ </div>
99
+ </slot>
100
+ </div>
101
+
102
+ <!-- 变体样式 -->
103
+ <div
104
+ v-if="variant === 'updown' && showUpdown"
105
+ class="el-x-sender-updown-wrap"
106
+ >
107
+ <!-- 变体 updown: Prefix 前缀 -->
108
+ <div
109
+ v-if="$slots.prefix"
110
+ class="el-x-sender-prefix"
111
+ >
112
+ <slot name="prefix"></slot>
113
+ </div>
114
+
115
+ <!-- 变体 updown:操作列表 -->
116
+ <div class="el-x-sender-action-list">
117
+ <slot name="action-list">
118
+ <div class="el-x-sender-action-list-presets">
119
+ <send-button
120
+ v-if="!loading"
121
+ :disabled="isSubmitDisabled"
122
+ @submit="submit"
123
+ ></send-button>
124
+
125
+ <loading-button
126
+ v-if="loading"
127
+ @cancel="cancel"
128
+ ></loading-button>
129
+
130
+ <speech-button
131
+ v-if="!speechLoading && allowSpeech"
132
+ @click="startRecognition"
133
+ ></speech-button>
134
+
135
+ <speech-loading-button
136
+ v-if="speechLoading && allowSpeech"
137
+ @click="stopRecognition"
138
+ ></speech-loading-button>
139
+
140
+ <clear-button
141
+ v-if="clearable"
142
+ @clear="clear"
143
+ ></clear-button>
144
+ </div>
145
+ </slot>
146
+ </div>
147
+ </div>
148
+ </div>
149
+
150
+ <!-- 底部容器 -->
151
+ <transition name="slide">
152
+ <div
153
+ v-if="$slots.footer"
154
+ class="el-x-sender-footer"
155
+ >
156
+ <slot name="footer"></slot>
157
+ </div>
158
+ </transition>
159
+
160
+ <!-- popover 直接附加到发送器 -->
161
+ <el-popover
162
+ ref="popoverRef"
163
+ v-model="popoverVisible"
164
+ :disabled="disabled"
165
+ :visible-arrow="false"
166
+ :placement="triggerPopoverPlacement"
167
+ :offset="triggerPopoverOffset"
168
+ popper-class="el-x-sender-trigger-popover"
169
+ trigger="manual"
170
+ @show="onPopoverShow"
171
+ >
172
+ <template slot="default">
173
+ <slot
174
+ name="trigger-popover"
175
+ :trigger-string="triggerString"
176
+ :readonly="readOnly"
177
+ >当前触发的字符为:{{ `${triggerString}` }} 在这里定义的内容,但注意这里的回车事件将会被
178
+ 输入框 覆盖</slot
179
+ >
180
+ </template>
181
+ </el-popover>
182
+ </div>
183
+ </div>
184
+ </template>
185
+
186
+ <script>
187
+ import ClearButton from './components/ClearButton.vue';
188
+ import LoadingButton from './components/LoadingButton.vue';
189
+ import SendButton from './components/SendButton.vue';
190
+ import SpeechButton from './components/SpeechButton.vue';
191
+ import SpeechLoadingButton from './components/SpeechLoadingButton.vue';
192
+
193
+ export default {
194
+ name: 'ElXSender',
195
+
196
+ components: {
197
+ ClearButton,
198
+ LoadingButton,
199
+ SendButton,
200
+ SpeechButton,
201
+ SpeechLoadingButton,
202
+ },
203
+
204
+ props: {
205
+ value: {
206
+ type: String,
207
+ default: '',
208
+ },
209
+ placeholder: {
210
+ type: String,
211
+ default: '请输入内容',
212
+ },
213
+ autoSize: {
214
+ type: Object,
215
+ default: () => ({
216
+ minRows: 1,
217
+ maxRows: 6,
218
+ }),
219
+ },
220
+ readOnly: Boolean,
221
+ disabled: Boolean,
222
+ loading: Boolean,
223
+ clearable: Boolean,
224
+ allowSpeech: Boolean,
225
+ submitType: {
226
+ type: String,
227
+ default: 'enter',
228
+ validator: value => ['enter', 'shiftEnter'].includes(value),
229
+ },
230
+ headerAnimationTimer: {
231
+ type: Number,
232
+ default: 300,
233
+ },
234
+ inputWidth: {
235
+ type: String,
236
+ default: '100%',
237
+ },
238
+
239
+ // 变体属性
240
+ variant: {
241
+ type: String,
242
+ default: 'default',
243
+ validator: value => ['default', 'updown'].includes(value),
244
+ },
245
+ showUpdown: {
246
+ type: Boolean,
247
+ default: true,
248
+ },
249
+ submitBtnDisabled: Boolean,
250
+
251
+ inputStyle: {
252
+ type: Object,
253
+ default: () => ({}),
254
+ },
255
+
256
+ // 新增 el-popover 样式透传
257
+ triggerStrings: {
258
+ type: Array,
259
+ default: () => [],
260
+ },
261
+ triggerPopoverVisible: {
262
+ type: Boolean,
263
+ default: false,
264
+ },
265
+ triggerPopoverWidth: {
266
+ type: String,
267
+ default: 'fit-content',
268
+ },
269
+ triggerPopoverLeft: {
270
+ type: String,
271
+ default: '0px',
272
+ },
273
+ triggerPopoverOffset: {
274
+ type: Number,
275
+ default: 0,
276
+ },
277
+ triggerPopoverPlacement: {
278
+ type: String,
279
+ default: 'top-start',
280
+ validator: value =>
281
+ [
282
+ 'top',
283
+ 'top-start',
284
+ 'top-end',
285
+ 'bottom',
286
+ 'bottom-start',
287
+ 'bottom-end',
288
+ 'left',
289
+ 'left-start',
290
+ 'left-end',
291
+ 'right',
292
+ 'right-start',
293
+ 'right-end',
294
+ ].includes(value),
295
+ },
296
+ },
297
+
298
+ data() {
299
+ return {
300
+ senderRef: null,
301
+ inputRef: null,
302
+ popoverVisible: this.triggerPopoverVisible,
303
+ internalValue: this.value,
304
+ isComposing: false,
305
+ popoverRef: null,
306
+ triggerString: '',
307
+ visiableHeader: false,
308
+ recognition: null,
309
+ speechLoading: false,
310
+ triggerDebounce: false,
311
+ };
312
+ },
313
+
314
+ computed: {
315
+ // 判断是否存在 recordingChange 监听器
316
+ hasOnRecordingChangeListener() {
317
+ return !!(this.$listeners && this.$listeners.recordingChange);
318
+ },
319
+
320
+ // 判断是否存在 trigger 监听器
321
+ hasOnTriggerListener() {
322
+ return !!(this.$listeners && this.$listeners.trigger);
323
+ },
324
+
325
+ // 计算提交按钮禁用状态
326
+ isSubmitDisabled() {
327
+ // 用户显式设置了 submitBtnDisabled 时优先使用
328
+ if (typeof this.submitBtnDisabled === 'boolean') {
329
+ return this.submitBtnDisabled;
330
+ }
331
+ // 否则保持默认逻辑:无内容时禁用
332
+ return !this.internalValue;
333
+ },
334
+
335
+ // 根据字体大小动态计算 autoSize
336
+ computedAutoSize() {
337
+ // 如果用户提供了autoSize,则优先使用
338
+ if (this.autoSize) return this.autoSize;
339
+
340
+ // 否则返回默认值
341
+ return {
342
+ minRows: 1,
343
+ maxRows: 6,
344
+ };
345
+ },
346
+ },
347
+
348
+ watch: {
349
+ value(val) {
350
+ this.internalValue = val;
351
+ },
352
+ // 监听样式变化
353
+ inputStyle: {
354
+ handler() {
355
+ this.$nextTick(() => {
356
+ this.applyInputStyles();
357
+ });
358
+ },
359
+ deep: true,
360
+ },
361
+ inputWidth() {
362
+ this.$nextTick(() => {
363
+ this.applyInputStyles();
364
+ });
365
+ },
366
+ // 监听外部传入的 triggerPopoverVisible 变化
367
+ triggerPopoverVisible(val) {
368
+ // 仅在值不同时更新,避免循环触发
369
+ if (this.popoverVisible !== val) {
370
+ this.popoverVisible = val;
371
+ }
372
+ },
373
+ // 监听内部 popoverVisible 变化,向外同步
374
+ popoverVisible(val) {
375
+ if (val !== this.triggerPopoverVisible) {
376
+ this.$emit('update:triggerPopoverVisible', val);
377
+ }
378
+
379
+ // 新增:当弹窗关闭时,设置短时间内的防抖状态
380
+ if (val === false) {
381
+ this.triggerDebounce = true;
382
+ setTimeout(() => {
383
+ this.triggerDebounce = false;
384
+ }, 300); // 300ms 防抖时间,防止频繁触发
385
+ }
386
+ },
387
+ internalValue(newVal, oldVal) {
388
+ this.$emit('input', newVal);
389
+
390
+ // 当内容变化时,修复高度问题
391
+
392
+ if (this.isComposing) return;
393
+ // 新增:如果处于防抖状态,则不进行触发检测
394
+ if (this.triggerDebounce) return;
395
+
396
+ // 触发逻辑:检测输入内容中是否出现了触发字符
397
+ const triggerStrings = this.triggerStrings || []; // 如果为 undefined,就使用空数组
398
+
399
+ // 新的检测逻辑:检查是否在当前光标位置前有触发字符
400
+ if (this.inputRef && triggerStrings.length > 0) {
401
+ const textArea = this.inputRef.$el.querySelector('textarea');
402
+ if (textArea) {
403
+ const cursorPosition = textArea.selectionStart;
404
+ // 检查光标前是否有新增的触发字符
405
+ if (cursorPosition > 0 && newVal.length > oldVal.length) {
406
+ const lastChar = newVal.charAt(cursorPosition - 1);
407
+ if (triggerStrings.includes(lastChar)) {
408
+ this.triggerString = lastChar;
409
+ if (this.hasOnTriggerListener) {
410
+ this.$emit('trigger', {
411
+ oldValue: oldVal,
412
+ newValue: newVal,
413
+ triggerString: lastChar,
414
+ isOpen: true,
415
+ cursorPosition: cursorPosition,
416
+ });
417
+ }
418
+ this.popoverVisible = true;
419
+ return;
420
+ }
421
+ }
422
+ }
423
+ }
424
+
425
+ // 原有的处理逻辑,用于向后兼容
426
+ const validOldVal = typeof oldVal === 'string' ? oldVal : '';
427
+ const wasOldValTrigger = triggerStrings.includes(validOldVal);
428
+ const isNewValTrigger = triggerStrings.includes(newVal);
429
+
430
+ // 触发显示:从空变为触发字符
431
+ if (oldVal === '' && isNewValTrigger) {
432
+ this.triggerString = newVal;
433
+ if (this.hasOnTriggerListener) {
434
+ this.$emit('trigger', {
435
+ oldValue: oldVal,
436
+ newValue: newVal,
437
+ triggerString: newVal,
438
+ isOpen: true,
439
+ });
440
+ }
441
+ this.popoverVisible = true;
442
+ }
443
+ // 关闭:从触发字符变为非触发字符
444
+ else if (!isNewValTrigger && wasOldValTrigger) {
445
+ if (this.hasOnTriggerListener) {
446
+ this.$emit('trigger', {
447
+ oldValue: oldVal,
448
+ newValue: newVal,
449
+ triggerString: undefined,
450
+ isOpen: false,
451
+ });
452
+ }
453
+ this.popoverVisible = false;
454
+ }
455
+ // 触发显示:从非空且非触发字符变为触发字符
456
+ else if (oldVal !== '' && isNewValTrigger && !wasOldValTrigger) {
457
+ this.triggerString = newVal;
458
+ if (this.hasOnTriggerListener) {
459
+ this.$emit('trigger', {
460
+ oldValue: oldVal,
461
+ newValue: newVal,
462
+ triggerString: newVal,
463
+ isOpen: true,
464
+ });
465
+ }
466
+ this.popoverVisible = true;
467
+ }
468
+ },
469
+ },
470
+
471
+ methods: {
472
+ /* 直接应用输入框样式 */
473
+ applyInputStyles() {
474
+ if (!this.inputRef) return;
475
+
476
+ const textareaEl = this.inputRef.$el.querySelector('textarea');
477
+ if (!textareaEl) return;
478
+
479
+ // 设置默认基础样式
480
+ const defaultStyles = {
481
+ maxHeight: '176px',
482
+ maxWidth: this.inputWidth || '100%',
483
+ boxSizing: 'border-box',
484
+ };
485
+
486
+ // 应用默认样式
487
+ Object.keys(defaultStyles).forEach(key => {
488
+ textareaEl.style[key] = defaultStyles[key];
489
+ });
490
+
491
+ // 如果用户传入了样式对象,则应用覆盖默认样式
492
+ if (this.inputStyle && typeof this.inputStyle === 'object') {
493
+ Object.keys(this.inputStyle).forEach(key => {
494
+ textareaEl.style[key] = this.inputStyle[key];
495
+ });
496
+
497
+ // 如果用户设置了字体大小,需要调整高度
498
+ if (this.inputStyle.fontSize) {
499
+ // 确保高度能完全容纳当前字体大小
500
+ const computedFontSize = window.getComputedStyle(textareaEl).fontSize;
501
+ const fontSize = parseInt(computedFontSize);
502
+ const minHeight = Math.max(fontSize * 1.5, 24) + 'px';
503
+ textareaEl.style.minHeight = minHeight;
504
+
505
+ // 重新触发 autosize
506
+ this.$nextTick(() => {
507
+ // 在某些情况下需要手动触发Element UI的autosize更新
508
+ const event = document.createEvent('Event');
509
+ event.initEvent('autosize:update', true, false);
510
+ textareaEl.dispatchEvent(event);
511
+ });
512
+ }
513
+ }
514
+ },
515
+
516
+ /* 手动更新 popover 位置 */
517
+ onPopoverShow() {
518
+ if (this.$refs.popoverRef) {
519
+ this.$nextTick(() => {
520
+ this.$refs.popoverRef.referenceElm = this.$refs.senderRef;
521
+ this.$refs.popoverRef.doDestroy();
522
+ this.$refs.popoverRef.updatePopper();
523
+ });
524
+ }
525
+ },
526
+ /* 内容容器聚焦 开始 */
527
+ onContentMouseDown(e) {
528
+ // 点击容器后设置输入框的聚焦,会触发 &:focus-within 样式
529
+ if (e.target !== this.$el.querySelector(`.el-textarea__inner`)) {
530
+ e.preventDefault();
531
+ }
532
+ this.inputRef.focus();
533
+ },
534
+ /* 内容容器聚焦 结束 */
535
+
536
+ /* 头部显示隐藏 开始 */
537
+ openHeader() {
538
+ if (!this.$slots.header) return false;
539
+ if (this.readOnly) return false;
540
+ this.visiableHeader = true;
541
+ },
542
+
543
+ closeHeader() {
544
+ if (!this.$slots.header) return;
545
+ if (this.readOnly) return;
546
+ this.visiableHeader = false;
547
+ },
548
+ /* 头部显示隐藏 结束 */
549
+
550
+ /* 使用浏览器自带的语音转文字功能 开始 */
551
+ startRecognition() {
552
+ if (this.readOnly || this.disabled) return; // 直接返回,不执行后续逻辑
553
+
554
+ if (this.hasOnRecordingChangeListener) {
555
+ this.speechLoading = true;
556
+ this.$emit('recordingChange', true);
557
+ return;
558
+ }
559
+
560
+ // 检查浏览器支持的 SpeechRecognition API
561
+ const SpeechRecognition =
562
+ window.SpeechRecognition ||
563
+ window.webkitSpeechRecognition ||
564
+ window.mozSpeechRecognition ||
565
+ window.msSpeechRecognition;
566
+
567
+ if (SpeechRecognition) {
568
+ try {
569
+ this.recognition = new SpeechRecognition();
570
+ this.recognition.continuous = true;
571
+ this.recognition.interimResults = true;
572
+ this.recognition.lang = 'zh-CN';
573
+ this.recognition.onresult = event => {
574
+ let results = '';
575
+ for (let i = 0; i <= event.resultIndex; i++) {
576
+ results += event.results[i][0].transcript;
577
+ }
578
+ if (!this.readOnly) {
579
+ this.internalValue = results;
580
+ }
581
+ };
582
+ this.recognition.onstart = () => {
583
+ this.speechLoading = true;
584
+ console.log('语音识别已启动');
585
+ };
586
+ this.recognition.onend = () => {
587
+ this.speechLoading = false;
588
+ console.log('语音识别已结束');
589
+ };
590
+ this.recognition.onerror = event => {
591
+ console.error('语音识别出错:', event.error);
592
+ this.speechLoading = false;
593
+ // 可以添加用户友好提示
594
+ if (event.error === 'not-allowed') {
595
+ console.error('用户拒绝了麦克风访问权限');
596
+ // 这里可以显示提示
597
+ }
598
+ };
599
+ this.recognition.start();
600
+ } catch (error) {
601
+ console.error('启动语音识别失败:', error);
602
+ this.speechLoading = false;
603
+ }
604
+ } else {
605
+ console.error('浏览器不支持 Web Speech API');
606
+ this.speechLoading = false;
607
+ }
608
+ },
609
+
610
+ stopRecognition() {
611
+ // 如果有自定义处理函数
612
+ if (this.hasOnRecordingChangeListener) {
613
+ this.speechLoading = false;
614
+ this.$emit('recordingChange', false);
615
+ return;
616
+ }
617
+
618
+ if (this.recognition) {
619
+ this.recognition.stop();
620
+ this.speechLoading = false;
621
+ }
622
+ },
623
+ /* 使用浏览器自带的语音转文字功能 结束 */
624
+
625
+ /* 输入框事件 开始 */
626
+ submit() {
627
+ if (this.readOnly || this.loading || this.disabled || this.isSubmitDisabled) return;
628
+ this.$emit('submit', this.internalValue);
629
+ },
630
+
631
+ // 取消按钮
632
+ cancel() {
633
+ if (this.readOnly) return;
634
+ this.$emit('cancel', this.internalValue);
635
+ },
636
+
637
+ clear() {
638
+ if (this.readOnly) return; // 直接返回,不执行后续逻辑
639
+ this.inputRef.clear();
640
+ this.internalValue = '';
641
+ },
642
+
643
+ // 在这判断组合键的回车键 (目前支持两种模式)
644
+ handleKeyDown(e) {
645
+ if (this.readOnly) return; // 直接返回,不执行后续逻辑
646
+
647
+ if (this.submitType === 'enter') {
648
+ // 判断是否按下了 Shift + 回车键
649
+ if (e.shiftKey && e.keyCode === 13) {
650
+ e.preventDefault();
651
+ const cursorPosition = e.target.selectionStart; // 获取光标位置
652
+ const textBeforeCursor = this.internalValue.slice(0, cursorPosition); // 光标前的文本
653
+ const textAfterCursor = this.internalValue.slice(cursorPosition); // 光标后的文本
654
+ this.internalValue = `${textBeforeCursor}\n${textAfterCursor}`; // 插入换行符
655
+ this.$nextTick(() => {
656
+ e.target.setSelectionRange(cursorPosition + 1, cursorPosition + 1); // 更新光标位置
657
+ });
658
+ } else if (e.keyCode === 13 && !e.shiftKey) {
659
+ // 阻止掉 Enter 的默认换行行为
660
+ e.preventDefault();
661
+ // 触发提交功能
662
+ this.submit();
663
+ }
664
+ } else if (this.submitType === 'shiftEnter') {
665
+ // 判断是否按下了 Shift + 回车键
666
+ if (e.shiftKey && e.keyCode === 13) {
667
+ // 阻止掉 Enter 的默认换行行为
668
+ e.preventDefault();
669
+ // 触发提交功能
670
+ this.submit();
671
+ } else if (e.keyCode === 13 && !e.shiftKey) {
672
+ e.preventDefault();
673
+ const cursorPosition = e.target.selectionStart; // 获取光标位置
674
+ const textBeforeCursor = this.internalValue.slice(0, cursorPosition); // 光标前的文本
675
+ const textAfterCursor = this.internalValue.slice(cursorPosition); // 光标后的文本
676
+ this.internalValue = `${textBeforeCursor}\n${textAfterCursor}`; // 插入换行符
677
+ this.$nextTick(() => {
678
+ e.target.setSelectionRange(cursorPosition + 1, cursorPosition + 1); // 更新光标位置
679
+ });
680
+ }
681
+ }
682
+ },
683
+ /* 输入框事件 结束 */
684
+
685
+ /* 焦点 事件 开始 */
686
+ blur() {
687
+ if (this.readOnly) return false;
688
+ this.inputRef.blur();
689
+ },
690
+
691
+ focus(type = 'all') {
692
+ if (this.readOnly) return false;
693
+
694
+ if (type === 'all') {
695
+ this.inputRef.select();
696
+ } else if (type === 'start') {
697
+ this.focusToStart();
698
+ } else if (type === 'end') {
699
+ this.focusToEnd();
700
+ }
701
+ },
702
+
703
+ // 聚焦到文本最前方
704
+ focusToStart() {
705
+ if (this.inputRef) {
706
+ // 获取底层的 textarea DOM 元素
707
+ const textarea = this.inputRef.$el.querySelector('textarea');
708
+ if (textarea) {
709
+ textarea.focus(); // 聚焦到输入框
710
+ textarea.setSelectionRange(0, 0); // 设置光标到最前方
711
+ }
712
+ }
713
+ },
714
+
715
+ // 聚焦到文本最后方
716
+ focusToEnd() {
717
+ if (this.inputRef) {
718
+ // 获取底层的 textarea DOM 元素
719
+ const textarea = this.inputRef.$el.querySelector('textarea');
720
+ if (textarea) {
721
+ textarea.focus(); // 聚焦到输入框
722
+ textarea.setSelectionRange(this.internalValue.length, this.internalValue.length); // 设置光标到最后方
723
+ }
724
+ }
725
+ },
726
+ /* 焦点 事件 结束 */
727
+
728
+ // 处理输入法开始/结束 (此方法是拼音输入法的时候用)
729
+ handleCompositionStart() {
730
+ this.isComposing = true;
731
+ },
732
+
733
+ handleCompositionEnd() {
734
+ this.isComposing = false;
735
+ },
736
+ },
737
+
738
+ mounted() {
739
+ // 应用样式
740
+ this.$nextTick(() => {
741
+ // 获取组件引用
742
+ this.senderRef = this.$refs.senderRef;
743
+ this.inputRef = this.$refs.inputRef;
744
+ this.popoverRef = this.$refs.popoverRef;
745
+ this.applyInputStyles();
746
+ });
747
+ },
748
+
749
+ updated() {
750
+ // 确保在组件更新后重新应用样式
751
+ this.applyInputStyles();
752
+ },
753
+ };
754
+ </script>
755
+
756
+ <style lang="scss" scoped>
757
+ @import '../../../styles/Sender.scss';
758
+ </style>