@tooee/renderers 0.8.0 → 0.9.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.
- 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/dist/mermaid.d.ts.map +1 -1
- package/dist/mermaid.js +44 -1
- package/dist/mermaid.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/src/mermaid.ts +64 -1
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,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AyB,EACzB,6BAA6B,GAC9B,MAAM,8BAA8B,CAAC;AAQtC,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC"}
|
package/dist/mermaid.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mermaid.d.ts","sourceRoot":"","sources":["../src/mermaid.ts"],"names":[],"mappings":"AACA,OAAO,EAAc,UAAU,EAAE,MAAM,eAAe,CAAC;AAGvD,UAAU,0BAA0B;IAClC,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,MAAM,iBAAiB,GAAG,OAAO,GAAG,MAAM,CAAC;AAEjD,MAAM,MAAM,mBAAmB,GAC3B;IAAE,EAAE,EAAE,IAAI,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,UAAU,CAAA;CAAE,GAC/C;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,kBAAkB,GAAG,OAAO,GAAG,cAAc,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC;AAE1F,MAAM,WAAW,oBAAoB;IACnC,IAAI,CAAC,EAAE,iBAAiB,CAAC;IACzB,KAAK,CAAC,EAAE,0BAA0B,CAAC;CACpC;AAED,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,UAAU,CAAC;CACrB;
|
|
1
|
+
{"version":3,"file":"mermaid.d.ts","sourceRoot":"","sources":["../src/mermaid.ts"],"names":[],"mappings":"AACA,OAAO,EAAc,UAAU,EAAE,MAAM,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300;
|
|
9
9
|
const MAX_FLOWCHART_EDGES = 32;
|
|
10
|
+
const MIN_UNSAFE_CYCLIC_FAN_DEGREE = 3;
|
|
10
11
|
// oxlint-disable-next-line no-control-regex -- ANSI SGR sequences start with the ESC control character
|
|
11
12
|
const SGR_SEQUENCE = /\u001B\[(?<params>[0-9;]*)m/gu;
|
|
12
13
|
/**
|
|
@@ -16,13 +17,55 @@ const SGR_SEQUENCE = /\u001B\[(?<params>[0-9;]*)m/gu;
|
|
|
16
17
|
export const isMermaidFence = function isMermaidFence(lang) {
|
|
17
18
|
return (lang ?? "").trim().split(/\s+/u)[0]?.toLowerCase() === "mermaid";
|
|
18
19
|
};
|
|
20
|
+
const hasDirectedPath = function hasDirectedPath(adjacency, source, target) {
|
|
21
|
+
const pending = [...(adjacency.get(source) ?? [])];
|
|
22
|
+
const visited = new Set([source]);
|
|
23
|
+
while (pending.length > 0) {
|
|
24
|
+
const node = pending.pop();
|
|
25
|
+
if (node === undefined) {
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
if (node === target) {
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
if (visited.has(node)) {
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
visited.add(node);
|
|
35
|
+
pending.push(...(adjacency.get(node) ?? []));
|
|
36
|
+
}
|
|
37
|
+
return false;
|
|
38
|
+
};
|
|
39
|
+
const hasUnsafeCyclicFanTopology = function hasUnsafeCyclicFanTopology(edges) {
|
|
40
|
+
const adjacency = new Map();
|
|
41
|
+
const incomingDegree = new Map();
|
|
42
|
+
const outgoingDegree = new Map();
|
|
43
|
+
for (const edge of edges) {
|
|
44
|
+
const targets = adjacency.get(edge.source) ?? new Set();
|
|
45
|
+
targets.add(edge.target);
|
|
46
|
+
adjacency.set(edge.source, targets);
|
|
47
|
+
incomingDegree.set(edge.target, (incomingDegree.get(edge.target) ?? 0) + 1);
|
|
48
|
+
outgoingDegree.set(edge.source, (outgoingDegree.get(edge.source) ?? 0) + 1);
|
|
49
|
+
}
|
|
50
|
+
const fanInNodes = [...incomingDegree]
|
|
51
|
+
.filter(([, degree]) => degree >= MIN_UNSAFE_CYCLIC_FAN_DEGREE)
|
|
52
|
+
.map(([node]) => node);
|
|
53
|
+
const fanOutNodes = [...outgoingDegree]
|
|
54
|
+
.filter(([, degree]) => degree >= MIN_UNSAFE_CYCLIC_FAN_DEGREE)
|
|
55
|
+
.map(([node]) => node);
|
|
56
|
+
return fanInNodes.some((fanIn) => fanOutNodes.some((fanOut) => hasDirectedPath(adjacency, fanIn, fanOut) && hasDirectedPath(adjacency, fanOut, fanIn)));
|
|
57
|
+
};
|
|
19
58
|
const exceedsSynchronousRenderBudget = function exceedsSynchronousRenderBudget(source) {
|
|
20
59
|
if (source.length > MAX_SOURCE_LENGTH || source.split("\n").length > MAX_SOURCE_LINES) {
|
|
21
60
|
return true;
|
|
22
61
|
}
|
|
23
62
|
const firstLine = source.trimStart().split(/[;\n]/u)[0]?.trim().toLowerCase() ?? "";
|
|
24
63
|
const isFlowchart = /^(?:flowchart|graph|statediagram(?:-v2)?)\b/u.test(firstLine);
|
|
25
|
-
|
|
64
|
+
if (!isFlowchart) {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
const { edges } = parseMermaid(source);
|
|
68
|
+
return edges.length > MAX_FLOWCHART_EDGES || hasUnsafeCyclicFanTopology(edges);
|
|
26
69
|
};
|
|
27
70
|
const appendStyledChunk = function appendStyledChunk(chunks, text, fg) {
|
|
28
71
|
if (text.length === 0) {
|
package/dist/mermaid.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mermaid.js","sourceRoot":"","sources":["../src/mermaid.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AACrE,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AA8BvD,oFAAoF;AACpF,mFAAmF;AACn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,MAAM,QAAQ,GAAG,kBAAkB,CAAC,MAAM,EAAE;YAC1C,SAAS,EAAE,IAAI,KAAK,MAAM,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,MAAM;YACjD,KAAK,EAAE,OAAO,CAAC,KAAK;SACrB,CAAC,CAAC,OAAO,EAAE,CAAC;QACb,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG,gBAAgB,CAAC,QAAQ,CAAC,CAAC;QAErD,IAAI,IAAI,CAAC,IAAI,EAAE,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YAC7B,OAAO,EAAE,OAAO,EAAE,qCAAqC,EAAE,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC;QACxF,CAAC;QAED,OAAO,EAAE,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC;IACrC,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO;YACL,OAAO,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC;YAC/D,EAAE,EAAE,KAAK;YACT,MAAM,EAAE,cAAc;SACvB,CAAC;IACJ,CAAC;AACH,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tooee/renderers",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.1",
|
|
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.1",
|
|
49
|
+
"@tooee/fuzzy": "0.9.1",
|
|
50
|
+
"@tooee/layout": "0.9.1",
|
|
51
|
+
"@tooee/marks": "0.9.1",
|
|
52
|
+
"@tooee/themes": "0.9.1",
|
|
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
|
+
};
|