-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathindex.vue
More file actions
232 lines (206 loc) · 5.87 KB
/
index.vue
File metadata and controls
232 lines (206 loc) · 5.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<script setup lang="ts">
import type {
TypewriterInstance,
TypingConfig
} from '../Typewriter/types.d.ts';
import type { BubbleEmits, BubbleProps } from './types.d.ts';
import Typewriter from '../Typewriter/index.vue';
const props = withDefaults(defineProps<BubbleProps>(), {
content: '',
avatar: '',
placement: 'start',
variant: 'filled',
maxWidth: '500px',
avatarSize: '',
avatarGap: '12px',
avatarShape: 'circle',
avatarSrcSet: '',
avatarAlt: '',
avatarFit: 'cover',
noStyle: false
});
const emits = defineEmits<BubbleEmits>();
const internalDestroyed = ref(false); // 内部销毁状态
// 新增:响应式变量跟踪打字状态
const isTypingClass = ref(false);
// 监听内容变化自动重置
watch(
() => props.content,
(newVal, oldVal) => {
if (newVal !== oldVal && internalDestroyed.value) {
restart(); // 内容变化时自动重置
}
}
);
const typewriterRef = ref<TypewriterInstance>();
const instance: TypewriterInstance = {
interrupt,
continue: continueTyping,
restart,
destroy,
renderedContent: computed(() =>
internalDestroyed.value
? ''
: typewriterRef.value?.renderedContent.value || ''
),
isTyping: computed(
() =>
!internalDestroyed.value && (typewriterRef.value?.isTyping.value || false)
),
progress: computed(() =>
internalDestroyed.value ? 0 : typewriterRef.value?.progress.value || 0
)
};
const DEFAULT_TYPING: TypingConfig = {
step: 2,
suffix: '|',
interval: 50,
isRequestEnd: true
};
const _typing = computed(() => {
if (typeof props.typing === 'undefined') {
return false;
} else if (typeof props.typing === 'boolean') {
return props.typing;
} else {
return Object.assign({}, DEFAULT_TYPING, props.typing);
}
}) as boolean | TypingConfig;
function onStart(instance: TypewriterInstance) {
emits('start', instance);
}
function onFinish(instance: TypewriterInstance) {
isTypingClass.value = false;
emits('finish', instance);
}
function onWriting(instance: TypewriterInstance) {
isTypingClass.value = true;
emits('writing', instance);
}
function avatarError(e: Event) {
emits('avatarError', e);
}
function interrupt() {
typewriterRef.value?.interrupt();
}
function continueTyping() {
typewriterRef.value?.continue();
}
function restart() {
internalDestroyed.value = false;
typewriterRef.value?.restart();
}
function destroy() {
typewriterRef.value?.destroy();
internalDestroyed.value = true;
}
// 组件卸载时自动销毁
onUnmounted(instance.destroy);
defineExpose(instance);
</script>
<template>
<div
v-if="!internalDestroyed"
class="el-bubble"
:class="{
'el-bubble-start': placement === 'start',
'el-bubble-end': placement === 'end',
'el-bubble-no-style': noStyle,
'el-bubble-is-typing': isTypingClass // 新增动态类名
}"
:style="{
'--el-box-shadow-tertiary': `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)`,
'--bubble-content-max-width': `${maxWidth}`,
'--el-bubble-avatar-placeholder-width': `${$slots.avatar ? '' : avatarSize}`,
'--el-bubble-avatar-placeholder-height': `${$slots.avatar ? '' : avatarSize}`,
'--el-bubble-avatar-placeholder-gap': `${avatarGap}`
}"
>
<!-- 头像 -->
<div
v-if="!$slots.avatar && avatar"
class="el-bubble-avatar el-bubble-avatar-size"
>
<el-avatar
:size="0"
:src="avatar"
:shape="avatarShape"
:src-set="avatarSrcSet"
:alt="avatarFit"
@error="avatarError"
/>
</div>
<!-- 头像属性进行占位 -->
<div
v-if="!$slots.avatar && !avatar && avatarSize"
class="el-bubble-avatar-placeholder"
/>
<div v-if="$slots.avatar" class="el-bubble-avatar">
<slot name="avatar" />
</div>
<!-- 内容 -->
<div class="el-bubble-content-wrapper">
<!-- 头部内容 -->
<div v-if="$slots.header" class="el-bubble-header">
<slot name="header" />
</div>
<div
class="el-bubble-content"
:class="{
'el-bubble-content-loading': loading,
'el-bubble-content-round': shape === 'round' && !noStyle,
'el-bubble-content-corner': shape === 'corner' && !noStyle,
'el-bubble-content-filled': variant === 'filled' && !noStyle,
'el-bubble-content-borderless': variant === 'borderless' && !noStyle,
'el-bubble-content-outlined': variant === 'outlined' && !noStyle,
'el-bubble-content-shadow': variant === 'shadow' && !noStyle,
'no-content': !content
}"
>
<div
v-if="!loading"
class="el-typewriter"
:class="{
'no-content': !content
}"
>
<Typewriter
v-if="!$slots.content && content"
ref="typewriterRef"
:typing="_typing"
:content="content"
:is-markdown="isMarkdown"
:is-fog="props.isFog"
@start="onStart"
@writing="onWriting"
@finish="onFinish"
/>
</div>
<!-- 内容-自定义 -->
<slot
v-if="!internalDestroyed && $slots.content && !loading"
name="content"
/>
<!-- 加载中-默认 -->
<div v-if="loading && !$slots.loading" class="el-bubble-loading-wrap">
<div
v-for="(_, index) in 3"
:key="index"
class="dot"
:style="{ animationDelay: `${index * 0.2}s` }"
/>
</div>
<!-- 加载中-自定义 -->
<div v-if="loading && $slots.loading" class="el-bubble-loading-wrap">
<slot name="loading" />
</div>
</div>
<div v-if="$slots.footer" class="el-bubble-footer">
<slot name="footer" />
</div>
</div>
</div>
</template>
<style scoped lang="scss" src="./style.scss"></style>