1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
<template>
<div class="searcher">
<input
type="text"
class="w-full bg-gray-800 p-2 mb-4"
v-model='search'
ref='input'
@keydown="hotkeys"
@blur="close()"
placeholder="Search!"
/>
<Sugestion
v-for="sugestion in sugestions"
:key="sugestion[0]"
:sugestion="sugestion"
:class="{'bg-gray-800': sugestion[0] === current[0]}"
/>
</div>
</template>
<script>
import CommandsService from '@/services/CommandsService'
import Sugestion from '@/components/searcher/Sugestion.vue'
export default {
data() {
return {
commands: CommandsService,
search: '',
sugestions: [],
current: []
}
},
methods: {
set_sugestions() {
const sugestions = []
let is_current_set = false
Object.entries(this.commands).forEach((element) => {
if(element[0].startsWith(this.search) && element[1] instanceof Function) {
sugestions.push(element)
if(!is_current_set){
this.current = element
is_current_set = true;
}
}
});
this.sugestions = sugestions
},
close() {
this.$emit('close')
},
change_current(a=1) {
let index = this.sugestions.findIndex(element => element===this.current)
let new_current = this.sugestions[index+a]
if (new_current) this.current = new_current
else this.current = this.sugestions[0]
},
async execute_current() {
let out = await this.current[1](this)
if(out instanceof Object) {
this.search = ''
this.commands = out
}
},
hotkeys(e) {
switch(e.code) {
case 'Escape':
e.preventDefault()
this.close()
break
case 'Delete':
e.preventDefault()
this.close()
break
case 'ArrowDown':
e.preventDefault()
this.change_current(1)
break
case 'ArrowUp':
e.preventDefault()
this.change_current(-1)
break
case 'Tab':
e.preventDefault()
this.change_current(1)
break
case 'Enter':
e.preventDefault()
this.execute_current()
break
case 'ShiftLeft':
e.preventDefault()
this.commands = CommandsService
break
}
},
},
watch: {
search() {
this.set_sugestions()
},
commands() {
this.set_sugestions()
}
},
computed: {
},
components: {
Sugestion
},
created() {
this.set_sugestions()
this.$nextTick(function () {
this.$refs.input.focus()
})
}
}
</script>
|