forked from quasarframework/quasar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQChatMessage.js
More file actions
156 lines (132 loc) · 3.94 KB
/
QChatMessage.js
File metadata and controls
156 lines (132 loc) · 3.94 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
import { h, computed } from 'vue'
import { createComponent } from '../../utils/private/create.js'
import { getNormalizedVNodes } from '../../utils/private/vm.js'
export default createComponent({
name: 'QChatMessage',
props: {
sent: Boolean,
label: String,
bgColor: String,
textColor: String,
name: String,
avatar: String,
text: Array,
stamp: String,
size: String,
labelHtml: Boolean,
nameHtml: Boolean,
textHtml: Boolean,
stampHtml: Boolean
},
setup (props, { slots }) {
const op = computed(() => (props.sent === true ? 'sent' : 'received'))
const textClass = computed(() =>
`q-message-text-content q-message-text-content--${ op.value }`
+ (props.textColor !== void 0 ? ` text-${ props.textColor }` : '')
)
const messageClass = computed(() =>
`q-message-text q-message-text--${ op.value }`
+ (props.bgColor !== void 0 ? ` text-${ props.bgColor }` : '')
)
const containerClass = computed(() =>
'q-message-container row items-end no-wrap'
+ (props.sent === true ? ' reverse' : '')
)
const sizeClass = computed(() => (props.size !== void 0 ? `col-${ props.size }` : ''))
const domProps = computed(() => ({
msg: props.textHtml === true ? 'innerHTML' : 'textContent',
stamp: props.stampHtml === true ? 'innerHTML' : 'textContent',
name: props.nameHtml === true ? 'innerHTML' : 'textContent',
label: props.labelHtml === true ? 'innerHTML' : 'textContent'
}))
function wrapStamp (node) {
if (slots.stamp !== void 0) {
return [ node, h('div', { class: 'q-message-stamp' }, slots.stamp()) ]
}
if (props.stamp) {
return [
node,
h('div', {
class: 'q-message-stamp',
[ domProps.value.stamp ]: props.stamp
})
]
}
return [ node ]
}
function getText (contentList, withSlots) {
const content = withSlots === true
? (contentList.length > 1 ? text => text : text => h('div', [ text ]))
: text => h('div', { [ domProps.value.msg ]: text })
return contentList.map((msg, index) => h('div', {
key: index,
class: messageClass.value
}, [
h('div', { class: textClass.value }, wrapStamp(content(msg)))
]))
}
return () => {
const container = []
if (slots.avatar !== void 0) {
container.push(slots.avatar())
}
else if (props.avatar !== void 0) {
container.push(
h('img', {
class: `q-message-avatar q-message-avatar--${ op.value }`,
src: props.avatar,
'aria-hidden': 'true'
})
)
}
const msg = []
if (slots.name !== void 0) {
msg.push(
h('div', { class: `q-message-name q-message-name--${ op.value }` }, slots.name())
)
}
else if (props.name !== void 0) {
msg.push(
h('div', {
class: `q-message-name q-message-name--${ op.value }`,
[ domProps.value.name ]: props.name
})
)
}
if (slots.default !== void 0) {
msg.push(
getText(
getNormalizedVNodes(slots.default()),
true
)
)
}
else if (props.text !== void 0) {
msg.push(getText(props.text))
}
container.push(
h('div', { class: sizeClass.value }, msg)
)
const child = []
if (slots.label !== void 0) {
child.push(
h('div', { class: 'q-message-label' }, slots.label())
)
}
else if (props.label !== void 0) {
child.push(
h('div', {
class: 'q-message-label',
[ domProps.value.label ]: props.label
})
)
}
child.push(
h('div', { class: containerClass.value }, container)
)
return h('div', {
class: `q-message q-message-${ op.value }`
}, child)
}
}
})