@farming-labs/theme 0.2.133 → 0.2.135
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/README.md +5 -0
- package/dist/command-search-dialog.mjs +701 -0
- package/dist/deferred-dialog.mjs +27 -12
- package/dist/docs-command-search.d.mts +2 -8
- package/dist/docs-command-search.mjs +17 -688
- package/package.json +4 -3
|
@@ -2,264 +2,16 @@
|
|
|
2
2
|
|
|
3
3
|
import { emitClientAnalyticsEvent } from "./client-analytics.mjs";
|
|
4
4
|
import { useWindowSearchParams } from "./client-location.mjs";
|
|
5
|
-
import { resolveClientLocale
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
|
|
5
|
+
import { resolveClientLocale } from "./i18n.mjs";
|
|
6
|
+
import { DeferredDialog } from "./deferred-dialog.mjs";
|
|
7
|
+
import { lazy, useCallback, useEffect, useState } from "react";
|
|
8
|
+
import { jsx } from "react/jsx-runtime";
|
|
10
9
|
|
|
11
10
|
//#region src/docs-command-search.tsx
|
|
12
|
-
|
|
13
|
-
return classes.filter(Boolean).join(" ");
|
|
14
|
-
}
|
|
15
|
-
const BREADCRUMB_SEPARATOR = "\xA0\xA0>\xA0\xA0";
|
|
16
|
-
const FILTER_LABELS = {
|
|
17
|
-
all: "All",
|
|
18
|
-
pages: "Pages",
|
|
19
|
-
inside: "Inside pages"
|
|
20
|
-
};
|
|
21
|
-
function stripHtml(html) {
|
|
22
|
-
if (typeof document !== "undefined") {
|
|
23
|
-
const el = document.createElement("div");
|
|
24
|
-
el.innerHTML = html;
|
|
25
|
-
return el.textContent || el.innerText || "";
|
|
26
|
-
}
|
|
27
|
-
return html.replace(/<[^>]+>/g, "");
|
|
28
|
-
}
|
|
29
|
-
function stripSearchPreview(text) {
|
|
30
|
-
return text.replace(/```[\s\S]*?```/g, "").replace(/~~~[\s\S]*?~~~/g, "").replace(/!\[([^\]]*)\]\([^)]+\)/g, "$1").replace(/\[([^\]]+)\]\([^)]+\)/g, "$1").replace(/^#{1,6}\s+/gm, "").replace(/^\|?[\s:-]+(\|[\s:-]+)+\|?\s*$/gm, "").replace(/\|/g, " ").replace(/^[-*+]\s+/gm, "").replace(/(\*{1,3}|_{1,3})(.*?)\1/g, "$2").replace(/`([^`]+)`/g, "$1").replace(/`+/g, "").replace(/\s{2,}/g, " ").trim();
|
|
31
|
-
}
|
|
32
|
-
function breadcrumbForUrl(url) {
|
|
33
|
-
try {
|
|
34
|
-
const parts = new URL(url, "https://docs.local").pathname.split("/").filter(Boolean).map((part) => decodeURIComponent(part).replace(/[-_]+/g, " ").replace(/\b\w/g, (char) => char.toUpperCase()));
|
|
35
|
-
return parts.length > 0 ? parts.join(BREADCRUMB_SEPARATOR) : "Docs";
|
|
36
|
-
} catch {
|
|
37
|
-
return "Docs";
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
function normalizeSearchPhrase(value) {
|
|
41
|
-
return value.toLowerCase().replace(/[?!.,;:]+$/g, "").replace(/\s+/g, " ").trim();
|
|
42
|
-
}
|
|
43
|
-
function escapeRegExp(value) {
|
|
44
|
-
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
45
|
-
}
|
|
46
|
-
function literalMatchPriority(query, value) {
|
|
47
|
-
const q = normalizeSearchPhrase(query);
|
|
48
|
-
const text = normalizeSearchPhrase(value ?? "");
|
|
49
|
-
if (!q || !text) return 0;
|
|
50
|
-
if (text === q) return 2;
|
|
51
|
-
const boundary = "[^\\p{L}\\p{N}]";
|
|
52
|
-
return new RegExp(`(^|${boundary})${escapeRegExp(q)}(?=$|${boundary})`, "u").test(text) ? 1 : 0;
|
|
53
|
-
}
|
|
54
|
-
function tokenizeLiteralQuery(query) {
|
|
55
|
-
return Array.from(new Set(query.toLowerCase().replace(/[^\p{L}\p{N}@/_:.-]+/gu, " ").split(/\s+/).map((word) => word.replace(/^[^\p{L}\p{N}@]+|[^\p{L}\p{N}]+$/gu, "")).filter((word) => word.length > 1)));
|
|
56
|
-
}
|
|
57
|
-
function isLiteralLookupQuery(query) {
|
|
58
|
-
const q = normalizeSearchPhrase(query);
|
|
59
|
-
const words = tokenizeLiteralQuery(q);
|
|
60
|
-
return words.length > 0 && words.length <= 3 && words.join(" ") === q;
|
|
61
|
-
}
|
|
62
|
-
function hasDistinctSearchSection(result, label) {
|
|
63
|
-
if (result.type === "page") return false;
|
|
64
|
-
if (!result.section) return true;
|
|
65
|
-
const title = label.split(/\s+[—–]\s+/)[0] ?? "";
|
|
66
|
-
return normalizeSearchPhrase(result.section) !== normalizeSearchPhrase(title);
|
|
67
|
-
}
|
|
68
|
-
function getUrlSearchSegments(url) {
|
|
69
|
-
try {
|
|
70
|
-
const parsed = new URL(url, "https://docs.local");
|
|
71
|
-
return Array.from(new Set(parsed.pathname.split("/").flatMap((segment) => {
|
|
72
|
-
const decoded = decodeURIComponent(segment);
|
|
73
|
-
return [decoded, decoded.replace(/[-_]+/g, " ")];
|
|
74
|
-
}).map(normalizeSearchPhrase).filter(Boolean)));
|
|
75
|
-
} catch {
|
|
76
|
-
return [];
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
function exactMatchPriority(query, label, url) {
|
|
80
|
-
const q = normalizeSearchPhrase(query);
|
|
81
|
-
if (!q) return 0;
|
|
82
|
-
const normalizedLabel = normalizeSearchPhrase(label);
|
|
83
|
-
const joinedLabel = normalizeSearchPhrase(label.replace(/\s+[—–-]\s+/g, " "));
|
|
84
|
-
const labelParts = normalizedLabel.split(/\s+[—–-]\s+/).map(normalizeSearchPhrase).filter(Boolean);
|
|
85
|
-
if (normalizedLabel === q || joinedLabel === q) return 4;
|
|
86
|
-
if (labelParts.includes(q)) return 3;
|
|
87
|
-
if (getUrlSearchSegments(url).includes(q)) return 2;
|
|
88
|
-
if (normalizedLabel.startsWith(q) || joinedLabel.startsWith(q)) return 1;
|
|
89
|
-
return 0;
|
|
90
|
-
}
|
|
91
|
-
function resultDisplayLabel(result) {
|
|
92
|
-
const section = result.section ? stripSearchPreview(stripHtml(result.section)) : "";
|
|
93
|
-
if (section) return section;
|
|
94
|
-
const label = stripSearchPreview(stripHtml(result.content));
|
|
95
|
-
const parts = label.split(/\s+[—–]\s+/).map((part) => part.trim()).filter(Boolean);
|
|
96
|
-
if (result.type === "heading" && parts.length > 1) return parts[parts.length - 1] ?? label;
|
|
97
|
-
return label;
|
|
98
|
-
}
|
|
99
|
-
function fuzzyScore(query, text) {
|
|
100
|
-
const q = query.trim().toLowerCase();
|
|
101
|
-
const t = text.toLowerCase();
|
|
102
|
-
if (!q) return {
|
|
103
|
-
score: 0,
|
|
104
|
-
indices: []
|
|
105
|
-
};
|
|
106
|
-
let score = 0;
|
|
107
|
-
const indices = [];
|
|
108
|
-
const idx = t.indexOf(q);
|
|
109
|
-
if (idx >= 0) {
|
|
110
|
-
score += 100 + Math.max(0, 20 - idx);
|
|
111
|
-
for (let i = 0; i < q.length; i++) indices.push(idx + i);
|
|
112
|
-
} else {
|
|
113
|
-
let tPos = 0;
|
|
114
|
-
let chain = 0;
|
|
115
|
-
for (let i = 0; i < q.length; i++) {
|
|
116
|
-
const found = t.indexOf(q[i], tPos);
|
|
117
|
-
if (found === -1) score -= 5;
|
|
118
|
-
else {
|
|
119
|
-
indices.push(found);
|
|
120
|
-
if (found === tPos) chain += 2;
|
|
121
|
-
else chain = 0;
|
|
122
|
-
score += 2 + chain;
|
|
123
|
-
tPos = found + 1;
|
|
124
|
-
if (found === 0 || /\s|-|_|\/|\./.test(text[found - 1])) score += 3;
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
return {
|
|
129
|
-
score,
|
|
130
|
-
indices: Array.from(new Set(indices)).sort((a, b) => a - b)
|
|
131
|
-
};
|
|
132
|
-
}
|
|
133
|
-
function HighlightedLabel({ label, indices = [] }) {
|
|
134
|
-
if (!indices.length) return /* @__PURE__ */ jsx(Fragment$1, { children: label });
|
|
135
|
-
const out = [];
|
|
136
|
-
const highlighted = new Set(indices);
|
|
137
|
-
let pos = 0;
|
|
138
|
-
while (pos < label.length) {
|
|
139
|
-
const marked = highlighted.has(pos);
|
|
140
|
-
let end = pos + 1;
|
|
141
|
-
while (end < label.length && highlighted.has(end) === marked) end++;
|
|
142
|
-
const run = label.slice(pos, end);
|
|
143
|
-
if (marked) out.push(/* @__PURE__ */ jsx("mark", {
|
|
144
|
-
className: "omni-highlight",
|
|
145
|
-
children: run
|
|
146
|
-
}, `m-${pos}`));
|
|
147
|
-
else out.push(/* @__PURE__ */ jsx("span", { children: run }, `t-${pos}`));
|
|
148
|
-
pos = end;
|
|
149
|
-
}
|
|
150
|
-
return /* @__PURE__ */ jsx(Fragment$1, { children: out });
|
|
151
|
-
}
|
|
152
|
-
function SearchIcon() {
|
|
153
|
-
return /* @__PURE__ */ jsxs("svg", {
|
|
154
|
-
width: "16",
|
|
155
|
-
height: "16",
|
|
156
|
-
viewBox: "0 0 24 24",
|
|
157
|
-
fill: "none",
|
|
158
|
-
stroke: "currentColor",
|
|
159
|
-
strokeWidth: "2",
|
|
160
|
-
strokeLinecap: "round",
|
|
161
|
-
strokeLinejoin: "round",
|
|
162
|
-
children: [/* @__PURE__ */ jsx("circle", {
|
|
163
|
-
cx: "11",
|
|
164
|
-
cy: "11",
|
|
165
|
-
r: "8"
|
|
166
|
-
}), /* @__PURE__ */ jsx("path", { d: "m21 21-4.3-4.3" })]
|
|
167
|
-
});
|
|
168
|
-
}
|
|
169
|
-
function ArrowDownIcon() {
|
|
170
|
-
return /* @__PURE__ */ jsx("svg", {
|
|
171
|
-
width: "12",
|
|
172
|
-
height: "12",
|
|
173
|
-
viewBox: "0 0 24 24",
|
|
174
|
-
fill: "none",
|
|
175
|
-
stroke: "currentColor",
|
|
176
|
-
strokeWidth: "2",
|
|
177
|
-
strokeLinecap: "round",
|
|
178
|
-
strokeLinejoin: "round",
|
|
179
|
-
children: /* @__PURE__ */ jsx("path", { d: "m6 9 6 6 6-6" })
|
|
180
|
-
});
|
|
181
|
-
}
|
|
182
|
-
function ArrowUpIcon() {
|
|
183
|
-
return /* @__PURE__ */ jsx("svg", {
|
|
184
|
-
width: "12",
|
|
185
|
-
height: "12",
|
|
186
|
-
viewBox: "0 0 24 24",
|
|
187
|
-
fill: "none",
|
|
188
|
-
stroke: "currentColor",
|
|
189
|
-
strokeWidth: "2",
|
|
190
|
-
strokeLinecap: "round",
|
|
191
|
-
strokeLinejoin: "round",
|
|
192
|
-
children: /* @__PURE__ */ jsx("path", { d: "m18 15-6-6-6 6" })
|
|
193
|
-
});
|
|
194
|
-
}
|
|
195
|
-
function CornerDownLeftIcon() {
|
|
196
|
-
return /* @__PURE__ */ jsxs("svg", {
|
|
197
|
-
width: "12",
|
|
198
|
-
height: "12",
|
|
199
|
-
viewBox: "0 0 24 24",
|
|
200
|
-
fill: "none",
|
|
201
|
-
stroke: "currentColor",
|
|
202
|
-
strokeWidth: "2",
|
|
203
|
-
strokeLinecap: "round",
|
|
204
|
-
strokeLinejoin: "round",
|
|
205
|
-
children: [/* @__PURE__ */ jsx("path", { d: "m9 10-5 5 5 5" }), /* @__PURE__ */ jsx("path", { d: "M20 4v7a4 4 0 0 1-4 4H4" })]
|
|
206
|
-
});
|
|
207
|
-
}
|
|
208
|
-
function LoaderIcon() {
|
|
209
|
-
return /* @__PURE__ */ jsx("svg", {
|
|
210
|
-
width: "12",
|
|
211
|
-
height: "12",
|
|
212
|
-
viewBox: "0 0 24 24",
|
|
213
|
-
fill: "none",
|
|
214
|
-
stroke: "currentColor",
|
|
215
|
-
strokeWidth: "2",
|
|
216
|
-
strokeLinecap: "round",
|
|
217
|
-
strokeLinejoin: "round",
|
|
218
|
-
className: "omni-spin",
|
|
219
|
-
children: /* @__PURE__ */ jsx("path", { d: "M21 12a9 9 0 1 1-6.219-8.56" })
|
|
220
|
-
});
|
|
221
|
-
}
|
|
222
|
-
function HistoryIcon() {
|
|
223
|
-
return /* @__PURE__ */ jsxs("svg", {
|
|
224
|
-
width: "16",
|
|
225
|
-
height: "16",
|
|
226
|
-
viewBox: "0 0 24 24",
|
|
227
|
-
fill: "none",
|
|
228
|
-
stroke: "currentColor",
|
|
229
|
-
strokeWidth: "2",
|
|
230
|
-
strokeLinecap: "round",
|
|
231
|
-
strokeLinejoin: "round",
|
|
232
|
-
children: [
|
|
233
|
-
/* @__PURE__ */ jsx("path", { d: "M3 12a9 9 0 1 0 9-9 9.75 9.75 0 0 0-6.74 2.74L3 8" }),
|
|
234
|
-
/* @__PURE__ */ jsx("path", { d: "M3 3v5h5" }),
|
|
235
|
-
/* @__PURE__ */ jsx("path", { d: "M12 7v5l4 2" })
|
|
236
|
-
]
|
|
237
|
-
});
|
|
238
|
-
}
|
|
239
|
-
/**
|
|
240
|
-
* Built-in docs search command palette.
|
|
241
|
-
* Intercepts Cmd+K and sidebar search button to provide an advanced
|
|
242
|
-
* fuzzy-search experience. Styled entirely via omni-* CSS classes
|
|
243
|
-
* so each theme provides its own visual variant.
|
|
244
|
-
*/
|
|
11
|
+
const CommandSearchDialog = lazy(() => import("./command-search-dialog.mjs").then((module) => ({ default: module.CommandSearchDialog })));
|
|
245
12
|
function DocsCommandSearch({ api = "/api/docs", locale, analytics = false }) {
|
|
246
|
-
const router = useRouter();
|
|
247
13
|
const [open, setOpen] = useState(false);
|
|
248
|
-
const [query, setQuery] = useState("");
|
|
249
|
-
const [debouncedQuery, setDebouncedQuery] = useState("");
|
|
250
|
-
const [results, setResults] = useState([]);
|
|
251
|
-
const [loading, setLoading] = useState(false);
|
|
252
|
-
const [activeIndex, setActiveIndex] = useState(0);
|
|
253
|
-
const [recents, setRecents] = useState([]);
|
|
254
|
-
const [filter, setFilter] = useState("all");
|
|
255
|
-
const [filterOpen, setFilterOpen] = useState(false);
|
|
256
|
-
const [mounted, setMounted] = useState(false);
|
|
257
14
|
const activeLocale = resolveClientLocale(useWindowSearchParams(), locale);
|
|
258
|
-
const searchApi = useMemo(() => withLangInUrl(api, activeLocale), [activeLocale, api]);
|
|
259
|
-
const inputRef = useRef(null);
|
|
260
|
-
const listRef = useRef(null);
|
|
261
|
-
const restoreFocusRef = useRef(null);
|
|
262
|
-
const searchCacheRef = useRef(/* @__PURE__ */ new Map());
|
|
263
15
|
const setOpenWithAnalytics = useCallback((nextOpen, trigger) => {
|
|
264
16
|
setOpen(nextOpen);
|
|
265
17
|
if (!analytics) return;
|
|
@@ -272,17 +24,6 @@ function DocsCommandSearch({ api = "/api/docs", locale, analytics = false }) {
|
|
|
272
24
|
}
|
|
273
25
|
});
|
|
274
26
|
}, [activeLocale, analytics]);
|
|
275
|
-
useEffect(() => {
|
|
276
|
-
setMounted(true);
|
|
277
|
-
try {
|
|
278
|
-
const raw = localStorage.getItem("fd:omni:recents");
|
|
279
|
-
if (raw) setRecents(JSON.parse(raw));
|
|
280
|
-
} catch {}
|
|
281
|
-
}, []);
|
|
282
|
-
useEffect(() => {
|
|
283
|
-
const id = setTimeout(() => setDebouncedQuery(query), 150);
|
|
284
|
-
return () => clearTimeout(id);
|
|
285
|
-
}, [query]);
|
|
286
27
|
useEffect(() => {
|
|
287
28
|
function handler(e) {
|
|
288
29
|
if ((e.metaKey || e.ctrlKey) && e.key === "k") {
|
|
@@ -311,430 +52,18 @@ function DocsCommandSearch({ api = "/api/docs", locale, analytics = false }) {
|
|
|
311
52
|
document.addEventListener("click", handler, true);
|
|
312
53
|
return () => document.removeEventListener("click", handler, true);
|
|
313
54
|
}, [setOpenWithAnalytics]);
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
setActiveIndex(0);
|
|
327
|
-
setLoading(false);
|
|
328
|
-
return;
|
|
329
|
-
}
|
|
330
|
-
setLoading(true);
|
|
331
|
-
(async () => {
|
|
332
|
-
const startedAt = Date.now();
|
|
333
|
-
try {
|
|
334
|
-
const requestUrl = new URL(searchApi, window.location.origin);
|
|
335
|
-
requestUrl.searchParams.set("query", debouncedQuery);
|
|
336
|
-
const res = await fetch(requestUrl.toString(), { signal: controller.signal });
|
|
337
|
-
if (!res.ok || cancelled) return;
|
|
338
|
-
const items = (await res.json()).map((r, sourceIndex) => {
|
|
339
|
-
const sourceLabel = stripSearchPreview(stripHtml(r.content));
|
|
340
|
-
const label = resultDisplayLabel(r);
|
|
341
|
-
const description = r.description ? stripSearchPreview(stripHtml(r.description)).slice(0, 220) : void 0;
|
|
342
|
-
const { score, indices } = fuzzyScore(debouncedQuery, label);
|
|
343
|
-
const descriptionMatch = description ? fuzzyScore(debouncedQuery, description) : { indices: [] };
|
|
344
|
-
const url = withLangInUrl(r.url, activeLocale);
|
|
345
|
-
return {
|
|
346
|
-
id: r.id,
|
|
347
|
-
label,
|
|
348
|
-
subtitle: breadcrumbForUrl(r.url),
|
|
349
|
-
description,
|
|
350
|
-
url,
|
|
351
|
-
type: r.type,
|
|
352
|
-
score,
|
|
353
|
-
exactPriority: exactMatchPriority(debouncedQuery, sourceLabel, r.url),
|
|
354
|
-
insideLiteralPriority: hasDistinctSearchSection(r, sourceLabel) && isLiteralLookupQuery(debouncedQuery) ? Math.max(literalMatchPriority(debouncedQuery, r.section), literalMatchPriority(debouncedQuery, description)) : 0,
|
|
355
|
-
sourceIndex,
|
|
356
|
-
indices,
|
|
357
|
-
descriptionIndices: descriptionMatch.indices
|
|
358
|
-
};
|
|
359
|
-
});
|
|
360
|
-
items.sort((a, b) => {
|
|
361
|
-
const literalDelta = b.insideLiteralPriority - a.insideLiteralPriority;
|
|
362
|
-
if (literalDelta) return literalDelta;
|
|
363
|
-
if (a.insideLiteralPriority > 0 && b.insideLiteralPriority > 0) return a.sourceIndex - b.sourceIndex;
|
|
364
|
-
return b.exactPriority - a.exactPriority || b.score - a.score || a.sourceIndex - b.sourceIndex || a.label.localeCompare(b.label);
|
|
365
|
-
});
|
|
366
|
-
if (!cancelled) {
|
|
367
|
-
if (searchCacheRef.current.size >= 20) {
|
|
368
|
-
const firstKey = searchCacheRef.current.keys().next().value;
|
|
369
|
-
if (firstKey) searchCacheRef.current.delete(firstKey);
|
|
370
|
-
}
|
|
371
|
-
searchCacheRef.current.set(cacheKey, items);
|
|
372
|
-
setResults(items);
|
|
373
|
-
setActiveIndex(0);
|
|
374
|
-
if (analytics) emitClientAnalyticsEvent({
|
|
375
|
-
type: "search_query",
|
|
376
|
-
locale: activeLocale,
|
|
377
|
-
properties: {
|
|
378
|
-
mode: "command",
|
|
379
|
-
queryLength: debouncedQuery.length,
|
|
380
|
-
resultCount: items.length,
|
|
381
|
-
durationMs: Math.max(0, Date.now() - startedAt)
|
|
382
|
-
}
|
|
383
|
-
});
|
|
384
|
-
}
|
|
385
|
-
} catch {
|
|
386
|
-
if (controller.signal.aborted) return;
|
|
387
|
-
if (!cancelled && analytics) emitClientAnalyticsEvent({
|
|
388
|
-
type: "search_error",
|
|
389
|
-
locale: activeLocale,
|
|
390
|
-
properties: {
|
|
391
|
-
mode: "command",
|
|
392
|
-
queryLength: debouncedQuery.length,
|
|
393
|
-
durationMs: Math.max(0, Date.now() - startedAt)
|
|
394
|
-
}
|
|
395
|
-
});
|
|
396
|
-
}
|
|
397
|
-
if (!cancelled) setLoading(false);
|
|
398
|
-
})();
|
|
399
|
-
return () => {
|
|
400
|
-
cancelled = true;
|
|
401
|
-
controller.abort();
|
|
402
|
-
};
|
|
403
|
-
}, [
|
|
404
|
-
activeLocale,
|
|
405
|
-
analytics,
|
|
406
|
-
debouncedQuery,
|
|
407
|
-
searchApi
|
|
408
|
-
]);
|
|
409
|
-
useEffect(() => {
|
|
410
|
-
if (open) {
|
|
411
|
-
const activeElement = document.activeElement;
|
|
412
|
-
restoreFocusRef.current = activeElement instanceof HTMLElement ? activeElement : null;
|
|
413
|
-
const focusTimer = window.setTimeout(() => inputRef.current?.focus(), 10);
|
|
414
|
-
return () => window.clearTimeout(focusTimer);
|
|
415
|
-
} else {
|
|
416
|
-
setQuery("");
|
|
417
|
-
setResults([]);
|
|
418
|
-
setFilter("all");
|
|
419
|
-
setFilterOpen(false);
|
|
420
|
-
setActiveIndex(0);
|
|
421
|
-
const restoreTarget = restoreFocusRef.current;
|
|
422
|
-
restoreFocusRef.current = null;
|
|
423
|
-
if (restoreTarget?.isConnected) {
|
|
424
|
-
const focusFrame = window.requestAnimationFrame(() => restoreTarget.focus({ preventScroll: true }));
|
|
425
|
-
return () => window.cancelAnimationFrame(focusFrame);
|
|
426
|
-
}
|
|
427
|
-
}
|
|
428
|
-
}, [open]);
|
|
429
|
-
useEffect(() => {
|
|
430
|
-
if (!open) return;
|
|
431
|
-
const { body, documentElement } = document;
|
|
432
|
-
const previousBodyOverflow = body.style.overflow;
|
|
433
|
-
const previousBodyPaddingRight = body.style.paddingRight;
|
|
434
|
-
const previousHtmlOverflow = documentElement.style.overflow;
|
|
435
|
-
const scrollbarWidth = window.innerWidth - documentElement.clientWidth;
|
|
436
|
-
body.style.overflow = "hidden";
|
|
437
|
-
documentElement.style.overflow = "hidden";
|
|
438
|
-
if (scrollbarWidth > 0) body.style.paddingRight = `${scrollbarWidth}px`;
|
|
439
|
-
return () => {
|
|
440
|
-
body.style.overflow = previousBodyOverflow;
|
|
441
|
-
body.style.paddingRight = previousBodyPaddingRight;
|
|
442
|
-
documentElement.style.overflow = previousHtmlOverflow;
|
|
443
|
-
};
|
|
444
|
-
}, [open]);
|
|
445
|
-
useEffect(() => {
|
|
446
|
-
if (!open) return;
|
|
447
|
-
function handler(e) {
|
|
448
|
-
if (e.key === "Escape") setOpenWithAnalytics(false, "escape");
|
|
449
|
-
}
|
|
450
|
-
document.addEventListener("keydown", handler);
|
|
451
|
-
return () => document.removeEventListener("keydown", handler);
|
|
452
|
-
}, [open, setOpenWithAnalytics]);
|
|
453
|
-
const saveRecent = useCallback((item) => {
|
|
454
|
-
try {
|
|
455
|
-
const entry = {
|
|
456
|
-
id: item.id,
|
|
457
|
-
label: item.label,
|
|
458
|
-
url: item.url
|
|
459
|
-
};
|
|
460
|
-
const next = [entry, ...recents.filter((r) => r.id !== entry.id)].slice(0, 8);
|
|
461
|
-
setRecents(next);
|
|
462
|
-
localStorage.setItem("fd:omni:recents", JSON.stringify(next));
|
|
463
|
-
} catch {}
|
|
464
|
-
}, [recents]);
|
|
465
|
-
const execute = useCallback((item) => {
|
|
466
|
-
if (analytics) emitClientAnalyticsEvent({
|
|
467
|
-
type: "search_result_click",
|
|
468
|
-
locale: activeLocale,
|
|
469
|
-
path: item.url,
|
|
470
|
-
properties: {
|
|
471
|
-
mode: "command",
|
|
472
|
-
resultId: item.id,
|
|
473
|
-
resultUrl: item.url,
|
|
474
|
-
labelLength: item.label.length,
|
|
475
|
-
queryLength: query.length
|
|
476
|
-
}
|
|
477
|
-
});
|
|
478
|
-
saveRecent(item);
|
|
479
|
-
setOpen(false);
|
|
480
|
-
if (item.url.startsWith("http")) window.open(item.url, "_blank", "noopener");
|
|
481
|
-
else router.push(item.url);
|
|
482
|
-
}, [
|
|
483
|
-
activeLocale,
|
|
484
|
-
analytics,
|
|
485
|
-
query.length,
|
|
486
|
-
router,
|
|
487
|
-
saveRecent
|
|
488
|
-
]);
|
|
489
|
-
const displayItems = useMemo(() => {
|
|
490
|
-
if (results.length === 0) return [];
|
|
491
|
-
if (filter === "pages") return results.filter((item) => item.type === "page");
|
|
492
|
-
if (filter === "inside") return results.filter((item) => item.type !== "page");
|
|
493
|
-
return results;
|
|
494
|
-
}, [filter, results]);
|
|
495
|
-
const recentItems = useMemo(() => {
|
|
496
|
-
if (query.trim() || results.length > 0) return [];
|
|
497
|
-
return recents.map((r) => ({
|
|
498
|
-
id: r.id,
|
|
499
|
-
label: r.label,
|
|
500
|
-
subtitle: "Recently viewed",
|
|
501
|
-
url: r.url,
|
|
502
|
-
type: "page",
|
|
503
|
-
score: 0,
|
|
504
|
-
exactPriority: 0,
|
|
505
|
-
insideLiteralPriority: 0,
|
|
506
|
-
sourceIndex: 0,
|
|
507
|
-
indices: [],
|
|
508
|
-
descriptionIndices: []
|
|
509
|
-
}));
|
|
510
|
-
}, [
|
|
511
|
-
query,
|
|
512
|
-
results,
|
|
513
|
-
recents
|
|
514
|
-
]);
|
|
515
|
-
useEffect(() => {
|
|
516
|
-
setActiveIndex(0);
|
|
517
|
-
}, [filter, debouncedQuery]);
|
|
518
|
-
function updateFilter(nextFilter) {
|
|
519
|
-
setFilter(nextFilter);
|
|
520
|
-
setFilterOpen(false);
|
|
521
|
-
setActiveIndex(0);
|
|
522
|
-
}
|
|
523
|
-
function moveActive(delta) {
|
|
524
|
-
const total = displayItems.length + recentItems.length;
|
|
525
|
-
if (!total) return;
|
|
526
|
-
let next = activeIndex + delta;
|
|
527
|
-
if (next < 0) next = total - 1;
|
|
528
|
-
if (next >= total) next = 0;
|
|
529
|
-
setActiveIndex(next);
|
|
530
|
-
const allItems = [...recentItems, ...displayItems];
|
|
531
|
-
(listRef.current?.querySelector(`[data-id="${allItems[next]?.id}"]`))?.scrollIntoView({ block: "nearest" });
|
|
532
|
-
}
|
|
533
|
-
function handleKeyDown(e) {
|
|
534
|
-
if (e.key === "ArrowDown") {
|
|
535
|
-
e.preventDefault();
|
|
536
|
-
moveActive(1);
|
|
537
|
-
} else if (e.key === "ArrowUp") {
|
|
538
|
-
e.preventDefault();
|
|
539
|
-
moveActive(-1);
|
|
540
|
-
} else if (e.key === "Enter") {
|
|
541
|
-
e.preventDefault();
|
|
542
|
-
const allItems = [...recentItems, ...displayItems];
|
|
543
|
-
if (allItems[activeIndex]) execute(allItems[activeIndex]);
|
|
544
|
-
}
|
|
545
|
-
}
|
|
546
|
-
if (!mounted || !open) return null;
|
|
547
|
-
const allItems = [...recentItems, ...displayItems];
|
|
548
|
-
return createPortal(/* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsx("div", {
|
|
549
|
-
className: "omni-overlay",
|
|
550
|
-
onClick: () => setOpenWithAnalytics(false, "overlay")
|
|
551
|
-
}), /* @__PURE__ */ jsxs("div", {
|
|
552
|
-
className: "omni-content",
|
|
553
|
-
role: "dialog",
|
|
554
|
-
"aria-modal": "true",
|
|
555
|
-
"aria-label": "Search documentation",
|
|
556
|
-
children: [
|
|
557
|
-
/* @__PURE__ */ jsx("div", {
|
|
558
|
-
className: "omni-header",
|
|
559
|
-
children: /* @__PURE__ */ jsxs("div", {
|
|
560
|
-
className: "omni-search-row",
|
|
561
|
-
children: [
|
|
562
|
-
/* @__PURE__ */ jsx("span", {
|
|
563
|
-
className: "omni-search-icon",
|
|
564
|
-
children: /* @__PURE__ */ jsx(SearchIcon, {})
|
|
565
|
-
}),
|
|
566
|
-
/* @__PURE__ */ jsx("input", {
|
|
567
|
-
ref: inputRef,
|
|
568
|
-
type: "text",
|
|
569
|
-
role: "combobox",
|
|
570
|
-
"aria-expanded": "true",
|
|
571
|
-
placeholder: "Search",
|
|
572
|
-
value: query,
|
|
573
|
-
onChange: (e) => setQuery(e.target.value),
|
|
574
|
-
onKeyDown: handleKeyDown,
|
|
575
|
-
className: "omni-search-input"
|
|
576
|
-
}),
|
|
577
|
-
/* @__PURE__ */ jsx("button", {
|
|
578
|
-
"aria-label": "Close",
|
|
579
|
-
className: "omni-close-btn",
|
|
580
|
-
onClick: () => setOpenWithAnalytics(false, "button"),
|
|
581
|
-
children: "ESC"
|
|
582
|
-
})
|
|
583
|
-
]
|
|
584
|
-
})
|
|
585
|
-
}),
|
|
586
|
-
/* @__PURE__ */ jsxs("div", {
|
|
587
|
-
className: "omni-body",
|
|
588
|
-
ref: listRef,
|
|
589
|
-
role: "listbox",
|
|
590
|
-
"aria-label": "Search results",
|
|
591
|
-
children: [
|
|
592
|
-
loading && /* @__PURE__ */ jsxs("div", {
|
|
593
|
-
className: "omni-loading",
|
|
594
|
-
children: [/* @__PURE__ */ jsx(LoaderIcon, {}), " Searching…"]
|
|
595
|
-
}),
|
|
596
|
-
recentItems.length > 0 && /* @__PURE__ */ jsxs("div", {
|
|
597
|
-
className: "omni-group",
|
|
598
|
-
children: [/* @__PURE__ */ jsx("div", {
|
|
599
|
-
className: "omni-group-label",
|
|
600
|
-
children: "Recent"
|
|
601
|
-
}), /* @__PURE__ */ jsx("div", {
|
|
602
|
-
className: "omni-group-items",
|
|
603
|
-
children: recentItems.map((item, i) => /* @__PURE__ */ jsx("button", {
|
|
604
|
-
"data-id": item.id,
|
|
605
|
-
role: "option",
|
|
606
|
-
"aria-selected": i === activeIndex,
|
|
607
|
-
onMouseEnter: () => setActiveIndex(i),
|
|
608
|
-
onClick: () => execute(item),
|
|
609
|
-
className: cn("omni-item", i === activeIndex && "omni-item-active"),
|
|
610
|
-
children: /* @__PURE__ */ jsxs("div", {
|
|
611
|
-
className: "omni-item-text",
|
|
612
|
-
children: [/* @__PURE__ */ jsx("div", {
|
|
613
|
-
className: "omni-item-subtitle",
|
|
614
|
-
children: item.subtitle
|
|
615
|
-
}), /* @__PURE__ */ jsx("div", {
|
|
616
|
-
className: "omni-item-label",
|
|
617
|
-
children: item.label
|
|
618
|
-
})]
|
|
619
|
-
})
|
|
620
|
-
}, item.id))
|
|
621
|
-
})]
|
|
622
|
-
}),
|
|
623
|
-
displayItems.length > 0 && /* @__PURE__ */ jsxs("div", {
|
|
624
|
-
className: "omni-group",
|
|
625
|
-
children: [/* @__PURE__ */ jsx("div", {
|
|
626
|
-
className: "omni-group-label",
|
|
627
|
-
children: "Documentation"
|
|
628
|
-
}), /* @__PURE__ */ jsx("div", {
|
|
629
|
-
className: "omni-group-items",
|
|
630
|
-
children: displayItems.map((item, i) => {
|
|
631
|
-
const idx = recentItems.length + i;
|
|
632
|
-
return /* @__PURE__ */ jsx("button", {
|
|
633
|
-
"data-id": item.id,
|
|
634
|
-
role: "option",
|
|
635
|
-
"aria-selected": idx === activeIndex,
|
|
636
|
-
onMouseEnter: () => setActiveIndex(idx),
|
|
637
|
-
onClick: () => execute(item),
|
|
638
|
-
className: cn("omni-item", idx === activeIndex && "omni-item-active"),
|
|
639
|
-
children: /* @__PURE__ */ jsxs("div", {
|
|
640
|
-
className: "omni-item-text",
|
|
641
|
-
children: [
|
|
642
|
-
/* @__PURE__ */ jsx("div", {
|
|
643
|
-
className: "omni-item-subtitle",
|
|
644
|
-
children: item.subtitle
|
|
645
|
-
}),
|
|
646
|
-
/* @__PURE__ */ jsx("div", {
|
|
647
|
-
className: "omni-item-label",
|
|
648
|
-
children: /* @__PURE__ */ jsx(HighlightedLabel, {
|
|
649
|
-
label: item.label,
|
|
650
|
-
indices: item.indices
|
|
651
|
-
})
|
|
652
|
-
}),
|
|
653
|
-
item.description && /* @__PURE__ */ jsx("div", {
|
|
654
|
-
className: "omni-item-description",
|
|
655
|
-
children: /* @__PURE__ */ jsx(HighlightedLabel, {
|
|
656
|
-
label: item.description,
|
|
657
|
-
indices: item.descriptionIndices
|
|
658
|
-
})
|
|
659
|
-
})
|
|
660
|
-
]
|
|
661
|
-
})
|
|
662
|
-
}, item.id);
|
|
663
|
-
})
|
|
664
|
-
})]
|
|
665
|
-
}),
|
|
666
|
-
allItems.length === 0 && !loading && /* @__PURE__ */ jsxs("div", {
|
|
667
|
-
className: "omni-empty",
|
|
668
|
-
children: [/* @__PURE__ */ jsx("div", {
|
|
669
|
-
className: "omni-empty-icon",
|
|
670
|
-
children: /* @__PURE__ */ jsx(HistoryIcon, {})
|
|
671
|
-
}), debouncedQuery && results.length > 0 ? `No ${FILTER_LABELS[filter].toLowerCase()} results found.` : debouncedQuery ? "No results found. Try a different query." : "Type to search the docs, or browse recent items."]
|
|
672
|
-
})
|
|
673
|
-
]
|
|
674
|
-
}),
|
|
675
|
-
/* @__PURE__ */ jsx("div", {
|
|
676
|
-
className: "omni-footer",
|
|
677
|
-
children: /* @__PURE__ */ jsxs("div", {
|
|
678
|
-
className: "omni-footer-inner",
|
|
679
|
-
children: [/* @__PURE__ */ jsxs("div", {
|
|
680
|
-
className: "omni-footer-hints",
|
|
681
|
-
children: [
|
|
682
|
-
/* @__PURE__ */ jsxs("span", {
|
|
683
|
-
className: "omni-footer-hint",
|
|
684
|
-
children: [/* @__PURE__ */ jsx(CornerDownLeftIcon, {}), " to select"]
|
|
685
|
-
}),
|
|
686
|
-
/* @__PURE__ */ jsxs("span", {
|
|
687
|
-
className: "omni-footer-hint",
|
|
688
|
-
children: [
|
|
689
|
-
/* @__PURE__ */ jsx(ArrowUpIcon, {}),
|
|
690
|
-
/* @__PURE__ */ jsx(ArrowDownIcon, {}),
|
|
691
|
-
" to navigate"
|
|
692
|
-
]
|
|
693
|
-
}),
|
|
694
|
-
/* @__PURE__ */ jsxs("span", {
|
|
695
|
-
className: "omni-footer-hint omni-footer-hint-desktop",
|
|
696
|
-
children: [/* @__PURE__ */ jsx("span", {
|
|
697
|
-
className: "omni-kbd-sm",
|
|
698
|
-
children: "ESC"
|
|
699
|
-
}), " to close"]
|
|
700
|
-
})
|
|
701
|
-
]
|
|
702
|
-
}), /* @__PURE__ */ jsxs("div", {
|
|
703
|
-
className: "omni-footer-filter",
|
|
704
|
-
children: [
|
|
705
|
-
/* @__PURE__ */ jsx("span", {
|
|
706
|
-
className: "omni-filter-label",
|
|
707
|
-
children: "Filter"
|
|
708
|
-
}),
|
|
709
|
-
/* @__PURE__ */ jsxs("button", {
|
|
710
|
-
type: "button",
|
|
711
|
-
className: "omni-filter-button",
|
|
712
|
-
"aria-expanded": filterOpen,
|
|
713
|
-
onClick: () => setFilterOpen((value) => !value),
|
|
714
|
-
children: [FILTER_LABELS[filter], /* @__PURE__ */ jsx(ArrowDownIcon, {})]
|
|
715
|
-
}),
|
|
716
|
-
filterOpen && /* @__PURE__ */ jsx("div", {
|
|
717
|
-
className: "omni-filter-menu",
|
|
718
|
-
role: "group",
|
|
719
|
-
"aria-label": "Search filter",
|
|
720
|
-
children: [
|
|
721
|
-
"all",
|
|
722
|
-
"pages",
|
|
723
|
-
"inside"
|
|
724
|
-
].map((mode) => /* @__PURE__ */ jsx("button", {
|
|
725
|
-
type: "button",
|
|
726
|
-
"aria-pressed": filter === mode,
|
|
727
|
-
className: cn("omni-filter-option", filter === mode && "omni-filter-option-active"),
|
|
728
|
-
onClick: () => updateFilter(mode),
|
|
729
|
-
children: FILTER_LABELS[mode]
|
|
730
|
-
}, mode))
|
|
731
|
-
})
|
|
732
|
-
]
|
|
733
|
-
})]
|
|
734
|
-
})
|
|
735
|
-
})
|
|
736
|
-
]
|
|
737
|
-
})] }), document.body);
|
|
55
|
+
return /* @__PURE__ */ jsx(DeferredDialog, {
|
|
56
|
+
open,
|
|
57
|
+
onOpenChange: setOpenWithAnalytics,
|
|
58
|
+
children: /* @__PURE__ */ jsx(CommandSearchDialog, {
|
|
59
|
+
api,
|
|
60
|
+
locale,
|
|
61
|
+
analytics,
|
|
62
|
+
open,
|
|
63
|
+
setOpen,
|
|
64
|
+
setOpenWithAnalytics
|
|
65
|
+
})
|
|
66
|
+
});
|
|
738
67
|
}
|
|
739
68
|
|
|
740
69
|
//#endregion
|