Skip to content

Commit e9342c1

Browse files
committed
Update chat example
1 parent c3929ef commit e9342c1

File tree

6 files changed

+376
-32
lines changed

6 files changed

+376
-32
lines changed

example/src/App.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ body {
3131
#nav {
3232
@media (max-width: 640px) {
3333
padding: 0;
34+
overflow-x: auto;
35+
white-space: nowrap;
3436
}
3537
a {
3638
margin-right: 1.5em;
Lines changed: 131 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,148 @@
11
<template>
22
<div class="editor">
3-
<div class="rich" contenteditable="true"></div>
3+
<div
4+
ref="rich"
5+
class="rich"
6+
contenteditable="true"
7+
:placehoder="placehoder"
8+
@keydown="onKeydown"
9+
@input="onInput"
10+
@paste="onPaste"
11+
></div>
12+
<div class="send" :class="{ disabled: disabledSend }" @click="eventSend"></div>
13+
<img class="author-avatar" src="https://avatars1.githubusercontent.com/u/6846113" alt="preload-avatar">
14+
<div class="auto">
15+
<button @click="eventClickMockReceived" class="button button-clear item">received one random</button>
16+
<span class="line">|</span>
17+
<button @click="eventClickMockSend" class="button button-clear item">send one random</button>
18+
</div>
419
</div>
520
</template>
621

722
<script>
23+
import { isMobile } from '../../common/ua'
24+
import { genBody, genSid } from './util'
25+
26+
const genMessage = (content) => {
27+
let body = genBody()
28+
body.user = {
29+
name: 'tangbc',
30+
avatar: 'https://avatars1.githubusercontent.com/u/6846113'
31+
}
32+
body.sid = genSid(),
33+
body.content = content
34+
body.isCreator = true
35+
return body
36+
}
37+
838
export default {
9-
name: 'editor'
39+
name: 'editor',
40+
41+
props: {
42+
send: {
43+
type: Function
44+
},
45+
received: {
46+
type: Function
47+
}
48+
},
49+
50+
data () {
51+
return {
52+
disabledSend: true,
53+
placehoder: isMobile ? 'Input message here' : 'Input message here and press enter to send'
54+
}
55+
},
56+
57+
mounted () {
58+
if (this.$refs.rich) {
59+
this.$refs.rich.focus()
60+
}
61+
},
62+
63+
methods: {
64+
onKeydown (e) {
65+
if (e.keyCode === 13) {
66+
this.eventSend()
67+
e.preventDefault()
68+
}
69+
},
70+
71+
onInput () {
72+
if (this.$refs.rich) {
73+
this.disabledSend = !(this.$refs.rich.textContent || '').trim('')
74+
}
75+
},
76+
77+
onPaste (e) {
78+
e.preventDefault()
79+
const plainText = (e.originalEvent || e).clipboardData.getData('text/plain')
80+
document.execCommand("insertText", false, plainText)
81+
},
82+
83+
eventSend () {
84+
if (this.$refs.rich && !this.disabledSend) {
85+
const content = (this.$refs.rich.textContent || '').trim()
86+
const message = genMessage(content)
87+
this.$refs.rich.innerHTML = ''
88+
this.$emit('send', message)
89+
}
90+
},
91+
92+
eventClickMockReceived () {
93+
this.received()
94+
},
95+
96+
eventClickMockSend () {
97+
this.send(genMessage(''))
98+
}
99+
}
10100
}
11101
</script>
12102

13103
<style lang="less" scoped>
14104
.editor {
15-
padding: 1em;
16-
border: 2px solid;
17-
border-color: dimgray;
18-
border-top: 1px solid;
105+
position: relative;
19106
}
20107
.rich {
108+
border: 2px solid;
109+
border-color: dimgray;
110+
border-top: none;
111+
width: 100%;
112+
padding: 1em 4em 1em 1em;
21113
outline: none;
114+
cursor: text;
115+
background-color: #fffaf0;
116+
&:empty::before{
117+
color: darkgray;
118+
content: attr(placehoder);
119+
}
120+
}
121+
.send {
122+
position: absolute;
123+
right: 20px;
124+
top: 15px;
125+
width: 33px;
126+
height: 30px;
127+
cursor: pointer;
128+
background-size: cover;
129+
background-image: url("data:image/svg+xml,%3Csvg t='1587742943994' class='icon' viewBox='0 0 1024 1024' version='1.1' xmlns='http://www.w3.org/2000/svg' p-id='3418' width='200' height='200'%3E%3Cpath d='M0 561.6l366.4 100.8 400-356.8-304 401.6 363.2 107.2L1024 46.4zM460.8 977.6l126.4-192-126.4-25.6z' p-id='3419' fill='%239b4dca'%3E%3C/path%3E%3C/svg%3E");
130+
&:active {
131+
opacity: .7;
132+
}
133+
&.disabled {
134+
opacity: .3;
135+
pointer-events: none;
136+
}
137+
}
138+
.author-avatar {
139+
display: none;
140+
}
141+
.item {
142+
padding: 0;
143+
}
144+
.line {
145+
font-size: 12px;
146+
margin: 0 1em;
22147
}
23148
</style>

example/src/views/chat-room/Item.vue

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<template>
2-
<div class="msg">
2+
<div class="item" :class="{ creator: source.isCreator }">
33
<div class="avatar">
44
<img :src="source.user.avatar" />
55
</div>
66
<div class="body">
7-
<div class="name">{{ source.user.name }}</div>
7+
<div class="name" v-if="!source.isCreator">{{ source.user.name }}</div>
88
<div class="content">
9-
{{ source.content }}
9+
<div class="text">{{ source.content }}</div>
1010
</div>
1111
</div>
1212
</div>
@@ -34,13 +34,17 @@ export default {
3434
</script>
3535

3636
<style lang="less" scoped>
37-
.msg {
37+
.item {
3838
display: flex;
3939
.avatar {
4040
width: 40px;
4141
height: 40px;
4242
border-radius: 50%;
43-
background: rgba(255, 192, 203, .2);;
43+
background: rgba(255, 192, 203, .2);
44+
@media (max-width: 640px) {
45+
width: 30px;
46+
height: 30px;
47+
}
4448
img {
4549
display: block;
4650
width: 100%;
@@ -52,15 +56,25 @@ export default {
5256
flex: 1;
5357
padding-left: 1em;
5458
font-size: 16px;
59+
max-width: 560px;
60+
word-break: break-word;
61+
@media (max-width: 640px) {
62+
font-size: 14px;
63+
max-width: unset;
64+
}
5565
.name {
5666
padding-bottom: .2em;
67+
font-size: 12px;
5768
}
5869
.content {
5970
position: relative;
6071
color: #000;
6172
background-color: aliceblue;
6273
border-radius: 15px;
6374
padding: .5em 1em;
75+
@media (max-width: 640px) {
76+
padding: .5em;
77+
}
6478
&:after{
6579
content: '';
6680
position: absolute;
@@ -78,5 +92,20 @@ export default {
7892
}
7993
}
8094
}
95+
&.creator {
96+
transform: rotateX(180deg);
97+
direction: rtl;
98+
align-items: flex-end;
99+
.avatar {
100+
transform: rotateX(180deg);
101+
}
102+
.body {
103+
transform: rotate(180deg);
104+
}
105+
.text {
106+
transform: rotateY(180deg);
107+
direction: ltr;
108+
}
109+
}
81110
}
82111
</style>

0 commit comments

Comments
 (0)