@tooee/renderers 0.8.0 → 0.9.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/dist/chooser/choose-filter.d.ts +13 -0
- package/dist/chooser/choose-filter.d.ts.map +1 -0
- package/dist/chooser/choose-filter.js +22 -0
- package/dist/chooser/choose-filter.js.map +1 -0
- package/dist/chooser/choose-highlighted-text.d.ts +9 -0
- package/dist/chooser/choose-highlighted-text.d.ts.map +1 -0
- package/dist/chooser/choose-highlighted-text.js +32 -0
- package/dist/chooser/choose-highlighted-text.js.map +1 -0
- package/dist/chooser/choose-list.d.ts +29 -0
- package/dist/chooser/choose-list.d.ts.map +1 -0
- package/dist/chooser/choose-list.js +66 -0
- package/dist/chooser/choose-list.js.map +1 -0
- package/dist/chooser/choose-store.d.ts +58 -0
- package/dist/chooser/choose-store.d.ts.map +1 -0
- package/dist/chooser/choose-store.js +102 -0
- package/dist/chooser/choose-store.js.map +1 -0
- package/dist/chooser/chooser.d.ts +18 -0
- package/dist/chooser/chooser.d.ts.map +1 -0
- package/dist/chooser/chooser.js +25 -0
- package/dist/chooser/chooser.js.map +1 -0
- package/dist/chooser/fuzzy.d.ts +9 -0
- package/dist/chooser/fuzzy.d.ts.map +1 -0
- package/dist/chooser/fuzzy.js +5 -0
- package/dist/chooser/fuzzy.js.map +1 -0
- package/dist/chooser/source.d.ts +5 -0
- package/dist/chooser/source.d.ts.map +1 -0
- package/dist/chooser/source.js +26 -0
- package/dist/chooser/source.js.map +1 -0
- package/dist/chooser/types.d.ts +22 -0
- package/dist/chooser/types.d.ts.map +1 -0
- package/dist/chooser/types.js +2 -0
- package/dist/chooser/types.js.map +1 -0
- package/dist/chooser/use-choose.d.ts +84 -0
- package/dist/chooser/use-choose.d.ts.map +1 -0
- package/dist/chooser/use-choose.js +382 -0
- package/dist/chooser/use-choose.js.map +1 -0
- package/dist/command-palette.d.ts.map +1 -1
- package/dist/command-palette.js +14 -68
- package/dist/command-palette.js.map +1 -1
- package/dist/index.d.ts +16 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -1
- package/package.json +8 -6
- package/src/chooser/choose-filter.tsx +66 -0
- package/src/chooser/choose-highlighted-text.tsx +49 -0
- package/src/chooser/choose-list.tsx +159 -0
- package/src/chooser/choose-store.ts +147 -0
- package/src/chooser/chooser.tsx +80 -0
- package/src/chooser/fuzzy.ts +13 -0
- package/src/chooser/source.ts +36 -0
- package/src/chooser/types.ts +28 -0
- package/src/chooser/use-choose.ts +540 -0
- package/src/command-palette.tsx +45 -148
- package/src/index.ts +39 -0
package/dist/index.js
CHANGED
|
@@ -6,6 +6,14 @@ export { CodeBlockChrome, defaultCodeBlockRenderer, mermaidCodeBlockRenderer, DE
|
|
|
6
6
|
export { CodeView } from "./code-view.js";
|
|
7
7
|
export { Table, computeColumnWidths, formatTableCell, isNumeric, sampleRows } from "./table.js";
|
|
8
8
|
export { CommandPalette } from "./command-palette.js";
|
|
9
|
+
export { Chooser } from "./chooser/chooser.js";
|
|
10
|
+
export { ChooseFilter } from "./chooser/choose-filter.js";
|
|
11
|
+
export { ChooseHighlightedText } from "./chooser/choose-highlighted-text.js";
|
|
12
|
+
export { ChooseList } from "./chooser/choose-list.js";
|
|
13
|
+
export { createChooseStore, selectActiveIndex, selectError, selectFilterQuery, selectItems, selectLoading, selectMatches, selectReloadRevision, selectSelectedOriginalIndices, } from "./chooser/choose-store.js";
|
|
14
|
+
export { fuzzyFilter } from "./chooser/fuzzy.js";
|
|
15
|
+
export { loadChooseSource } from "./chooser/source.js";
|
|
16
|
+
export { useChoose } from "./chooser/use-choose.js";
|
|
9
17
|
export { ContextMenu } from "./context-menu.js";
|
|
10
18
|
export { parseCSV, parseTSV, parseJSON, parseAuto, detectFormat } from "./parsers.js";
|
|
11
19
|
export { RowDocumentRenderable } from "./row-document-renderable.js";
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAElD,OAAO,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAEzE,OAAO,EACL,uBAAuB,EACvB,0BAA0B,EAC1B,mBAAmB,GACpB,MAAM,sBAAsB,CAAC;AAE9B,OAAO,EAAE,WAAW,EAAE,iBAAiB,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAQ1E,OAAO,EACL,eAAe,EACf,wBAAwB,EACxB,wBAAwB,EACxB,4BAA4B,EAC5B,YAAY,GACb,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,EAAE,KAAK,EAAE,mBAAmB,EAAE,eAAe,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAGhG,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAEtD,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAEhD,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,SAAS,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAEtF,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAC;AACrE,OAAO,EACL,yBAAyB,EACzB,6BAA6B,GAC9B,MAAM,8BAA8B,CAAC;AAQtC,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAElD,OAAO,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAEzE,OAAO,EACL,uBAAuB,EACvB,0BAA0B,EAC1B,mBAAmB,GACpB,MAAM,sBAAsB,CAAC;AAE9B,OAAO,EAAE,WAAW,EAAE,iBAAiB,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAQ1E,OAAO,EACL,eAAe,EACf,wBAAwB,EACxB,wBAAwB,EACxB,4BAA4B,EAC5B,YAAY,GACb,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,EAAE,KAAK,EAAE,mBAAmB,EAAE,eAAe,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAGhG,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAEtD,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAE/C,OAAO,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAE1D,OAAO,EAAE,qBAAqB,EAAE,MAAM,sCAAsC,CAAC;AAE7E,OAAO,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC;AAEtD,OAAO,EACL,iBAAiB,EACjB,iBAAiB,EACjB,WAAW,EACX,iBAAiB,EACjB,WAAW,EACX,aAAa,EACb,aAAa,EACb,oBAAoB,EACpB,6BAA6B,GAC9B,MAAM,2BAA2B,CAAC;AAEnC,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAEjD,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AACvD,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AAgBpD,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAEhD,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,SAAS,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAEtF,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAC;AACrE,OAAO,EACL,yBAAyB,EACzB,6BAA6B,GAC9B,MAAM,8BAA8B,CAAC;AAQtC,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tooee/renderers",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"description": "Content renderers (markdown, code, text, table) for Tooee",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cli",
|
|
@@ -45,11 +45,13 @@
|
|
|
45
45
|
"typecheck": "tsc --noEmit"
|
|
46
46
|
},
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"@tooee/commands": "0.
|
|
49
|
-
"@tooee/fuzzy": "0.
|
|
50
|
-
"@tooee/layout": "0.
|
|
51
|
-
"@tooee/marks": "0.
|
|
52
|
-
"@tooee/themes": "0.
|
|
48
|
+
"@tooee/commands": "0.9.0",
|
|
49
|
+
"@tooee/fuzzy": "0.9.0",
|
|
50
|
+
"@tooee/layout": "0.9.0",
|
|
51
|
+
"@tooee/marks": "0.9.0",
|
|
52
|
+
"@tooee/themes": "0.9.0",
|
|
53
|
+
"@xstate/store": "^4",
|
|
54
|
+
"@xstate/store-react": "^2",
|
|
53
55
|
"beautiful-mermaid": "^1.1.3",
|
|
54
56
|
"marked": "^17.0.4"
|
|
55
57
|
},
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { ReactNode } from "react";
|
|
2
|
+
import { CloseButton, decodeReactContent } from "@tooee/layout";
|
|
3
|
+
import { useTheme } from "@tooee/themes";
|
|
4
|
+
import type { UseChooseResult } from "./use-choose.js";
|
|
5
|
+
|
|
6
|
+
export interface ChooseFilterProps {
|
|
7
|
+
choose: UseChooseResult;
|
|
8
|
+
prompt?: ReactNode;
|
|
9
|
+
placeholder?: string;
|
|
10
|
+
right?: ReactNode;
|
|
11
|
+
onClose?: () => void;
|
|
12
|
+
count?: "matches" | "ratio" | false;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** Controlled filter row shared by fullscreen and overlay chooser assemblies. */
|
|
16
|
+
export const ChooseFilter = function ChooseFilter({
|
|
17
|
+
choose,
|
|
18
|
+
prompt = "> ",
|
|
19
|
+
placeholder = "Filter...",
|
|
20
|
+
right,
|
|
21
|
+
onClose,
|
|
22
|
+
count = "ratio",
|
|
23
|
+
}: ChooseFilterProps): ReactNode {
|
|
24
|
+
const { theme } = useTheme();
|
|
25
|
+
const { state, view } = choose;
|
|
26
|
+
const handleFilterInput = view.onFilterInput;
|
|
27
|
+
const handleFilterKeyDown = view.onFilterKeyDown;
|
|
28
|
+
const promptContent = decodeReactContent(prompt);
|
|
29
|
+
let renderedPrompt: ReactNode = null;
|
|
30
|
+
if (promptContent.kind === "string") {
|
|
31
|
+
renderedPrompt = <text content={promptContent.value} fg={theme.accent} />;
|
|
32
|
+
} else if (promptContent.kind === "node") {
|
|
33
|
+
renderedPrompt = promptContent.value;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return (
|
|
37
|
+
<box flexDirection="row" height={1} style={{ paddingLeft: 1, paddingRight: 1 }}>
|
|
38
|
+
{renderedPrompt}
|
|
39
|
+
<input
|
|
40
|
+
ref={view.filterRef}
|
|
41
|
+
focused={view.filterFocused}
|
|
42
|
+
value={state.filterQuery}
|
|
43
|
+
placeholder={placeholder}
|
|
44
|
+
onInput={handleFilterInput}
|
|
45
|
+
onKeyDown={handleFilterKeyDown}
|
|
46
|
+
backgroundColor="transparent"
|
|
47
|
+
textColor={theme.text}
|
|
48
|
+
placeholderColor={theme.textMuted}
|
|
49
|
+
cursorColor={theme.primary}
|
|
50
|
+
style={{ flexGrow: 1 }}
|
|
51
|
+
/>
|
|
52
|
+
{count !== false && (
|
|
53
|
+
<text
|
|
54
|
+
content={
|
|
55
|
+
count === "matches"
|
|
56
|
+
? ` ${state.matches.length}`
|
|
57
|
+
: ` ${state.matches.length}/${state.items.length}`
|
|
58
|
+
}
|
|
59
|
+
fg={theme.textMuted}
|
|
60
|
+
/>
|
|
61
|
+
)}
|
|
62
|
+
{right}
|
|
63
|
+
{onClose && <CloseButton onClose={onClose} />}
|
|
64
|
+
</box>
|
|
65
|
+
);
|
|
66
|
+
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { Fragment } from "react";
|
|
2
|
+
import type { ReactNode } from "react";
|
|
3
|
+
|
|
4
|
+
export interface ChooseHighlightedTextProps {
|
|
5
|
+
text: string;
|
|
6
|
+
positions: number[];
|
|
7
|
+
highlightColor: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** Render fuzzy-match characters with one shared segmentation implementation. */
|
|
11
|
+
export const ChooseHighlightedText = function ChooseHighlightedText({
|
|
12
|
+
text,
|
|
13
|
+
positions,
|
|
14
|
+
highlightColor,
|
|
15
|
+
}: ChooseHighlightedTextProps): ReactNode {
|
|
16
|
+
if (positions.length === 0) {
|
|
17
|
+
return text;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const positionSet = new Set(positions);
|
|
21
|
+
const parts: { text: string; highlighted: boolean }[] = [];
|
|
22
|
+
let current = "";
|
|
23
|
+
let currentHighlighted = false;
|
|
24
|
+
|
|
25
|
+
for (let index = 0; index < text.length; index += 1) {
|
|
26
|
+
const highlighted = positionSet.has(index);
|
|
27
|
+
if (index === 0) {
|
|
28
|
+
current = text[index];
|
|
29
|
+
currentHighlighted = highlighted;
|
|
30
|
+
} else if (highlighted === currentHighlighted) {
|
|
31
|
+
current += text[index];
|
|
32
|
+
} else {
|
|
33
|
+
parts.push({ highlighted: currentHighlighted, text: current });
|
|
34
|
+
current = text[index];
|
|
35
|
+
currentHighlighted = highlighted;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (current) {
|
|
39
|
+
parts.push({ highlighted: currentHighlighted, text: current });
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return parts.map(
|
|
43
|
+
(part, index): ReactNode => (
|
|
44
|
+
<Fragment key={index}>
|
|
45
|
+
{part.highlighted ? <span fg={highlightColor}>{part.text}</span> : part.text}
|
|
46
|
+
</Fragment>
|
|
47
|
+
),
|
|
48
|
+
);
|
|
49
|
+
};
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { useEffect, useRef } from "react";
|
|
2
|
+
import type { ReactNode } from "react";
|
|
3
|
+
import { decodeReactContent } from "@tooee/layout";
|
|
4
|
+
import type { ScrollBoxRenderable } from "@opentui/core";
|
|
5
|
+
import { useTheme } from "@tooee/themes";
|
|
6
|
+
import { ChooseHighlightedText } from "./choose-highlighted-text.js";
|
|
7
|
+
import type { FuzzyMatch } from "./fuzzy.js";
|
|
8
|
+
import type { ChooseItem } from "./types.js";
|
|
9
|
+
import type { UseChooseResult } from "./use-choose.js";
|
|
10
|
+
|
|
11
|
+
export interface ChooseItemRenderContext {
|
|
12
|
+
item: ChooseItem;
|
|
13
|
+
match: FuzzyMatch;
|
|
14
|
+
index: number;
|
|
15
|
+
originalIndex: number;
|
|
16
|
+
positions: number[];
|
|
17
|
+
isActive: boolean;
|
|
18
|
+
isSelected: boolean;
|
|
19
|
+
defaultContent: ReactNode;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface ChooseListProps {
|
|
23
|
+
choose: UseChooseResult;
|
|
24
|
+
/** Fullscreen defaults to activate; picker overlays default to submit. */
|
|
25
|
+
rowClick?: "activate" | "submit" | "none";
|
|
26
|
+
renderItem?: (context: ChooseItemRenderContext) => ReactNode;
|
|
27
|
+
loadingContent?: ReactNode;
|
|
28
|
+
errorContent?: (error: string) => ReactNode;
|
|
29
|
+
emptyContent?: ReactNode;
|
|
30
|
+
rowHeight?: number;
|
|
31
|
+
/** Mouse guard for visible rows covered by another surface. */
|
|
32
|
+
suspended?: boolean;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const ThemedLine = function ThemedLine({
|
|
36
|
+
content,
|
|
37
|
+
color,
|
|
38
|
+
}: {
|
|
39
|
+
content: ReactNode;
|
|
40
|
+
color: string;
|
|
41
|
+
}): ReactNode {
|
|
42
|
+
const decoded = decodeReactContent(content);
|
|
43
|
+
let rendered: ReactNode = null;
|
|
44
|
+
if (decoded.kind === "string") {
|
|
45
|
+
rendered = <text content={decoded.value} fg={color} />;
|
|
46
|
+
} else if (decoded.kind === "node") {
|
|
47
|
+
rendered = decoded.value;
|
|
48
|
+
}
|
|
49
|
+
return (
|
|
50
|
+
<box height={1} style={{ paddingLeft: 2 }}>
|
|
51
|
+
{rendered}
|
|
52
|
+
</box>
|
|
53
|
+
);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const defaultErrorContent = function defaultErrorContent(error: string): ReactNode {
|
|
57
|
+
return `Error: ${error}`;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
/** Scrollable chooser rows plus loading/error/empty presentation. */
|
|
61
|
+
export const ChooseList = function ChooseList({
|
|
62
|
+
choose,
|
|
63
|
+
rowClick = "activate",
|
|
64
|
+
renderItem,
|
|
65
|
+
loadingContent = "Loading...",
|
|
66
|
+
errorContent = defaultErrorContent,
|
|
67
|
+
emptyContent,
|
|
68
|
+
rowHeight,
|
|
69
|
+
suspended,
|
|
70
|
+
}: ChooseListProps): ReactNode {
|
|
71
|
+
const { theme } = useTheme();
|
|
72
|
+
const scrollRef = useRef<ScrollBoxRenderable>(null);
|
|
73
|
+
const { state, controller } = choose;
|
|
74
|
+
const interactionSuspended = choose.view.suspended || (suspended ?? false);
|
|
75
|
+
|
|
76
|
+
useEffect(() => {
|
|
77
|
+
const activeMatch = state.matches[state.activeIndex];
|
|
78
|
+
if (scrollRef.current && activeMatch !== undefined) {
|
|
79
|
+
scrollRef.current.scrollChildIntoView(`choose-item-${activeMatch.originalIndex}`);
|
|
80
|
+
}
|
|
81
|
+
}, [state.activeIndex, state.matches]);
|
|
82
|
+
|
|
83
|
+
return (
|
|
84
|
+
<scrollbox ref={scrollRef} style={{ flexGrow: 1 }} focused={false}>
|
|
85
|
+
{state.loading && <ThemedLine content={loadingContent} color={theme.textMuted} />}
|
|
86
|
+
|
|
87
|
+
{!state.loading && state.error !== null && state.error !== "" && (
|
|
88
|
+
<ThemedLine content={errorContent(state.error)} color={theme.error} />
|
|
89
|
+
)}
|
|
90
|
+
|
|
91
|
+
{!state.loading &&
|
|
92
|
+
(state.error?.length ?? 0) === 0 &&
|
|
93
|
+
state.matches.length === 0 &&
|
|
94
|
+
emptyContent !== null &&
|
|
95
|
+
emptyContent !== undefined && <ThemedLine content={emptyContent} color={theme.textMuted} />}
|
|
96
|
+
|
|
97
|
+
{state.matches.map((match, index): ReactNode => {
|
|
98
|
+
const isActive = index === state.activeIndex;
|
|
99
|
+
const isSelected = state.selectedOriginalIndices.has(match.originalIndex);
|
|
100
|
+
const defaultContent = (
|
|
101
|
+
<text fg={isActive ? theme.primary : theme.text} style={{ flexGrow: 1 }}>
|
|
102
|
+
{state.multi && (
|
|
103
|
+
<span fg={isSelected ? theme.accent : theme.textMuted}>
|
|
104
|
+
{isSelected ? "✓ " : " "}
|
|
105
|
+
</span>
|
|
106
|
+
)}
|
|
107
|
+
{(match.item.icon?.length ?? 0) > 0 && (
|
|
108
|
+
<span fg={theme.textMuted}>{`${match.item.icon} `}</span>
|
|
109
|
+
)}
|
|
110
|
+
<ChooseHighlightedText
|
|
111
|
+
text={match.item.text}
|
|
112
|
+
positions={match.positions}
|
|
113
|
+
highlightColor={theme.warning}
|
|
114
|
+
/>
|
|
115
|
+
{(match.item.description?.length ?? 0) > 0 && (
|
|
116
|
+
<span fg={theme.textMuted}>{` ${match.item.description}`}</span>
|
|
117
|
+
)}
|
|
118
|
+
</text>
|
|
119
|
+
);
|
|
120
|
+
const context: ChooseItemRenderContext = {
|
|
121
|
+
defaultContent,
|
|
122
|
+
index,
|
|
123
|
+
isActive,
|
|
124
|
+
isSelected,
|
|
125
|
+
item: match.item,
|
|
126
|
+
match,
|
|
127
|
+
originalIndex: match.originalIndex,
|
|
128
|
+
positions: match.positions,
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
return (
|
|
132
|
+
<box
|
|
133
|
+
key={match.originalIndex}
|
|
134
|
+
id={`choose-item-${match.originalIndex}`}
|
|
135
|
+
flexDirection="row"
|
|
136
|
+
height={rowHeight}
|
|
137
|
+
backgroundColor={isActive ? theme.backgroundElement : undefined}
|
|
138
|
+
style={{ paddingLeft: 1, paddingRight: 1 }}
|
|
139
|
+
onMouseDown={(event) => {
|
|
140
|
+
if (interactionSuspended || event.button !== 0 || rowClick === "none") {
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
if (rowClick === "submit") {
|
|
144
|
+
event.preventDefault();
|
|
145
|
+
event.stopPropagation();
|
|
146
|
+
controller.setActiveIndex(index);
|
|
147
|
+
controller.submit();
|
|
148
|
+
} else {
|
|
149
|
+
controller.setActiveIndex(index);
|
|
150
|
+
}
|
|
151
|
+
}}
|
|
152
|
+
>
|
|
153
|
+
{renderItem ? renderItem(context) : defaultContent}
|
|
154
|
+
</box>
|
|
155
|
+
);
|
|
156
|
+
})}
|
|
157
|
+
</scrollbox>
|
|
158
|
+
);
|
|
159
|
+
};
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { createStore } from "@xstate/store";
|
|
2
|
+
import { fuzzyFilter } from "./fuzzy.js";
|
|
3
|
+
import type { FuzzyMatch } from "./fuzzy.js";
|
|
4
|
+
import type { ChooseItem } from "./types.js";
|
|
5
|
+
|
|
6
|
+
export interface ChooseStoreContext {
|
|
7
|
+
items: ChooseItem[];
|
|
8
|
+
matches: FuzzyMatch[];
|
|
9
|
+
filterQuery: string;
|
|
10
|
+
activeIndex: number;
|
|
11
|
+
selectedOriginalIndices: ReadonlySet<number>;
|
|
12
|
+
loading: boolean;
|
|
13
|
+
error: string | null;
|
|
14
|
+
requestId: number;
|
|
15
|
+
reloadRevision: number;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface ChooseStoreEventDefinitions {
|
|
19
|
+
filterChanged: { query: string };
|
|
20
|
+
activeIndexSet: { index: number };
|
|
21
|
+
moved: { delta: number };
|
|
22
|
+
activeToggled: Record<never, never>;
|
|
23
|
+
requestStarted: Record<never, never>;
|
|
24
|
+
loadingStarted: { requestId: number };
|
|
25
|
+
loadSucceeded: { requestId: number; items: ChooseItem[] };
|
|
26
|
+
loadFailed: { requestId: number; error: string };
|
|
27
|
+
reloadRequested: Record<never, never>;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export type ChooseStoreEvents = {
|
|
31
|
+
[EventName in keyof ChooseStoreEventDefinitions]: ChooseStoreEventDefinitions[EventName];
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const replaceItems = function replaceItems(
|
|
35
|
+
ctx: ChooseStoreContext,
|
|
36
|
+
items: ChooseItem[],
|
|
37
|
+
): ChooseStoreContext {
|
|
38
|
+
return {
|
|
39
|
+
...ctx,
|
|
40
|
+
activeIndex: 0,
|
|
41
|
+
items,
|
|
42
|
+
matches: fuzzyFilter(items, ctx.filterQuery),
|
|
43
|
+
selectedOriginalIndices: new Set(),
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const clampActiveIndex = function clampActiveIndex(index: number, matchCount: number): number {
|
|
48
|
+
return Math.min(Math.max(0, matchCount - 1), Math.max(0, index));
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export const createChooseStore = function createChooseStore(options: {
|
|
52
|
+
items?: ChooseItem[];
|
|
53
|
+
initialFilter?: string;
|
|
54
|
+
initialActiveIndex?: number;
|
|
55
|
+
loading?: boolean;
|
|
56
|
+
}) {
|
|
57
|
+
const items = options.items ?? [];
|
|
58
|
+
const filterQuery = options.initialFilter ?? "";
|
|
59
|
+
|
|
60
|
+
return createStore<ChooseStoreContext, ChooseStoreEvents>({
|
|
61
|
+
context: {
|
|
62
|
+
activeIndex: clampActiveIndex(options.initialActiveIndex ?? 0, items.length),
|
|
63
|
+
error: null,
|
|
64
|
+
filterQuery,
|
|
65
|
+
items,
|
|
66
|
+
loading: options.loading ?? false,
|
|
67
|
+
matches: fuzzyFilter(items, filterQuery),
|
|
68
|
+
reloadRevision: 0,
|
|
69
|
+
requestId: 0,
|
|
70
|
+
selectedOriginalIndices: new Set(),
|
|
71
|
+
},
|
|
72
|
+
on: {
|
|
73
|
+
activeIndexSet: (ctx, event) => {
|
|
74
|
+
const activeIndex = clampActiveIndex(event.index, ctx.matches.length);
|
|
75
|
+
return activeIndex === ctx.activeIndex ? ctx : { ...ctx, activeIndex };
|
|
76
|
+
},
|
|
77
|
+
activeToggled: (ctx) => {
|
|
78
|
+
const originalIndex = ctx.matches[ctx.activeIndex]?.originalIndex;
|
|
79
|
+
if (originalIndex === undefined) {
|
|
80
|
+
return ctx;
|
|
81
|
+
}
|
|
82
|
+
const selectedOriginalIndices = new Set(ctx.selectedOriginalIndices);
|
|
83
|
+
if (selectedOriginalIndices.has(originalIndex)) {
|
|
84
|
+
selectedOriginalIndices.delete(originalIndex);
|
|
85
|
+
} else {
|
|
86
|
+
selectedOriginalIndices.add(originalIndex);
|
|
87
|
+
}
|
|
88
|
+
return { ...ctx, selectedOriginalIndices };
|
|
89
|
+
},
|
|
90
|
+
filterChanged: (ctx, event) => {
|
|
91
|
+
if (event.query === ctx.filterQuery) {
|
|
92
|
+
return ctx;
|
|
93
|
+
}
|
|
94
|
+
return {
|
|
95
|
+
...ctx,
|
|
96
|
+
activeIndex: 0,
|
|
97
|
+
filterQuery: event.query,
|
|
98
|
+
matches: fuzzyFilter(ctx.items, event.query),
|
|
99
|
+
};
|
|
100
|
+
},
|
|
101
|
+
loadFailed: (ctx, event) => {
|
|
102
|
+
if (event.requestId !== ctx.requestId) {
|
|
103
|
+
return ctx;
|
|
104
|
+
}
|
|
105
|
+
return {
|
|
106
|
+
...replaceItems(ctx, []),
|
|
107
|
+
error: event.error,
|
|
108
|
+
loading: false,
|
|
109
|
+
};
|
|
110
|
+
},
|
|
111
|
+
loadSucceeded: (ctx, event) => {
|
|
112
|
+
if (event.requestId !== ctx.requestId) {
|
|
113
|
+
return ctx;
|
|
114
|
+
}
|
|
115
|
+
return {
|
|
116
|
+
...replaceItems(ctx, event.items),
|
|
117
|
+
error: null,
|
|
118
|
+
loading: false,
|
|
119
|
+
};
|
|
120
|
+
},
|
|
121
|
+
loadingStarted: (ctx, event) =>
|
|
122
|
+
event.requestId === ctx.requestId && !ctx.loading ? { ...ctx, loading: true } : ctx,
|
|
123
|
+
moved: (ctx, event) => {
|
|
124
|
+
const activeIndex = clampActiveIndex(ctx.activeIndex + event.delta, ctx.matches.length);
|
|
125
|
+
return activeIndex === ctx.activeIndex ? ctx : { ...ctx, activeIndex };
|
|
126
|
+
},
|
|
127
|
+
reloadRequested: (ctx) => ({ ...ctx, reloadRevision: ctx.reloadRevision + 1 }),
|
|
128
|
+
requestStarted: (ctx) => ({
|
|
129
|
+
...ctx,
|
|
130
|
+
error: null,
|
|
131
|
+
requestId: ctx.requestId + 1,
|
|
132
|
+
}),
|
|
133
|
+
},
|
|
134
|
+
});
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
export type ChooseStore = ReturnType<typeof createChooseStore>;
|
|
138
|
+
|
|
139
|
+
export const selectItems = (ctx: ChooseStoreContext): ChooseItem[] => ctx.items;
|
|
140
|
+
export const selectMatches = (ctx: ChooseStoreContext): FuzzyMatch[] => ctx.matches;
|
|
141
|
+
export const selectFilterQuery = (ctx: ChooseStoreContext): string => ctx.filterQuery;
|
|
142
|
+
export const selectActiveIndex = (ctx: ChooseStoreContext): number => ctx.activeIndex;
|
|
143
|
+
export const selectSelectedOriginalIndices = (ctx: ChooseStoreContext): ReadonlySet<number> =>
|
|
144
|
+
ctx.selectedOriginalIndices;
|
|
145
|
+
export const selectLoading = (ctx: ChooseStoreContext): boolean => ctx.loading;
|
|
146
|
+
export const selectError = (ctx: ChooseStoreContext): string | null => ctx.error;
|
|
147
|
+
export const selectReloadRevision = (ctx: ChooseStoreContext): number => ctx.reloadRevision;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import type { ReactNode } from "react";
|
|
2
|
+
import { useTheme } from "@tooee/themes";
|
|
3
|
+
import { ChooseFilter } from "./choose-filter.js";
|
|
4
|
+
import { ChooseList } from "./choose-list.js";
|
|
5
|
+
import type { ChooseListProps } from "./choose-list.js";
|
|
6
|
+
import type { ChooseItem } from "./types.js";
|
|
7
|
+
import { useChoose } from "./use-choose.js";
|
|
8
|
+
|
|
9
|
+
export interface ChooserProps {
|
|
10
|
+
items: ChooseItem[];
|
|
11
|
+
commandScope: string;
|
|
12
|
+
prompt: ReactNode;
|
|
13
|
+
placeholder?: string;
|
|
14
|
+
initialActiveIndex?: number;
|
|
15
|
+
onActiveChange?: (item: ChooseItem | undefined) => void;
|
|
16
|
+
onSelect: (item: ChooseItem) => void | Promise<void>;
|
|
17
|
+
onCancel: () => void;
|
|
18
|
+
renderItem?: ChooseListProps["renderItem"];
|
|
19
|
+
emptyContent?: ReactNode;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Shared modal filtered-list assembly used by shell chooser surfaces. */
|
|
23
|
+
export const Chooser = function Chooser({
|
|
24
|
+
items,
|
|
25
|
+
commandScope,
|
|
26
|
+
prompt,
|
|
27
|
+
placeholder,
|
|
28
|
+
initialActiveIndex,
|
|
29
|
+
onActiveChange,
|
|
30
|
+
onSelect,
|
|
31
|
+
onCancel,
|
|
32
|
+
renderItem,
|
|
33
|
+
emptyContent,
|
|
34
|
+
}: ChooserProps): ReactNode {
|
|
35
|
+
const { theme } = useTheme();
|
|
36
|
+
const choose = useChoose({
|
|
37
|
+
commandScope,
|
|
38
|
+
escapeToCursor: false,
|
|
39
|
+
initialActiveIndex,
|
|
40
|
+
onActiveChange,
|
|
41
|
+
onCancel,
|
|
42
|
+
onSubmit: async ({ items: selectedItems }) => {
|
|
43
|
+
const [item] = selectedItems;
|
|
44
|
+
if (item !== undefined) {
|
|
45
|
+
await onSelect(item);
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
source: items,
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
return (
|
|
52
|
+
<box
|
|
53
|
+
position="absolute"
|
|
54
|
+
left="20%"
|
|
55
|
+
right="20%"
|
|
56
|
+
top={2}
|
|
57
|
+
maxHeight="60%"
|
|
58
|
+
flexDirection="column"
|
|
59
|
+
backgroundColor={theme.backgroundPanel}
|
|
60
|
+
border
|
|
61
|
+
borderColor={theme.border}
|
|
62
|
+
>
|
|
63
|
+
<ChooseFilter
|
|
64
|
+
choose={choose}
|
|
65
|
+
prompt={prompt}
|
|
66
|
+
placeholder={placeholder}
|
|
67
|
+
count="matches"
|
|
68
|
+
onClose={onCancel}
|
|
69
|
+
/>
|
|
70
|
+
<box height={1} width="100%" backgroundColor={theme.border} />
|
|
71
|
+
<ChooseList
|
|
72
|
+
choose={choose}
|
|
73
|
+
rowClick="submit"
|
|
74
|
+
rowHeight={1}
|
|
75
|
+
renderItem={renderItem}
|
|
76
|
+
emptyContent={emptyContent}
|
|
77
|
+
/>
|
|
78
|
+
</box>
|
|
79
|
+
);
|
|
80
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { rankBy } from "@tooee/fuzzy";
|
|
2
|
+
import type { ChooseItem } from "./types.js";
|
|
3
|
+
|
|
4
|
+
export interface FuzzyMatch {
|
|
5
|
+
item: ChooseItem;
|
|
6
|
+
originalIndex: number;
|
|
7
|
+
score: number;
|
|
8
|
+
positions: number[];
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export const fuzzyFilter = function fuzzyFilter(items: ChooseItem[], query: string): FuzzyMatch[] {
|
|
12
|
+
return rankBy(items, query, (item) => item.text);
|
|
13
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { ChooseContentProvider, ChooseItem, ChooseSource } from "./types.js";
|
|
2
|
+
|
|
3
|
+
type DecodedChooseSource =
|
|
4
|
+
| { kind: "items"; items: ChooseItem[] }
|
|
5
|
+
| { kind: "loader"; load: () => ChooseItem[] | Promise<ChooseItem[]> }
|
|
6
|
+
| { kind: "provider"; provider: ChooseContentProvider };
|
|
7
|
+
|
|
8
|
+
const decodeChooseSource = function decodeChooseSource(source: ChooseSource): DecodedChooseSource {
|
|
9
|
+
if (Array.isArray(source)) {
|
|
10
|
+
return { items: source, kind: "items" };
|
|
11
|
+
}
|
|
12
|
+
// oxlint-disable-next-line anti-slop/no-runtime-typeof -- public source boundary distinguishes the documented loader callback from a provider
|
|
13
|
+
if (typeof source === "function") {
|
|
14
|
+
return { kind: "loader", load: source };
|
|
15
|
+
}
|
|
16
|
+
return { kind: "provider", provider: source };
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/** Resolve every public Choose source shape through one compatibility boundary. */
|
|
20
|
+
export const loadChooseSource = function loadChooseSource(
|
|
21
|
+
source: ChooseSource,
|
|
22
|
+
): ChooseItem[] | Promise<ChooseItem[]> {
|
|
23
|
+
const decoded = decodeChooseSource(source);
|
|
24
|
+
if (decoded.kind === "items") {
|
|
25
|
+
return decoded.items;
|
|
26
|
+
}
|
|
27
|
+
if (decoded.kind === "loader") {
|
|
28
|
+
return decoded.load();
|
|
29
|
+
}
|
|
30
|
+
return decoded.provider.load();
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
// oxlint-disable-next-line anti-slop/no-unknown-parameters -- caught loader failures enter here before conversion to display text
|
|
34
|
+
export const chooseSourceError = function chooseSourceError(error: unknown): string {
|
|
35
|
+
return error instanceof Error ? error.message : String(error);
|
|
36
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export interface ChooseItem {
|
|
2
|
+
text: string;
|
|
3
|
+
value?: string;
|
|
4
|
+
icon?: string;
|
|
5
|
+
description?: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface ChooseContentProvider {
|
|
9
|
+
load: () => Promise<ChooseItem[]> | ChooseItem[];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** Any item source accepted by the shared chooser core. */
|
|
13
|
+
export type ChooseSource =
|
|
14
|
+
| ChooseItem[]
|
|
15
|
+
| ChooseContentProvider
|
|
16
|
+
| (() => ChooseItem[] | Promise<ChooseItem[]>);
|
|
17
|
+
|
|
18
|
+
export interface ChooseResult {
|
|
19
|
+
items: ChooseItem[];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface ChooseOptions {
|
|
23
|
+
multi?: boolean;
|
|
24
|
+
title?: string;
|
|
25
|
+
prompt?: string;
|
|
26
|
+
placeholder?: string;
|
|
27
|
+
emptyMessage?: string;
|
|
28
|
+
}
|