@salesforcedevs/docs-components 1.30.1 → 1.31.0-md-alpha
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/lwc.config.json +3 -0
- package/package.json +28 -28
- package/src/modules/doc/contentLayout/contentLayout.ts +16 -3
- package/src/modules/docUtils/searchClient/searchClient.ts +160 -0
- package/src/modules/docUtils/searchController/__mocks__/searchController.ts +90 -0
- package/src/modules/docUtils/searchController/searchController.ts +420 -0
- package/src/modules/docUtils/searchLocale/searchLocale.ts +85 -0
- package/LICENSE +0 -12
package/lwc.config.json
CHANGED
package/package.json
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}
|
|
2
|
+
"name": "@salesforcedevs/docs-components",
|
|
3
|
+
"version": "1.31.0-md-alpha",
|
|
4
|
+
"description": "Docs Lightning web components for DSC",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"engines": {
|
|
8
|
+
"node": "22.x"
|
|
9
|
+
},
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"@api-components/amf-helper-mixin": "4.5.29",
|
|
15
|
+
"classnames": "2.5.1",
|
|
16
|
+
"dompurify": "3.2.4",
|
|
17
|
+
"kagekiri": "1.4.2",
|
|
18
|
+
"lodash.orderby": "4.6.0",
|
|
19
|
+
"lodash.uniqby": "4.7.0",
|
|
20
|
+
"query-string": "7.1.3",
|
|
21
|
+
"sentence-case": "3.0.4"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@types/classnames": "2.3.1",
|
|
25
|
+
"@types/lodash.orderby": "4.6.9",
|
|
26
|
+
"@types/lodash.uniqby": "4.7.9"
|
|
27
|
+
},
|
|
28
|
+
"gitHead": "4629fdd9ca18a13480044ad43515b91945d16aad"
|
|
29
|
+
}
|
|
@@ -10,6 +10,8 @@ import ContentActionToolbar from "doc/contentActionToolbar";
|
|
|
10
10
|
|
|
11
11
|
const CONTENT_ACTION_TOOLBAR_TAG = "doc-content-action-toolbar";
|
|
12
12
|
const PAGE_HEADING_SELECTOR = "h1";
|
|
13
|
+
const FALLBACK_HEADING_SELECTOR = "h1, h2, h3, h4, h5, h6";
|
|
14
|
+
const CONTENT_BODY_SELECTOR = ".content-body-container .content-body";
|
|
13
15
|
|
|
14
16
|
type AnchorMap = { [key: string]: { intersect: boolean; id: string } };
|
|
15
17
|
|
|
@@ -286,7 +288,8 @@ export default class ContentLayout extends LightningElement {
|
|
|
286
288
|
|
|
287
289
|
/**
|
|
288
290
|
* Inserts the content action toolbar into the slotted content immediately
|
|
289
|
-
* after the first H1 found inside this layout
|
|
291
|
+
* after the first H1 found inside this layout, falling back to the first
|
|
292
|
+
* heading of any level if no H1 exists.
|
|
290
293
|
*/
|
|
291
294
|
protected updateContentActionToolbar(): void {
|
|
292
295
|
if (!this.showContentActionToolbar || !this.sidebarValue) {
|
|
@@ -294,11 +297,21 @@ export default class ContentLayout extends LightningElement {
|
|
|
294
297
|
return;
|
|
295
298
|
}
|
|
296
299
|
|
|
297
|
-
const
|
|
298
|
-
|
|
300
|
+
const contentBody = querySelector(
|
|
301
|
+
CONTENT_BODY_SELECTOR,
|
|
299
302
|
this.template.host
|
|
300
303
|
) as HTMLElement | null;
|
|
301
304
|
|
|
305
|
+
if (!contentBody) {
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
const heading = (querySelector(PAGE_HEADING_SELECTOR, contentBody) ??
|
|
310
|
+
querySelector(
|
|
311
|
+
FALLBACK_HEADING_SELECTOR,
|
|
312
|
+
contentBody
|
|
313
|
+
)) as HTMLElement | null;
|
|
314
|
+
|
|
302
315
|
if (!heading) {
|
|
303
316
|
return;
|
|
304
317
|
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
// Neutral shared core for the DWO unified-search UI (P4.1).
|
|
2
|
+
// The ONLY place a /api/search URL is built, and the ONLY place the HTTP
|
|
3
|
+
// contract lives. No LWC / arch / dx imports — browser DOM APIs only.
|
|
4
|
+
|
|
5
|
+
export interface SearchResult {
|
|
6
|
+
url: string;
|
|
7
|
+
title: string;
|
|
8
|
+
description: string | null;
|
|
9
|
+
locale: string;
|
|
10
|
+
site: string | null;
|
|
11
|
+
contentType: string | null;
|
|
12
|
+
lastmod: string | null;
|
|
13
|
+
score: number;
|
|
14
|
+
lexScore: number;
|
|
15
|
+
semScore: number;
|
|
16
|
+
matchType: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface FacetBucket {
|
|
20
|
+
value: string;
|
|
21
|
+
count: number;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export type SortOption = "relevance" | "date";
|
|
25
|
+
|
|
26
|
+
export interface SearchResponse {
|
|
27
|
+
query: string;
|
|
28
|
+
locale: string;
|
|
29
|
+
site: string | null;
|
|
30
|
+
sort: SortOption;
|
|
31
|
+
limit: number;
|
|
32
|
+
offset: number;
|
|
33
|
+
total: number;
|
|
34
|
+
count: number;
|
|
35
|
+
facets: { contentType: FacetBucket[] };
|
|
36
|
+
results: SearchResult[];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface SearchRequest {
|
|
40
|
+
query: string;
|
|
41
|
+
locale: string;
|
|
42
|
+
site?: string | null;
|
|
43
|
+
type?: string | null;
|
|
44
|
+
sort?: SortOption;
|
|
45
|
+
limit?: number;
|
|
46
|
+
offset?: number;
|
|
47
|
+
page?: number;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export const DEFAULT_LIMIT = 20;
|
|
51
|
+
|
|
52
|
+
export const SORT_OPTIONS: SortOption[] = ["relevance", "date"];
|
|
53
|
+
|
|
54
|
+
export function isSortOption(v: unknown): v is SortOption {
|
|
55
|
+
return v === "relevance" || v === "date";
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Build the query string for a /api/search request.
|
|
60
|
+
* - 'q' always carries the query.
|
|
61
|
+
* - 'lang' (lowercased) ONLY when the query has non-whitespace content.
|
|
62
|
+
* - 'site' / 'type' ONLY when truthy.
|
|
63
|
+
* - 'sort' ONLY when it is a known SortOption (never send an unknown sort).
|
|
64
|
+
* - 'limit' when provided.
|
|
65
|
+
* - OFFSET WINS OVER PAGE: if offset is given, emit it; otherwise derive
|
|
66
|
+
* offset from page. Never emit both, and never emit 'page'.
|
|
67
|
+
*/
|
|
68
|
+
export function buildSearchParams(req: SearchRequest): URLSearchParams {
|
|
69
|
+
const params = new URLSearchParams();
|
|
70
|
+
|
|
71
|
+
params.set("q", req.query);
|
|
72
|
+
|
|
73
|
+
if (req.query.trim() !== "") {
|
|
74
|
+
params.set("lang", req.locale.toLowerCase());
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (req.site) {
|
|
78
|
+
params.set("site", req.site);
|
|
79
|
+
}
|
|
80
|
+
if (req.type) {
|
|
81
|
+
params.set("type", req.type);
|
|
82
|
+
}
|
|
83
|
+
if (isSortOption(req.sort)) {
|
|
84
|
+
params.set("sort", req.sort);
|
|
85
|
+
}
|
|
86
|
+
if (req.limit != null) {
|
|
87
|
+
params.set("limit", String(req.limit));
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (req.offset != null) {
|
|
91
|
+
params.set("offset", String(req.offset));
|
|
92
|
+
} else if (req.page != null) {
|
|
93
|
+
const limit = req.limit || DEFAULT_LIMIT;
|
|
94
|
+
const offset = (Math.max(1, req.page) - 1) * limit;
|
|
95
|
+
params.set("offset", String(offset));
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return params;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export function searchUrl(
|
|
102
|
+
req: SearchRequest,
|
|
103
|
+
basePath = "/api/search"
|
|
104
|
+
): string {
|
|
105
|
+
return `${basePath}?${buildSearchParams(req)}`;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export class SearchHttpError extends Error {
|
|
109
|
+
readonly status: number;
|
|
110
|
+
|
|
111
|
+
constructor(status: number, message?: string) {
|
|
112
|
+
super(message ?? `Search request failed with status ${status}`);
|
|
113
|
+
this.name = "SearchHttpError";
|
|
114
|
+
this.status = status;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export async function fetchSearch(
|
|
119
|
+
req: SearchRequest,
|
|
120
|
+
opts?: { signal?: AbortSignal; basePath?: string; fetchImpl?: typeof fetch }
|
|
121
|
+
): Promise<SearchResponse> {
|
|
122
|
+
const fetchImpl = opts?.fetchImpl ?? fetch.bind(window);
|
|
123
|
+
const url = searchUrl(req, opts?.basePath);
|
|
124
|
+
|
|
125
|
+
const response = await fetchImpl(url, { signal: opts?.signal });
|
|
126
|
+
|
|
127
|
+
if (!response.ok) {
|
|
128
|
+
throw new SearchHttpError(response.status);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const data = (await response.json()) as Partial<SearchResponse>;
|
|
132
|
+
|
|
133
|
+
if (
|
|
134
|
+
!Array.isArray(data.results) ||
|
|
135
|
+
typeof data.total !== "number" ||
|
|
136
|
+
!data.facets ||
|
|
137
|
+
!Array.isArray(data.facets.contentType)
|
|
138
|
+
) {
|
|
139
|
+
throw new Error(
|
|
140
|
+
"Malformed search response: missing results/total/facets"
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return data as SearchResponse;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Decode HTML entities in a backend-supplied string (a response-normalization
|
|
149
|
+
* concern, ported from arch/searchList). No-op on plain strings.
|
|
150
|
+
*/
|
|
151
|
+
export function decodeHtmlEntities(value: unknown): string {
|
|
152
|
+
if (typeof value !== "string" || value.length === 0) {
|
|
153
|
+
return String(value ?? "");
|
|
154
|
+
}
|
|
155
|
+
if (!value.includes("&")) {
|
|
156
|
+
return value;
|
|
157
|
+
}
|
|
158
|
+
const doc = new DOMParser().parseFromString(value, "text/html");
|
|
159
|
+
return doc.documentElement.textContent ?? "";
|
|
160
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
// Manual mock of SearchController for wrapper-test isolation.
|
|
2
|
+
//
|
|
3
|
+
// USE THE AUTOMOCK FORM — bare jest.mock with NO factory:
|
|
4
|
+
//
|
|
5
|
+
// jest.mock("docUtils/searchController");
|
|
6
|
+
//
|
|
7
|
+
// Despite docUtils/* being remapped by jest's moduleNameMapper ($1/$2/$2),
|
|
8
|
+
// jest still resolves the bare jest.mock() to this adjacent __mocks__ folder
|
|
9
|
+
// (verified by probe). Do NOT use a factory that re-requires the BARE
|
|
10
|
+
// specifier `docUtils/searchController/__mocks__/searchController` — the
|
|
11
|
+
// mapper rewrites it to .../__mocks__/searchController/searchController and
|
|
12
|
+
// jest cannot find it (also verified by probe). If a factory is ever needed,
|
|
13
|
+
// use a RELATIVE require: require("../__mocks__/searchController").
|
|
14
|
+
|
|
15
|
+
import type {
|
|
16
|
+
SearchControllerArgs,
|
|
17
|
+
SearchViewModel
|
|
18
|
+
} from "docUtils/searchController";
|
|
19
|
+
|
|
20
|
+
const baseViewModel: SearchViewModel = {
|
|
21
|
+
status: "idle",
|
|
22
|
+
query: "",
|
|
23
|
+
locale: "en-us",
|
|
24
|
+
selectedType: null,
|
|
25
|
+
sort: "relevance",
|
|
26
|
+
results: [],
|
|
27
|
+
contentTypeFacets: [],
|
|
28
|
+
total: 0,
|
|
29
|
+
count: 0,
|
|
30
|
+
limit: 20,
|
|
31
|
+
offset: 0,
|
|
32
|
+
page: 1,
|
|
33
|
+
totalPages: 1,
|
|
34
|
+
errorMessage: undefined
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export class SearchController {
|
|
38
|
+
// eslint-disable-next-line no-use-before-define
|
|
39
|
+
static instances: SearchController[] = [];
|
|
40
|
+
|
|
41
|
+
args: SearchControllerArgs;
|
|
42
|
+
vm: SearchViewModel = { ...baseViewModel };
|
|
43
|
+
private subscribers = new Set<(vm: SearchViewModel) => void>();
|
|
44
|
+
|
|
45
|
+
init = jest.fn();
|
|
46
|
+
setQuery = jest.fn();
|
|
47
|
+
setLanguage = jest.fn();
|
|
48
|
+
setType = jest.fn();
|
|
49
|
+
setSort = jest.fn();
|
|
50
|
+
setPage = jest.fn();
|
|
51
|
+
buildUrl = jest.fn(() => "/api/search?q=&lang=en-us");
|
|
52
|
+
dispose = jest.fn();
|
|
53
|
+
|
|
54
|
+
constructor(args: SearchControllerArgs) {
|
|
55
|
+
this.args = args;
|
|
56
|
+
this.vm = {
|
|
57
|
+
...baseViewModel,
|
|
58
|
+
locale: args.defaultLanguage ?? baseViewModel.locale,
|
|
59
|
+
limit: args.limit ?? baseViewModel.limit
|
|
60
|
+
};
|
|
61
|
+
SearchController.instances.push(this);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
get viewModel(): SearchViewModel {
|
|
65
|
+
return this.vm;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
get localeOptions() {
|
|
69
|
+
return this.args.catalog;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
get showLocaleSelector(): boolean {
|
|
73
|
+
return this.args.catalog.length > 1;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
subscribe = jest.fn((cb: (vm: SearchViewModel) => void): (() => void) => {
|
|
77
|
+
this.subscribers.add(cb);
|
|
78
|
+
return () => {
|
|
79
|
+
this.subscribers.delete(cb);
|
|
80
|
+
};
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
/** Test helper: push a (partial) view-model to all subscribers. */
|
|
84
|
+
__emit(vm: Partial<SearchViewModel>): void {
|
|
85
|
+
this.vm = { ...this.vm, ...vm };
|
|
86
|
+
for (const cb of this.subscribers) {
|
|
87
|
+
cb(this.vm);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
@@ -0,0 +1,420 @@
|
|
|
1
|
+
// Plain (non-LWC) stateful orchestrator shared by the arch + dx search
|
|
2
|
+
// wrappers. No LWC / arch / dx imports — brand specifics are injected.
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
DEFAULT_LIMIT,
|
|
6
|
+
fetchSearch,
|
|
7
|
+
isSortOption,
|
|
8
|
+
searchUrl,
|
|
9
|
+
SearchHttpError
|
|
10
|
+
} from "docUtils/searchClient";
|
|
11
|
+
import type {
|
|
12
|
+
FacetBucket,
|
|
13
|
+
SearchRequest,
|
|
14
|
+
SearchResult,
|
|
15
|
+
SortOption
|
|
16
|
+
} from "docUtils/searchClient";
|
|
17
|
+
import { resolveInitialLanguage } from "docUtils/searchLocale";
|
|
18
|
+
import type { LocaleDeps, LocaleOption } from "docUtils/searchLocale";
|
|
19
|
+
|
|
20
|
+
export type SearchStatus = "idle" | "loading" | "results" | "empty" | "error";
|
|
21
|
+
|
|
22
|
+
export interface SearchViewModel {
|
|
23
|
+
status: SearchStatus;
|
|
24
|
+
query: string;
|
|
25
|
+
locale: string;
|
|
26
|
+
selectedType: string | null;
|
|
27
|
+
sort: SortOption;
|
|
28
|
+
results: SearchResult[];
|
|
29
|
+
contentTypeFacets: FacetBucket[];
|
|
30
|
+
total: number;
|
|
31
|
+
count: number;
|
|
32
|
+
limit: number;
|
|
33
|
+
offset: number;
|
|
34
|
+
page: number;
|
|
35
|
+
totalPages: number;
|
|
36
|
+
errorMessage?: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface SearchControllerArgs {
|
|
40
|
+
catalog: LocaleOption[];
|
|
41
|
+
defaultLanguage: string;
|
|
42
|
+
supportedIds: string[];
|
|
43
|
+
site?: string | null;
|
|
44
|
+
limit?: number;
|
|
45
|
+
basePath?: string;
|
|
46
|
+
deps: LocaleDeps;
|
|
47
|
+
instrument?: (event: string, payload: Record<string, unknown>) => void;
|
|
48
|
+
fetchImpl?: typeof fetch;
|
|
49
|
+
historyMethod?: (
|
|
50
|
+
data: unknown,
|
|
51
|
+
unused: string,
|
|
52
|
+
url?: string | null
|
|
53
|
+
) => void;
|
|
54
|
+
debounceMs?: number;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export class SearchController {
|
|
58
|
+
private readonly catalog: LocaleOption[];
|
|
59
|
+
private readonly defaultLanguage: string;
|
|
60
|
+
private readonly supportedIds: string[];
|
|
61
|
+
private readonly site: string | null;
|
|
62
|
+
private readonly limit: number;
|
|
63
|
+
private readonly basePath?: string;
|
|
64
|
+
private readonly deps: LocaleDeps;
|
|
65
|
+
private readonly instrument?: SearchControllerArgs["instrument"];
|
|
66
|
+
private readonly fetchImpl: typeof fetch;
|
|
67
|
+
private readonly historyMethod: (
|
|
68
|
+
data: unknown,
|
|
69
|
+
unused: string,
|
|
70
|
+
url?: string | null
|
|
71
|
+
) => void;
|
|
72
|
+
private readonly debounceMs: number;
|
|
73
|
+
|
|
74
|
+
// Mutable state.
|
|
75
|
+
private status: SearchStatus = "idle";
|
|
76
|
+
private query = "";
|
|
77
|
+
private locale: string;
|
|
78
|
+
private selectedType: string | null = null;
|
|
79
|
+
private sort: SortOption = "relevance";
|
|
80
|
+
private results: SearchResult[] = [];
|
|
81
|
+
private contentTypeFacets: FacetBucket[] = [];
|
|
82
|
+
private total = 0;
|
|
83
|
+
private count = 0;
|
|
84
|
+
private offset = 0;
|
|
85
|
+
private page = 1;
|
|
86
|
+
private errorMessage?: string;
|
|
87
|
+
|
|
88
|
+
private abortController?: AbortController;
|
|
89
|
+
private debounceTimer?: ReturnType<typeof setTimeout>;
|
|
90
|
+
private subscribers = new Set<(vm: SearchViewModel) => void>();
|
|
91
|
+
|
|
92
|
+
constructor(args: SearchControllerArgs) {
|
|
93
|
+
this.catalog = args.catalog;
|
|
94
|
+
this.defaultLanguage = args.defaultLanguage;
|
|
95
|
+
this.supportedIds = args.supportedIds;
|
|
96
|
+
this.site = args.site ?? null;
|
|
97
|
+
this.limit = args.limit ?? DEFAULT_LIMIT;
|
|
98
|
+
this.basePath = args.basePath;
|
|
99
|
+
this.deps = args.deps;
|
|
100
|
+
this.instrument = args.instrument;
|
|
101
|
+
this.fetchImpl = args.fetchImpl ?? fetch.bind(window);
|
|
102
|
+
this.historyMethod = (
|
|
103
|
+
args.historyMethod ?? window.history.replaceState
|
|
104
|
+
).bind(window.history);
|
|
105
|
+
this.debounceMs = args.debounceMs ?? 250;
|
|
106
|
+
this.locale = this.defaultLanguage;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// ---- public reads -------------------------------------------------
|
|
110
|
+
|
|
111
|
+
get viewModel(): SearchViewModel {
|
|
112
|
+
return {
|
|
113
|
+
status: this.status,
|
|
114
|
+
query: this.query,
|
|
115
|
+
locale: this.locale,
|
|
116
|
+
selectedType: this.selectedType,
|
|
117
|
+
sort: this.sort,
|
|
118
|
+
results: this.results,
|
|
119
|
+
contentTypeFacets: this.contentTypeFacets,
|
|
120
|
+
total: this.total,
|
|
121
|
+
count: this.count,
|
|
122
|
+
limit: this.limit,
|
|
123
|
+
offset: this.offset,
|
|
124
|
+
page: this.page,
|
|
125
|
+
totalPages: Math.max(1, Math.ceil(this.total / this.limit)),
|
|
126
|
+
errorMessage: this.errorMessage
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
get localeOptions(): LocaleOption[] {
|
|
131
|
+
return this.catalog;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
get showLocaleSelector(): boolean {
|
|
135
|
+
return this.catalog.length > 1;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// ---- lifecycle ----------------------------------------------------
|
|
139
|
+
|
|
140
|
+
init(): void {
|
|
141
|
+
const search = window.location.search;
|
|
142
|
+
this.locale = resolveInitialLanguage({
|
|
143
|
+
supportedIds: this.supportedIds,
|
|
144
|
+
defaultLanguage: this.defaultLanguage,
|
|
145
|
+
search,
|
|
146
|
+
navigatorLanguages:
|
|
147
|
+
typeof navigator !== "undefined"
|
|
148
|
+
? navigator.languages
|
|
149
|
+
: undefined,
|
|
150
|
+
deps: this.deps
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
const params = new URLSearchParams(search);
|
|
154
|
+
this.query = params.get("q") ?? "";
|
|
155
|
+
this.selectedType = params.get("type") || null;
|
|
156
|
+
const rawSort = params.get("sort");
|
|
157
|
+
this.sort = isSortOption(rawSort) ? rawSort : "relevance";
|
|
158
|
+
const rawPage = parseInt(params.get("page") ?? "", 10);
|
|
159
|
+
this.page = Number.isFinite(rawPage) && rawPage >= 1 ? rawPage : 1;
|
|
160
|
+
this.offset = (this.page - 1) * this.limit;
|
|
161
|
+
|
|
162
|
+
window.addEventListener("popstate", this.handlePopState);
|
|
163
|
+
|
|
164
|
+
if (this.query.trim() !== "") {
|
|
165
|
+
this.runQuery();
|
|
166
|
+
} else {
|
|
167
|
+
// No query at boot is the resting state, not an executed empty
|
|
168
|
+
// search — keep status 'idle' so the UI can show its prompt.
|
|
169
|
+
this.status = "idle";
|
|
170
|
+
this.notify();
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
dispose(): void {
|
|
175
|
+
this.abortController?.abort();
|
|
176
|
+
this.abortController = undefined;
|
|
177
|
+
if (this.debounceTimer != null) {
|
|
178
|
+
clearTimeout(this.debounceTimer);
|
|
179
|
+
this.debounceTimer = undefined;
|
|
180
|
+
}
|
|
181
|
+
window.removeEventListener("popstate", this.handlePopState);
|
|
182
|
+
this.subscribers.clear();
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// ---- subscription -------------------------------------------------
|
|
186
|
+
|
|
187
|
+
subscribe(cb: (vm: SearchViewModel) => void): () => void {
|
|
188
|
+
this.subscribers.add(cb);
|
|
189
|
+
return () => {
|
|
190
|
+
this.subscribers.delete(cb);
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// ---- mutations ----------------------------------------------------
|
|
195
|
+
|
|
196
|
+
setQuery(q: string): void {
|
|
197
|
+
this.query = q;
|
|
198
|
+
this.page = 1;
|
|
199
|
+
this.offset = 0;
|
|
200
|
+
|
|
201
|
+
// Abort any prior in-flight request before scheduling/firing.
|
|
202
|
+
this.abortController?.abort();
|
|
203
|
+
this.abortController = undefined;
|
|
204
|
+
if (this.debounceTimer != null) {
|
|
205
|
+
clearTimeout(this.debounceTimer);
|
|
206
|
+
this.debounceTimer = undefined;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
if (q.trim() === "") {
|
|
210
|
+
this.applyEmpty();
|
|
211
|
+
this.syncUrl();
|
|
212
|
+
this.notify();
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
this.debounceTimer = setTimeout(() => {
|
|
217
|
+
this.debounceTimer = undefined;
|
|
218
|
+
this.syncUrl();
|
|
219
|
+
this.runQuery();
|
|
220
|
+
}, this.debounceMs);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
setLanguage(id: string): void {
|
|
224
|
+
this.locale = id;
|
|
225
|
+
this.deps.setStored(id);
|
|
226
|
+
this.page = 1;
|
|
227
|
+
this.offset = 0;
|
|
228
|
+
this.syncUrl();
|
|
229
|
+
if (this.query.trim() !== "") {
|
|
230
|
+
this.runQuery();
|
|
231
|
+
} else {
|
|
232
|
+
this.applyEmpty();
|
|
233
|
+
this.notify();
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
setType(type: string | null): void {
|
|
238
|
+
// Disjunctive toggle: selecting the active type clears it.
|
|
239
|
+
this.selectedType = this.selectedType === type ? null : type;
|
|
240
|
+
this.page = 1;
|
|
241
|
+
this.offset = 0;
|
|
242
|
+
this.syncUrl();
|
|
243
|
+
if (this.query.trim() !== "") {
|
|
244
|
+
this.runQuery();
|
|
245
|
+
} else {
|
|
246
|
+
this.applyEmpty();
|
|
247
|
+
this.notify();
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
setSort(s: SortOption): void {
|
|
252
|
+
this.sort = isSortOption(s) ? s : "relevance";
|
|
253
|
+
this.page = 1;
|
|
254
|
+
this.offset = 0;
|
|
255
|
+
this.syncUrl();
|
|
256
|
+
if (this.query.trim() !== "") {
|
|
257
|
+
this.runQuery();
|
|
258
|
+
} else {
|
|
259
|
+
this.applyEmpty();
|
|
260
|
+
this.notify();
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
setPage(page: number): void {
|
|
265
|
+
this.page = Math.max(1, Math.floor(page));
|
|
266
|
+
this.offset = (this.page - 1) * this.limit;
|
|
267
|
+
this.syncUrl();
|
|
268
|
+
if (this.query.trim() !== "") {
|
|
269
|
+
this.runQuery();
|
|
270
|
+
} else {
|
|
271
|
+
this.applyEmpty();
|
|
272
|
+
this.notify();
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// ---- URLs ---------------------------------------------------------
|
|
277
|
+
|
|
278
|
+
buildUrl(): string {
|
|
279
|
+
return searchUrl(this.currentRequest(), this.basePath);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// ---- internals ----------------------------------------------------
|
|
283
|
+
|
|
284
|
+
private currentRequest(): SearchRequest {
|
|
285
|
+
return {
|
|
286
|
+
query: this.query,
|
|
287
|
+
locale: this.locale,
|
|
288
|
+
site: this.site,
|
|
289
|
+
type: this.selectedType,
|
|
290
|
+
sort: this.sort,
|
|
291
|
+
limit: this.limit,
|
|
292
|
+
offset: this.offset
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
private applyEmpty(): void {
|
|
297
|
+
this.status = "empty";
|
|
298
|
+
this.total = 0;
|
|
299
|
+
this.count = 0;
|
|
300
|
+
this.results = [];
|
|
301
|
+
this.contentTypeFacets = [];
|
|
302
|
+
this.errorMessage = undefined;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
private async runQuery(): Promise<void> {
|
|
306
|
+
// Empty query never fires a request.
|
|
307
|
+
if (this.query.trim() === "") {
|
|
308
|
+
this.applyEmpty();
|
|
309
|
+
this.notify();
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
this.status = "loading";
|
|
314
|
+
this.errorMessage = undefined;
|
|
315
|
+
this.notify();
|
|
316
|
+
|
|
317
|
+
// Fresh controller; abort the previous one.
|
|
318
|
+
this.abortController?.abort();
|
|
319
|
+
const controller = new AbortController();
|
|
320
|
+
this.abortController = controller;
|
|
321
|
+
|
|
322
|
+
try {
|
|
323
|
+
const response = await fetchSearch(this.currentRequest(), {
|
|
324
|
+
signal: controller.signal,
|
|
325
|
+
basePath: this.basePath,
|
|
326
|
+
fetchImpl: this.fetchImpl
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
this.results = response.results;
|
|
330
|
+
this.contentTypeFacets = response.facets.contentType;
|
|
331
|
+
this.total = response.total;
|
|
332
|
+
this.count = response.results.length;
|
|
333
|
+
this.status = response.results.length ? "results" : "empty";
|
|
334
|
+
this.errorMessage = undefined;
|
|
335
|
+
|
|
336
|
+
if (this.instrument) {
|
|
337
|
+
this.instrument("search", {
|
|
338
|
+
query: this.query,
|
|
339
|
+
locale: this.locale,
|
|
340
|
+
total: this.total
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
this.notify();
|
|
345
|
+
} catch (error) {
|
|
346
|
+
// A superseded/disposed request is not an error.
|
|
347
|
+
if (error instanceof Error && error.name === "AbortError") {
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
this.status = "error";
|
|
351
|
+
this.errorMessage =
|
|
352
|
+
error instanceof SearchHttpError
|
|
353
|
+
? `Search failed (status ${error.status})`
|
|
354
|
+
: error instanceof Error
|
|
355
|
+
? error.message
|
|
356
|
+
: "Search failed";
|
|
357
|
+
this.notify();
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
private syncUrl(): void {
|
|
362
|
+
const params = new URLSearchParams(window.location.search);
|
|
363
|
+
|
|
364
|
+
if (this.query.trim() !== "") {
|
|
365
|
+
params.set("q", this.query);
|
|
366
|
+
} else {
|
|
367
|
+
params.delete("q");
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
params.set("lang", this.locale.toLowerCase());
|
|
371
|
+
|
|
372
|
+
if (this.selectedType) {
|
|
373
|
+
params.set("type", this.selectedType);
|
|
374
|
+
} else {
|
|
375
|
+
params.delete("type");
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
if (this.sort && this.sort !== "relevance") {
|
|
379
|
+
params.set("sort", this.sort);
|
|
380
|
+
} else {
|
|
381
|
+
params.delete("sort");
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
if (this.page > 1) {
|
|
385
|
+
params.set("page", String(this.page));
|
|
386
|
+
} else {
|
|
387
|
+
params.delete("page");
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
const url = new URL(window.location.href);
|
|
391
|
+
url.search = params.toString();
|
|
392
|
+
this.historyMethod({}, "", url.toString());
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
private notify(): void {
|
|
396
|
+
const vm = this.viewModel;
|
|
397
|
+
for (const cb of this.subscribers) {
|
|
398
|
+
cb(vm);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
private handlePopState = (): void => {
|
|
403
|
+
const search = window.location.search;
|
|
404
|
+
const params = new URLSearchParams(search);
|
|
405
|
+
this.query = params.get("q") ?? "";
|
|
406
|
+
this.selectedType = params.get("type") || null;
|
|
407
|
+
const rawSort = params.get("sort");
|
|
408
|
+
this.sort = isSortOption(rawSort) ? rawSort : "relevance";
|
|
409
|
+
const rawPage = parseInt(params.get("page") ?? "", 10);
|
|
410
|
+
this.page = Number.isFinite(rawPage) && rawPage >= 1 ? rawPage : 1;
|
|
411
|
+
this.offset = (this.page - 1) * this.limit;
|
|
412
|
+
|
|
413
|
+
if (this.query.trim() !== "") {
|
|
414
|
+
this.runQuery();
|
|
415
|
+
} else {
|
|
416
|
+
this.applyEmpty();
|
|
417
|
+
this.notify();
|
|
418
|
+
}
|
|
419
|
+
};
|
|
420
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// Brand-neutral locale cascade for the DWO unified-search UI (P4.1).
|
|
2
|
+
// Lifted from arch/searchList (parseSupportedLocales + resolveInitialLanguage),
|
|
3
|
+
// with browser-match and consent-gated storage INJECTED so the core never
|
|
4
|
+
// imports arch/* or dx/*.
|
|
5
|
+
|
|
6
|
+
export interface LocaleOption {
|
|
7
|
+
id: string;
|
|
8
|
+
displayText?: string;
|
|
9
|
+
label?: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface LocaleDeps {
|
|
13
|
+
getStored: () => string | null;
|
|
14
|
+
setStored: (v: string) => boolean;
|
|
15
|
+
resolveBrowser: (
|
|
16
|
+
langs: readonly string[] | undefined,
|
|
17
|
+
ids: string[]
|
|
18
|
+
) => string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export const DEFAULT_LANGUAGE = "en-us";
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Parse an injected locale catalog (a JSON string), restricting to entries
|
|
25
|
+
* with a string id. Returns [] on bad / non-array JSON.
|
|
26
|
+
*/
|
|
27
|
+
export function parseSupportedLocales(json: string): LocaleOption[] {
|
|
28
|
+
if (!json) {
|
|
29
|
+
return [];
|
|
30
|
+
}
|
|
31
|
+
try {
|
|
32
|
+
const parsed = JSON.parse(json);
|
|
33
|
+
if (!Array.isArray(parsed)) {
|
|
34
|
+
return [];
|
|
35
|
+
}
|
|
36
|
+
return parsed.filter(
|
|
37
|
+
(entry): entry is LocaleOption =>
|
|
38
|
+
!!entry && typeof entry.id === "string"
|
|
39
|
+
);
|
|
40
|
+
} catch (e) {
|
|
41
|
+
return [];
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Resolve the language to search in, most-explicit-wins:
|
|
47
|
+
* 1. ?lang= URL param (shared/bookmarked links)
|
|
48
|
+
* 2. stored preference (consented, from a prior visit)
|
|
49
|
+
* 3. browser preferred language mapped to a supported id
|
|
50
|
+
* 4. the page's server locale, else en-us
|
|
51
|
+
* Any candidate must be in the supported catalog (when one was provided).
|
|
52
|
+
* An unsupported ?lang is IGNORED (we fall through to the next candidate).
|
|
53
|
+
*/
|
|
54
|
+
export function resolveInitialLanguage(args: {
|
|
55
|
+
supportedIds: string[];
|
|
56
|
+
defaultLanguage: string;
|
|
57
|
+
search: string;
|
|
58
|
+
navigatorLanguages: readonly string[] | undefined;
|
|
59
|
+
deps: LocaleDeps;
|
|
60
|
+
}): string {
|
|
61
|
+
const { supportedIds, defaultLanguage, search, navigatorLanguages, deps } =
|
|
62
|
+
args;
|
|
63
|
+
const ids = supportedIds;
|
|
64
|
+
const isSupported = (value: string | null | undefined): boolean =>
|
|
65
|
+
!!value && (ids.length === 0 || ids.includes(value));
|
|
66
|
+
|
|
67
|
+
const fromUrl = new URLSearchParams(search).get("lang");
|
|
68
|
+
if (isSupported(fromUrl)) {
|
|
69
|
+
return fromUrl as string;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const stored = deps.getStored();
|
|
73
|
+
if (isSupported(stored)) {
|
|
74
|
+
return stored as string;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (ids.length > 0) {
|
|
78
|
+
const detected = deps.resolveBrowser(navigatorLanguages, ids);
|
|
79
|
+
if (isSupported(detected)) {
|
|
80
|
+
return detected;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return isSupported(defaultLanguage) ? defaultLanguage : DEFAULT_LANGUAGE;
|
|
85
|
+
}
|
package/LICENSE
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
Copyright (c) 2020, Salesforce.com, Inc.
|
|
2
|
-
All rights reserved.
|
|
3
|
-
|
|
4
|
-
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
|
5
|
-
|
|
6
|
-
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
|
7
|
-
|
|
8
|
-
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
|
9
|
-
|
|
10
|
-
* Neither the name of Salesforce.com nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
|
11
|
-
|
|
12
|
-
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|