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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
|
<!--
- @copyright Copyright (c) 2019 Marco Ambrosini <marcoambrosini@pm.me>
-
- @author Marco Ambrosini <marcoambrosini@pm.me>
-
- @license GNU AGPL version 3 or any later version
-
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License as
- published by the Free Software Foundation, either version 3 of the
- License, or (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Affero General Public License for more details.
-
- You should have received a copy of the GNU Affero General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<template>
<At ref="at"
v-model="text"
class="atwho-wrapper"
name-key="label"
:members="autoCompleteMentionCandidates"
:filter-match="atFilter"
:tab-select="true"
:allow-spaces="false"
@at="handleAtEvent">
<template #item="scope">
<Avatar v-if="isMentionToAll(scope.item.id)"
:size="44"
:icon-class="'icon-group-forced-white'"
:disable-tooltip="true"
:disable-menu="true"
:is-no-user="true" />
<div v-else-if="isMentionToGuest(scope.item.id)"
class="avatar guest"
:style="getGuestAvatarStyle()">
{{ getFirstLetterOfGuestName(scope.item.label) }}
</div>
<Avatar v-else
:size="44"
:user="atRemoveQuotesFromUserIdForAvatars(scope.item.id)"
:display-name="scope.item.label"
:disable-tooltip="true"
:disable-menu="true" />
<span class="mention-suggestion">
<span>{{ scope.item.label }}</span>
<em v-if="getStatusMessage(scope.item)"
class="user-status">
{{ getStatusMessage(scope.item) }}
</em>
</span>
</template>
<template #embeddedItem="scope">
<!-- The root element itself is ignored, only its contents are taken
into account. -->
<span>
<!-- vue-at seems to try to create an embedded item at some
strange times in which no item is selected and thus there
is no data, so do not use the Mention component in those
cases. -->
<Mention
v-if="scope.current.id"
:id="scope.current.id"
:type="getTypeForMentionComponent(scope.current)"
:name="scope.current.label"
:data-mention-id="scope.current.id" />
</span>
</template>
<div
ref="contentEditable"
v-shortkey.once="['c']"
:contenteditable="activeInput"
:placeHolder="placeholderText"
role="textbox"
:aria-label="ariaLabel"
aria-multiline="true"
class="new-message-form__advancedinput"
@shortkey="focusInput"
@keydown.enter="handleKeydownEnter"
@keydown.esc.prevent="handleKeydownEsc"
@blur="onBlur"
@paste="onPaste" />
</At>
</template>
<script>
import At from 'vue-at'
import UserStatus from '../../../mixins/userStatus'
import VueAtReparenter from '../../../mixins/vueAtReparenter'
import { EventBus } from '../../../services/EventBus'
import { searchPossibleMentions } from '../../../services/mentionsService'
import { fetchClipboardContent } from '../../../utils/clipboard'
import Avatar from '@nextcloud/vue/dist/Components/Avatar'
import Mention from '../../MessagesList/MessagesGroup/Message/MessagePart/Mention'
import escapeHtml from 'escape-html'
import debounce from 'debounce'
/**
* Checks whether the given style sheet is the default style sheet from the
* vue-at component or not.
*
* @param {CSSStyleSheet} sheet the style sheet to check.
* @return {boolean} True if it is the style sheet from vue-at, false
* otherwise.
*/
function isDefaultAtWhoStyleSheet(sheet) {
try {
// cssRules may not be defined in Chromium if the stylesheet is loaded
// from a different domain.
if (!sheet.cssRules) {
return false
}
if (sheet.cssRules.length !== 15) {
return false
}
for (const cssRule of sheet.cssRules) {
// The only way to identify the style sheet is by looking to its
// rules. Moreover, a rather complex rule is needed so the style
// sheet is not mismatched with a different atwho stylesheet, for
// example, the one added by the Comments app.
if (cssRule.cssText === '.atwho-view { color: rgb(0, 0, 0); border-radius: 3px; box-shadow: rgba(0, 0, 0, 0.1) 0px 0px 5px; min-width: 120px; z-index: 11110 !important; }') {
return true
}
}
} catch (exception) {
// Accessing cssRules may throw a SecurityError in Firefox if the style
// sheet is loaded from a different domain.
if (exception.name !== 'SecurityError') {
throw exception
}
}
return false
}
/**
* Removes the default atwho style sheet added by the vue-at component.
*
* The rules in that style sheet are too broad and affect other elements
* besides those from the vue-at panel.
*/
function removeDefaultAtWhoStyleSheet() {
const styleElements = document.querySelectorAll('style')
for (let i = 0; i < styleElements.length; i++) {
if (isDefaultAtWhoStyleSheet(styleElements[i].sheet)) {
styleElements[i].remove()
return
}
}
}
removeDefaultAtWhoStyleSheet()
export default {
name: 'AdvancedInput',
components: {
At,
Avatar,
Mention,
},
mixins: [
VueAtReparenter,
UserStatus,
],
props: {
/**
* The placeholder for the input field
*/
placeholderText: {
type: String,
default: '',
},
ariaLabel: {
type: String,
default: '',
},
/**
* Determines if the input is active
*/
activeInput: {
type: Boolean,
default: true,
},
value: {
type: String,
required: true,
},
/**
* The token of the conversation to get candidate mentions for.
*/
token: {
type: String,
required: true,
},
},
data() {
return {
text: '',
autoCompleteMentionCandidates: [],
}
},
watch: {
text(text) {
this.$nextTick(() => {
this.$emit('update:contentEditable', this.$refs.contentEditable.cloneNode(true))
})
this.$emit('update:value', text)
this.$emit('input', text)
this.$emit('change', text)
},
value(value) {
this.text = value
},
atwho(atwho) {
if (!atwho) {
// Clear mention candidates when closing the panel. Otherwise
// they would be shown when the panel is opened again until the
// new ones are received.
this.autoCompleteMentionCandidates = []
}
},
},
mounted() {
/**
* Listen to routeChange global events and focus on the
*/
EventBus.$on('focusChatInput', this.focusInput)
this.atWhoPanelExtraClasses = 'talk candidate-mentions'
},
beforeDestroy() {
EventBus.$off('focusChatInput', this.focusInput)
},
methods: {
onBlur() {
// requires a short delay to avoid blocking click event handlers
// from vue-at which also have some delay in place...
// a setTimeout was recommended by the library author here:
// https://github.com/fritx/vue-at/issues/114#issuecomment-565777450
setTimeout(() => {
if (this.$refs.at) {
this.$refs.at.closePanel()
}
}, 100)
},
onPaste(e) {
e.preventDefault()
const content = fetchClipboardContent(e)
if (content.kind === 'file') {
this.$emit('files-pasted', content.files)
} else if (content.kind === 'text') {
const text = content.text
const div = document.createElement('div').innerText = escapeHtml(text)
document.execCommand('insertHtml', false, div)
this.focusInput()
}
},
/**
* The vue-at library only searches in the display name by default.
* But luckily our server responds already only with matching items,
* so we just filter none and show them all.
*
* @return {boolean} True as we never filter anything out
*/
atFilter() {
return true
},
atRemoveQuotesFromUserIdForAvatars(userId) {
if (userId.startsWith('"')) {
return userId.substring(1, userId.length - 1)
}
return userId
},
/**
* Focuses the contenteditable div input
*/
focusInput() {
if (!this.$route || this.$route.name === 'conversation') {
const contentEditable = this.$refs.contentEditable
// This is a hack but it's the only way I've found to focus a contenteditable div
setTimeout(function() {
contentEditable.focus()
}, 0)
}
},
/**
* Emits the submit event when enter is pressed (look
* at the v-on in the template) unless shift is pressed:
* in this case a new line will be created.
*
* @param {object} event the event object;
*/
handleKeydownEnter(event) {
// Prevent submit event when vue-at panel is open, as that should
// just select the mention from the panel.
if (this.atwho) {
return
}
// If the user is stil composing by an input method,
// we should not submit the message
if (event.isComposing) {
return
}
// TODO: add support for CTRL+ENTER new line
if (!(event.shiftKey)) {
event.preventDefault()
this.$emit('submit', event)
}
},
/**
* Loose focus of the chat so shortcuts can be used
*
* @param {object} event the event object;
*/
handleKeydownEsc(event) {
document.activeElement.blur()
},
/**
* Sets the autocomplete mention candidates based on the matched text
* after the "@".
*
* @param {string} chunk the matched text to look candidate mentions for.
*/
handleAtEvent: debounce(function(chunk) {
this.queryPossibleMentions(chunk)
}, 400),
async queryPossibleMentions(chunk) {
const response = await searchPossibleMentions(this.token, chunk)
const possibleMentions = response.data.ocs.data
// Wrap mention ids with spaces in quotes.
possibleMentions.forEach(possibleMention => {
if (possibleMention.id.indexOf(' ') !== -1
|| possibleMention.id.indexOf('guest/') === 0) {
possibleMention.id = '"' + possibleMention.id + '"'
}
})
this.autoCompleteMentionCandidates = possibleMentions
},
isMentionToAll(mentionId) {
return mentionId === 'all'
},
isMentionToGuest(mentionId) {
// Guest ids, like ids of users with spaces, are wrapped in quotes.
return mentionId.startsWith('"guest/')
},
getGuestAvatarStyle() {
return {
width: '44px',
height: '44px',
'line-height': '44px',
'background-color': '#b9b9b9',
'text-align': 'center',
}
},
getFirstLetterOfGuestName(displayName) {
const customName = displayName !== t('spreed', 'Guest') ? displayName : '?'
return customName.charAt(0)
},
getTypeForMentionComponent(candidate) {
if (this.isMentionToAll(candidate.id)) {
return 'call'
} else if (this.isMentionToGuest(candidate.id)) {
return 'guest'
}
return 'user'
},
},
}
</script>
<style lang="scss" scoped>
@use 'sass:math';
@import '../../../assets/variables';
.atwho-wrapper {
display: flex;
}
.new-message-form__advancedinput {
overflow: visible;
width: 100%;
border:none;
margin: 0 4px !important;
word-break: break-word;
white-space: pre-wrap;
padding: 8px 16px 8px 48px;
}
// Support for the placeholder text in the div contenteditable
div[contenteditable] {
font-size: $chat-font-size;
line-height: $chat-line-height;
min-height: $clickable-area;
border-radius: math.div($clickable-area, 2);
border: 1px solid var(--color-border-dark);
max-height: 180px;
overflow-y: auto;
&:hover,
&:focus,
&:active {
border: 1px solid var(--color-primary-element) !important;
}
}
// Support for the placeholder text in the div contenteditable
[contenteditable]:empty:before{
content: attr(placeholder);
display: block;
color: var(--color-text-maxcontrast);
}
.mention-suggestion {
max-width: 250px;
padding-left: 8px;
.user-status {
overflow: hidden;
text-overflow: ellipsis;
display: block;
margin-top: 2px;
}
}
</style>
|