@rrstudioapps/ngx-command-palette 0.3.0 → 0.4.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: it does not bind anything. */
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,9 +166,27 @@ 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;
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';
147
190
  /** localStorage key. Default: `rr-cp:recent`. */
148
191
  readonly storageKey?: string;
149
192
  }
@@ -166,6 +209,8 @@ interface CommandPaletteConfig {
166
209
  readonly maxEmptyResults: number;
167
210
  /** Matching and ranking of local commands for a non-empty query. Default: the built-in fuzzy search. */
168
211
  readonly filter: CommandFilter;
212
+ /** Search modes triggered by a leading character, e.g. `[{ prefix: '>', kind: 'action' }]`. Default: none. */
213
+ readonly prefixes: readonly CommandPrefix[];
169
214
  readonly labels: CommandPaletteLabels;
170
215
  }
171
216
  type CommandPaletteOptions = Partial<Omit<CommandPaletteConfig, 'labels'>> & {
@@ -177,6 +222,7 @@ export declare const COMMAND_PALETTE_CONFIG: InjectionToken<CommandPaletteConfig
177
222
  /** Configures the command palette. Optional: without it the defaults are used. */
178
223
  export declare function provideCommandPalette(options?: CommandPaletteOptions): EnvironmentProviders;
179
224
  export declare const RECENT_SECTION_ID = "recent";
225
+ export declare const PINNED_SECTION_ID = "pinned";
180
226
  /**
181
227
  * State of the command palette. Register commands from any component: they are removed
182
228
  * automatically when that component is destroyed.
@@ -194,9 +240,12 @@ export declare class CommandPaletteService {
194
240
  private readonly recents;
195
241
  private readonly pending;
196
242
  private readonly pathIds;
243
+ private readonly promptCommand;
197
244
  private returnFocus;
198
245
  /** Whether the palette is open. */
199
246
  readonly isOpen: Signal<boolean>;
247
+ /** The command asking for a value (see `prompt`), while its second step is shown. */
248
+ readonly prompt: Signal<ActionCommand | null>;
200
249
  /** Current search text. */
201
250
  readonly query: Signal<string>;
202
251
  /**
@@ -210,8 +259,17 @@ export declare class CommandPaletteService {
210
259
  readonly commands: Signal<readonly Command[]>;
211
260
  /** True while at least one async provider is waiting for results. */
212
261
  readonly loading: Signal<boolean>;
262
+ /** The search mode (see the `prefixes` option) the current search starts with, if any. */
263
+ readonly activePrefix: Signal<CommandPrefix | null>;
213
264
  /** Sections shown in the list, already filtered and sorted. */
214
265
  readonly sections: Signal<readonly CommandSection[]>;
266
+ /** Leaf commands with a `hotkey`, not hidden. */
267
+ private readonly hotkeyCommands;
268
+ /**
269
+ * Runs the command bound to the key event, if any (called by the component for every keydown).
270
+ * Returns true when a command matched, so the caller can `preventDefault()`.
271
+ */
272
+ runHotkey(event: KeyboardEvent, apple: boolean): boolean;
215
273
  /** Opens the palette, optionally with an initial query. */
216
274
  open(query?: string): void;
217
275
  /** Closes the palette, cancels pending searches and restores the focus. */
@@ -220,7 +278,7 @@ export declare class CommandPaletteService {
220
278
  /** Updates the search text and schedules the async providers. */
221
279
  setQuery(query: string): void;
222
280
  /**
223
- * Goes back `levels` levels of a sub-palette (one by default) and clears the search.
281
+ * Goes back `levels` levels of a sub-palette or value step (one by default) and clears the search.
224
282
  * Returns `false`, doing nothing, when the top level is already shown.
225
283
  */
226
284
  back(levels?: number): boolean;
@@ -237,6 +295,8 @@ export declare class CommandPaletteService {
237
295
  /** Removes all recents. */
238
296
  clearRecent(): void;
239
297
  private bindToDestroy;
298
+ /** Splits the raw search into the mode it starts with (if any) and the text to search. */
299
+ private parseQuery;
240
300
  private buildSections;
241
301
  private recentResults;
242
302
  private scheduleProviders;
@@ -305,6 +365,8 @@ export declare class CommandPaletteComponent {
305
365
  private readonly shortcuts;
306
366
  /** Label of the first shortcut, e.g. `⌘K` or `Ctrl+K`, for a "search" button in your UI. */
307
367
  readonly shortcutLabel: string;
368
+ /** Keys shown on a row: `shortcut`, or the formatted `hotkey` when there is no `shortcut`. */
369
+ protected keysOf(command: Command): readonly string[];
308
370
  private readonly prefersDark;
309
371
  protected readonly resolvedTheme: import("@angular/core").Signal<"light" | "dark">;
310
372
  /** Sections with the flat index of each row (used for ids and keyboard navigation). */
@@ -321,6 +383,9 @@ export declare class CommandPaletteComponent {
321
383
  protected readonly activeIndex: import("@angular/core").Signal<number>;
322
384
  protected readonly activeDescendant: import("@angular/core").Signal<string | null>;
323
385
  protected readonly status: import("@angular/core").Signal<string>;
386
+ /** Sub-palettes opened plus, last, the command waiting for a value. Shown as the path above the search. */
387
+ protected readonly crumbs: import("@angular/core").Signal<readonly Command[]>;
388
+ protected readonly placeholder: import("@angular/core").Signal<string>;
324
389
  constructor();
325
390
  protected optionId(index: number): string;
326
391
  protected hasChildren(command: Command): boolean;
@@ -364,4 +429,4 @@ type KeyEventLike = Pick<KeyboardEvent, 'key' | 'code' | 'ctrlKey' | 'metaKey' |
364
429
  export declare function matchesShortcut(event: KeyEventLike, shortcut: ParsedShortcut, apple: boolean): boolean;
365
430
  /** Human readable label, e.g. `⌘K` on macOS and `Ctrl+K` elsewhere. */
366
431
  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 };
432
+ export type { ActionCommand, Command, CommandEmptyContext, CommandFilter, CommandItemContext, CommandPaletteConfig, CommandPaletteLabels, CommandPaletteOptions, CommandPaletteRecentOptions, CommandPaletteTheme, CommandPrefix, CommandProvider, CommandResult, CommandRunContext, CommandSection, CommandSubscribable, FuzzyMatch, HighlightSegment, ParentCommand, ParsedShortcut, RouteCommand };