-
Notifications
You must be signed in to change notification settings - Fork 8
/
KtComment.vue
188 lines (183 loc) · 4.54 KB
/
KtComment.vue
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
<template>
<div class="comment">
<KtAvatar size="sm" :src="userAvatar" />
<div class="comment__wrapper">
<div class="comment__info">
<div class="info__name" v-text="userName" />
<div class="info__time" v-text="createdTime" />
</div>
<!-- eslint-disable vue/no-v-html -->
<div
v-if="!isInlineEdit"
class="comment__message"
v-html="postEscapeParser(dangerouslyOverrideParser(inlineMessage))"
/>
<!-- eslint-enable vue/no-v-html -->
<div v-else class="comment-inline-edit form-group">
<textarea
v-model="inlineMessageValue"
class="comment-inline-edit-input form-input"
></textarea>
<KtButtonGroup class="comment-inline-edit-buttons">
<KtButton icon="close" @click="cancelInlineEdit" />
<KtButton icon="check" @click="handleEditConfirm" />
</KtButtonGroup>
</div>
<div class="comment__action">
<div
class="action__reply"
@click="handleInlineReplyClick({ userName, userId })"
>
<i class="yoco">comment</i> Reply
</div>
<div v-if="actionOptions.length > 0" class="action__more">
<i class="yoco">dots</i>
<div class="action__options">
<a
v-for="option in actionOptions"
:key="option.type"
@click="option.onClick"
>
<li>{{ option.label }}</li>
</a>
</div>
</div>
</div>
<div v-for="reply in replies" :key="reply.id">
<CommentReply
:id="reply.id"
:createdTime="reply.createdTime"
:dangerouslyOverrideParser="dangerouslyOverrideParser"
:isDeletable="reply.isDeletable"
:isEditable="reply.isEditable"
:message="reply.message"
:postEscapeParser="postEscapeParser"
:userAvatar="reply.userAvatar"
:userId="reply.userId"
:userName="reply.userName"
@_inlineDeleteClick="handleDelete($event, 'INLINE')"
@_inlineEditSumbit="$emit('edit', $event)"
@_inlineReplyClick="handleInlineReplyClick"
/>
</div>
<KtCommentInput
v-if="showInlineReply"
isInline
:parentId="id"
:placeholder="replyToText"
:replyToUserId="replyToUserId"
:userAvatar="userAvatar"
@submit="handleInlineSubmit($event)"
/>
</div>
</div>
</template>
<script>
import escape from 'lodash/escape'
import { KtAvatar } from '../kotti-avatar'
import { KtButton } from '../kotti-button'
import { KtButtonGroup } from '../kotti-button-group'
import CommentReply from './components/CommentReply.vue'
import KtCommentInput from './KtCommentInput.vue'
import { defaultParser } from './utilities'
export default {
defaultParser,
name: 'KtComment',
components: {
KtAvatar,
KtButton,
KtButtonGroup,
CommentReply,
KtCommentInput,
},
props: {
createdTime: String,
dangerouslyOverrideParser: { default: escape, type: Function },
id: [Number, String],
isDeletable: { default: false, type: Boolean },
isEditable: { default: false, type: Boolean },
message: String,
postEscapeParser: { default: (_) => _, type: Function },
replies: Array,
userAvatar: String,
userId: [Number, String],
userName: String,
},
data() {
return {
showInlineReply: false,
replyToText: null,
replyToUserId: Number,
isInlineEdit: false,
inlineMessageValue: '',
}
},
computed: {
styleObject() {
return {
avatar: true,
'avatar-selected': this.selected,
}
},
inlineMessage() {
return this.inlineMessageValue || this.message
},
actionOptions() {
const options = []
if (this.isEditable)
options.push({
label: 'Edit',
onClick: () => {
this.inlineMessageValue = this.inlineMessage
this.isInlineEdit = true
},
type: 'EDIT',
})
if (this.isDeletable)
options.push({
label: 'Delete',
onClick: () => this.handleDelete(this.id),
type: 'DELETE',
})
return options
},
},
methods: {
cancelInlineEdit() {
this.inlineMessageValue = ''
this.isInlineEdit = false
},
handleInlineReplyClick(user) {
this.showInlineReply = true
this.replyToUserId = user.userId
this.replyToText = `Reply to ${user.userName}`
},
handleDelete(event, type) {
const _payload = {
id: event,
parentId: type ? this.id : null,
}
this.$emit('delete', _payload)
},
handleInlineSubmit(event) {
this.showInlineReply = false
this.$emit('submit', event)
},
handleEditConfirm() {
this.isInlineEdit = false
if (!this.inlineMessageValue) return
const _payload = {
id: this.id,
message: this.inlineMessageValue,
}
this.$emit('edit', _payload)
},
},
}
</script>
<style lang="scss" scoped>
.action__reply {
display: flex;
align-items: center;
}
</style>