@molecule/app-search-ui-angular 0.0.0-stage → 1.1.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.
- package/LICENSE +202 -0
- package/README.md +1274 -2
- package/dist/highlight.component.d.ts +33 -0
- package/dist/highlight.component.d.ts.map +1 -0
- package/dist/highlight.component.js +84 -0
- package/dist/highlight.component.js.map +1 -0
- package/dist/index.d.ts +99 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +99 -0
- package/dist/index.js.map +1 -0
- package/dist/kbd.component.d.ts +28 -0
- package/dist/kbd.component.d.ts.map +1 -0
- package/dist/kbd.component.js +58 -0
- package/dist/kbd.component.js.map +1 -0
- package/dist/quick-search-dialog.component.d.ts +131 -0
- package/dist/quick-search-dialog.component.d.ts.map +1 -0
- package/dist/quick-search-dialog.component.js +297 -0
- package/dist/quick-search-dialog.component.js.map +1 -0
- package/dist/search-box.component.d.ts +266 -0
- package/dist/search-box.component.d.ts.map +1 -0
- package/dist/search-box.component.js +654 -0
- package/dist/search-box.component.js.map +1 -0
- package/dist/search-results.component.d.ts +174 -0
- package/dist/search-results.component.d.ts.map +1 -0
- package/dist/search-results.component.js +519 -0
- package/dist/search-results.component.js.map +1 -0
- package/dist/types.d.ts +172 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +8 -0
- package/dist/types.js.map +1 -0
- package/package.json +77 -4
package/README.md
CHANGED
|
@@ -1,3 +1,1275 @@
|
|
|
1
|
-
|
|
1
|
+
<!--
|
|
2
|
+
AUTO-GENERATED — DO NOT EDIT THIS FILE.
|
|
3
|
+
Generated by `mlcl sync-docs` from the package's src/index.ts JSDoc + mlcl/registry.json.
|
|
4
|
+
Edits here are overwritten on the next commit (molecule's pre-commit hook regenerates).
|
|
5
|
+
To change this document, edit the module-level JSDoc in src/index.ts.
|
|
6
|
+
Generated: 2026-11-21T12:47:13.664Z
|
|
7
|
+
-->
|
|
2
8
|
|
|
3
|
-
|
|
9
|
+
# @molecule/app-search-ui-angular
|
|
10
|
+
|
|
11
|
+
Angular search UI for `@molecule/app-client-search`.
|
|
12
|
+
|
|
13
|
+
The Angular counterpart of `@molecule/app-search-ui-react` — the same props
|
|
14
|
+
(React names) and the same DOM for the same input. Three pieces that fit a
|
|
15
|
+
`useSearchSession` from `@molecule/app-client-search-angular`:
|
|
16
|
+
{@link SearchBox} (the field, its clear button and shortcut cap, a help icon
|
|
17
|
+
whose tooltip shows the search syntax, an `afterField` projection for the
|
|
18
|
+
consumer's own tabs, the active filter chips and the hit count),
|
|
19
|
+
{@link SearchResults} (ranked rows with the matched terms marked, hover
|
|
20
|
+
prefetch, an optional action per row; the keyboard-active row is marked with
|
|
21
|
+
`aria-current` and announced through a polite status region), and
|
|
22
|
+
{@link QuickSearchDialog} (both in a `mod+k` dialog named for screen readers
|
|
23
|
+
by its `label`, "Search" by default). Every class comes from
|
|
24
|
+
the ClassMap's search tokens (`searchField`, `resultList`, …), so a UI bond
|
|
25
|
+
restyles all of it at once.
|
|
26
|
+
|
|
27
|
+
> **Auto-generated, AI-first package reference** for the [molecule.dev](https://molecule.dev) ecosystem.
|
|
28
|
+
> It is written to be read by coding agents as much as by people, and is generated from this
|
|
29
|
+
> package's source — edit `src/index.ts` JSDoc, not this file.
|
|
30
|
+
|
|
31
|
+
## Quick Start
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
import { Component } from '@angular/core'
|
|
35
|
+
import { Link } from '@molecule/app-angular'
|
|
36
|
+
import { useSearchSession } from '@molecule/app-client-search-angular'
|
|
37
|
+
import { QuickSearchDialog, SearchBox, SearchResults } from '@molecule/app-search-ui-angular'
|
|
38
|
+
import { useTranslation } from '@molecule/app-angular'
|
|
39
|
+
|
|
40
|
+
@Component({
|
|
41
|
+
selector: 'app-catalog',
|
|
42
|
+
standalone: true,
|
|
43
|
+
imports: [Link, SearchBox, SearchResults],
|
|
44
|
+
template: `
|
|
45
|
+
<div
|
|
46
|
+
mol-search-box
|
|
47
|
+
[value]="session.query()"
|
|
48
|
+
[onChange]="session.setQuery"
|
|
49
|
+
[onKeyDown]="session.onKeyDown"
|
|
50
|
+
[inputRef]="session.inputRef"
|
|
51
|
+
placeholder="Search packages"
|
|
52
|
+
[count]="session.hits().length"
|
|
53
|
+
[filters]="session.parsed().filters"
|
|
54
|
+
[onRemoveFilter]="session.removeFilter"
|
|
55
|
+
[onClear]="session.clear"
|
|
56
|
+
[examples]="['category:auth', 'type:bond']"
|
|
57
|
+
></div>
|
|
58
|
+
<mol-search-results
|
|
59
|
+
[hits]="session.hits()"
|
|
60
|
+
[view]="view"
|
|
61
|
+
[activeIndex]="session.activeIndex()"
|
|
62
|
+
[onActivate]="session.setActiveIndex"
|
|
63
|
+
[onOpen]="session.open"
|
|
64
|
+
[onPrefetch]="session.prefetch"
|
|
65
|
+
[renderLink]="link"
|
|
66
|
+
emptyText="Nothing matches"
|
|
67
|
+
></mol-search-results>
|
|
68
|
+
<ng-template #link let-p>
|
|
69
|
+
<a
|
|
70
|
+
molLink
|
|
71
|
+
[to]="p.href"
|
|
72
|
+
[state]="p.state"
|
|
73
|
+
[className]="p.className"
|
|
74
|
+
(click)="p.onClick($event)"
|
|
75
|
+
>
|
|
76
|
+
<ng-template
|
|
77
|
+
[ngTemplateOutlet]="p.children"
|
|
78
|
+
[ngTemplateOutletContext]="{ $implicit: hit }"
|
|
79
|
+
></ng-template>
|
|
80
|
+
</a>
|
|
81
|
+
</ng-template>
|
|
82
|
+
`,
|
|
83
|
+
})
|
|
84
|
+
export class Catalog {
|
|
85
|
+
readonly t = useTranslation().t
|
|
86
|
+
readonly session = useSearchSession({
|
|
87
|
+
docs: packages,
|
|
88
|
+
options: PACKAGE_INDEX_OPTIONS,
|
|
89
|
+
rememberKey: 'catalog.search',
|
|
90
|
+
onOpen: (p) => navigate(`/packages/${p.name}`),
|
|
91
|
+
})
|
|
92
|
+
readonly view = (p: Pkg) => ({
|
|
93
|
+
href: `/packages/${p.name}`,
|
|
94
|
+
title: `@molecule/${p.name}`,
|
|
95
|
+
description: p.description,
|
|
96
|
+
meta: [p.type, p.category],
|
|
97
|
+
mono: true,
|
|
98
|
+
})
|
|
99
|
+
}
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
(`searchPlaceholder` and `nothingMatches` are the app's own translated strings.)
|
|
103
|
+
|
|
104
|
+
## Type
|
|
105
|
+
|
|
106
|
+
`feature`
|
|
107
|
+
|
|
108
|
+
## Installation
|
|
109
|
+
|
|
110
|
+
```bash
|
|
111
|
+
npm install @molecule/app-search-ui-angular @angular/common @angular/core @angular/platform-browser @molecule/app-angular @molecule/app-client-search @molecule/app-client-search-angular @molecule/app-ui @molecule/app-ui-angular
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## API
|
|
115
|
+
|
|
116
|
+
### Interfaces
|
|
117
|
+
|
|
118
|
+
#### `KbdProps`
|
|
119
|
+
|
|
120
|
+
Props accepted by {@link Kbd} — `children` is projected content in Angular
|
|
121
|
+
(an input cannot carry a node), so it is typed `unknown`: put it inside the
|
|
122
|
+
element, not in a binding.
|
|
123
|
+
|
|
124
|
+
```typescript
|
|
125
|
+
interface KbdProps {
|
|
126
|
+
/** The key or keys shown, e.g. `/`, `↵`, `esc`, inside the element. */
|
|
127
|
+
children?: unknown
|
|
128
|
+
/** Extra classes composed onto the cap. */
|
|
129
|
+
className?: string
|
|
130
|
+
}
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
#### `HighlightProps`
|
|
134
|
+
|
|
135
|
+
Props for {@link Highlight} — the text renders inside the element.
|
|
136
|
+
|
|
137
|
+
```typescript
|
|
138
|
+
interface HighlightProps {
|
|
139
|
+
/** The text to show. */
|
|
140
|
+
text: string
|
|
141
|
+
/** The matched terms to mark (a hit's `terms`). */
|
|
142
|
+
terms: string[]
|
|
143
|
+
}
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
#### `SearchBoxProps`
|
|
147
|
+
|
|
148
|
+
Props accepted by {@link SearchBox}. `afterField` is projected content
|
|
149
|
+
(`[slot=afterField]`), not an input.
|
|
150
|
+
|
|
151
|
+
```typescript
|
|
152
|
+
interface SearchBoxProps {
|
|
153
|
+
/** The text. */
|
|
154
|
+
value: string
|
|
155
|
+
/** Called with the new text on every keystroke. */
|
|
156
|
+
onChange: (value: string) => void
|
|
157
|
+
/** Key handling for the input (arrows, Enter, Escape), usually a session's `onKeyDown`. */
|
|
158
|
+
onKeyDown?: (event: KeyboardEvent) => void
|
|
159
|
+
/** A ref to the input, so a shortcut can focus it. */
|
|
160
|
+
inputRef?: { current: HTMLInputElement | null }
|
|
161
|
+
/** Placeholder, already translated. Also the accessible name unless `label` is given. */
|
|
162
|
+
placeholder: string
|
|
163
|
+
/** Accessible name for the input, already translated. Defaults to `placeholder`. */
|
|
164
|
+
label?: string
|
|
165
|
+
/** How many hits the current text has. Omit to show no count. */
|
|
166
|
+
count?: number
|
|
167
|
+
/** Active `field:value` filters, shown as removable chips. */
|
|
168
|
+
filters?: ClientSearchFilter[]
|
|
169
|
+
/** Removes the i-th filter. */
|
|
170
|
+
onRemoveFilter?: (index: number) => void
|
|
171
|
+
/** Clears the text. Shown as an × while there is text. */
|
|
172
|
+
onClear?: () => void
|
|
173
|
+
/**
|
|
174
|
+
* Example tokens for the search tips, e.g. `['category:auth', 'type:bond']`.
|
|
175
|
+
* Given, a help icon sits inside the field and shows the syntax on hover or
|
|
176
|
+
* focus. Omit for no help icon.
|
|
177
|
+
*/
|
|
178
|
+
examples?: string[]
|
|
179
|
+
/** The key that focuses the box, shown as a cap while it is empty. `null` hides it. Defaults to `/`. */
|
|
180
|
+
shortcut?: string | null
|
|
181
|
+
/** Focus the input on mount. */
|
|
182
|
+
autoFocus?: boolean
|
|
183
|
+
/**
|
|
184
|
+
* Rendered directly under the field, before the status row (chips and
|
|
185
|
+
* count), so a consumer can put its own controls — tabs, a scope switch —
|
|
186
|
+
* right below the input. Projected as `[slot=afterField]` content.
|
|
187
|
+
*/
|
|
188
|
+
afterField?: unknown
|
|
189
|
+
/** `data-mol-id` prefix: `<molId>-input`, `-clear`, `-help`, `-filter`, `-count`. */
|
|
190
|
+
molId?: string
|
|
191
|
+
/** Extra classes composed onto the field. */
|
|
192
|
+
className?: string
|
|
193
|
+
}
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
#### `SearchResultHit`
|
|
197
|
+
|
|
198
|
+
What the list needs from a hit. A session's hits already have this shape.
|
|
199
|
+
|
|
200
|
+
```typescript
|
|
201
|
+
interface SearchResultHit<T> {
|
|
202
|
+
id: string
|
|
203
|
+
doc: T
|
|
204
|
+
/** The matched terms, marked in the title and description. */
|
|
205
|
+
terms: string[]
|
|
206
|
+
/** Found by meaning rather than by the words typed. */
|
|
207
|
+
related?: boolean
|
|
208
|
+
}
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
#### `SearchResultView`
|
|
212
|
+
|
|
213
|
+
How one hit renders.
|
|
214
|
+
|
|
215
|
+
```typescript
|
|
216
|
+
interface SearchResultView {
|
|
217
|
+
/** Where the row goes. */
|
|
218
|
+
href: string
|
|
219
|
+
/** The row's title. */
|
|
220
|
+
title: string
|
|
221
|
+
/** Under the title, clamped to two lines. */
|
|
222
|
+
description?: string
|
|
223
|
+
/** Small facts after the title: type, category, source. */
|
|
224
|
+
meta?: string[]
|
|
225
|
+
/** Render the title in monospace (a package name). */
|
|
226
|
+
mono?: boolean
|
|
227
|
+
/** Opaque data handed to `renderLink` (router state). */
|
|
228
|
+
state?: unknown
|
|
229
|
+
}
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
#### `SearchResultLinkProps`
|
|
233
|
+
|
|
234
|
+
Props the row's link gets; `renderLink` turns them into markup.
|
|
235
|
+
|
|
236
|
+
```typescript
|
|
237
|
+
interface SearchResultLinkProps {
|
|
238
|
+
href: string
|
|
239
|
+
className: string
|
|
240
|
+
children: unknown
|
|
241
|
+
onClick: (event: MouseEvent) => void
|
|
242
|
+
state?: unknown
|
|
243
|
+
'data-mol-id': string
|
|
244
|
+
}
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
#### `SearchResultsProps`
|
|
248
|
+
|
|
249
|
+
Props for {@link SearchResults}.
|
|
250
|
+
|
|
251
|
+
```typescript
|
|
252
|
+
interface SearchResultsProps<T extends ClientSearchDocument> {
|
|
253
|
+
hits: SearchResultHit<T>[]
|
|
254
|
+
/** How a hit renders. */
|
|
255
|
+
view: (doc: T) => SearchResultView
|
|
256
|
+
/** The keyboard- or pointer-active row, or `-1`. */
|
|
257
|
+
activeIndex: number
|
|
258
|
+
/** The pointer moved onto a row. */
|
|
259
|
+
onActivate?: (index: number) => void
|
|
260
|
+
/** A row was chosen (a plain click, or Enter elsewhere). Navigation is the caller's. */
|
|
261
|
+
onOpen: (doc: T) => void
|
|
262
|
+
/** The person is about to open a row (hover, focus, touch): warm its page. */
|
|
263
|
+
onPrefetch?: (doc: T) => void
|
|
264
|
+
/**
|
|
265
|
+
* Renders the row's link. Defaults to a plain `<a>`; pass a router link to
|
|
266
|
+
* keep navigation client-side. In Angular this is a `TemplateRef` whose
|
|
267
|
+
* context carries the link props as `$implicit`.
|
|
268
|
+
*/
|
|
269
|
+
renderLink?: TemplateRef<{ $implicit: SearchResultLinkProps }>
|
|
270
|
+
/** Something at the row's end (an action button); a `TemplateRef` over the hit's document. */
|
|
271
|
+
action?: TemplateRef<{ $implicit: T }>
|
|
272
|
+
/** Shown when there are no hits. */
|
|
273
|
+
emptyText: string
|
|
274
|
+
/** `data-mol-id` prefix: `<molId>-results`, `-hit`, `-link`, `-related`, `-empty`, `-active`. */
|
|
275
|
+
molId?: string
|
|
276
|
+
/** Extra classes composed onto the list. */
|
|
277
|
+
className?: string
|
|
278
|
+
}
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
#### `QuickSearchDialogProps`
|
|
282
|
+
|
|
283
|
+
Props for {@link QuickSearchDialog}.
|
|
284
|
+
|
|
285
|
+
```typescript
|
|
286
|
+
interface QuickSearchDialogProps<T extends ClientSearchDocument> {
|
|
287
|
+
/** Whether the dialog is open. Rendered only while open. */
|
|
288
|
+
open: boolean
|
|
289
|
+
/** Close it (Escape with nothing typed, the overlay, or after opening a hit). */
|
|
290
|
+
onClose: () => void
|
|
291
|
+
/** The search session (from `useSearchSession`), created with `syncUrl: false` and `onEscape: onClose`. */
|
|
292
|
+
session: SearchSession<T>
|
|
293
|
+
/** How a hit renders. */
|
|
294
|
+
view: (doc: T) => SearchResultView
|
|
295
|
+
/** Placeholder for the box, already translated. */
|
|
296
|
+
placeholder: string
|
|
297
|
+
/** Example tokens for the tips line. */
|
|
298
|
+
examples?: string[]
|
|
299
|
+
/** Shown while the text matches nothing, already translated. */
|
|
300
|
+
emptyText: string
|
|
301
|
+
/** Shown before anything is typed, already translated. Defaults to the empty text. */
|
|
302
|
+
idleText?: string
|
|
303
|
+
/** Renders each row's link; a `TemplateRef` whose context carries the link props as `$implicit`. */
|
|
304
|
+
renderLink?: TemplateRef<{ $implicit: SearchResultLinkProps }>
|
|
305
|
+
/**
|
|
306
|
+
* The dialog's accessible name, already translated (like `placeholder`, it
|
|
307
|
+
* names nothing visible). Defaults to `t('searchUi.dialogLabel')`
|
|
308
|
+
* ("Search"); the field's placeholder already names the field.
|
|
309
|
+
*/
|
|
310
|
+
label?: string
|
|
311
|
+
/** `data-mol-id` prefix. Defaults to `quick-search`. */
|
|
312
|
+
molId?: string
|
|
313
|
+
}
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
### Classes
|
|
317
|
+
|
|
318
|
+
#### `Highlight`
|
|
319
|
+
|
|
320
|
+
Highlight component.
|
|
321
|
+
|
|
322
|
+
Text with the matched terms marked: each term is marked where it starts a
|
|
323
|
+
word, case-insensitively, through the ClassMap's `resultMark`. React's root
|
|
324
|
+
is a FRAGMENT (the segments, plain text and `<mark>` side by side), so the
|
|
325
|
+
host is unwrapped (`data-parity-unwrap`, `display: contents`) and renders
|
|
326
|
+
exactly React's node list. The text is an attribute of the element — pass
|
|
327
|
+
`text` and `terms` as inputs, not as content.
|
|
328
|
+
|
|
329
|
+
```typescript
|
|
330
|
+
@Component({
|
|
331
|
+
selector: 'mol-highlight',
|
|
332
|
+
standalone: true,
|
|
333
|
+
inputs: ['text', 'terms'],
|
|
334
|
+
host: {
|
|
335
|
+
'data-parity-unwrap': '',
|
|
336
|
+
style: 'display: contents',
|
|
337
|
+
},
|
|
338
|
+
// prettier-ignore
|
|
339
|
+
template: `@for (seg of segments(); track $index) {@if (seg.hit) {<mark [class]="cm().cn(cm().resultMark)">{{ seg.text }}</mark>} @else {{{ seg.text }}}}`,
|
|
340
|
+
})
|
|
341
|
+
export class Highlight implements HighlightProps {
|
|
342
|
+
/** The text to show. */
|
|
343
|
+
text = ''
|
|
344
|
+
/** The matched terms to mark (a hit's `terms`). */
|
|
345
|
+
terms: string[] = []
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* The current class map.
|
|
349
|
+
* @returns The UIClassMap.
|
|
350
|
+
*/
|
|
351
|
+
cm(): UIClassMap {
|
|
352
|
+
return getClassMap()
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* The plain and matched segments, as React derives them per render.
|
|
357
|
+
* @returns The segments, in order.
|
|
358
|
+
*/
|
|
359
|
+
segments(): ReturnType<typeof highlightMatches> {
|
|
360
|
+
return highlightMatches(this.text, this.terms)
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
```
|
|
364
|
+
|
|
365
|
+
#### `Kbd`
|
|
366
|
+
|
|
367
|
+
Kbd component.
|
|
368
|
+
|
|
369
|
+
A key cap, for shortcut hints (`/` to focus, `↵` to open). The host IS the
|
|
370
|
+
`<kbd>` React renders (`kbd[mol-kbd]`), and the key label is the projected
|
|
371
|
+
content — the same children React's `Kbd` carries. The cap shows a key name,
|
|
372
|
+
not prose (a physical key such as `esc` reads the same in every language), so
|
|
373
|
+
the host carries HTML's own `translate="no"`.
|
|
374
|
+
|
|
375
|
+
```typescript
|
|
376
|
+
@Component({
|
|
377
|
+
selector: 'kbd[mol-kbd]',
|
|
378
|
+
standalone: true,
|
|
379
|
+
inputs: ['className'],
|
|
380
|
+
host: {
|
|
381
|
+
'[class]': 'classes()',
|
|
382
|
+
translate: 'no',
|
|
383
|
+
},
|
|
384
|
+
template: `<ng-content></ng-content>`,
|
|
385
|
+
})
|
|
386
|
+
export class Kbd {
|
|
387
|
+
/** Extra classes composed onto the cap. */
|
|
388
|
+
className?: string
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* The current class map.
|
|
392
|
+
* @returns The UIClassMap.
|
|
393
|
+
*/
|
|
394
|
+
cm(): UIClassMap {
|
|
395
|
+
return getClassMap()
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* The cap's class list (`cm.kbd` + `className`).
|
|
400
|
+
* @returns The class string.
|
|
401
|
+
*/
|
|
402
|
+
classes(): string {
|
|
403
|
+
return this.cm().cn(this.cm().kbd, this.className)
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
```
|
|
407
|
+
|
|
408
|
+
#### `QuickSearchDialog`
|
|
409
|
+
|
|
410
|
+
QuickSearchDialog component.
|
|
411
|
+
|
|
412
|
+
A search dialog for `mod+k`: the box on top with focus, the hits below in
|
|
413
|
+
a scrolling pane, and the key hints in the footer. Before anything is
|
|
414
|
+
typed the pane shows `idleText` rather than a browse list. Enter opens the active
|
|
415
|
+
hit through the session (which closes the dialog by the caller's `onOpen`).
|
|
416
|
+
The dialog is named for screen readers through `label` (or the translated
|
|
417
|
+
"Search" default). React renders `null` while closed, so the host is
|
|
418
|
+
unwrapped (`data-parity-unwrap`, `display: contents`) and renders exactly
|
|
419
|
+
React's node list — nothing at all while closed.
|
|
420
|
+
|
|
421
|
+
```typescript
|
|
422
|
+
@Component({
|
|
423
|
+
selector: 'mol-quick-search-dialog',
|
|
424
|
+
standalone: true,
|
|
425
|
+
imports: [Kbd, Modal, SearchBox, SearchResults],
|
|
426
|
+
inputs: [
|
|
427
|
+
'open',
|
|
428
|
+
'onClose',
|
|
429
|
+
'session',
|
|
430
|
+
'view',
|
|
431
|
+
'placeholder',
|
|
432
|
+
'examples',
|
|
433
|
+
'emptyText',
|
|
434
|
+
'idleText',
|
|
435
|
+
'renderLink',
|
|
436
|
+
'label',
|
|
437
|
+
'molId',
|
|
438
|
+
],
|
|
439
|
+
host: {
|
|
440
|
+
'data-parity-unwrap': '',
|
|
441
|
+
style: 'display: contents',
|
|
442
|
+
},
|
|
443
|
+
template: `@if (open) {
|
|
444
|
+
<mol-modal
|
|
445
|
+
[open]="true"
|
|
446
|
+
(close)="emitClose()"
|
|
447
|
+
[size]="'lg'"
|
|
448
|
+
[closeOnEscape]="false"
|
|
449
|
+
[showCloseButton]="false"
|
|
450
|
+
[closeLabel]="closeLabel()"
|
|
451
|
+
[attr.aria-label]="label ?? dialogLabel()"
|
|
452
|
+
[attr.data-mol-id]="id()"
|
|
453
|
+
>
|
|
454
|
+
<div
|
|
455
|
+
mol-search-box
|
|
456
|
+
[value]="read(session.query)"
|
|
457
|
+
[onChange]="setQuery()"
|
|
458
|
+
[onKeyDown]="sessionKeyDown()"
|
|
459
|
+
[inputRef]="session.inputRef"
|
|
460
|
+
[placeholder]="placeholder"
|
|
461
|
+
[count]="read(session.active) ? read(session.hits).length : undefined"
|
|
462
|
+
[filters]="read(session.parsed).filters"
|
|
463
|
+
[onRemoveFilter]="removeFilter()"
|
|
464
|
+
[onClear]="clearSession()"
|
|
465
|
+
[examples]="examples"
|
|
466
|
+
[shortcut]="null"
|
|
467
|
+
[molId]="id()"
|
|
468
|
+
></div>
|
|
469
|
+
<div [class]="cm().cn(cm().dialogBody)" [attr.data-mol-id]="id() + '-pane'">
|
|
470
|
+
<mol-search-results
|
|
471
|
+
[hits]="read(session.active) ? read(session.hits) : []"
|
|
472
|
+
[view]="view"
|
|
473
|
+
[activeIndex]="read(session.activeIndex)"
|
|
474
|
+
[onActivate]="setActiveIndex()"
|
|
475
|
+
[onOpen]="openDoc()"
|
|
476
|
+
[onPrefetch]="prefetchDoc()"
|
|
477
|
+
[renderLink]="renderLink"
|
|
478
|
+
[emptyText]="paneEmptyText()"
|
|
479
|
+
[molId]="id()"
|
|
480
|
+
/>
|
|
481
|
+
</div>
|
|
482
|
+
<p [class]="cm().cn(cm().dialogFooter, cm().resultMeta)" [attr.data-mol-id]="id() + '-keys'">
|
|
483
|
+
<kbd mol-kbd>↑</kbd> <kbd mol-kbd>↓</kbd> <kbd mol-kbd>↵</kbd> <kbd mol-kbd>esc</kbd>
|
|
484
|
+
{{ keysText() }}
|
|
485
|
+
</p>
|
|
486
|
+
</mol-modal>
|
|
487
|
+
}`,
|
|
488
|
+
})
|
|
489
|
+
export class QuickSearchDialog<
|
|
490
|
+
T extends ClientSearchDocument = ClientSearchDocument,
|
|
491
|
+
> implements OnChanges {
|
|
492
|
+
/** Whether the dialog is open. Rendered only while open. */
|
|
493
|
+
open = false
|
|
494
|
+
/** Close it (Escape with nothing typed, the overlay, or after opening a hit). */
|
|
495
|
+
onClose?: () => void
|
|
496
|
+
/** The search session (from `useSearchSession`), created with `syncUrl: false` and `onEscape: onClose`. */
|
|
497
|
+
session!: SearchSession<T>
|
|
498
|
+
/** How a hit renders. */
|
|
499
|
+
view!: (doc: T) => SearchResultView
|
|
500
|
+
/** Placeholder for the box, already translated. */
|
|
501
|
+
placeholder = ''
|
|
502
|
+
/** Example tokens for the tips line. */
|
|
503
|
+
examples?: string[]
|
|
504
|
+
/** Shown while the text matches nothing, already translated. */
|
|
505
|
+
emptyText = ''
|
|
506
|
+
/** Shown before anything is typed, already translated. Defaults to the empty text. */
|
|
507
|
+
idleText?: string
|
|
508
|
+
/** Renders each row's link; a `TemplateRef` whose context carries the link props as `$implicit`. */
|
|
509
|
+
renderLink?: TemplateRef<{ $implicit: SearchResultLinkProps }>
|
|
510
|
+
/**
|
|
511
|
+
* The dialog's accessible name, already translated (like `placeholder`, it
|
|
512
|
+
* names nothing visible). Defaults to `t('searchUi.dialogLabel')`
|
|
513
|
+
* ("Search"); the field's placeholder already names the field.
|
|
514
|
+
*/
|
|
515
|
+
label?: string
|
|
516
|
+
/** `data-mol-id` prefix. Defaults to `quick-search`. */
|
|
517
|
+
molId?: string
|
|
518
|
+
|
|
519
|
+
/** i18n handle, read in an injection context. */
|
|
520
|
+
readonly t = useTranslation().t
|
|
521
|
+
|
|
522
|
+
/**
|
|
523
|
+
* The current class map.
|
|
524
|
+
* @returns The UIClassMap.
|
|
525
|
+
*/
|
|
526
|
+
cm(): UIClassMap {
|
|
527
|
+
return getClassMap()
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
/**
|
|
531
|
+
* Reads a session field, unwrapping a signal and passing a plain value
|
|
532
|
+
* through.
|
|
533
|
+
*
|
|
534
|
+
* @param value - The session field.
|
|
535
|
+
* @returns The value.
|
|
536
|
+
*/
|
|
537
|
+
read<V>(value: Signal<V> | V): V {
|
|
538
|
+
return read(value)
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
/**
|
|
542
|
+
* The `data-mol-id` prefix (React's default parameter).
|
|
543
|
+
* @returns The prefix.
|
|
544
|
+
*/
|
|
545
|
+
id(): string {
|
|
546
|
+
return this.molId ?? 'quick-search'
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
/**
|
|
550
|
+
* The Modal's close label, translated like React passes it.
|
|
551
|
+
* @returns The label.
|
|
552
|
+
*/
|
|
553
|
+
closeLabel(): string {
|
|
554
|
+
return this.t('searchUi.close', undefined, { defaultValue: 'Close' })
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
/**
|
|
558
|
+
* The dialog's accessible name when no `label` is passed, translated like
|
|
559
|
+
* React passes it.
|
|
560
|
+
* @returns The default name ("Search").
|
|
561
|
+
*/
|
|
562
|
+
dialogLabel(): string {
|
|
563
|
+
return this.t('searchUi.dialogLabel', undefined, { defaultValue: 'Search' })
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
/**
|
|
567
|
+
* The keys sentence (the footer's last text).
|
|
568
|
+
* @returns The translated text.
|
|
569
|
+
*/
|
|
570
|
+
keysText(): string {
|
|
571
|
+
return this.t('searchUi.dialogKeys', undefined, {
|
|
572
|
+
defaultValue: 'The arrow keys move through the results, Enter opens one, Escape closes.',
|
|
573
|
+
})
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
/**
|
|
577
|
+
* The pane's empty text: the empty text while the session is active, else
|
|
578
|
+
* the idle text (which falls back to the empty text).
|
|
579
|
+
* @returns The text.
|
|
580
|
+
*/
|
|
581
|
+
paneEmptyText(): string {
|
|
582
|
+
return read(this.session.active) ? this.emptyText : (this.idleText ?? this.emptyText)
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
/** Reports the session's `setQuery` as the box's `onChange`. */
|
|
586
|
+
setQuery(): (value: string) => void {
|
|
587
|
+
return (value: string) => this.session.setQuery(value)
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
/** Reports the session's `onKeyDown` as the box's key handling. */
|
|
591
|
+
sessionKeyDown(): (event: KeyboardEvent) => void {
|
|
592
|
+
return (event: KeyboardEvent) => this.session.onKeyDown(event)
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
/** Reports the session's `removeFilter` as the box's chip removal. */
|
|
596
|
+
removeFilter(): (index: number) => void {
|
|
597
|
+
return (index: number) => this.session.removeFilter(index)
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
/** Reports the session's `clear` as the box's clear callback. */
|
|
601
|
+
clearSession(): () => void {
|
|
602
|
+
return () => this.session.clear()
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
/** Reports the session's `setActiveIndex` as the list's activation. */
|
|
606
|
+
setActiveIndex(): (index: number) => void {
|
|
607
|
+
return (index: number) => this.session.setActiveIndex(index)
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
/** Reports the session's `open` as the list's open callback. */
|
|
611
|
+
openDoc(): (doc: T) => void {
|
|
612
|
+
return (doc: T) => this.session.open(doc)
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
/** Reports the session's `prefetch` as the list's prefetch callback. */
|
|
616
|
+
prefetchDoc(): (doc: T) => void {
|
|
617
|
+
return (doc: T) => this.session.prefetch(doc)
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
/** Closes the dialog through the caller's `onClose`. */
|
|
621
|
+
emitClose(): void {
|
|
622
|
+
this.onClose?.()
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
/**
|
|
626
|
+
* Focus moves into the field after the first render, the way React's effect
|
|
627
|
+
* keyed on `open` and `session.inputRef` does. The move runs after the view
|
|
628
|
+
* check: focusing during it would fire the field's `focusin` listeners while
|
|
629
|
+
* Angular is still checking, and a state change they make would never render.
|
|
630
|
+
*/
|
|
631
|
+
constructor() {
|
|
632
|
+
afterNextRender(() => this.focusField())
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
/**
|
|
636
|
+
* Focuses after an open flip too; the field mounts within the same change
|
|
637
|
+
* detection pass, and the focus waits one microtask for it.
|
|
638
|
+
*
|
|
639
|
+
* @param changes - The changed inputs.
|
|
640
|
+
*/
|
|
641
|
+
ngOnChanges(changes: SimpleChanges): void {
|
|
642
|
+
if (changes['open'] && this.open) queueMicrotask(() => this.focusField())
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
/** Focuses the session's input while the dialog is open. */
|
|
646
|
+
private focusField(): void {
|
|
647
|
+
if (this.open) this.session.inputRef.current?.focus()
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
```
|
|
651
|
+
|
|
652
|
+
#### `SearchBox`
|
|
653
|
+
|
|
654
|
+
SearchBox component.
|
|
655
|
+
|
|
656
|
+
A search field: a leading icon, the input, a help icon that shows the
|
|
657
|
+
syntax on hover, an × to clear, and the shortcut cap; under it, whatever
|
|
658
|
+
`afterField` holds (projected as `[slot=afterField]`), then the active
|
|
659
|
+
filters as removable chips and the hit count. The field stays mounted, and
|
|
660
|
+
its text with it, across searches. The host IS the outer `<div>` React
|
|
661
|
+
renders (`div[mol-search-box]`).
|
|
662
|
+
|
|
663
|
+
```typescript
|
|
664
|
+
@Component({
|
|
665
|
+
selector: 'div[mol-search-box]',
|
|
666
|
+
standalone: true,
|
|
667
|
+
imports: [Icon, Kbd, Tooltip],
|
|
668
|
+
inputs: [
|
|
669
|
+
'value',
|
|
670
|
+
'onChange',
|
|
671
|
+
'onKeyDown',
|
|
672
|
+
'inputRef',
|
|
673
|
+
'placeholder',
|
|
674
|
+
'label',
|
|
675
|
+
'count',
|
|
676
|
+
'filters',
|
|
677
|
+
'onRemoveFilter',
|
|
678
|
+
'onClear',
|
|
679
|
+
'examples',
|
|
680
|
+
'shortcut',
|
|
681
|
+
'autoFocus',
|
|
682
|
+
'molId',
|
|
683
|
+
'className',
|
|
684
|
+
],
|
|
685
|
+
host: {
|
|
686
|
+
'[attr.data-mol-id]': 'id() + "-box"',
|
|
687
|
+
},
|
|
688
|
+
template: `<div [class]="fieldClasses()">
|
|
689
|
+
<span [class]="cm().cn(cm().searchFieldIcon)" aria-hidden="true">
|
|
690
|
+
<svg mol-icon-root [name]="'search'" [size]="18"></svg>
|
|
691
|
+
</span>
|
|
692
|
+
<input
|
|
693
|
+
#searchInput
|
|
694
|
+
type="search"
|
|
695
|
+
[value]="value"
|
|
696
|
+
[attr.value]="restoreValue()"
|
|
697
|
+
(input)="onType($event)"
|
|
698
|
+
(keydown)="onKeyDown?.($event)"
|
|
699
|
+
(focus)="focused.set(true)"
|
|
700
|
+
(blur)="focused.set(false)"
|
|
701
|
+
[attr.placeholder]="placeholder"
|
|
702
|
+
[attr.aria-label]="ariaLabel()"
|
|
703
|
+
autocomplete="off"
|
|
704
|
+
autocorrect="off"
|
|
705
|
+
spellcheck="false"
|
|
706
|
+
enterkeyhint="search"
|
|
707
|
+
[class]="cm().cn(cm().searchFieldInput)"
|
|
708
|
+
[attr.data-mol-id]="id() + '-input'"
|
|
709
|
+
/>
|
|
710
|
+
<span [class]="cm().cn(cm().searchFieldActions)">
|
|
711
|
+
@if (showHelp()) {
|
|
712
|
+
<mol-tooltip placement="bottom-end">
|
|
713
|
+
<button
|
|
714
|
+
type="button"
|
|
715
|
+
[attr.aria-label]="helpLabel()"
|
|
716
|
+
[class]="cm().cn(cm().searchFieldClear)"
|
|
717
|
+
[attr.data-mol-id]="id() + '-help'"
|
|
718
|
+
>
|
|
719
|
+
<svg mol-icon-root [name]="'question'" [size]="16"></svg>
|
|
720
|
+
</button>
|
|
721
|
+
<ng-container ngProjectAs="[slot=content]">
|
|
722
|
+
<div [class]="cm().cn(cm().searchTips)" [style]="tipsStyle()">
|
|
723
|
+
{{ syntaxText() }}{{ ' ' }}
|
|
724
|
+
@for (ex of exampleList(); track ex; let i = $index) {
|
|
725
|
+
<span
|
|
726
|
+
>{{ separator(i) }}<code>{{ ex }}</code></span
|
|
727
|
+
>
|
|
728
|
+
}
|
|
729
|
+
{{ ' · ' }}<kbd mol-kbd>↑</kbd> {{ ' ' }}<kbd mol-kbd>↓</kbd> {{ ' '
|
|
730
|
+
}}<kbd mol-kbd>↵</kbd> {{ ' ' }}{{ keysText() }}
|
|
731
|
+
</div>
|
|
732
|
+
</ng-container>
|
|
733
|
+
</mol-tooltip>
|
|
734
|
+
}
|
|
735
|
+
@if (showClear()) {
|
|
736
|
+
<button
|
|
737
|
+
type="button"
|
|
738
|
+
(click)="onClearClick()"
|
|
739
|
+
[attr.aria-label]="clearLabel()"
|
|
740
|
+
[class]="cm().cn(cm().searchFieldClear)"
|
|
741
|
+
[attr.data-mol-id]="id() + '-clear'"
|
|
742
|
+
>
|
|
743
|
+
<span aria-hidden="true">×</span>
|
|
744
|
+
</button>
|
|
745
|
+
}
|
|
746
|
+
@if (showCap()) {
|
|
747
|
+
<kbd mol-kbd>{{ shortcutText() }}</kbd>
|
|
748
|
+
}
|
|
749
|
+
</span>
|
|
750
|
+
</div>
|
|
751
|
+
<ng-content select="[slot=afterField]"></ng-content>
|
|
752
|
+
@if (hasStatus()) {
|
|
753
|
+
<div [class]="cm().cn(cm().searchStatusRow)">
|
|
754
|
+
@for (chip of chipRecords(); track chip) {
|
|
755
|
+
<button
|
|
756
|
+
type="button"
|
|
757
|
+
(click)="onRemoveFilter?.(chip.index)"
|
|
758
|
+
[attr.aria-label]="chipLabel(chip.filter)"
|
|
759
|
+
[class]="chipClasses(chip.filter)"
|
|
760
|
+
[attr.data-mol-id]="id() + '-filter'"
|
|
761
|
+
[attr.data-filter-field]="chip.filter.field"
|
|
762
|
+
>
|
|
763
|
+
{{ chipText(chip.filter) }}
|
|
764
|
+
<span [class]="cm().cn(cm().filterChipRemove)" aria-hidden="true">×</span>
|
|
765
|
+
</button>
|
|
766
|
+
}
|
|
767
|
+
<span [class]="cm().cn(cm().mxAuto)"></span>
|
|
768
|
+
@if (countText(); as countLabel) {
|
|
769
|
+
<span [attr.data-mol-id]="id() + '-count'">{{ countLabel }}</span>
|
|
770
|
+
}
|
|
771
|
+
</div>
|
|
772
|
+
}`,
|
|
773
|
+
})
|
|
774
|
+
export class SearchBox implements AfterViewInit, OnChanges {
|
|
775
|
+
value = ''
|
|
776
|
+
onChange?: (value: string) => void
|
|
777
|
+
onKeyDown?: (event: KeyboardEvent) => void
|
|
778
|
+
inputRef?: { current: HTMLInputElement | null }
|
|
779
|
+
placeholder = ''
|
|
780
|
+
label?: string
|
|
781
|
+
count?: number
|
|
782
|
+
filters?: ClientSearchFilter[]
|
|
783
|
+
onRemoveFilter?: (index: number) => void
|
|
784
|
+
onClear?: () => void
|
|
785
|
+
examples?: string[]
|
|
786
|
+
shortcut?: string | null
|
|
787
|
+
autoFocus?: boolean
|
|
788
|
+
molId?: string
|
|
789
|
+
className?: string
|
|
790
|
+
|
|
791
|
+
readonly focused: WritableSignal<boolean> = signal(false)
|
|
792
|
+
|
|
793
|
+
@ViewChild('searchInput') searchInput?: ElementRef<HTMLInputElement>
|
|
794
|
+
|
|
795
|
+
private readonly injector = inject(Injector)
|
|
796
|
+
|
|
797
|
+
readonly t = useTranslation().t
|
|
798
|
+
|
|
799
|
+
cm(): UIClassMap {
|
|
800
|
+
return getClassMap()
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
id(): string {
|
|
804
|
+
return this.molId ?? 'search'
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
filterList(): ClientSearchFilter[] {
|
|
808
|
+
return this.filters ?? []
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
fieldClasses(): string {
|
|
812
|
+
return this.cm().cn(this.cm().searchField, this.className)
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
ariaLabel(): string {
|
|
816
|
+
return this.label ?? this.placeholder
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
hasText(): boolean {
|
|
820
|
+
return this.value.trim().length > 0
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
showCount(): boolean {
|
|
824
|
+
return this.count !== undefined && this.hasText()
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
hasStatus(): boolean {
|
|
828
|
+
return this.filterList().length > 0 || this.showCount()
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
showHelp(): boolean {
|
|
832
|
+
return Boolean(this.examples?.length)
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
exampleList(): string[] {
|
|
836
|
+
return [
|
|
837
|
+
...(this.examples ?? []),
|
|
838
|
+
this.t('searchUi.examplePhrase', undefined, { defaultValue: '"exact phrase"' }),
|
|
839
|
+
this.t('searchUi.exampleExcluded', undefined, { defaultValue: '-word' }),
|
|
840
|
+
]
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
showClear(): boolean {
|
|
844
|
+
return Boolean(this.value) && Boolean(this.onClear)
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
capKey(): string | null {
|
|
848
|
+
return this.shortcut === undefined ? '/' : this.shortcut
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
showCap(): boolean {
|
|
852
|
+
return Boolean(this.capKey()) && !this.value && !this.focused()
|
|
853
|
+
}
|
|
854
|
+
|
|
855
|
+
shortcutText(): string {
|
|
856
|
+
return this.capKey() ?? ''
|
|
857
|
+
}
|
|
858
|
+
|
|
859
|
+
tipsStyle(): string {
|
|
860
|
+
return 'white-space: normal; max-width: 360px'
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
separator(index: number): string {
|
|
864
|
+
return index > 0 ? ' · ' : ''
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
helpLabel(): string {
|
|
868
|
+
return this.t('searchUi.help', undefined, { defaultValue: 'Search tips' })
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
clearLabel(): string {
|
|
872
|
+
return this.t('searchUi.clear', undefined, { defaultValue: 'Clear search' })
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
syntaxText(): string {
|
|
876
|
+
return this.t('searchUi.syntax', undefined, {
|
|
877
|
+
defaultValue: 'Narrow with a field, quote a phrase, or exclude a word:',
|
|
878
|
+
})
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
keysText(): string {
|
|
882
|
+
return this.t('searchUi.keys', undefined, {
|
|
883
|
+
defaultValue: 'The arrow keys move through the results and Enter opens one.',
|
|
884
|
+
})
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
countText(): string | null {
|
|
888
|
+
const count = this.count
|
|
889
|
+
if (count === undefined || !this.hasText()) return null
|
|
890
|
+
return this.t('searchUi.count', { count }, { defaultValue: '{{count}} results' })
|
|
891
|
+
}
|
|
892
|
+
|
|
893
|
+
chipText(f: ClientSearchFilter): string {
|
|
894
|
+
return `${f.negate ? '−' : ''}${f.field}: ${f.values.join(', ')}`
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
chipLabel(f: ClientSearchFilter): string {
|
|
898
|
+
return this.t(
|
|
899
|
+
'searchUi.removeFilter',
|
|
900
|
+
{ filter: this.chipText(f) },
|
|
901
|
+
{
|
|
902
|
+
defaultValue: 'Remove filter {{filter}}',
|
|
903
|
+
},
|
|
904
|
+
)
|
|
905
|
+
}
|
|
906
|
+
|
|
907
|
+
chipKey(f: ClientSearchFilter, index: number): string {
|
|
908
|
+
return `${f.field}:${f.values.join(',')}:${index}`
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
private chipCache: { signature: string; records: ChipRecord[] } | null = null
|
|
912
|
+
|
|
913
|
+
chipRecords(): ChipRecord[] {
|
|
914
|
+
const filters = this.filterList()
|
|
915
|
+
const signature = filters.map((f, i) => this.chipKey(f, i)).join('|')
|
|
916
|
+
if (!this.chipCache || this.chipCache.signature !== signature) {
|
|
917
|
+
this.chipCache = {
|
|
918
|
+
signature,
|
|
919
|
+
records: filters.map((f, i) => ({
|
|
920
|
+
key: this.chipKey(f, i),
|
|
921
|
+
filter: f,
|
|
922
|
+
index: i,
|
|
923
|
+
})),
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
return this.chipCache.records
|
|
927
|
+
}
|
|
928
|
+
|
|
929
|
+
chipClasses(f: ClientSearchFilter): string {
|
|
930
|
+
return this.cm().cn(this.cm().filterChip, f.negate && this.cm().filterChipNegated)
|
|
931
|
+
}
|
|
932
|
+
|
|
933
|
+
restoreValue(): string {
|
|
934
|
+
return this.value
|
|
935
|
+
}
|
|
936
|
+
|
|
937
|
+
onType(event: Event): void {
|
|
938
|
+
const el = event.target as HTMLInputElement
|
|
939
|
+
this.onChange?.(el.value)
|
|
940
|
+
el.value = this.value
|
|
941
|
+
el.setAttribute('value', this.value)
|
|
942
|
+
}
|
|
943
|
+
|
|
944
|
+
onClearClick(): void {
|
|
945
|
+
this.onClear?.()
|
|
946
|
+
this.inputRef?.current?.focus()
|
|
947
|
+
}
|
|
948
|
+
|
|
949
|
+
ngAfterViewInit(): void {
|
|
950
|
+
this.captureRef()
|
|
951
|
+
this.scheduleFocus()
|
|
952
|
+
}
|
|
953
|
+
// … truncated — 4 more members: ngOnChanges, scheduleFocus, captureRef, focusIfAsked; the full declaration is in node_modules/@molecule/app-search-ui-angular/dist/search-box.component.d.ts
|
|
954
|
+
```
|
|
955
|
+
|
|
956
|
+
_(Member comments are left out to fit; the full declaration is in `node_modules/@molecule/app-search-ui-angular/dist/search-box.component.d.ts`.)_
|
|
957
|
+
|
|
958
|
+
#### `SearchResults`
|
|
959
|
+
|
|
960
|
+
The default row link is a plain anchor; the row body renders through the
|
|
961
|
+
`rowBody` template. React's root is a FRAGMENT of two nodes (the `<ul>` and
|
|
962
|
+
a visually hidden status `<p>`, or the empty text `<p>` and the status `<p>`),
|
|
963
|
+
so the host is unwrapped (`data-parity-unwrap`, `display: contents`) and
|
|
964
|
+
renders exactly React's root.
|
|
965
|
+
|
|
966
|
+
The ranked hits: title and description with the matched terms marked, the
|
|
967
|
+
meta line, hover making a row active and warming its page. The whole row is
|
|
968
|
+
the link, so the list is a plain list of links (no listbox roles: an option's
|
|
969
|
+
children are presentational and would hide the row's link and action); the
|
|
970
|
+
active row is marked with `aria-current` on its `<li>` and announced through
|
|
971
|
+
a polite, visually hidden status region after the list.
|
|
972
|
+
|
|
973
|
+
```typescript
|
|
974
|
+
@Component({
|
|
975
|
+
selector: 'mol-search-results',
|
|
976
|
+
standalone: true,
|
|
977
|
+
imports: [Highlight, NgTemplateOutlet],
|
|
978
|
+
inputs: [
|
|
979
|
+
'hits',
|
|
980
|
+
'view',
|
|
981
|
+
'activeIndex',
|
|
982
|
+
'onActivate',
|
|
983
|
+
'onOpen',
|
|
984
|
+
'onPrefetch',
|
|
985
|
+
'renderLink',
|
|
986
|
+
'action',
|
|
987
|
+
'emptyText',
|
|
988
|
+
'molId',
|
|
989
|
+
'className',
|
|
990
|
+
],
|
|
991
|
+
host: {
|
|
992
|
+
'data-parity-unwrap': '',
|
|
993
|
+
style: 'display: contents',
|
|
994
|
+
},
|
|
995
|
+
// prettier-ignore
|
|
996
|
+
template: `@if (hits.length === 0) {
|
|
997
|
+
<p [class]="cm().cn(cm().resultEmpty)" [attr.data-mol-id]="id() + '-empty'">{{ emptyText }}</p>
|
|
998
|
+
<!-- The status region exists before results arrive, so it is live. -->
|
|
999
|
+
<p role="status" [class]="cm().cn(cm().srOnly)" [attr.data-mol-id]="id() + '-active'"></p>
|
|
1000
|
+
} @else {
|
|
1001
|
+
<ul
|
|
1002
|
+
#listEl
|
|
1003
|
+
[attr.aria-label]="t('searchUi.results', undefined, { defaultValue: 'Search results' })"
|
|
1004
|
+
[class]="listClasses()"
|
|
1005
|
+
[attr.data-mol-id]="id() + '-results'"
|
|
1006
|
+
>
|
|
1007
|
+
@for (hit of hits; track hit.id; let i = $index) {
|
|
1008
|
+
<li
|
|
1009
|
+
[attr.aria-current]="i === activeIndex ? 'true' : null"
|
|
1010
|
+
[attr.data-index]="i"
|
|
1011
|
+
[attr.data-mol-id]="id() + '-hit'"
|
|
1012
|
+
[attr.data-hit-id]="hit.id"
|
|
1013
|
+
[class]="itemClasses(i)"
|
|
1014
|
+
(pointerover)="onPointerOver(i, hit)"
|
|
1015
|
+
(focusin)="onPrefetch?.(hit.doc)"
|
|
1016
|
+
(touchstart)="onPrefetch?.(hit.doc)"
|
|
1017
|
+
>
|
|
1018
|
+
@if (renderLink) {
|
|
1019
|
+
<ng-container
|
|
1020
|
+
[ngTemplateOutlet]="renderLink"
|
|
1021
|
+
[ngTemplateOutletContext]="renderContext(linkProps(hit))"
|
|
1022
|
+
></ng-container>
|
|
1023
|
+
} @else {
|
|
1024
|
+
<a
|
|
1025
|
+
[attr.href]="viewOf(hit).href"
|
|
1026
|
+
[class]="cm().cn(cm().resultBody)"
|
|
1027
|
+
[attr.data-mol-id]="id() + '-link'"
|
|
1028
|
+
(click)="openFromLink($event, hit)"
|
|
1029
|
+
><ng-container
|
|
1030
|
+
[ngTemplateOutlet]="rowBody"
|
|
1031
|
+
[ngTemplateOutletContext]="{ $implicit: hit }"
|
|
1032
|
+
></ng-container
|
|
1033
|
+
></a>
|
|
1034
|
+
}
|
|
1035
|
+
@if (action) {
|
|
1036
|
+
<span [class]="cm().cn(cm().resultAction)"
|
|
1037
|
+
><ng-container
|
|
1038
|
+
[ngTemplateOutlet]="action"
|
|
1039
|
+
[ngTemplateOutletContext]="renderContext(hit.doc)"
|
|
1040
|
+
></ng-container
|
|
1041
|
+
></span>
|
|
1042
|
+
}
|
|
1043
|
+
</li>
|
|
1044
|
+
}
|
|
1045
|
+
</ul>
|
|
1046
|
+
<p role="status" [class]="cm().cn(cm().srOnly)" [attr.data-mol-id]="id() + '-active'">
|
|
1047
|
+
{{ activeText() }}
|
|
1048
|
+
</p>
|
|
1049
|
+
}
|
|
1050
|
+
<ng-template #rowBody let-hit>
|
|
1051
|
+
<span [class]="cm().flex({ align: 'baseline', wrap: 'wrap', gap: 2 })">
|
|
1052
|
+
<span [class]="titleClasses(hit)">
|
|
1053
|
+
<mol-highlight [text]="viewOf(hit).title" [terms]="hit.terms"></mol-highlight>
|
|
1054
|
+
</span>
|
|
1055
|
+
@if (metaOf(hit).length > 0) {
|
|
1056
|
+
<span [class]="cm().cn(cm().resultMeta)">{{ metaOf(hit).join(' · ') }}</span>
|
|
1057
|
+
}
|
|
1058
|
+
@if (hit.related) {
|
|
1059
|
+
<span
|
|
1060
|
+
[class]="cm().cn(cm().resultMeta)"
|
|
1061
|
+
[attr.data-mol-id]="id() + '-related'"
|
|
1062
|
+
[attr.title]="relatedTitle()"
|
|
1063
|
+
>{{ relatedText() }}</span
|
|
1064
|
+
>
|
|
1065
|
+
}
|
|
1066
|
+
</span>
|
|
1067
|
+
@if (viewOf(hit).description) {
|
|
1068
|
+
<span [class]="cm().cn(cm().resultDescription, cm().displayBlock)">
|
|
1069
|
+
<mol-highlight [text]="viewOf(hit).description!" [terms]="hit.terms"></mol-highlight>
|
|
1070
|
+
</span>
|
|
1071
|
+
}
|
|
1072
|
+
</ng-template>`,
|
|
1073
|
+
})
|
|
1074
|
+
export class SearchResults<T extends ClientSearchDocument = ClientSearchDocument>
|
|
1075
|
+
implements SearchResultsProps<T>, AfterViewInit
|
|
1076
|
+
{
|
|
1077
|
+
/** The hits to render. */
|
|
1078
|
+
hits: SearchResultHit<T>[] = []
|
|
1079
|
+
/** How a hit renders. */
|
|
1080
|
+
view!: (doc: T) => SearchResultView
|
|
1081
|
+
/** The keyboard- or pointer-active row, or `-1`. */
|
|
1082
|
+
activeIndex = -1
|
|
1083
|
+
/** The pointer moved onto a row. */
|
|
1084
|
+
onActivate?: (index: number) => void
|
|
1085
|
+
/** A row was chosen (a plain click, or Enter elsewhere). */
|
|
1086
|
+
onOpen!: (doc: T) => void
|
|
1087
|
+
onPrefetch?: (doc: T) => void
|
|
1088
|
+
/** Renders the row's link. */
|
|
1089
|
+
renderLink?: TemplateRef<{ $implicit: SearchResultLinkProps }>
|
|
1090
|
+
action?: TemplateRef<{ $implicit: T }>
|
|
1091
|
+
/** Shown when there are no hits. */
|
|
1092
|
+
emptyText = ''
|
|
1093
|
+
molId?: string
|
|
1094
|
+
/** Extra classes composed onto the list. */
|
|
1095
|
+
className?: string
|
|
1096
|
+
|
|
1097
|
+
@ViewChild('listEl') listEl?: ElementRef<HTMLUListElement>
|
|
1098
|
+
@ViewChild('rowBody') rowBodyTpl?: TemplateRef<{ $implicit: SearchResultHit<T> }>
|
|
1099
|
+
|
|
1100
|
+
/** i18n handle, read in an injection context. */
|
|
1101
|
+
readonly t = useTranslation().t
|
|
1102
|
+
|
|
1103
|
+
/** The current class map. */
|
|
1104
|
+
cm(): UIClassMap {
|
|
1105
|
+
return getClassMap()
|
|
1106
|
+
}
|
|
1107
|
+
|
|
1108
|
+
/** The `data-mol-id` prefix (React's default parameter). */
|
|
1109
|
+
id(): string {
|
|
1110
|
+
return this.molId ?? 'search'
|
|
1111
|
+
}
|
|
1112
|
+
|
|
1113
|
+
viewOf(hit: SearchResultHit<T>): SearchResultView {
|
|
1114
|
+
return this.view(hit.doc)
|
|
1115
|
+
}
|
|
1116
|
+
|
|
1117
|
+
/** A hit's meta facts (a copy, as React makes). */
|
|
1118
|
+
metaOf(hit: SearchResultHit<T>): string[] {
|
|
1119
|
+
return [...(this.viewOf(hit).meta ?? [])]
|
|
1120
|
+
}
|
|
1121
|
+
|
|
1122
|
+
renderContext(arg: unknown): Record<string, unknown> {
|
|
1123
|
+
return { $implicit: arg, a0: arg, a1: undefined, a2: undefined }
|
|
1124
|
+
}
|
|
1125
|
+
|
|
1126
|
+
/** The list's class list (`cm.resultList` + `className`). */
|
|
1127
|
+
listClasses(): string {
|
|
1128
|
+
return this.cm().cn(this.cm().resultList, this.className)
|
|
1129
|
+
}
|
|
1130
|
+
|
|
1131
|
+
itemClasses(index: number): string {
|
|
1132
|
+
return this.cm().cn(
|
|
1133
|
+
this.cm().resultItem,
|
|
1134
|
+
index === this.activeIndex && this.cm().resultItemActive,
|
|
1135
|
+
)
|
|
1136
|
+
}
|
|
1137
|
+
|
|
1138
|
+
relatedText(): string {
|
|
1139
|
+
return this.t('searchUi.related', undefined, { defaultValue: 'related' })
|
|
1140
|
+
}
|
|
1141
|
+
|
|
1142
|
+
relatedTitle(): string {
|
|
1143
|
+
return this.t('searchUi.relatedTitle', undefined, {
|
|
1144
|
+
defaultValue: 'Matches the meaning of your search, not its words',
|
|
1145
|
+
})
|
|
1146
|
+
}
|
|
1147
|
+
|
|
1148
|
+
activeText(): string {
|
|
1149
|
+
const hit = this.activeIndex >= 0 ? this.hits[this.activeIndex] : undefined
|
|
1150
|
+
if (!hit) return ''
|
|
1151
|
+
return this.t(
|
|
1152
|
+
'searchUi.activeResult',
|
|
1153
|
+
{
|
|
1154
|
+
title: this.viewOf(hit).title,
|
|
1155
|
+
position: this.activeIndex + 1,
|
|
1156
|
+
count: this.hits.length,
|
|
1157
|
+
},
|
|
1158
|
+
{ defaultValue: '{{title}}, {{position}} of {{count}}' },
|
|
1159
|
+
)
|
|
1160
|
+
}
|
|
1161
|
+
|
|
1162
|
+
titleClasses(hit: SearchResultHit<T>): string {
|
|
1163
|
+
const v = this.viewOf(hit)
|
|
1164
|
+
return this.cm().cn(this.cm().resultTitle, v.mono && this.cm().resultTitleMono)
|
|
1165
|
+
}
|
|
1166
|
+
|
|
1167
|
+
/** The link props one row's link gets. */
|
|
1168
|
+
linkProps(hit: SearchResultHit<T>): SearchResultLinkProps {
|
|
1169
|
+
const v = this.viewOf(hit)
|
|
1170
|
+
return {
|
|
1171
|
+
href: v.href,
|
|
1172
|
+
state: v.state,
|
|
1173
|
+
className: this.cm().resultBody,
|
|
1174
|
+
'data-mol-id': `${this.id()}-link`,
|
|
1175
|
+
children: this.rowBodyTpl,
|
|
1176
|
+
onClick: (event: MouseEvent) => {
|
|
1177
|
+
if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey || event.button !== 0)
|
|
1178
|
+
return
|
|
1179
|
+
event.preventDefault()
|
|
1180
|
+
this.onOpen(hit.doc)
|
|
1181
|
+
},
|
|
1182
|
+
}
|
|
1183
|
+
}
|
|
1184
|
+
|
|
1185
|
+
/** Opens a hit for a plain click on the default anchor. */
|
|
1186
|
+
openFromLink(event: MouseEvent, hit: SearchResultHit<T>): void {
|
|
1187
|
+
if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey || event.button !== 0)
|
|
1188
|
+
return
|
|
1189
|
+
event.preventDefault()
|
|
1190
|
+
this.onOpen(hit.doc)
|
|
1191
|
+
}
|
|
1192
|
+
|
|
1193
|
+
onPointerOver(index: number, hit: SearchResultHit<T>): void {
|
|
1194
|
+
this.onActivate?.(index)
|
|
1195
|
+
this.onPrefetch?.(hit.doc)
|
|
1196
|
+
}
|
|
1197
|
+
|
|
1198
|
+
ngOnChanges(changes: SimpleChanges): void {
|
|
1199
|
+
if (changes['activeIndex']) this.keepInView()
|
|
1200
|
+
}
|
|
1201
|
+
|
|
1202
|
+
ngAfterViewInit(): void {
|
|
1203
|
+
this.adopt()
|
|
1204
|
+
this.keepInView()
|
|
1205
|
+
}
|
|
1206
|
+
|
|
1207
|
+
ngDoCheck(): void {
|
|
1208
|
+
this.adopt()
|
|
1209
|
+
}
|
|
1210
|
+
|
|
1211
|
+
private readonly hostEl: HTMLElement = inject(ElementRef<HTMLElement>).nativeElement
|
|
1212
|
+
|
|
1213
|
+
private adopt(): void {
|
|
1214
|
+
const parent = this.hostEl.parentNode
|
|
1215
|
+
if (!parent) return
|
|
1216
|
+
for (const node of Array.from(this.hostEl.childNodes)) parent.insertBefore(node, this.hostEl)
|
|
1217
|
+
}
|
|
1218
|
+
|
|
1219
|
+
/** Scrolls the active row into view when there is one. */
|
|
1220
|
+
private keepInView(): void {
|
|
1221
|
+
const index = this.activeIndex
|
|
1222
|
+
if (index < 0) return
|
|
1223
|
+
const el = this.listEl?.nativeElement.querySelector<HTMLElement>(`[data-index="${index}"]`)
|
|
1224
|
+
el?.scrollIntoView?.({ block: 'nearest' })
|
|
1225
|
+
}
|
|
1226
|
+
}
|
|
1227
|
+
```
|
|
1228
|
+
|
|
1229
|
+
_(Member comments are shortened to their first sentence, and the 20 longest are left out to fit; the full declaration is in `node_modules/@molecule/app-search-ui-angular/dist/search-results.component.d.ts`.)_
|
|
1230
|
+
|
|
1231
|
+
## Injection Notes
|
|
1232
|
+
|
|
1233
|
+
### Requirements
|
|
1234
|
+
|
|
1235
|
+
Peer dependencies:
|
|
1236
|
+
|
|
1237
|
+
- `@angular/common` ^22.0.0
|
|
1238
|
+
- `@angular/core` ^22.0.0
|
|
1239
|
+
- `@angular/platform-browser` ^22.0.0
|
|
1240
|
+
- `@molecule/app-angular` ^3.5.0
|
|
1241
|
+
- `@molecule/app-client-search` ^1.0.0
|
|
1242
|
+
- `@molecule/app-client-search-angular` ^1.0.0
|
|
1243
|
+
- `@molecule/app-ui` ^1.3.3
|
|
1244
|
+
- `@molecule/app-ui-angular` ^1.1.0
|
|
1245
|
+
|
|
1246
|
+
### Runtime Dependencies
|
|
1247
|
+
|
|
1248
|
+
- `@angular/common`
|
|
1249
|
+
- `@angular/core`
|
|
1250
|
+
- `@angular/platform-browser`
|
|
1251
|
+
- `@molecule/app-angular`
|
|
1252
|
+
- `@molecule/app-client-search`
|
|
1253
|
+
- `@molecule/app-client-search-angular`
|
|
1254
|
+
- `@molecule/app-ui`
|
|
1255
|
+
- `@molecule/app-ui-angular`
|
|
1256
|
+
|
|
1257
|
+
- **Pass a router link through `renderLink`**, or every row is a full page
|
|
1258
|
+
load: the list renders a plain `<a>` by default because it does not know
|
|
1259
|
+
your router. A plain click calls `onOpen` (which the session turns into
|
|
1260
|
+
navigation); a modified click is left to the browser so "open in new tab" works.
|
|
1261
|
+
- The box does not own the text: bind `value`/`onChange` to a session (or
|
|
1262
|
+
any state) and it stays mounted, with its text, across searches. Do not
|
|
1263
|
+
remount it per search.
|
|
1264
|
+
- `placeholder`, `emptyText` and the like are already-translated strings
|
|
1265
|
+
from the app; the package translates only its own chrome ("Clear
|
|
1266
|
+
search", "{{count}} results", "Search tips") through `@molecule/app-locales-search-ui`.
|
|
1267
|
+
- The quick dialog needs a session created with `syncUrl: false` and
|
|
1268
|
+
`onEscape: onClose`; the page's own session keeps the URL.
|
|
1269
|
+
- Node-valued React props are projected content: `afterField` is selected
|
|
1270
|
+
by `[slot=afterField]`, and a render function (`renderLink`, `action`) is
|
|
1271
|
+
a `TemplateRef` whose context carries its argument as `$implicit`.
|
|
1272
|
+
|
|
1273
|
+
## Translations
|
|
1274
|
+
|
|
1275
|
+
Translation strings are provided by `@molecule/app-locales-search-ui`.
|