@tooee/renderers 0.8.0 → 0.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chooser/choose-filter.d.ts +13 -0
- package/dist/chooser/choose-filter.d.ts.map +1 -0
- package/dist/chooser/choose-filter.js +22 -0
- package/dist/chooser/choose-filter.js.map +1 -0
- package/dist/chooser/choose-highlighted-text.d.ts +9 -0
- package/dist/chooser/choose-highlighted-text.d.ts.map +1 -0
- package/dist/chooser/choose-highlighted-text.js +32 -0
- package/dist/chooser/choose-highlighted-text.js.map +1 -0
- package/dist/chooser/choose-list.d.ts +29 -0
- package/dist/chooser/choose-list.d.ts.map +1 -0
- package/dist/chooser/choose-list.js +66 -0
- package/dist/chooser/choose-list.js.map +1 -0
- package/dist/chooser/choose-store.d.ts +58 -0
- package/dist/chooser/choose-store.d.ts.map +1 -0
- package/dist/chooser/choose-store.js +102 -0
- package/dist/chooser/choose-store.js.map +1 -0
- package/dist/chooser/chooser.d.ts +18 -0
- package/dist/chooser/chooser.d.ts.map +1 -0
- package/dist/chooser/chooser.js +25 -0
- package/dist/chooser/chooser.js.map +1 -0
- package/dist/chooser/fuzzy.d.ts +9 -0
- package/dist/chooser/fuzzy.d.ts.map +1 -0
- package/dist/chooser/fuzzy.js +5 -0
- package/dist/chooser/fuzzy.js.map +1 -0
- package/dist/chooser/source.d.ts +5 -0
- package/dist/chooser/source.d.ts.map +1 -0
- package/dist/chooser/source.js +26 -0
- package/dist/chooser/source.js.map +1 -0
- package/dist/chooser/types.d.ts +22 -0
- package/dist/chooser/types.d.ts.map +1 -0
- package/dist/chooser/types.js +2 -0
- package/dist/chooser/types.js.map +1 -0
- package/dist/chooser/use-choose.d.ts +84 -0
- package/dist/chooser/use-choose.d.ts.map +1 -0
- package/dist/chooser/use-choose.js +382 -0
- package/dist/chooser/use-choose.js.map +1 -0
- package/dist/command-palette.d.ts.map +1 -1
- package/dist/command-palette.js +14 -68
- package/dist/command-palette.js.map +1 -1
- package/dist/index.d.ts +16 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -1
- package/dist/mermaid.d.ts.map +1 -1
- package/dist/mermaid.js +44 -1
- package/dist/mermaid.js.map +1 -1
- package/package.json +8 -6
- package/src/chooser/choose-filter.tsx +66 -0
- package/src/chooser/choose-highlighted-text.tsx +49 -0
- package/src/chooser/choose-list.tsx +159 -0
- package/src/chooser/choose-store.ts +147 -0
- package/src/chooser/chooser.tsx +80 -0
- package/src/chooser/fuzzy.ts +13 -0
- package/src/chooser/source.ts +36 -0
- package/src/chooser/types.ts +28 -0
- package/src/chooser/use-choose.ts +540 -0
- package/src/command-palette.tsx +45 -148
- package/src/index.ts +39 -0
- package/src/mermaid.ts +64 -1
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { ChooseContentProvider, ChooseItem, ChooseSource } from "./types.js";
|
|
2
|
+
|
|
3
|
+
type DecodedChooseSource =
|
|
4
|
+
| { kind: "items"; items: ChooseItem[] }
|
|
5
|
+
| { kind: "loader"; load: () => ChooseItem[] | Promise<ChooseItem[]> }
|
|
6
|
+
| { kind: "provider"; provider: ChooseContentProvider };
|
|
7
|
+
|
|
8
|
+
const decodeChooseSource = function decodeChooseSource(source: ChooseSource): DecodedChooseSource {
|
|
9
|
+
if (Array.isArray(source)) {
|
|
10
|
+
return { items: source, kind: "items" };
|
|
11
|
+
}
|
|
12
|
+
// oxlint-disable-next-line anti-slop/no-runtime-typeof -- public source boundary distinguishes the documented loader callback from a provider
|
|
13
|
+
if (typeof source === "function") {
|
|
14
|
+
return { kind: "loader", load: source };
|
|
15
|
+
}
|
|
16
|
+
return { kind: "provider", provider: source };
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/** Resolve every public Choose source shape through one compatibility boundary. */
|
|
20
|
+
export const loadChooseSource = function loadChooseSource(
|
|
21
|
+
source: ChooseSource,
|
|
22
|
+
): ChooseItem[] | Promise<ChooseItem[]> {
|
|
23
|
+
const decoded = decodeChooseSource(source);
|
|
24
|
+
if (decoded.kind === "items") {
|
|
25
|
+
return decoded.items;
|
|
26
|
+
}
|
|
27
|
+
if (decoded.kind === "loader") {
|
|
28
|
+
return decoded.load();
|
|
29
|
+
}
|
|
30
|
+
return decoded.provider.load();
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
// oxlint-disable-next-line anti-slop/no-unknown-parameters -- caught loader failures enter here before conversion to display text
|
|
34
|
+
export const chooseSourceError = function chooseSourceError(error: unknown): string {
|
|
35
|
+
return error instanceof Error ? error.message : String(error);
|
|
36
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export interface ChooseItem {
|
|
2
|
+
text: string;
|
|
3
|
+
value?: string;
|
|
4
|
+
icon?: string;
|
|
5
|
+
description?: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface ChooseContentProvider {
|
|
9
|
+
load: () => Promise<ChooseItem[]> | ChooseItem[];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** Any item source accepted by the shared chooser core. */
|
|
13
|
+
export type ChooseSource =
|
|
14
|
+
| ChooseItem[]
|
|
15
|
+
| ChooseContentProvider
|
|
16
|
+
| (() => ChooseItem[] | Promise<ChooseItem[]>);
|
|
17
|
+
|
|
18
|
+
export interface ChooseResult {
|
|
19
|
+
items: ChooseItem[];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface ChooseOptions {
|
|
23
|
+
multi?: boolean;
|
|
24
|
+
title?: string;
|
|
25
|
+
prompt?: string;
|
|
26
|
+
placeholder?: string;
|
|
27
|
+
emptyMessage?: string;
|
|
28
|
+
}
|
|
@@ -0,0 +1,540 @@
|
|
|
1
|
+
import { useCallback, useEffect, useMemo, useRef } from "react";
|
|
2
|
+
import type { InputRenderable, KeyEvent } from "@opentui/core";
|
|
3
|
+
import { useSelector } from "@xstate/store-react";
|
|
4
|
+
import {
|
|
5
|
+
useActiveCommandSurface,
|
|
6
|
+
useActions,
|
|
7
|
+
useCommandSurfaceId,
|
|
8
|
+
useLatest,
|
|
9
|
+
useLazyRef,
|
|
10
|
+
useMode,
|
|
11
|
+
useProvideCommandContext,
|
|
12
|
+
useSetMode,
|
|
13
|
+
} from "@tooee/commands";
|
|
14
|
+
import type { ActionDefinition, CommandContext, Mode } from "@tooee/commands";
|
|
15
|
+
import {
|
|
16
|
+
createChooseStore,
|
|
17
|
+
selectActiveIndex,
|
|
18
|
+
selectError,
|
|
19
|
+
selectFilterQuery,
|
|
20
|
+
selectItems,
|
|
21
|
+
selectLoading,
|
|
22
|
+
selectMatches,
|
|
23
|
+
selectReloadRevision,
|
|
24
|
+
selectSelectedOriginalIndices,
|
|
25
|
+
} from "./choose-store.js";
|
|
26
|
+
import type { FuzzyMatch } from "./fuzzy.js";
|
|
27
|
+
import { chooseSourceError, loadChooseSource } from "./source.js";
|
|
28
|
+
import type { ChooseItem, ChooseResult, ChooseSource } from "./types.js";
|
|
29
|
+
|
|
30
|
+
declare module "@tooee/commands" {
|
|
31
|
+
interface CommandContext {
|
|
32
|
+
/** Contributed by chooser primitives: current filtering and selection state. */
|
|
33
|
+
choose?: {
|
|
34
|
+
activeItem: ChooseItem | undefined;
|
|
35
|
+
selectedItems: ChooseItem[];
|
|
36
|
+
filterQuery: string;
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export type ChooseCommandGroup = "navigation" | "mode" | "submit" | "cancel" | "multi-select";
|
|
42
|
+
|
|
43
|
+
export interface UseChooseOptions {
|
|
44
|
+
source: ChooseSource;
|
|
45
|
+
multi?: boolean;
|
|
46
|
+
initialFilter?: string;
|
|
47
|
+
initialActiveIndex?: number;
|
|
48
|
+
onSubmit: (result: ChooseResult) => void | Promise<void>;
|
|
49
|
+
onCancel?: () => void;
|
|
50
|
+
onActiveChange?: (item: ChooseItem | undefined) => void;
|
|
51
|
+
commands?: ActionDefinition[];
|
|
52
|
+
/** Prefix for built-in command ids (default `choose`). */
|
|
53
|
+
commandScope?: string;
|
|
54
|
+
/** Disable a built-in command group when a host owns those keys. */
|
|
55
|
+
disable?: ChooseCommandGroup[];
|
|
56
|
+
/** Whether Escape leaves insert mode before cancelling (default true). */
|
|
57
|
+
escapeToCursor?: boolean;
|
|
58
|
+
/** Explicitly blur/disable mouse views while a legacy covering surface is open. */
|
|
59
|
+
suspended?: boolean;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface ChooseController {
|
|
63
|
+
getFilter: () => string;
|
|
64
|
+
setFilter: (query: string) => void;
|
|
65
|
+
clearFilter: () => void;
|
|
66
|
+
moveUp: () => void;
|
|
67
|
+
moveDown: () => void;
|
|
68
|
+
setActiveIndex: (index: number) => void;
|
|
69
|
+
getActiveItem: () => ChooseItem | undefined;
|
|
70
|
+
getSelectedItems: () => ChooseItem[];
|
|
71
|
+
toggleActive: () => void;
|
|
72
|
+
submit: () => void;
|
|
73
|
+
cancel: () => void;
|
|
74
|
+
reload: () => void;
|
|
75
|
+
readonly mode: Mode;
|
|
76
|
+
setMode: (mode: Mode) => void;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export interface ChooseState {
|
|
80
|
+
items: ChooseItem[];
|
|
81
|
+
matches: FuzzyMatch[];
|
|
82
|
+
filterQuery: string;
|
|
83
|
+
activeIndex: number;
|
|
84
|
+
activeItem: ChooseItem | undefined;
|
|
85
|
+
selectedOriginalIndices: ReadonlySet<number>;
|
|
86
|
+
selectedItems: ChooseItem[];
|
|
87
|
+
loading: boolean;
|
|
88
|
+
error: string | null;
|
|
89
|
+
multi: boolean;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export interface ChooseViewModel {
|
|
93
|
+
filterRef: { current: InputRenderable | null };
|
|
94
|
+
mode: Mode;
|
|
95
|
+
/** True while another modal surface (or an explicit host guard) owns interaction. */
|
|
96
|
+
suspended: boolean;
|
|
97
|
+
filterFocused: boolean;
|
|
98
|
+
onFilterInput: (query: string) => void;
|
|
99
|
+
/** Input-level Tab bridge; OpenTUI consumes Tab before global dispatch. */
|
|
100
|
+
onFilterKeyDown: (event: KeyEvent) => void;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export interface UseChooseResult {
|
|
104
|
+
controller: ChooseController;
|
|
105
|
+
state: ChooseState;
|
|
106
|
+
view: ChooseViewModel;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
interface ChooseCommandContextContribution {
|
|
110
|
+
choose: CommandContext["choose"];
|
|
111
|
+
exit?: () => void;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
interface ChooseKeymapDefinition extends Omit<ActionDefinition, "group" | "when"> {
|
|
115
|
+
group: () => ChooseCommandGroup;
|
|
116
|
+
when?: () => boolean;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Shared headless chooser: source lifecycle, fuzzy matches, selection,
|
|
121
|
+
* command context, controller, and all built-in keyboard commands.
|
|
122
|
+
*/
|
|
123
|
+
export const useChoose = function useChoose(options: UseChooseOptions): UseChooseResult {
|
|
124
|
+
const {
|
|
125
|
+
source,
|
|
126
|
+
multi = false,
|
|
127
|
+
initialFilter = "",
|
|
128
|
+
initialActiveIndex = 0,
|
|
129
|
+
commandScope = "choose",
|
|
130
|
+
escapeToCursor = true,
|
|
131
|
+
} = options;
|
|
132
|
+
const initialItems = Array.isArray(source) ? source : [];
|
|
133
|
+
const store = useLazyRef(() =>
|
|
134
|
+
createChooseStore({
|
|
135
|
+
initialActiveIndex,
|
|
136
|
+
initialFilter,
|
|
137
|
+
items: initialItems,
|
|
138
|
+
loading: !Array.isArray(source),
|
|
139
|
+
}),
|
|
140
|
+
).current;
|
|
141
|
+
const filterRef = useRef<InputRenderable>(null);
|
|
142
|
+
const initialArraySourceRef = useLazyRef<ChooseItem[] | null>(() =>
|
|
143
|
+
Array.isArray(source) ? source : null,
|
|
144
|
+
);
|
|
145
|
+
const didHandleInitialSourceRef = useRef(false);
|
|
146
|
+
|
|
147
|
+
const items = useSelector(store, (snapshot) => selectItems(snapshot.context));
|
|
148
|
+
const matches = useSelector(store, (snapshot) => selectMatches(snapshot.context));
|
|
149
|
+
const filterQuery = useSelector(store, (snapshot) => selectFilterQuery(snapshot.context));
|
|
150
|
+
const activeIndex = useSelector(store, (snapshot) => selectActiveIndex(snapshot.context));
|
|
151
|
+
const selectedOriginalIndices = useSelector(store, (snapshot) =>
|
|
152
|
+
selectSelectedOriginalIndices(snapshot.context),
|
|
153
|
+
);
|
|
154
|
+
const loading = useSelector(store, (snapshot) => selectLoading(snapshot.context));
|
|
155
|
+
const error = useSelector(store, (snapshot) => selectError(snapshot.context));
|
|
156
|
+
const reloadRevision = useSelector(store, (snapshot) => selectReloadRevision(snapshot.context));
|
|
157
|
+
const activeItem = matches[activeIndex]?.item;
|
|
158
|
+
const selectedItems = useMemo(() => {
|
|
159
|
+
if (!multi) {
|
|
160
|
+
return activeItem === undefined ? [] : [activeItem];
|
|
161
|
+
}
|
|
162
|
+
const selected = [...selectedOriginalIndices].flatMap((index) => {
|
|
163
|
+
const item = items[index];
|
|
164
|
+
return item === undefined ? [] : [item];
|
|
165
|
+
});
|
|
166
|
+
if (selected.length > 0) {
|
|
167
|
+
return selected;
|
|
168
|
+
}
|
|
169
|
+
return activeItem === undefined ? [] : [activeItem];
|
|
170
|
+
}, [multi, activeItem, selectedOriginalIndices, items]);
|
|
171
|
+
|
|
172
|
+
const mode = useMode();
|
|
173
|
+
const setMode = useSetMode();
|
|
174
|
+
const optionsRef = useLatest(options);
|
|
175
|
+
const multiRef = useLatest(multi);
|
|
176
|
+
const modeRef = useLatest(mode);
|
|
177
|
+
|
|
178
|
+
useEffect(() => {
|
|
179
|
+
optionsRef.current.onActiveChange?.(activeItem);
|
|
180
|
+
}, [activeItem, optionsRef]);
|
|
181
|
+
|
|
182
|
+
useEffect(() => {
|
|
183
|
+
let active = true;
|
|
184
|
+
const deactivate = () => {
|
|
185
|
+
active = false;
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
// Direct arrays seed the store before the first render. Avoid replacing
|
|
189
|
+
// state after mount, which could overwrite immediate controller calls.
|
|
190
|
+
if (
|
|
191
|
+
!didHandleInitialSourceRef.current &&
|
|
192
|
+
Array.isArray(source) &&
|
|
193
|
+
source === initialArraySourceRef.current
|
|
194
|
+
) {
|
|
195
|
+
didHandleInitialSourceRef.current = true;
|
|
196
|
+
return deactivate;
|
|
197
|
+
}
|
|
198
|
+
didHandleInitialSourceRef.current = true;
|
|
199
|
+
|
|
200
|
+
store.trigger.requestStarted({});
|
|
201
|
+
const { requestId } = store.getSnapshot().context;
|
|
202
|
+
let result: ChooseItem[] | Promise<ChooseItem[]>;
|
|
203
|
+
try {
|
|
204
|
+
result = loadChooseSource(source);
|
|
205
|
+
} catch (loadError) {
|
|
206
|
+
store.trigger.loadFailed({ error: chooseSourceError(loadError), requestId });
|
|
207
|
+
return deactivate;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
if (result instanceof Promise) {
|
|
211
|
+
store.trigger.loadingStarted({ requestId });
|
|
212
|
+
void (async () => {
|
|
213
|
+
try {
|
|
214
|
+
const loaded = await result;
|
|
215
|
+
if (active) {
|
|
216
|
+
store.trigger.loadSucceeded({ items: loaded, requestId });
|
|
217
|
+
}
|
|
218
|
+
} catch (loadError) {
|
|
219
|
+
if (active) {
|
|
220
|
+
store.trigger.loadFailed({ error: chooseSourceError(loadError), requestId });
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
})();
|
|
224
|
+
return deactivate;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
store.trigger.loadSucceeded({ items: result, requestId });
|
|
228
|
+
return deactivate;
|
|
229
|
+
}, [source, reloadRevision, store, initialArraySourceRef]);
|
|
230
|
+
|
|
231
|
+
const setFilter = useCallback(
|
|
232
|
+
(query: string) => {
|
|
233
|
+
store.trigger.filterChanged({ query });
|
|
234
|
+
},
|
|
235
|
+
[store],
|
|
236
|
+
);
|
|
237
|
+
const updateActiveIndex = useCallback(
|
|
238
|
+
(index: number) => {
|
|
239
|
+
store.trigger.activeIndexSet({ index });
|
|
240
|
+
},
|
|
241
|
+
[store],
|
|
242
|
+
);
|
|
243
|
+
const moveUp = useCallback(() => {
|
|
244
|
+
store.trigger.moved({ delta: -1 });
|
|
245
|
+
}, [store]);
|
|
246
|
+
const moveDown = useCallback(() => {
|
|
247
|
+
store.trigger.moved({ delta: 1 });
|
|
248
|
+
}, [store]);
|
|
249
|
+
const getActiveItem = useCallback(() => {
|
|
250
|
+
const { context } = store.getSnapshot();
|
|
251
|
+
return context.matches[context.activeIndex]?.item;
|
|
252
|
+
}, [store]);
|
|
253
|
+
const getSelectedItems = useCallback((): ChooseItem[] => {
|
|
254
|
+
const { context } = store.getSnapshot();
|
|
255
|
+
const active = context.matches[context.activeIndex]?.item;
|
|
256
|
+
if (!multiRef.current) {
|
|
257
|
+
return active === undefined ? [] : [active];
|
|
258
|
+
}
|
|
259
|
+
const selected = [...context.selectedOriginalIndices].flatMap((index) => {
|
|
260
|
+
const item = context.items[index];
|
|
261
|
+
return item === undefined ? [] : [item];
|
|
262
|
+
});
|
|
263
|
+
if (selected.length > 0) {
|
|
264
|
+
return selected;
|
|
265
|
+
}
|
|
266
|
+
return active === undefined ? [] : [active];
|
|
267
|
+
}, [multiRef, store]);
|
|
268
|
+
const toggleActive = useCallback(() => {
|
|
269
|
+
if (multiRef.current) {
|
|
270
|
+
store.trigger.activeToggled({});
|
|
271
|
+
}
|
|
272
|
+
}, [multiRef, store]);
|
|
273
|
+
const submit = useCallback(() => {
|
|
274
|
+
void optionsRef.current.onSubmit({ items: getSelectedItems() });
|
|
275
|
+
}, [getSelectedItems, optionsRef]);
|
|
276
|
+
const cancel = useCallback(() => optionsRef.current.onCancel?.(), [optionsRef]);
|
|
277
|
+
const reload = useCallback(() => {
|
|
278
|
+
store.trigger.reloadRequested({});
|
|
279
|
+
}, [store]);
|
|
280
|
+
const setModeExternal = useCallback(
|
|
281
|
+
(nextMode: Mode) => {
|
|
282
|
+
setMode(nextMode);
|
|
283
|
+
},
|
|
284
|
+
[setMode],
|
|
285
|
+
);
|
|
286
|
+
|
|
287
|
+
useProvideCommandContext(() => {
|
|
288
|
+
const context: ChooseCommandContextContribution = {
|
|
289
|
+
choose: {
|
|
290
|
+
activeItem: getActiveItem(),
|
|
291
|
+
filterQuery: store.getSnapshot().context.filterQuery,
|
|
292
|
+
selectedItems: getSelectedItems(),
|
|
293
|
+
},
|
|
294
|
+
};
|
|
295
|
+
if (optionsRef.current.onCancel !== undefined) {
|
|
296
|
+
context.exit = cancel;
|
|
297
|
+
}
|
|
298
|
+
return context;
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
useActions(options.commands);
|
|
302
|
+
|
|
303
|
+
const enabled = useCallback(
|
|
304
|
+
(group: ChooseCommandGroup) => !(optionsRef.current.disable?.includes(group) ?? false),
|
|
305
|
+
[optionsRef],
|
|
306
|
+
);
|
|
307
|
+
const builtInActions = useMemo<ActionDefinition[]>(() => {
|
|
308
|
+
const definitions: ChooseKeymapDefinition[] = [
|
|
309
|
+
{
|
|
310
|
+
group: () => (escapeToCursor && modeRef.current === "insert" ? "mode" : "cancel"),
|
|
311
|
+
handler: () => {
|
|
312
|
+
if (escapeToCursor && modeRef.current === "insert") {
|
|
313
|
+
setMode("cursor");
|
|
314
|
+
} else {
|
|
315
|
+
cancel();
|
|
316
|
+
}
|
|
317
|
+
},
|
|
318
|
+
hidden: true,
|
|
319
|
+
hotkey: "Escape",
|
|
320
|
+
id: `${commandScope}:escape`,
|
|
321
|
+
modes: ["insert", "cursor"],
|
|
322
|
+
title: "Back / cancel",
|
|
323
|
+
when: () =>
|
|
324
|
+
escapeToCursor && modeRef.current === "insert"
|
|
325
|
+
? true
|
|
326
|
+
: optionsRef.current.onCancel !== undefined,
|
|
327
|
+
},
|
|
328
|
+
{
|
|
329
|
+
group: () => "cancel",
|
|
330
|
+
handler: cancel,
|
|
331
|
+
hidden: true,
|
|
332
|
+
hotkey: "q",
|
|
333
|
+
id: `${commandScope}:cancel`,
|
|
334
|
+
modes: ["cursor"],
|
|
335
|
+
title: "Cancel",
|
|
336
|
+
when: () => optionsRef.current.onCancel !== undefined,
|
|
337
|
+
},
|
|
338
|
+
{
|
|
339
|
+
group: () => "mode",
|
|
340
|
+
handler: () => {
|
|
341
|
+
setMode("insert");
|
|
342
|
+
},
|
|
343
|
+
hidden: true,
|
|
344
|
+
hotkey: "i",
|
|
345
|
+
id: `${commandScope}:insert-mode-i`,
|
|
346
|
+
modes: ["cursor"],
|
|
347
|
+
title: "Insert mode",
|
|
348
|
+
},
|
|
349
|
+
{
|
|
350
|
+
group: () => "mode",
|
|
351
|
+
handler: () => {
|
|
352
|
+
setMode("insert");
|
|
353
|
+
},
|
|
354
|
+
hidden: true,
|
|
355
|
+
hotkey: "a",
|
|
356
|
+
id: `${commandScope}:insert-mode-a`,
|
|
357
|
+
modes: ["cursor"],
|
|
358
|
+
title: "Insert mode",
|
|
359
|
+
},
|
|
360
|
+
{
|
|
361
|
+
group: () => "navigation",
|
|
362
|
+
handler: moveDown,
|
|
363
|
+
hidden: true,
|
|
364
|
+
hotkey: "j",
|
|
365
|
+
id: `${commandScope}:move-down-vim`,
|
|
366
|
+
modes: ["cursor"],
|
|
367
|
+
title: "Move down",
|
|
368
|
+
},
|
|
369
|
+
{
|
|
370
|
+
group: () => "navigation",
|
|
371
|
+
handler: moveUp,
|
|
372
|
+
hidden: true,
|
|
373
|
+
hotkey: "k",
|
|
374
|
+
id: `${commandScope}:move-up-vim`,
|
|
375
|
+
modes: ["cursor"],
|
|
376
|
+
title: "Move up",
|
|
377
|
+
},
|
|
378
|
+
{
|
|
379
|
+
group: () => "submit",
|
|
380
|
+
handler: submit,
|
|
381
|
+
hidden: true,
|
|
382
|
+
hotkey: "Enter",
|
|
383
|
+
id: `${commandScope}:confirm`,
|
|
384
|
+
modes: ["insert", "cursor"],
|
|
385
|
+
title: "Confirm",
|
|
386
|
+
},
|
|
387
|
+
{
|
|
388
|
+
group: () => "navigation",
|
|
389
|
+
handler: moveUp,
|
|
390
|
+
hidden: true,
|
|
391
|
+
hotkey: "up",
|
|
392
|
+
id: `${commandScope}:move-up`,
|
|
393
|
+
modes: ["insert", "cursor"],
|
|
394
|
+
title: "Move up",
|
|
395
|
+
},
|
|
396
|
+
{
|
|
397
|
+
group: () => "navigation",
|
|
398
|
+
handler: moveUp,
|
|
399
|
+
hidden: true,
|
|
400
|
+
hotkey: "ctrl+p",
|
|
401
|
+
id: `${commandScope}:move-up-ctrl-p`,
|
|
402
|
+
modes: ["insert", "cursor"],
|
|
403
|
+
title: "Move up",
|
|
404
|
+
},
|
|
405
|
+
{
|
|
406
|
+
group: () => "navigation",
|
|
407
|
+
handler: moveDown,
|
|
408
|
+
hidden: true,
|
|
409
|
+
hotkey: "down",
|
|
410
|
+
id: `${commandScope}:move-down`,
|
|
411
|
+
modes: ["insert", "cursor"],
|
|
412
|
+
title: "Move down",
|
|
413
|
+
},
|
|
414
|
+
{
|
|
415
|
+
group: () => "navigation",
|
|
416
|
+
handler: moveDown,
|
|
417
|
+
hidden: true,
|
|
418
|
+
hotkey: "ctrl+n",
|
|
419
|
+
id: `${commandScope}:move-down-ctrl-n`,
|
|
420
|
+
modes: ["insert", "cursor"],
|
|
421
|
+
title: "Move down",
|
|
422
|
+
},
|
|
423
|
+
{
|
|
424
|
+
group: () => "multi-select",
|
|
425
|
+
handler: () => {
|
|
426
|
+
toggleActive();
|
|
427
|
+
moveDown();
|
|
428
|
+
},
|
|
429
|
+
hidden: true,
|
|
430
|
+
hotkey: "Tab",
|
|
431
|
+
id: `${commandScope}:toggle-next`,
|
|
432
|
+
modes: ["insert", "cursor"],
|
|
433
|
+
title: "Toggle selection and move down",
|
|
434
|
+
when: () => multiRef.current,
|
|
435
|
+
},
|
|
436
|
+
{
|
|
437
|
+
group: () => "multi-select",
|
|
438
|
+
handler: () => {
|
|
439
|
+
toggleActive();
|
|
440
|
+
moveUp();
|
|
441
|
+
},
|
|
442
|
+
hidden: true,
|
|
443
|
+
hotkey: "shift+Tab",
|
|
444
|
+
id: `${commandScope}:toggle-previous`,
|
|
445
|
+
modes: ["insert", "cursor"],
|
|
446
|
+
title: "Toggle selection and move up",
|
|
447
|
+
when: () => multiRef.current,
|
|
448
|
+
},
|
|
449
|
+
];
|
|
450
|
+
return definitions.map(({ group, when, ...definition }) => ({
|
|
451
|
+
...definition,
|
|
452
|
+
when: () => enabled(group()) && (when?.() ?? true),
|
|
453
|
+
}));
|
|
454
|
+
}, [
|
|
455
|
+
cancel,
|
|
456
|
+
commandScope,
|
|
457
|
+
enabled,
|
|
458
|
+
escapeToCursor,
|
|
459
|
+
modeRef,
|
|
460
|
+
moveDown,
|
|
461
|
+
moveUp,
|
|
462
|
+
multiRef,
|
|
463
|
+
optionsRef,
|
|
464
|
+
setMode,
|
|
465
|
+
submit,
|
|
466
|
+
toggleActive,
|
|
467
|
+
]);
|
|
468
|
+
useActions(builtInActions);
|
|
469
|
+
|
|
470
|
+
const surfaceId = useCommandSurfaceId();
|
|
471
|
+
const activeSurface = useActiveCommandSurface();
|
|
472
|
+
const suspended =
|
|
473
|
+
(options.suspended ?? false) || (activeSurface !== null && activeSurface.id !== surfaceId);
|
|
474
|
+
|
|
475
|
+
const onFilterKeyDown = useCallback(
|
|
476
|
+
(event: KeyEvent) => {
|
|
477
|
+
if (
|
|
478
|
+
event.name !== "tab" ||
|
|
479
|
+
!multiRef.current ||
|
|
480
|
+
!enabled("multi-select") ||
|
|
481
|
+
modeRef.current !== "insert"
|
|
482
|
+
) {
|
|
483
|
+
return;
|
|
484
|
+
}
|
|
485
|
+
event.preventDefault();
|
|
486
|
+
toggleActive();
|
|
487
|
+
if (event.shift) {
|
|
488
|
+
moveUp();
|
|
489
|
+
} else {
|
|
490
|
+
moveDown();
|
|
491
|
+
}
|
|
492
|
+
},
|
|
493
|
+
[enabled, modeRef, moveDown, moveUp, multiRef, toggleActive],
|
|
494
|
+
);
|
|
495
|
+
|
|
496
|
+
const controller = useLazyRef<ChooseController>(() => ({
|
|
497
|
+
cancel,
|
|
498
|
+
clearFilter: () => {
|
|
499
|
+
setFilter("");
|
|
500
|
+
},
|
|
501
|
+
getActiveItem,
|
|
502
|
+
getFilter: () => store.getSnapshot().context.filterQuery,
|
|
503
|
+
getSelectedItems,
|
|
504
|
+
get mode() {
|
|
505
|
+
return modeRef.current;
|
|
506
|
+
},
|
|
507
|
+
moveDown,
|
|
508
|
+
moveUp,
|
|
509
|
+
reload,
|
|
510
|
+
setActiveIndex: updateActiveIndex,
|
|
511
|
+
setFilter,
|
|
512
|
+
setMode: setModeExternal,
|
|
513
|
+
submit,
|
|
514
|
+
toggleActive,
|
|
515
|
+
})).current;
|
|
516
|
+
|
|
517
|
+
return {
|
|
518
|
+
controller,
|
|
519
|
+
state: {
|
|
520
|
+
activeIndex,
|
|
521
|
+
activeItem,
|
|
522
|
+
error,
|
|
523
|
+
filterQuery,
|
|
524
|
+
items,
|
|
525
|
+
loading,
|
|
526
|
+
matches,
|
|
527
|
+
multi,
|
|
528
|
+
selectedItems,
|
|
529
|
+
selectedOriginalIndices,
|
|
530
|
+
},
|
|
531
|
+
view: {
|
|
532
|
+
filterFocused: mode === "insert" && !suspended,
|
|
533
|
+
filterRef,
|
|
534
|
+
mode,
|
|
535
|
+
onFilterInput: setFilter,
|
|
536
|
+
onFilterKeyDown,
|
|
537
|
+
suspended,
|
|
538
|
+
},
|
|
539
|
+
};
|
|
540
|
+
};
|