Skip to content

Fixes #209: Allow inserting emojis alongside text #202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ yarn-error.log*

# Misc
.ghnpmpkgtoken
package-lock.json
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ For more detailed examples see the demo folder.
| *close | Function | The function passed to the component that mutates the above mentioned bool toggle for closing the chat |
| messageList | [message] | An array of message objects to be rendered as a conversation. |
| showEmoji | Boolean | A bool indicating whether or not to show the emoji button
| sendEmojisDirectly | Boolean | A bool indicating whether or not to send emojis directly as a message as opposed to inserting them into the user input
| showFile | Boolean | A bool indicating whether or not to show the file chooser button
| showDeletion | Boolean | A bool indicating whether or not to show the edit button for a message
| showConfirmationDeletion | Boolean | A bool indicating whether or not to show the confirmation text when we remove a message
Expand Down
5 changes: 5 additions & 0 deletions src/ChatWindow.vue
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<UserInput
v-if="!showUserList"
:show-emoji="showEmoji"
:send-emojis-directly="sendEmojisDirectly"
:on-submit="onUserInputSubmit"
:suggestions="getSuggestions()"
:show-file="showFile"
Expand Down Expand Up @@ -78,6 +79,10 @@ export default {
type: Boolean,
default: false
},
sendEmojisDirectly: {
type: Boolean,
default: true
},
showFile: {
type: Boolean,
default: false
Expand Down
7 changes: 6 additions & 1 deletion src/Launcher.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
:title="chatWindowTitle"
:is-open="isOpen"
:show-emoji="showEmoji"
:send-emojis-directly="sendEmojisDirectly"
:show-file="showFile"
:show-confirmation-deletion="showConfirmationDeletion"
:confirmation-deletion-message="confirmationDeletionMessage"
Expand Down Expand Up @@ -91,7 +92,11 @@ export default {
},
showEmoji: {
type: Boolean,
default: false
default: () => false
},
sendEmojisDirectly: {
type: Boolean,
default: () => true
},
showEdition: {
type: Boolean,
Expand Down
55 changes: 54 additions & 1 deletion src/UserInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ export default {
type: Boolean,
default: () => false
},
sendEmojisDirectly: {
type: Boolean,
default: () => true
},
suggestions: {
type: Array,
default: () => []
Expand All @@ -145,7 +149,8 @@ export default {
data() {
return {
file: null,
inputActive: false
inputActive: false,
previousSelectionRange: null
}
},
computed: {
Expand All @@ -172,6 +177,24 @@ export default {
this.focusUserInput()
}
})

document.addEventListener('selectionchange', () => {
var selection = document.getSelection()
if (
!selection ||
!selection.anchorNode ||
(selection.anchorNode != this.$refs.userInput &&
selection.anchorNode.parentNode != this.$refs.userInput)
) {
return
}

if (selection.rangeCount) {
this.previousSelectionRange = selection.getRangeAt(0).cloneRange()
} else {
this.previousSelectionRange = null
}
})
},
methods: {
cancelFile() {
Expand Down Expand Up @@ -268,6 +291,13 @@ export default {
}
},
_handleEmojiPicked(emoji) {
if (this.sendEmojisDirectly) {
this._submitEmoji(emoji)
} else {
this._insertEmoji(emoji)
}
},
_submitEmoji(emoji) {
this._checkSubmitSuccess(
this.onSubmit({
author: 'me',
Expand All @@ -276,6 +306,29 @@ export default {
})
)
},
_insertEmoji(emoji) {
var range = this.previousSelectionRange
if (!range) {
if (!this.$refs.userInput.firstChild) {
this.$refs.userInput.append(document.createTextNode(''))
}
range = document.createRange()
range.setStart(this.$refs.userInput.firstChild, this.$refs.userInput.textContent.length)
range.collapse(true)
}

var selection = window.getSelection()
selection.removeAllRanges()
selection.addRange(range)

var textNode = document.createTextNode(emoji)

range.deleteContents()
range.insertNode(textNode)
range.collapse(false)

this.$refs.userInput.focus()
},
_handleFileSubmit(file) {
this.file = file
},
Expand Down