-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathApp.svelte
More file actions
174 lines (160 loc) · 4.2 KB
/
Copy pathApp.svelte
File metadata and controls
174 lines (160 loc) · 4.2 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<script>
import Fuse from "fuse.js";
import PaletteContainer from "./PaletteContainer.svelte";
import CommandList from "./CommandList.svelte";
import SearchField from "./SearchField.svelte";
import MobileButton from "./MobileButton.svelte";
import { setContext, onMount, createEventDispatcher } from "svelte";
import {
asyncTimeout,
setMainShortCut,
setAllShortCuts,
initShortCuts
} from "./shortcuts";
const dispatch = createEventDispatcher();
export let hotkey;
export let inputData = [];
export let hotkeysGlobal;
export let placeholderText;
export let hideButton;
export let paletteId;
export let footerText;
const optionsFuse = {
isCaseSensitive: false,
shouldSort: true,
keys: ["name", "description"]
};
let showModal = false;
let searchField;
let loadingChildren = false;
let currentText = "";
let selectedIndex = "";
let items = inputData;
let itemsFiltered = inputData;
let fuse = new Fuse(items, optionsFuse);
onMount(() => {
initShortCuts(hotkeysGlobal);
setMainShortCut(hotkey, async () => {
showModal = true;
selectedIndex = 0;
dispatch("opened");
});
setAllShortCuts(inputData, async command => {
showModal = true;
dispatch("opened");
await asyncTimeout(200);
selectedIndex = inputData.findIndex(i => i.name === command.name);
await asyncTimeout(100);
onHandleCommand(command);
});
});
function setItems(newItems) {
items = newItems;
itemsFiltered = items;
fuse = new Fuse(items, optionsFuse);
}
async function onHandleCommand(command) {
if (!command) {
return;
}
const hasChildren =
Array.isArray(command.children) && command.children.length;
if (hasChildren) {
showModal = true;
loadingChildren = true;
setItems(command.children);
searchField.value = "";
await asyncTimeout(200);
searchField.focus();
loadingChildren = false;
} else {
dispatch("exec", command);
showModal = false;
}
selectedIndex = 0;
}
function onClickedIndex(e) {
selectedIndex = e.detail;
const command = itemsFiltered[selectedIndex];
onHandleCommand(command);
}
function onKeyEnter(e) {
const command = itemsFiltered[selectedIndex];
onHandleCommand(command);
}
function onKeyUp(e) {
selectedIndex--;
const minIndex = 0;
const maxIndex = itemsFiltered.length - 1;
if (selectedIndex < minIndex) {
selectedIndex = maxIndex;
}
}
function onKeyDown(e) {
selectedIndex++;
const minIndex = 0;
const maxIndex = itemsFiltered.length - 1;
if (selectedIndex > maxIndex) {
selectedIndex = minIndex;
}
}
function onTextChange(e) {
const text = e.detail;
dispatch("textChanged", text);
selectedIndex = 0;
if (!text) {
itemsFiltered = items;
} else {
const fuseResult = fuse.search(text);
itemsFiltered = fuseResult.map(i => i.item);
}
}
async function onClosed(e) {
await asyncTimeout(10);
if (loadingChildren) {
return;
}
dispatch("closed");
selectedIndex = 0;
setItems(inputData);
showModal = false;
}
function onMobileClick(e) {
dispatch("opened");
showModal = true;
selectedIndex = 0;
}
</script>
<div id={paletteId}>
{#if !hideButton}
<MobileButton on:click={onMobileClick} />
{/if}
<PaletteContainer bind:show={showModal}>
<div slot="search">
<SearchField
placeholderText={placeholderText}
show={showModal}
bind:inputEl={searchField}
on:closed={onClosed}
on:enter={onKeyEnter}
on:arrowup={onKeyUp}
on:arrowdown={onKeyDown}
on:textChange={onTextChange} />
</div>
<div slot="items">
<CommandList
items={itemsFiltered}
{selectedIndex}
on:clickedIndex={onClickedIndex} />
</div>
<!-- when svelte gets conditional slots
https://github.com/sveltejs/svelte/issues/5604
re-implement to remove the empty div.hidden.
-->
<div class="{ footerText === null ? 'hidden': 'footer' }" slot="footer">
{#if footerText !== null }
{footerText}
{/if}
</div>
</PaletteContainer>
</div>