straplight 1.1.0 → 1.2.1

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.
@@ -4,7 +4,7 @@ const jsxRuntime = require("react/jsx-runtime");
4
4
  const react = require("react");
5
5
  const admin = require("@strapi/strapi/admin");
6
6
  const designSystem = require("@strapi/design-system");
7
- const index = require("./index-CuzgWWKQ.js");
7
+ const index = require("./index-Cii-JcUl.js");
8
8
  const DEFAULTS = {
9
9
  debounceMs: 200,
10
10
  minQueryLength: 1,
@@ -2,7 +2,7 @@ import { jsxs, jsx } from "react/jsx-runtime";
2
2
  import { useState, useEffect } from "react";
3
3
  import { useNotification, getFetchClient, Layouts } from "@strapi/strapi/admin";
4
4
  import { Box, Typography, Button, Flex, Grid, Field, NumberInput, Table, Thead, Tr, Th, Tbody, Td, Switch, MultiSelect, MultiSelectOption } from "@strapi/design-system";
5
- import { P as PLUGIN_ID } from "./index-B2B6CFFk.mjs";
5
+ import { P as PLUGIN_ID } from "./index-C5h4P4yg.mjs";
6
6
  const DEFAULTS = {
7
7
  debounceMs: 200,
8
8
  minQueryLength: 1,
@@ -0,0 +1,569 @@
1
+ import { useEffect, useState, useRef, useCallback } from "react";
2
+ import { useNavigate } from "react-router-dom";
3
+ import { jsxs, Fragment, jsx } from "react/jsx-runtime";
4
+ import { Box, Flex, Loader, Divider, Typography, DesignSystemProvider, darkTheme, lightTheme } from "@strapi/design-system";
5
+ import { getFetchClient } from "@strapi/strapi/admin";
6
+ import { Search } from "@strapi/icons";
7
+ const __variableDynamicImportRuntimeHelper = (glob, path, segs) => {
8
+ const v = glob[path];
9
+ if (v) {
10
+ return typeof v === "function" ? v() : Promise.resolve(v);
11
+ }
12
+ return new Promise((_, reject) => {
13
+ (typeof queueMicrotask === "function" ? queueMicrotask : setTimeout)(
14
+ reject.bind(
15
+ null,
16
+ new Error(
17
+ "Unknown variable dynamic import: " + path + (path.split("/").length !== segs ? ". Note that variables only represent file names one level deep." : "")
18
+ )
19
+ )
20
+ );
21
+ });
22
+ };
23
+ const PLUGIN_ID = "straplight";
24
+ function NavigateCapture() {
25
+ const navigate = useNavigate();
26
+ useEffect(() => {
27
+ window.__straplight = window.__straplight || {};
28
+ window.__straplight.navigate = navigate;
29
+ }, [navigate]);
30
+ return null;
31
+ }
32
+ const DEFAULTS = {
33
+ debounceMs: 200,
34
+ minQueryLength: 1
35
+ };
36
+ function useStraplightSettings() {
37
+ const [settings, setSettings] = useState(DEFAULTS);
38
+ useEffect(() => {
39
+ const { get } = getFetchClient();
40
+ get(`/${PLUGIN_ID}/settings`).then(({ data }) => {
41
+ setSettings({
42
+ debounceMs: data.settings?.debounceMs ?? DEFAULTS.debounceMs,
43
+ minQueryLength: data.settings?.minQueryLength ?? DEFAULTS.minQueryLength
44
+ });
45
+ }).catch(() => {
46
+ });
47
+ }, []);
48
+ return settings;
49
+ }
50
+ function useStraplight() {
51
+ const settings = useStraplightSettings();
52
+ const [isOpen, setIsOpen] = useState(false);
53
+ const [query, setQuery] = useState("");
54
+ const [results, setResults] = useState([]);
55
+ const [loading, setLoading] = useState(false);
56
+ const [selectedIndex, setSelectedIndex] = useState(0);
57
+ const debounceRef = useRef(null);
58
+ const requestIdRef = useRef(0);
59
+ useEffect(() => {
60
+ const handler = (e) => {
61
+ if ((e.metaKey || e.ctrlKey) && e.key === "k") {
62
+ e.preventDefault();
63
+ setIsOpen((prev) => !prev);
64
+ }
65
+ };
66
+ document.addEventListener("keydown", handler);
67
+ return () => document.removeEventListener("keydown", handler);
68
+ }, []);
69
+ const close = useCallback(() => {
70
+ setIsOpen(false);
71
+ setQuery("");
72
+ setResults([]);
73
+ setSelectedIndex(0);
74
+ setLoading(false);
75
+ requestIdRef.current++;
76
+ }, []);
77
+ useEffect(() => {
78
+ if (!isOpen) return;
79
+ if (debounceRef.current) {
80
+ clearTimeout(debounceRef.current);
81
+ }
82
+ const trimmed = query.trim();
83
+ if (trimmed.length < settings.minQueryLength) {
84
+ setResults([]);
85
+ setSelectedIndex(0);
86
+ setLoading(false);
87
+ return;
88
+ }
89
+ debounceRef.current = setTimeout(async () => {
90
+ const id = ++requestIdRef.current;
91
+ setLoading(true);
92
+ try {
93
+ const { get } = getFetchClient();
94
+ const { data } = await get(`/${PLUGIN_ID}/search`, {
95
+ params: { q: trimmed }
96
+ });
97
+ if (id !== requestIdRef.current) return;
98
+ setResults(data.results || []);
99
+ setSelectedIndex(0);
100
+ } catch {
101
+ if (id !== requestIdRef.current) return;
102
+ setResults([]);
103
+ } finally {
104
+ if (id === requestIdRef.current) {
105
+ setLoading(false);
106
+ }
107
+ }
108
+ }, settings.debounceMs);
109
+ return () => {
110
+ if (debounceRef.current) clearTimeout(debounceRef.current);
111
+ };
112
+ }, [query, isOpen, settings.debounceMs, settings.minQueryLength]);
113
+ const navigateToResult = useCallback(
114
+ (result) => {
115
+ const path = `/content-manager/collection-types/${result.uid}/${result.documentId}`;
116
+ const nav = window.__straplight?.navigate;
117
+ if (nav) {
118
+ nav(path);
119
+ } else {
120
+ window.location.href = `/admin${path}`;
121
+ }
122
+ close();
123
+ },
124
+ [close]
125
+ );
126
+ const onKeyDown = useCallback(
127
+ (e) => {
128
+ if (e.key === "Escape") {
129
+ e.preventDefault();
130
+ close();
131
+ } else if (e.key === "ArrowDown") {
132
+ e.preventDefault();
133
+ setSelectedIndex((i) => i < results.length - 1 ? i + 1 : 0);
134
+ } else if (e.key === "ArrowUp") {
135
+ e.preventDefault();
136
+ setSelectedIndex((i) => i > 0 ? i - 1 : results.length - 1);
137
+ } else if (e.key === "Enter" && results.length > 0) {
138
+ e.preventDefault();
139
+ navigateToResult(results[selectedIndex]);
140
+ }
141
+ },
142
+ [results, selectedIndex, navigateToResult, close]
143
+ );
144
+ return {
145
+ isOpen,
146
+ query,
147
+ setQuery,
148
+ results,
149
+ loading,
150
+ selectedIndex,
151
+ close,
152
+ onKeyDown,
153
+ navigateToResult
154
+ };
155
+ }
156
+ const OVERLAY_STYLES = `
157
+ #straplight-portal input::placeholder {
158
+ color: inherit;
159
+ opacity: 0.6;
160
+ }
161
+ @keyframes straplight-fade-in {
162
+ from { opacity: 0; }
163
+ to { opacity: 1; }
164
+ }
165
+ @keyframes straplight-slide-in {
166
+ from { opacity: 0; transform: translateX(-50%) translateY(16px); }
167
+ to { opacity: 1; transform: translateX(-50%); }
168
+ }
169
+ @keyframes straplight-spin {
170
+ to { transform: rotate(360deg); }
171
+ }
172
+ `;
173
+ function StraplightOverlay() {
174
+ const {
175
+ isOpen,
176
+ query,
177
+ setQuery,
178
+ results,
179
+ loading,
180
+ selectedIndex,
181
+ close,
182
+ onKeyDown,
183
+ navigateToResult
184
+ } = useStraplight();
185
+ const inputRef = useRef(null);
186
+ const listRef = useRef(null);
187
+ useEffect(() => {
188
+ if (isOpen && inputRef.current) {
189
+ inputRef.current.focus();
190
+ }
191
+ }, [isOpen]);
192
+ useEffect(() => {
193
+ if (!listRef.current) return;
194
+ const selected = listRef.current.children[selectedIndex];
195
+ if (selected) {
196
+ selected.scrollIntoView({ block: "nearest" });
197
+ }
198
+ }, [selectedIndex]);
199
+ if (!isOpen) return null;
200
+ const isMac = typeof navigator !== "undefined" && /mac/i.test(navigator.platform || navigator.userAgent);
201
+ const modKey = isMac ? "⌘" : "Ctrl";
202
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
203
+ /* @__PURE__ */ jsx("style", { children: OVERLAY_STYLES }),
204
+ /* @__PURE__ */ jsx(
205
+ Box,
206
+ {
207
+ onClick: close,
208
+ background: "rgba(0, 0, 0, 0.5)",
209
+ position: "fixed",
210
+ zIndex: 99999,
211
+ animation: "straplight-fade-in 0.15s ease-out",
212
+ style: {
213
+ inset: 0,
214
+ backdropFilter: "blur(4px)"
215
+ }
216
+ }
217
+ ),
218
+ /* @__PURE__ */ jsxs(
219
+ Box,
220
+ {
221
+ onKeyDown,
222
+ background: "neutral0",
223
+ shadow: "filterShadow",
224
+ borderRadius: "12px",
225
+ maxHeight: "440px",
226
+ maxWidth: "calc(100vw - 32px)",
227
+ overflow: "hidden",
228
+ position: "fixed",
229
+ top: "calc(50% - 50px)",
230
+ left: "50%",
231
+ width: "580px",
232
+ zIndex: 1e5,
233
+ animation: "straplight-slide-in 0.2s ease-out 0.05s both",
234
+ children: [
235
+ /* @__PURE__ */ jsx(
236
+ Box,
237
+ {
238
+ paddingLeft: 4,
239
+ paddingRight: 4,
240
+ paddingTop: 3,
241
+ paddingBottom: 3,
242
+ children: /* @__PURE__ */ jsxs(
243
+ Flex,
244
+ {
245
+ items: "center",
246
+ gap: "10px",
247
+ children: [
248
+ /* @__PURE__ */ jsx(
249
+ Box,
250
+ {
251
+ display: "contents",
252
+ color: "neutral400",
253
+ children: /* @__PURE__ */ jsx(Search, { width: 20, height: 20 })
254
+ }
255
+ ),
256
+ /* @__PURE__ */ jsx(
257
+ Box,
258
+ {
259
+ color: "neutral700",
260
+ flex: "1",
261
+ children: /* @__PURE__ */ jsx(
262
+ "input",
263
+ {
264
+ ref: inputRef,
265
+ type: "text",
266
+ value: query,
267
+ onChange: (e) => setQuery(e.target.value),
268
+ placeholder: "Search content...",
269
+ style: {
270
+ border: "none",
271
+ outline: "none",
272
+ fontSize: "16px",
273
+ fontFamily: "inherit",
274
+ color: "inherit",
275
+ backgroundColor: "transparent"
276
+ }
277
+ }
278
+ )
279
+ }
280
+ ),
281
+ loading && /* @__PURE__ */ jsx(Loader, { small: true })
282
+ ]
283
+ }
284
+ )
285
+ }
286
+ ),
287
+ /* @__PURE__ */ jsx(Divider, {}),
288
+ /* @__PURE__ */ jsxs(
289
+ Box,
290
+ {
291
+ flex: "1",
292
+ overflow: "auto",
293
+ ref: listRef,
294
+ padding: results.length > 0 ? 2 : 0,
295
+ children: [
296
+ query.trim() && !loading && results.length === 0 && /* @__PURE__ */ jsx(
297
+ Box,
298
+ {
299
+ color: "neutral600",
300
+ paddingLeft: 4,
301
+ paddingRight: 4,
302
+ paddingTop: 8,
303
+ paddingBottom: 8,
304
+ textAlign: "center",
305
+ children: /* @__PURE__ */ jsx(Typography, { children: "No results found" })
306
+ }
307
+ ),
308
+ results.map((result, index2) => /* @__PURE__ */ jsx(
309
+ ResultItem,
310
+ {
311
+ result,
312
+ isSelected: index2 === selectedIndex,
313
+ onClick: () => navigateToResult(result)
314
+ },
315
+ `${result.uid}-${result.documentId}`
316
+ ))
317
+ ]
318
+ }
319
+ ),
320
+ /* @__PURE__ */ jsx(Divider, {}),
321
+ /* @__PURE__ */ jsx(
322
+ Box,
323
+ {
324
+ paddingLeft: 4,
325
+ paddingRight: 4,
326
+ paddingTop: 3,
327
+ paddingBottom: 3,
328
+ children: /* @__PURE__ */ jsxs(
329
+ Flex,
330
+ {
331
+ color: "neutral600",
332
+ gap: "16px",
333
+ children: [
334
+ /* @__PURE__ */ jsxs(Typography, { fontSize: "12px", children: [
335
+ /* @__PURE__ */ jsx(Kbd, { children: "↑↓" }),
336
+ " navigate"
337
+ ] }),
338
+ /* @__PURE__ */ jsxs(Typography, { fontSize: "12px", children: [
339
+ /* @__PURE__ */ jsx(Kbd, { children: "⏎" }),
340
+ " open"
341
+ ] }),
342
+ /* @__PURE__ */ jsxs(Typography, { fontSize: "12px", children: [
343
+ /* @__PURE__ */ jsx(Kbd, { children: "esc" }),
344
+ " close"
345
+ ] }),
346
+ /* @__PURE__ */ jsxs(Typography, { fontSize: "12px", style: { marginLeft: "auto" }, children: [
347
+ /* @__PURE__ */ jsxs(Kbd, { children: [
348
+ modKey,
349
+ "+K"
350
+ ] }),
351
+ " toggle"
352
+ ] })
353
+ ]
354
+ }
355
+ )
356
+ }
357
+ )
358
+ ]
359
+ }
360
+ )
361
+ ] });
362
+ }
363
+ function ResultItem({
364
+ result,
365
+ isSelected,
366
+ onClick
367
+ }) {
368
+ const [hovered, setHovered] = useState(false);
369
+ return /* @__PURE__ */ jsx(
370
+ Box,
371
+ {
372
+ paddingLeft: 3,
373
+ paddingRight: 3,
374
+ paddingTop: 2,
375
+ paddingBottom: 2,
376
+ borderRadius: "8px",
377
+ transition: "background-color 0.1s",
378
+ cursor: "pointer",
379
+ background: isSelected || hovered ? "neutral150" : "transparent",
380
+ onClick,
381
+ onMouseEnter: () => setHovered(true),
382
+ onMouseLeave: () => setHovered(false),
383
+ children: /* @__PURE__ */ jsxs(
384
+ Flex,
385
+ {
386
+ alignItems: "center",
387
+ justifyContent: "space-between",
388
+ children: [
389
+ /* @__PURE__ */ jsx(
390
+ Box,
391
+ {
392
+ flex: "1",
393
+ overflow: "hidden",
394
+ marginRight: 3,
395
+ children: /* @__PURE__ */ jsxs(
396
+ Flex,
397
+ {
398
+ alignItems: "baseline",
399
+ gap: "8px",
400
+ children: [
401
+ /* @__PURE__ */ jsx(
402
+ Box,
403
+ {
404
+ shrink: "0",
405
+ minWidth: 0,
406
+ children: /* @__PURE__ */ jsx(
407
+ Typography,
408
+ {
409
+ fontWeight: isSelected ? 600 : 400,
410
+ style: {
411
+ overflow: "hidden",
412
+ textOverflow: "ellipsis",
413
+ whiteSpace: "nowrap"
414
+ },
415
+ children: result.label
416
+ }
417
+ )
418
+ }
419
+ ),
420
+ result.fields && result.fields.length > 0 && /* @__PURE__ */ jsx(
421
+ Box,
422
+ {
423
+ shrink: "1",
424
+ minWidth: 0,
425
+ children: /* @__PURE__ */ jsx(
426
+ Typography,
427
+ {
428
+ fontSize: "12px",
429
+ textColor: "neutral600",
430
+ style: {
431
+ overflow: "hidden",
432
+ textOverflow: "ellipsis",
433
+ whiteSpace: "nowrap"
434
+ },
435
+ children: result.fields.map((f) => f.value).join(" · ")
436
+ }
437
+ )
438
+ }
439
+ )
440
+ ]
441
+ }
442
+ )
443
+ }
444
+ ),
445
+ /* @__PURE__ */ jsx(
446
+ Box,
447
+ {
448
+ color: "neutral600",
449
+ background: "neutral150",
450
+ paddingLeft: 2,
451
+ paddingRight: 2,
452
+ paddingTop: 1,
453
+ paddingBottom: 1,
454
+ borderRadius: "4px",
455
+ shrink: "0",
456
+ children: /* @__PURE__ */ jsx(Typography, { fontSize: "11px", children: result.contentType })
457
+ }
458
+ )
459
+ ]
460
+ }
461
+ )
462
+ }
463
+ );
464
+ }
465
+ function Kbd({ children }) {
466
+ return /* @__PURE__ */ jsx(
467
+ Box,
468
+ {
469
+ display: "inline-block",
470
+ borderColor: "neutral200",
471
+ background: "neutral150",
472
+ borderRadius: "4px",
473
+ children: /* @__PURE__ */ jsx(
474
+ "kbd",
475
+ {
476
+ style: {
477
+ display: "inline-block",
478
+ padding: "1px 5px",
479
+ fontSize: "11px",
480
+ fontFamily: "inherit",
481
+ borderRadius: "4px"
482
+ },
483
+ children
484
+ }
485
+ )
486
+ }
487
+ );
488
+ }
489
+ function resolveIsDark() {
490
+ const stored = localStorage.getItem("STRAPI_THEME") || "system";
491
+ if (stored === "dark") return true;
492
+ if (stored === "light") return false;
493
+ return window.matchMedia("(prefers-color-scheme: dark)").matches;
494
+ }
495
+ function StraplightProvider() {
496
+ const [isDark, setIsDark] = useState(resolveIsDark);
497
+ useEffect(() => {
498
+ function update() {
499
+ setIsDark(resolveIsDark());
500
+ }
501
+ const mq = window.matchMedia("(prefers-color-scheme: dark)");
502
+ mq.addEventListener("change", update);
503
+ window.addEventListener("storage", update);
504
+ const originalSetItem = localStorage.setItem.bind(localStorage);
505
+ localStorage.setItem = function(key, value) {
506
+ originalSetItem(key, value);
507
+ if (key === "STRAPI_THEME") update();
508
+ };
509
+ return () => {
510
+ mq.removeEventListener("change", update);
511
+ window.removeEventListener("storage", update);
512
+ localStorage.setItem = originalSetItem;
513
+ };
514
+ }, []);
515
+ return /* @__PURE__ */ jsx(DesignSystemProvider, { theme: isDark ? darkTheme : lightTheme, children: /* @__PURE__ */ jsx(StraplightOverlay, {}) });
516
+ }
517
+ const index = {
518
+ register(app) {
519
+ app.registerPlugin({
520
+ id: PLUGIN_ID,
521
+ name: PLUGIN_ID
522
+ });
523
+ },
524
+ bootstrap(app) {
525
+ app.addSettingsLink("global", {
526
+ id: `${PLUGIN_ID}-settings`,
527
+ to: PLUGIN_ID,
528
+ intlLabel: {
529
+ id: `${PLUGIN_ID}.settings.link`,
530
+ defaultMessage: "Straplight"
531
+ },
532
+ permissions: [],
533
+ async Component() {
534
+ const { SettingsPage } = await import("./Settings-Chz6UwDr.mjs");
535
+ return { default: SettingsPage };
536
+ }
537
+ });
538
+ const contentManager = app.getPlugin("content-manager");
539
+ contentManager.injectComponent("listView", "actions", {
540
+ name: "straplight-nav-capture",
541
+ Component: NavigateCapture
542
+ });
543
+ import("react-dom/client").then(({ createRoot }) => {
544
+ import("react/jsx-runtime").then(({ jsx: jsx2 }) => {
545
+ const el = document.createElement("div");
546
+ el.id = "straplight-portal";
547
+ document.body.appendChild(el);
548
+ const root = createRoot(el);
549
+ root.render(jsx2(StraplightProvider, {}));
550
+ });
551
+ });
552
+ },
553
+ async registerTrads({ locales }) {
554
+ return Promise.all(
555
+ locales.map(async (locale) => {
556
+ try {
557
+ const { default: data } = await __variableDynamicImportRuntimeHelper(/* @__PURE__ */ Object.assign({ "./translations/en.json": () => import("./en-Byx4XI2L.mjs") }), `./translations/${locale}.json`, 3);
558
+ return { data, locale };
559
+ } catch {
560
+ return { data: {}, locale };
561
+ }
562
+ })
563
+ );
564
+ }
565
+ };
566
+ export {
567
+ PLUGIN_ID as P,
568
+ index as i
569
+ };