refs #543 Set shortcut key to move toot in home

This commit is contained in:
AkiraFukushima 2018-08-21 23:02:05 +09:00
parent 8d7292f549
commit 0bd39a22a7
3 changed files with 84 additions and 4 deletions

View File

@ -1,5 +1,12 @@
<template> <template>
<div class="status" tabIndex="0"> <div
class="status"
tabIndex="0"
v-shortkey="{next: ['ctrl', 'n'], prev: ['ctrl', 'p']}"
@shortkey="handleTootControl"
ref="status"
@click="$emit('selectToot')"
>
<div v-show="filtered(message)" class="filtered"> <div v-show="filtered(message)" class="filtered">
Filtered Filtered
</div> </div>
@ -131,6 +138,10 @@ export default {
filter: { filter: {
type: String, type: String,
default: '' default: ''
},
focused: {
type: Boolean,
default: false
} }
}, },
computed: { computed: {
@ -138,6 +149,24 @@ export default {
displayNameStyle: state => state.App.displayNameStyle displayNameStyle: state => state.App.displayNameStyle
}) })
}, },
mounted () {
if (this.focused) {
this.$refs.status.focus()
}
},
watch: {
focused: function (newState, oldState) {
if (newState) {
this.$nextTick(function () {
this.$refs.status.focus()
})
} else if (oldState && !newState) {
this.$nextTick(function () {
this.$refs.status.blur()
})
}
}
},
methods: { methods: {
originalMessage (message) { originalMessage (message) {
if (message.reblog !== null) { if (message.reblog !== null) {
@ -354,6 +383,16 @@ export default {
spoilerText (message) { spoilerText (message) {
const original = this.originalMessage(message) const original = this.originalMessage(message)
return emojify(original.spoiler_text, original.emojis) return emojify(original.spoiler_text, original.emojis)
},
handleTootControl (event) {
switch (event.srcKey) {
case 'next':
this.$emit('focusNext')
break
case 'prev':
this.$emit('focusPrev')
break
}
} }
} }
} }

View File

@ -4,8 +4,18 @@
<div v-shortkey="{linux: ['ctrl', 'r'], mac: ['meta', 'r']}" @shortkey="reload()"> <div v-shortkey="{linux: ['ctrl', 'r'], mac: ['meta', 'r']}" @shortkey="reload()">
</div> </div>
<transition-group name="timeline" tag="div"> <transition-group name="timeline" tag="div">
<div class="home-timeline" v-for="message in timeline" :key="message.id"> <div class="home-timeline" v-for="(message, index) in timeline" :key="message.id">
<toot :message="message" :filter="filter" v-on:update="updateToot" v-on:delete="deleteToot"></toot> <toot
:message="message"
:filter="filter"
:focused="index === focusedIndex"
v-on:update="updateToot"
v-on:delete="deleteToot"
@focusNext="focusNext"
@focusPrev="focusPrev"
@selectToot="focusToot(index)"
>
</toot>
</div> </div>
</transition-group> </transition-group>
<div class="loading-card" v-loading="lazyLoading" :element-loading-background="backgroundColor"> <div class="loading-card" v-loading="lazyLoading" :element-loading-background="backgroundColor">
@ -25,6 +35,11 @@ import scrollTop from '../../utils/scroll'
export default { export default {
name: 'home', name: 'home',
components: { Toot }, components: { Toot },
data () {
return {
focusedIndex: null
}
},
computed: { computed: {
...mapState({ ...mapState({
backgroundColor: state => state.App.theme.background_color, backgroundColor: state => state.App.theme.background_color,
@ -62,6 +77,14 @@ export default {
this.$store.commit('TimelineSpace/HeaderMenu/changeReload', false) this.$store.commit('TimelineSpace/HeaderMenu/changeReload', false)
}) })
} }
},
focusedIndex: function (newState, oldState) {
if (newState > 0 && this.heading) {
this.$store.commit('TimelineSpace/Contents/Home/changeHeading', false)
} else if (newState === null && !this.heading) {
this.$store.commit('TimelineSpace/Contents/Home/changeHeading', true)
this.$store.commit('TimelineSpace/Contents/Home/mergeTimeline')
}
} }
}, },
methods: { methods: {
@ -82,6 +105,7 @@ export default {
} else if ((event.target.scrollTop <= 10) && !this.heading) { } else if ((event.target.scrollTop <= 10) && !this.heading) {
this.$store.commit('TimelineSpace/Contents/Home/changeHeading', true) this.$store.commit('TimelineSpace/Contents/Home/changeHeading', true)
this.$store.commit('TimelineSpace/Contents/Home/mergeTimeline') this.$store.commit('TimelineSpace/Contents/Home/mergeTimeline')
this.focusedIndex = null
} }
}, },
updateToot (message) { updateToot (message) {
@ -129,6 +153,24 @@ export default {
document.getElementById('scrollable'), document.getElementById('scrollable'),
0 0
) )
this.focusedIndex = null
},
focusNext () {
if (this.focusedIndex === null) {
this.focusedIndex = 0
} else if (this.focusedIndex < this.timeline.length) {
this.focusedIndex++
}
},
focusPrev () {
if (this.focusedIndex === 0) {
this.focusedIndex = null
} else if (this.focusedIndex > 0) {
this.focusedIndex--
}
},
focusToot (index) {
this.focusedIndex = index
} }
} }
} }

View File

@ -80,7 +80,6 @@ export default {
opened: function (newState, oldState) { opened: function (newState, oldState) {
if (!oldState && newState) { if (!oldState && newState) {
this.$nextTick(function () { this.$nextTick(function () {
console.log('focus')
this.$refs.status.focus() this.$refs.status.focus()
}) })
} else if (oldState && !newState) { } else if (oldState && !newState) {