#187 - Enter to focus chat when not focused
This commit is contained in:
parent
b3d68ef562
commit
5d9b4fd19a
@ -21,8 +21,8 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { onBeforeUnmount, ref, nextTick } from 'vue'
|
import { onBeforeUnmount, ref, nextTick, onMounted } from 'vue'
|
||||||
import { onClickOutside } from '@vueuse/core'
|
import { onClickOutside, useFocus } from '@vueuse/core'
|
||||||
import { useGameStore } from '@/stores/gameStore'
|
import { useGameStore } from '@/stores/gameStore'
|
||||||
import type { Chat } from '@/types'
|
import type { Chat } from '@/types'
|
||||||
import { useZoneStore } from '@/stores/zoneStore'
|
import { useZoneStore } from '@/stores/zoneStore'
|
||||||
@ -37,6 +37,14 @@ const chats = ref([] as Chat[])
|
|||||||
const chatWindow = ref<HTMLElement | null>(null)
|
const chatWindow = ref<HTMLElement | null>(null)
|
||||||
const chatInput = ref<HTMLElement | null>(null)
|
const chatInput = ref<HTMLElement | null>(null)
|
||||||
|
|
||||||
|
const { focused } = useFocus(chatInput)
|
||||||
|
|
||||||
|
function focusChat(event: KeyboardEvent) {
|
||||||
|
if (event.key === 'Enter' && !focused.value) {
|
||||||
|
focused.value = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
onClickOutside(chatInput, (event) => unfocusChat(event, chatInput.value as HTMLElement))
|
onClickOutside(chatInput, (event) => unfocusChat(event, chatInput.value as HTMLElement))
|
||||||
|
|
||||||
function unfocusChat(event: Event, targetElement: HTMLElement) {
|
function unfocusChat(event: Event, targetElement: HTMLElement) {
|
||||||
@ -128,7 +136,12 @@ gameStore.connection?.on('chat:message', (data: Chat) => {
|
|||||||
})
|
})
|
||||||
scrollToBottom()
|
scrollToBottom()
|
||||||
|
|
||||||
|
onMounted(() => {
|
||||||
|
addEventListener('keydown', focusChat)
|
||||||
|
})
|
||||||
|
|
||||||
onBeforeUnmount(() => {
|
onBeforeUnmount(() => {
|
||||||
gameStore.connection?.off('chat:message')
|
gameStore.connection?.off('chat:message')
|
||||||
|
removeEventListener('keydown', focusChat)
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user