chore: Move Canned Responses list to a separate component (#1681)

This commit is contained in:
Pranav Raj S
2021-01-23 14:07:01 +05:30
committed by GitHub
parent d64ba1df8b
commit e75916d562
4 changed files with 145 additions and 97 deletions

View File

@@ -8,22 +8,6 @@
}
.reply-box__top {
.canned {
background: var(--white);
border-bottom: var(--space-small) solid var(--white);
border-top: 1px solid var(--color-border);
left: 0;
max-height: 14rem;
overflow: auto;
padding-top: var(--space-small);
position: absolute;
width: 100%;
z-index: 100;
.active a {
background: $color-woot;
}
}
.icon {
color: $medium-gray;

View File

@@ -1,95 +1,45 @@
<template>
<ul
v-if="cannedMessages.length"
id="canned-list"
class="vertical dropdown menu canned"
:style="{ top: getTopPadding() + 'rem' }"
>
<li
v-for="(item, index) in cannedMessages"
:id="`canned-${index}`"
:key="item.short_code"
:class="{ active: index === selectedIndex }"
@click="onListItemSelection(index)"
@mouseover="onHover(index)"
>
<a class="text-truncate">
<strong>{{ item.short_code }}</strong> - {{ item.content }}
</a>
</li>
</ul>
<mention-box :items="items" @mention-select="handleMentionClick" />
</template>
<script>
import { mapGetters } from 'vuex';
import MentionBox from '../mentions/MentionBox.vue';
export default {
props: ['onKeyenter', 'onClick'],
data() {
return {
selectedIndex: 0,
};
components: { MentionBox },
props: {
searchKey: {
type: String,
default: '',
},
},
computed: {
...mapGetters({
cannedMessages: 'getCannedResponses',
}),
items() {
return this.cannedMessages.map(cannedMessage => ({
label: cannedMessage.short_code,
key: cannedMessage.short_code,
description: cannedMessage.content,
}));
},
},
watch: {
cannedMessages(newCannedMessages) {
if (newCannedMessages.length < this.selectedIndex + 1) {
this.selectedIndex = 0;
}
searchKey() {
this.fetchCannedResponses();
},
},
mounted() {
document.addEventListener('keydown', this.keyListener);
},
beforeDestroy() {
document.removeEventListener('keydown', this.keyListener);
this.fetchCannedResponses();
},
methods: {
getTopPadding() {
if (this.cannedMessages.length <= 4) {
return -(this.cannedMessages.length * 2.8 + 1.7);
}
return -14;
fetchCannedResponses() {
this.$store.dispatch('getCannedResponse', { searchKey: this.searchKey });
},
isUp(e) {
return e.keyCode === 38 || (e.ctrlKey && e.keyCode === 80); // UP, Ctrl-P
},
isDown(e) {
return e.keyCode === 40 || (e.ctrlKey && e.keyCode === 78); // DOWN, Ctrl-N
},
isEnter(e) {
return e.keyCode === 13;
},
keyListener(e) {
if (this.isUp(e)) {
if (!this.selectedIndex) {
this.selectedIndex = this.cannedMessages.length - 1;
} else {
this.selectedIndex -= 1;
}
}
if (this.isDown(e)) {
if (this.selectedIndex === this.cannedMessages.length - 1) {
this.selectedIndex = 0;
} else {
this.selectedIndex += 1;
}
}
if (this.isEnter(e)) {
this.onKeyenter(this.cannedMessages[this.selectedIndex].content);
}
this.$el.scrollTop = 28 * this.selectedIndex;
},
onHover(index) {
this.selectedIndex = index;
},
onListItemSelection(index) {
this.selectedIndex = index;
this.onClick(this.cannedMessages[this.selectedIndex].content);
handleMentionClick(item = {}) {
this.$emit('click', item.description);
},
},
};

View File

@@ -10,9 +10,8 @@
<canned-response
v-if="showCannedResponsesList"
v-on-clickaway="hideCannedResponse"
data-dropdown-menu
:on-keyenter="replaceText"
:on-click="replaceText"
:search-key="cannedResponseSearchKey"
@click="replaceText"
/>
<emoji-input
v-if="showEmojiPicker"
@@ -114,6 +113,7 @@ export default {
isUploading: false,
replyType: REPLY_EDITOR_MODES.REPLY,
isFormatMode: false,
cannedResponseSearchKey: '',
};
},
computed: {
@@ -234,14 +234,13 @@ export default {
const hasNextWord = updatedMessage.includes(' ');
const isShortCodeActive = isSlashCommand && !hasNextWord;
if (isShortCodeActive) {
this.cannedResponseSearchKey = updatedMessage.substr(
1,
updatedMessage.length
);
this.showCannedResponsesList = true;
if (updatedMessage.length > 1) {
const searchKey = updatedMessage.substr(1, updatedMessage.length);
this.$store.dispatch('getCannedResponse', { searchKey });
} else {
this.$store.dispatch('getCannedResponse');
}
} else {
this.cannedResponseSearchKey = '';
this.showCannedResponsesList = false;
}
},

View File

@@ -0,0 +1,115 @@
<template>
<ul
v-if="items.length"
class="vertical dropdown menu mention--box"
:style="{ top: getTopPadding() + 'rem' }"
>
<li
v-for="(item, index) in items"
:id="`mention-item-${index}`"
:key="item.key"
:class="{ active: index === selectedIndex }"
@click="onListItemSelection(index)"
@mouseover="onHover(index)"
>
<a class="text-truncate">
<strong>{{ item.label }}</strong> - {{ item.description }}
</a>
</li>
</ul>
</template>
<script>
export default {
props: {
items: {
type: Array,
default: () => {},
},
},
data() {
return {
selectedIndex: 0,
};
},
watch: {
items(newItems) {
if (newItems.length < this.selectedIndex + 1) {
this.selectedIndex = 0;
}
},
},
mounted() {
document.addEventListener('keydown', this.keyListener);
},
beforeDestroy() {
document.removeEventListener('keydown', this.keyListener);
},
methods: {
getTopPadding() {
if (this.items.length <= 4) {
return -(this.items.length * 2.8 + 1.7);
}
return -14;
},
isUp(e) {
return e.keyCode === 38 || (e.ctrlKey && e.keyCode === 80); // UP, Ctrl-P
},
isDown(e) {
return e.keyCode === 40 || (e.ctrlKey && e.keyCode === 78); // DOWN, Ctrl-N
},
isEnter(e) {
return e.keyCode === 13;
},
keyListener(e) {
if (this.isUp(e)) {
if (!this.selectedIndex) {
this.selectedIndex = this.items.length - 1;
} else {
this.selectedIndex -= 1;
}
}
if (this.isDown(e)) {
if (this.selectedIndex === this.items.length - 1) {
this.selectedIndex = 0;
} else {
this.selectedIndex += 1;
}
}
if (this.isEnter(e)) {
this.onMentionSelect();
}
this.$el.scrollTop = 28 * this.selectedIndex;
},
onHover(index) {
this.selectedIndex = index;
},
onListItemSelection(index) {
this.selectedIndex = index;
this.onMentionSelect();
},
onMentionSelect() {
this.$emit('mention-select', this.items[this.selectedIndex]);
},
},
};
</script>
<style scoped lang="scss">
.mention--box {
background: var(--white);
border-bottom: var(--space-small) solid var(--white);
border-top: 1px solid var(--color-border);
left: 0;
max-height: 14rem;
overflow: auto;
padding-top: var(--space-small);
position: absolute;
width: 100%;
z-index: 100;
.active a {
background: var(--w-500);
}
}
</style>