@rrstudioapps/ngx-command-palette 0.3.1 → 0.5.0
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.
|
@@ -5,6 +5,8 @@ import { NavigationExtras } from "@angular/router";
|
|
|
5
5
|
interface CommandRunContext {
|
|
6
6
|
/** The search text typed when the command was executed. */
|
|
7
7
|
readonly query: string;
|
|
8
|
+
/** The text typed in the second step of a command with `prompt`. Not set for other commands. */
|
|
9
|
+
readonly value?: string;
|
|
8
10
|
}
|
|
9
11
|
interface CommandBase {
|
|
10
12
|
/** Unique, stable id. Used for recents and as DOM id suffix. */
|
|
@@ -17,12 +19,24 @@ interface CommandBase {
|
|
|
17
19
|
readonly description?: string;
|
|
18
20
|
/** Text/emoji (or an icon font ligature) or a template rendered before the label. */
|
|
19
21
|
readonly icon?: string | TemplateRef<unknown>;
|
|
20
|
-
/** Keys shown on the right, e.g. `['⌘', 'S']`. Display only:
|
|
22
|
+
/** Keys shown on the right, e.g. `['⌘', 'S']`. Display only: use `hotkey` to bind a key. */
|
|
21
23
|
readonly shortcut?: readonly string[];
|
|
24
|
+
/**
|
|
25
|
+
* Global key combination(s) that run the command even while the palette is closed, e.g. `'mod+s'`
|
|
26
|
+
* (same syntax as the `shortcut` option). It needs Ctrl, ⌘ or Alt, and Alt-only combinations are
|
|
27
|
+
* ignored while typing in a text field (on macOS Alt types special characters). Ignored by commands
|
|
28
|
+
* that open a sub-palette, by disabled or hidden commands and while the palette is open. Without `shortcut`, the keys are shown on the row.
|
|
29
|
+
*/
|
|
30
|
+
readonly hotkey?: string | readonly string[];
|
|
22
31
|
/** Extra words matched by the search but not shown. */
|
|
23
32
|
readonly keywords?: readonly string[];
|
|
24
33
|
/** A disabled command is shown but cannot be executed or selected. */
|
|
25
34
|
readonly disabled?: boolean;
|
|
35
|
+
/**
|
|
36
|
+
* A pinned command is also listed in a "Pinned" section at the top while the search is empty.
|
|
37
|
+
* Pass a function or a signal to pin it depending on your app state.
|
|
38
|
+
*/
|
|
39
|
+
readonly pinned?: boolean | (() => boolean);
|
|
26
40
|
/**
|
|
27
41
|
* A hidden command is not listed and not searched (unlike `disabled`, which shows it greyed out).
|
|
28
42
|
* Pass a function or a signal to hide it depending on your app state, e.g. `hidden: () => !user()`.
|
|
@@ -34,11 +48,18 @@ interface RouteCommand extends CommandBase {
|
|
|
34
48
|
readonly route: string | readonly unknown[];
|
|
35
49
|
readonly navigationExtras?: NavigationExtras;
|
|
36
50
|
readonly run?: never;
|
|
51
|
+
readonly prompt?: never;
|
|
37
52
|
readonly children?: never;
|
|
38
53
|
}
|
|
39
54
|
/** A command that runs a function. */
|
|
40
55
|
interface ActionCommand extends CommandBase {
|
|
41
56
|
readonly run: (context: CommandRunContext) => void | Promise<unknown>;
|
|
57
|
+
/**
|
|
58
|
+
* Asks for a value before running: selecting the command opens a second step whose search field
|
|
59
|
+
* has this text as placeholder. Enter runs the command with the typed text in `context.value`
|
|
60
|
+
* (nothing happens while it is empty). Back and Backspace leave the step.
|
|
61
|
+
*/
|
|
62
|
+
readonly prompt?: string;
|
|
42
63
|
readonly route?: never;
|
|
43
64
|
readonly children?: never;
|
|
44
65
|
}
|
|
@@ -50,6 +71,7 @@ interface ParentCommand extends CommandBase {
|
|
|
50
71
|
readonly children: readonly Command[];
|
|
51
72
|
readonly route?: never;
|
|
52
73
|
readonly run?: never;
|
|
74
|
+
readonly prompt?: never;
|
|
53
75
|
}
|
|
54
76
|
type Command = RouteCommand | ActionCommand | ParentCommand;
|
|
55
77
|
/** Minimal subscribable shape, so the library does not depend on RxJS. */
|
|
@@ -125,6 +147,9 @@ interface CommandPaletteLabels {
|
|
|
125
147
|
readonly empty: string;
|
|
126
148
|
readonly loading: string;
|
|
127
149
|
readonly recent: string;
|
|
150
|
+
readonly pinned: string;
|
|
151
|
+
/** Announced when a command asks for a value. `{name}` is the command label. */
|
|
152
|
+
readonly prompt: string;
|
|
128
153
|
readonly defaultGroup: string;
|
|
129
154
|
/** Screen reader announcement, `{count}` is replaced by the number of results. */
|
|
130
155
|
readonly results: string;
|
|
@@ -141,15 +166,52 @@ interface CommandPaletteLabels {
|
|
|
141
166
|
* command. A higher `score` ranks first; `indices` are the label characters to highlight.
|
|
142
167
|
*/
|
|
143
168
|
type CommandFilter = (query: string, command: Command) => FuzzyMatch | null;
|
|
169
|
+
/**
|
|
170
|
+
* A mode entered by starting the search with `prefix` (e.g. `>` for actions). The prefix is removed from
|
|
171
|
+
* the search text and only the commands that satisfy every given condition are listed.
|
|
172
|
+
*/
|
|
173
|
+
interface CommandPrefix {
|
|
174
|
+
/** Text the search must start with, e.g. `>` or `/`. */
|
|
175
|
+
readonly prefix: string;
|
|
176
|
+
/** Only commands (and async providers) of these sections. */
|
|
177
|
+
readonly groups?: readonly string[];
|
|
178
|
+
/** Only route commands or only commands that run a function. */
|
|
179
|
+
readonly kind?: 'route' | 'action';
|
|
180
|
+
}
|
|
144
181
|
interface CommandPaletteRecentOptions {
|
|
145
182
|
/** Max number of recents shown. Default: 5. */
|
|
146
183
|
readonly max?: number;
|
|
147
|
-
/**
|
|
184
|
+
/**
|
|
185
|
+
* `recent` (default) lists the last executed commands. `frequent` ranks them by how often and how
|
|
186
|
+
* recently they were executed (the count halves every 7 days), and drops the lowest ranked one
|
|
187
|
+
* when the list is full.
|
|
188
|
+
*/
|
|
189
|
+
readonly rank?: 'recent' | 'frequent';
|
|
190
|
+
/** Storage key. Default: `rr-cp:recent`. */
|
|
148
191
|
readonly storageKey?: string;
|
|
192
|
+
/**
|
|
193
|
+
* Where recents are saved. Default: `localStorage` (none during SSR or when it is blocked). Pass
|
|
194
|
+
* `sessionStorage` or any object with the same three synchronous methods, e.g. an in-memory one.
|
|
195
|
+
* Errors thrown by it are swallowed: recents just stay in memory.
|
|
196
|
+
*/
|
|
197
|
+
readonly storage?: RecentStorage | null;
|
|
149
198
|
}
|
|
199
|
+
/** Minimal synchronous storage for recent commands. `localStorage` and `sessionStorage` fit. */
|
|
200
|
+
type RecentStorage = Pick<Storage, 'getItem' | 'setItem' | 'removeItem'>;
|
|
150
201
|
interface CommandPaletteConfig {
|
|
151
202
|
/** Shortcut(s) that toggle the palette. Default: `mod+k` (⌘K on macOS, Ctrl+K elsewhere). */
|
|
152
203
|
readonly shortcut: string | readonly string[];
|
|
204
|
+
/**
|
|
205
|
+
* Whether shortcuts that use Ctrl/⌘ still fire while typing in an input, textarea, select or
|
|
206
|
+
* contenteditable (this also applies to command `hotkey`s). Default: `true`.
|
|
207
|
+
*/
|
|
208
|
+
readonly shortcutInFields: boolean;
|
|
209
|
+
/**
|
|
210
|
+
* Turns the shortcut off while `false`: it no longer opens the palette (an open palette can still be
|
|
211
|
+
* closed with it), but `open()` keeps working. A function or a signal is read on every key press.
|
|
212
|
+
* Default: `true`.
|
|
213
|
+
*/
|
|
214
|
+
readonly shortcutEnabled: boolean | (() => boolean);
|
|
153
215
|
/** `auto` follows `prefers-color-scheme`. Default: `auto`. */
|
|
154
216
|
readonly theme: CommandPaletteTheme;
|
|
155
217
|
/** "Recent" section shown with an empty query; `false` disables it. */
|
|
@@ -166,6 +228,8 @@ interface CommandPaletteConfig {
|
|
|
166
228
|
readonly maxEmptyResults: number;
|
|
167
229
|
/** Matching and ranking of local commands for a non-empty query. Default: the built-in fuzzy search. */
|
|
168
230
|
readonly filter: CommandFilter;
|
|
231
|
+
/** Search modes triggered by a leading character, e.g. `[{ prefix: '>', kind: 'action' }]`. Default: none. */
|
|
232
|
+
readonly prefixes: readonly CommandPrefix[];
|
|
169
233
|
readonly labels: CommandPaletteLabels;
|
|
170
234
|
}
|
|
171
235
|
type CommandPaletteOptions = Partial<Omit<CommandPaletteConfig, 'labels'>> & {
|
|
@@ -177,6 +241,7 @@ export declare const COMMAND_PALETTE_CONFIG: InjectionToken<CommandPaletteConfig
|
|
|
177
241
|
/** Configures the command palette. Optional: without it the defaults are used. */
|
|
178
242
|
export declare function provideCommandPalette(options?: CommandPaletteOptions): EnvironmentProviders;
|
|
179
243
|
export declare const RECENT_SECTION_ID = "recent";
|
|
244
|
+
export declare const PINNED_SECTION_ID = "pinned";
|
|
180
245
|
/**
|
|
181
246
|
* State of the command palette. Register commands from any component: they are removed
|
|
182
247
|
* automatically when that component is destroyed.
|
|
@@ -194,9 +259,12 @@ export declare class CommandPaletteService {
|
|
|
194
259
|
private readonly recents;
|
|
195
260
|
private readonly pending;
|
|
196
261
|
private readonly pathIds;
|
|
262
|
+
private readonly promptCommand;
|
|
197
263
|
private returnFocus;
|
|
198
264
|
/** Whether the palette is open. */
|
|
199
265
|
readonly isOpen: Signal<boolean>;
|
|
266
|
+
/** The command asking for a value (see `prompt`), while its second step is shown. */
|
|
267
|
+
readonly prompt: Signal<ActionCommand | null>;
|
|
200
268
|
/** Current search text. */
|
|
201
269
|
readonly query: Signal<string>;
|
|
202
270
|
/**
|
|
@@ -210,8 +278,17 @@ export declare class CommandPaletteService {
|
|
|
210
278
|
readonly commands: Signal<readonly Command[]>;
|
|
211
279
|
/** True while at least one async provider is waiting for results. */
|
|
212
280
|
readonly loading: Signal<boolean>;
|
|
281
|
+
/** The search mode (see the `prefixes` option) the current search starts with, if any. */
|
|
282
|
+
readonly activePrefix: Signal<CommandPrefix | null>;
|
|
213
283
|
/** Sections shown in the list, already filtered and sorted. */
|
|
214
284
|
readonly sections: Signal<readonly CommandSection[]>;
|
|
285
|
+
/** Leaf commands with a `hotkey`, not hidden. */
|
|
286
|
+
private readonly hotkeyCommands;
|
|
287
|
+
/**
|
|
288
|
+
* Runs the command bound to the key event, if any (called by the component for every keydown).
|
|
289
|
+
* Returns true when a command matched, so the caller can `preventDefault()`.
|
|
290
|
+
*/
|
|
291
|
+
runHotkey(event: KeyboardEvent, apple: boolean): boolean;
|
|
215
292
|
/** Opens the palette, optionally with an initial query. */
|
|
216
293
|
open(query?: string): void;
|
|
217
294
|
/** Closes the palette, cancels pending searches and restores the focus. */
|
|
@@ -220,7 +297,7 @@ export declare class CommandPaletteService {
|
|
|
220
297
|
/** Updates the search text and schedules the async providers. */
|
|
221
298
|
setQuery(query: string): void;
|
|
222
299
|
/**
|
|
223
|
-
* Goes back `levels` levels of a sub-palette (one by default) and clears the search.
|
|
300
|
+
* Goes back `levels` levels of a sub-palette or value step (one by default) and clears the search.
|
|
224
301
|
* Returns `false`, doing nothing, when the top level is already shown.
|
|
225
302
|
*/
|
|
226
303
|
back(levels?: number): boolean;
|
|
@@ -237,6 +314,8 @@ export declare class CommandPaletteService {
|
|
|
237
314
|
/** Removes all recents. */
|
|
238
315
|
clearRecent(): void;
|
|
239
316
|
private bindToDestroy;
|
|
317
|
+
/** Splits the raw search into the mode it starts with (if any) and the text to search. */
|
|
318
|
+
private parseQuery;
|
|
240
319
|
private buildSections;
|
|
241
320
|
private recentResults;
|
|
242
321
|
private scheduleProviders;
|
|
@@ -303,8 +382,11 @@ export declare class CommandPaletteComponent {
|
|
|
303
382
|
protected readonly labels: import("@rrstudioapps/ngx-command-palette").CommandPaletteLabels;
|
|
304
383
|
private readonly apple;
|
|
305
384
|
private readonly shortcuts;
|
|
385
|
+
private shortcutEnabled;
|
|
306
386
|
/** Label of the first shortcut, e.g. `⌘K` or `Ctrl+K`, for a "search" button in your UI. */
|
|
307
387
|
readonly shortcutLabel: string;
|
|
388
|
+
/** Keys shown on a row: `shortcut`, or the formatted `hotkey` when there is no `shortcut`. */
|
|
389
|
+
protected keysOf(command: Command): readonly string[];
|
|
308
390
|
private readonly prefersDark;
|
|
309
391
|
protected readonly resolvedTheme: import("@angular/core").Signal<"light" | "dark">;
|
|
310
392
|
/** Sections with the flat index of each row (used for ids and keyboard navigation). */
|
|
@@ -321,6 +403,9 @@ export declare class CommandPaletteComponent {
|
|
|
321
403
|
protected readonly activeIndex: import("@angular/core").Signal<number>;
|
|
322
404
|
protected readonly activeDescendant: import("@angular/core").Signal<string | null>;
|
|
323
405
|
protected readonly status: import("@angular/core").Signal<string>;
|
|
406
|
+
/** Sub-palettes opened plus, last, the command waiting for a value. Shown as the path above the search. */
|
|
407
|
+
protected readonly crumbs: import("@angular/core").Signal<readonly Command[]>;
|
|
408
|
+
protected readonly placeholder: import("@angular/core").Signal<string>;
|
|
324
409
|
constructor();
|
|
325
410
|
protected optionId(index: number): string;
|
|
326
411
|
protected hasChildren(command: Command): boolean;
|
|
@@ -364,4 +449,4 @@ type KeyEventLike = Pick<KeyboardEvent, 'key' | 'code' | 'ctrlKey' | 'metaKey' |
|
|
|
364
449
|
export declare function matchesShortcut(event: KeyEventLike, shortcut: ParsedShortcut, apple: boolean): boolean;
|
|
365
450
|
/** Human readable label, e.g. `⌘K` on macOS and `Ctrl+K` elsewhere. */
|
|
366
451
|
export declare function formatShortcut(shortcut: ParsedShortcut, apple: boolean): string;
|
|
367
|
-
export type { ActionCommand, Command, CommandEmptyContext, CommandFilter, CommandItemContext, CommandPaletteConfig, CommandPaletteLabels, CommandPaletteOptions, CommandPaletteRecentOptions, CommandPaletteTheme, CommandProvider, CommandResult, CommandRunContext, CommandSection, CommandSubscribable, FuzzyMatch, HighlightSegment, ParentCommand, ParsedShortcut, RouteCommand };
|
|
452
|
+
export type { ActionCommand, Command, CommandEmptyContext, CommandFilter, CommandItemContext, CommandPaletteConfig, CommandPaletteLabels, CommandPaletteOptions, CommandPaletteRecentOptions, CommandPaletteTheme, CommandPrefix, CommandProvider, CommandResult, CommandRunContext, CommandSection, CommandSubscribable, FuzzyMatch, HighlightSegment, ParentCommand, ParsedShortcut, RecentStorage, RouteCommand };
|