Skip to content

Commit 8f8deaa

Browse files
committed
allow emojis to by inserted into the userinput field
1 parent 64a09cb commit 8f8deaa

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

src/ChatWindow.vue

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
<UserInput
5050
v-if="!showUserList"
5151
:show-emoji="showEmoji"
52+
:send-emojis-directly="sendEmojisDirectly"
5253
:on-submit="onUserInputSubmit"
5354
:suggestions="getSuggestions()"
5455
:show-file="showFile"
@@ -78,6 +79,10 @@ export default {
7879
type: Boolean,
7980
default: false
8081
},
82+
sendEmojisDirectly: {
83+
type: Boolean,
84+
default: true
85+
},
8186
showFile: {
8287
type: Boolean,
8388
default: false

src/Launcher.vue

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
:title="chatWindowTitle"
2121
:is-open="isOpen"
2222
:show-emoji="showEmoji"
23+
:send-emojis-directly="sendEmojisDirectly"
2324
:show-file="showFile"
2425
:show-confirmation-deletion="showConfirmationDeletion"
2526
:confirmation-deletion-message="confirmationDeletionMessage"
@@ -91,7 +92,11 @@ export default {
9192
},
9293
showEmoji: {
9394
type: Boolean,
94-
default: false
95+
default: () => false
96+
},
97+
sendEmojisDirectly: {
98+
type: Boolean,
99+
default: () => true
95100
},
96101
showEdition: {
97102
type: Boolean,

src/UserInput.vue

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ export default {
121121
type: Boolean,
122122
default: () => false
123123
},
124+
sendEmojisDirectly: {
125+
type: Boolean,
126+
default: () => true
127+
},
124128
suggestions: {
125129
type: Array,
126130
default: () => []
@@ -145,7 +149,8 @@ export default {
145149
data() {
146150
return {
147151
file: null,
148-
inputActive: false
152+
inputActive: false,
153+
previousSelectionRange: null
149154
}
150155
},
151156
computed: {
@@ -172,6 +177,22 @@ export default {
172177
this.focusUserInput()
173178
}
174179
})
180+
181+
document.addEventListener('selectionchange', () => {
182+
var selection = document.getSelection()
183+
if (
184+
selection.anchorNode != this.$refs.userInput &&
185+
selection.anchorNode.parentNode != this.$refs.userInput
186+
) {
187+
return
188+
}
189+
190+
if (selection.rangeCount) {
191+
this.previousSelectionRange = selection.getRangeAt(0).cloneRange()
192+
} else {
193+
this.previousSelectionRange = null
194+
}
195+
})
175196
},
176197
methods: {
177198
cancelFile() {
@@ -268,6 +289,13 @@ export default {
268289
}
269290
},
270291
_handleEmojiPicked(emoji) {
292+
if (this.sendEmojisDirectly) {
293+
this._submitEmoji(emoji)
294+
} else {
295+
this._insertEmoji(emoji)
296+
}
297+
},
298+
_submitEmoji(emoji) {
271299
this._checkSubmitSuccess(
272300
this.onSubmit({
273301
author: 'me',
@@ -276,6 +304,27 @@ export default {
276304
})
277305
)
278306
},
307+
_insertEmoji(emoji) {
308+
var range = this.previousSelectionRange
309+
if (!range) {
310+
console.log('First child: ' + this.$refs.userInput.firstChild)
311+
range = document.createRange()
312+
range.setStart(this.$refs.userInput.firstChild, this.$refs.userInput.textContent.length)
313+
range.collapse(true)
314+
}
315+
316+
var selection = window.getSelection()
317+
selection.removeAllRanges()
318+
selection.addRange(range)
319+
320+
var textNode = document.createTextNode(emoji)
321+
322+
range.deleteContents()
323+
range.insertNode(textNode)
324+
range.collapse(false)
325+
326+
this.$refs.userInput.focus()
327+
},
279328
_handleFileSubmit(file) {
280329
this.file = file
281330
},

0 commit comments

Comments
 (0)