neko/client/src/store/remote.ts

165 lines
3.7 KiB
TypeScript
Raw Normal View History

2020-01-21 03:36:18 +13:00
import { getterTree, mutationTree, actionTree } from 'typed-vuex'
2020-01-24 04:23:26 +13:00
import { Member } from '~/neko/types'
import { EVENT } from '~/neko/events'
2020-01-23 06:16:40 +13:00
import { accessor } from '~/store'
2020-01-21 03:36:18 +13:00
2021-03-30 00:03:25 +13:00
const keyboardModifierState = (capsLock: boolean, numLock: boolean, scrollLock: boolean) =>
Number(capsLock) + 2 * Number(numLock) + 4 * Number(scrollLock)
2020-01-21 03:36:18 +13:00
export const namespaced = true
export const state = () => ({
id: '',
2020-01-26 03:29:52 +13:00
clipboard: '',
2020-02-12 12:51:57 +13:00
locked: false,
2021-03-30 00:03:25 +13:00
keyboardModifierState: -1,
2020-01-21 03:36:18 +13:00
})
export const getters = getterTree(state, {
hosting: (state, getters, root) => {
return root.user.id === state.id
},
2020-01-23 06:16:40 +13:00
hosted: (state, getters, root) => {
return state.id !== ''
},
2020-01-21 03:36:18 +13:00
host: (state, getters, root) => {
return root.user.members[state.id] || null
2020-01-21 03:36:18 +13:00
},
})
export const mutations = mutationTree(state, {
setHost(state, host: string | Member) {
if (typeof host === 'string') {
state.id = host
} else {
state.id = host.id
}
},
2020-01-24 04:23:26 +13:00
2020-01-26 03:29:52 +13:00
setClipboard(state, clipboard: string) {
state.clipboard = clipboard
},
2021-03-30 00:03:25 +13:00
setKeyboardModifierState(state, { capsLock, numLock, scrollLock }) {
state.keyboardModifierState = keyboardModifierState(capsLock, numLock, scrollLock)
},
2020-02-12 12:51:57 +13:00
setLocked(state, locked: boolean) {
state.locked = locked
},
reset(state) {
2020-01-24 04:23:26 +13:00
state.id = ''
2020-01-26 03:29:52 +13:00
state.clipboard = ''
2020-02-12 12:51:57 +13:00
state.locked = false
2021-03-30 00:03:25 +13:00
state.keyboardModifierState = -1
2020-01-24 04:23:26 +13:00
},
2020-01-21 03:36:18 +13:00
})
export const actions = actionTree(
{ state, getters, mutations },
{
2020-01-26 03:29:52 +13:00
sendClipboard({ getters }, clipboard: string) {
if (!accessor.connected || !getters.hosting) {
return
}
$client.sendMessage(EVENT.CONTROL.CLIPBOARD, { text: clipboard })
},
2020-01-23 06:16:40 +13:00
toggle({ getters }) {
if (!accessor.connected) {
return
}
if (!getters.hosting) {
$client.sendMessage(EVENT.CONTROL.REQUEST)
2020-01-23 06:16:40 +13:00
} else {
$client.sendMessage(EVENT.CONTROL.RELEASE)
}
},
request({ getters }) {
2021-03-30 00:11:26 +13:00
if (!accessor.connected || getters.hosting) {
2020-01-23 06:16:40 +13:00
return
}
$client.sendMessage(EVENT.CONTROL.REQUEST)
},
release({ getters }) {
2021-03-30 00:11:26 +13:00
if (!accessor.connected || !getters.hosting) {
2020-01-23 06:16:40 +13:00
return
}
$client.sendMessage(EVENT.CONTROL.RELEASE)
},
2020-01-24 04:23:26 +13:00
give({ getters }, member: string | Member) {
if (!accessor.connected || !getters.hosting) {
return
}
if (typeof member === 'string') {
member = accessor.user.members[member]
}
if (!member) {
return
}
$client.sendMessage(EVENT.CONTROL.GIVE, { id: member.id })
},
2020-01-23 06:16:40 +13:00
adminControl() {
if (!accessor.connected || !accessor.user.admin) {
return
}
$client.sendMessage(EVENT.ADMIN.CONTROL)
},
adminRelease() {
if (!accessor.connected || !accessor.user.admin) {
return
}
$client.sendMessage(EVENT.ADMIN.RELEASE)
},
2020-01-24 04:23:26 +13:00
adminGive({ getters }, member: string | Member) {
if (!accessor.connected) {
return
}
if (typeof member === 'string') {
member = accessor.user.members[member]
}
if (!member) {
return
}
$client.sendMessage(EVENT.ADMIN.GIVE, { id: member.id })
},
2020-06-16 10:55:14 +12:00
changeKeyboard({ getters }) {
if (!accessor.connected || !getters.hosting) {
return
}
$client.sendMessage(EVENT.CONTROL.KEYBOARD, { layout: accessor.settings.keyboard_layout })
2021-03-30 00:03:25 +13:00
},
syncKeyboardModifierState({ state, getters }, { capsLock, numLock, scrollLock }) {
if (state.keyboardModifierState === keyboardModifierState(capsLock, numLock, scrollLock)) {
return
}
accessor.remote.setKeyboardModifierState({ capsLock, numLock, scrollLock })
$client.sendMessage(EVENT.CONTROL.KEYBOARD, { capsLock, numLock, scrollLock })
},
2020-01-21 03:36:18 +13:00
},
)