@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/src/command-palette.tsx
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useMemo } from "react";
|
|
2
2
|
import type { ReactNode } from "react";
|
|
3
|
-
import { useCommand } from "@tooee/commands";
|
|
4
|
-
import { CloseButton } from "@tooee/layout";
|
|
5
3
|
import { useTheme } from "@tooee/themes";
|
|
6
|
-
import {
|
|
4
|
+
import { Chooser } from "./chooser/chooser.js";
|
|
5
|
+
import { ChooseHighlightedText } from "./chooser/choose-highlighted-text.js";
|
|
6
|
+
import type { ChooseItem } from "./chooser/types.js";
|
|
7
7
|
|
|
8
8
|
export interface CommandPaletteEntry {
|
|
9
9
|
id: string;
|
|
@@ -19,160 +19,57 @@ interface CommandPaletteProps {
|
|
|
19
19
|
onClose: () => void;
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
const HighlightedTitle = function HighlightedTitle({
|
|
23
|
-
title,
|
|
24
|
-
positions,
|
|
25
|
-
}: {
|
|
26
|
-
title: string;
|
|
27
|
-
positions: readonly number[];
|
|
28
|
-
}): ReactNode {
|
|
29
|
-
const { theme } = useTheme();
|
|
30
|
-
const highlighted = new Set(positions);
|
|
31
|
-
return (
|
|
32
|
-
<text fg={theme.text} style={{ flexGrow: 1 }}>
|
|
33
|
-
{Array.from(
|
|
34
|
-
title,
|
|
35
|
-
(character, index): ReactNode => (
|
|
36
|
-
<span key={index} fg={highlighted.has(index) ? theme.warning : theme.text}>
|
|
37
|
-
{character}
|
|
38
|
-
</span>
|
|
39
|
-
),
|
|
40
|
-
)}
|
|
41
|
-
</text>
|
|
42
|
-
);
|
|
43
|
-
};
|
|
44
|
-
|
|
45
22
|
export const CommandPalette = function CommandPalette({
|
|
46
23
|
commands,
|
|
47
24
|
onSelect,
|
|
48
25
|
onClose,
|
|
49
26
|
}: CommandPaletteProps): ReactNode {
|
|
50
27
|
const { theme } = useTheme();
|
|
51
|
-
const
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
const matches = useMemo(
|
|
55
|
-
() => rankBy(commands, filter, (entry) => entry.title),
|
|
56
|
-
[commands, filter],
|
|
28
|
+
const entriesById = useMemo(
|
|
29
|
+
() => new Map(commands.map((entry) => [entry.id, entry])),
|
|
30
|
+
[commands],
|
|
57
31
|
);
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
32
|
+
const items = useMemo<ChooseItem[]>(
|
|
33
|
+
() => commands.map((entry) => ({ text: entry.title, value: entry.id })),
|
|
34
|
+
[commands],
|
|
35
|
+
);
|
|
36
|
+
const handleSelect = (item: ChooseItem): void => {
|
|
37
|
+
if (item.value !== undefined) {
|
|
38
|
+
onSelect(item.value);
|
|
63
39
|
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const moveUp = useCallback(() => {
|
|
67
|
-
setActiveIndex((i) => Math.max(0, i - 1));
|
|
68
|
-
}, []);
|
|
69
|
-
|
|
70
|
-
const moveDown = useCallback(() => {
|
|
71
|
-
setActiveIndex((i) => Math.min(matches.length - 1, i + 1));
|
|
72
|
-
}, [matches.length]);
|
|
73
|
-
|
|
74
|
-
useCommand({
|
|
75
|
-
handler: onClose,
|
|
76
|
-
hidden: true,
|
|
77
|
-
hotkey: "Escape",
|
|
78
|
-
id: "command-palette:close",
|
|
79
|
-
modes: ["insert", "cursor"],
|
|
80
|
-
title: "Close command palette",
|
|
81
|
-
});
|
|
82
|
-
useCommand({
|
|
83
|
-
handler: handleSelect,
|
|
84
|
-
hidden: true,
|
|
85
|
-
hotkey: "Enter",
|
|
86
|
-
id: "command-palette:select",
|
|
87
|
-
modes: ["insert", "cursor"],
|
|
88
|
-
title: "Run selected command",
|
|
89
|
-
});
|
|
90
|
-
useCommand({
|
|
91
|
-
handler: moveUp,
|
|
92
|
-
hidden: true,
|
|
93
|
-
hotkey: "up",
|
|
94
|
-
id: "command-palette:move-up",
|
|
95
|
-
modes: ["insert", "cursor"],
|
|
96
|
-
title: "Move up",
|
|
97
|
-
});
|
|
98
|
-
useCommand({
|
|
99
|
-
handler: moveDown,
|
|
100
|
-
hidden: true,
|
|
101
|
-
hotkey: "down",
|
|
102
|
-
id: "command-palette:move-down",
|
|
103
|
-
modes: ["insert", "cursor"],
|
|
104
|
-
title: "Move down",
|
|
105
|
-
});
|
|
40
|
+
};
|
|
106
41
|
|
|
107
42
|
return (
|
|
108
|
-
<
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
<input
|
|
123
|
-
focused
|
|
124
|
-
placeholder="Filter commands..."
|
|
125
|
-
onSubmit={handleSelect}
|
|
126
|
-
onInput={(value: string) => {
|
|
127
|
-
setFilter(value);
|
|
128
|
-
setActiveIndex(0);
|
|
129
|
-
}}
|
|
130
|
-
backgroundColor="transparent"
|
|
131
|
-
focusedBackgroundColor="transparent"
|
|
132
|
-
textColor={theme.text}
|
|
133
|
-
placeholderColor={theme.textMuted}
|
|
134
|
-
cursorColor={theme.accent}
|
|
135
|
-
style={{ flexGrow: 1 }}
|
|
136
|
-
/>
|
|
137
|
-
<text content={` ${matches.length}`} fg={theme.textMuted} />
|
|
138
|
-
<CloseButton onClose={onClose} />
|
|
139
|
-
</box>
|
|
140
|
-
|
|
141
|
-
{/* Separator */}
|
|
142
|
-
<box height={1} width="100%" backgroundColor={theme.border} />
|
|
143
|
-
|
|
144
|
-
{/* Command list */}
|
|
145
|
-
<scrollbox focused={false} style={{ flexGrow: 1 }}>
|
|
146
|
-
{matches.map(
|
|
147
|
-
(match, i): ReactNode => (
|
|
148
|
-
<box
|
|
149
|
-
key={match.item.id}
|
|
150
|
-
flexDirection="row"
|
|
151
|
-
paddingLeft={1}
|
|
152
|
-
paddingRight={1}
|
|
153
|
-
height={1}
|
|
154
|
-
backgroundColor={i === activeIndex ? theme.backgroundElement : undefined}
|
|
155
|
-
onMouseDown={(event) => {
|
|
156
|
-
// Left-click runs the entry — same code path as Enter.
|
|
157
|
-
if (event.button !== 0) {
|
|
158
|
-
return;
|
|
159
|
-
}
|
|
160
|
-
event.preventDefault();
|
|
161
|
-
event.stopPropagation();
|
|
162
|
-
onSelect(match.item.id);
|
|
163
|
-
}}
|
|
164
|
-
>
|
|
165
|
-
<HighlightedTitle title={match.item.title} positions={match.positions} />
|
|
166
|
-
{(match.item.hotkey?.length ?? 0) > 0 && (
|
|
167
|
-
<text content={match.item.hotkey} fg={theme.textMuted} />
|
|
168
|
-
)}
|
|
169
|
-
{(match.item.category?.length ?? 0) > 0 && (
|
|
170
|
-
<text content={` ${match.item.category}`} fg={theme.textMuted} />
|
|
43
|
+
<Chooser
|
|
44
|
+
items={items}
|
|
45
|
+
commandScope="command-palette"
|
|
46
|
+
prompt={<text content=":" fg={theme.accent} />}
|
|
47
|
+
placeholder="Filter commands..."
|
|
48
|
+
onCancel={onClose}
|
|
49
|
+
onSelect={handleSelect}
|
|
50
|
+
renderItem={({ item, positions }): ReactNode => {
|
|
51
|
+
const entry = item.value === undefined ? undefined : entriesById.get(item.value);
|
|
52
|
+
return (
|
|
53
|
+
<>
|
|
54
|
+
<text fg={theme.text} style={{ flexGrow: 1 }}>
|
|
55
|
+
{(entry?.icon?.length ?? 0) > 0 && (
|
|
56
|
+
<span fg={theme.textMuted}>{`${entry?.icon} `}</span>
|
|
171
57
|
)}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
58
|
+
<ChooseHighlightedText
|
|
59
|
+
text={item.text}
|
|
60
|
+
positions={positions}
|
|
61
|
+
highlightColor={theme.warning}
|
|
62
|
+
/>
|
|
63
|
+
</text>
|
|
64
|
+
{(entry?.hotkey?.length ?? 0) > 0 && (
|
|
65
|
+
<text content={entry?.hotkey} fg={theme.textMuted} />
|
|
66
|
+
)}
|
|
67
|
+
{(entry?.category?.length ?? 0) > 0 && (
|
|
68
|
+
<text content={` ${entry?.category}`} fg={theme.textMuted} />
|
|
69
|
+
)}
|
|
70
|
+
</>
|
|
71
|
+
);
|
|
72
|
+
}}
|
|
73
|
+
/>
|
|
177
74
|
);
|
|
178
75
|
};
|
package/src/index.ts
CHANGED
|
@@ -30,6 +30,45 @@ export type { TableProps, ColumnWidthOptions } from "./table.js";
|
|
|
30
30
|
export type { ColumnDef, TableRow } from "./table-types.js";
|
|
31
31
|
export { CommandPalette } from "./command-palette.js";
|
|
32
32
|
export type { CommandPaletteEntry } from "./command-palette.js";
|
|
33
|
+
export { Chooser } from "./chooser/chooser.js";
|
|
34
|
+
export type { ChooserProps } from "./chooser/chooser.js";
|
|
35
|
+
export { ChooseFilter } from "./chooser/choose-filter.js";
|
|
36
|
+
export type { ChooseFilterProps } from "./chooser/choose-filter.js";
|
|
37
|
+
export { ChooseHighlightedText } from "./chooser/choose-highlighted-text.js";
|
|
38
|
+
export type { ChooseHighlightedTextProps } from "./chooser/choose-highlighted-text.js";
|
|
39
|
+
export { ChooseList } from "./chooser/choose-list.js";
|
|
40
|
+
export type { ChooseItemRenderContext, ChooseListProps } from "./chooser/choose-list.js";
|
|
41
|
+
export {
|
|
42
|
+
createChooseStore,
|
|
43
|
+
selectActiveIndex,
|
|
44
|
+
selectError,
|
|
45
|
+
selectFilterQuery,
|
|
46
|
+
selectItems,
|
|
47
|
+
selectLoading,
|
|
48
|
+
selectMatches,
|
|
49
|
+
selectReloadRevision,
|
|
50
|
+
selectSelectedOriginalIndices,
|
|
51
|
+
} from "./chooser/choose-store.js";
|
|
52
|
+
export type { ChooseStore, ChooseStoreContext, ChooseStoreEvents } from "./chooser/choose-store.js";
|
|
53
|
+
export { fuzzyFilter } from "./chooser/fuzzy.js";
|
|
54
|
+
export type { FuzzyMatch } from "./chooser/fuzzy.js";
|
|
55
|
+
export { loadChooseSource } from "./chooser/source.js";
|
|
56
|
+
export { useChoose } from "./chooser/use-choose.js";
|
|
57
|
+
export type {
|
|
58
|
+
ChooseCommandGroup,
|
|
59
|
+
ChooseController,
|
|
60
|
+
ChooseState,
|
|
61
|
+
ChooseViewModel,
|
|
62
|
+
UseChooseOptions,
|
|
63
|
+
UseChooseResult,
|
|
64
|
+
} from "./chooser/use-choose.js";
|
|
65
|
+
export type {
|
|
66
|
+
ChooseContentProvider,
|
|
67
|
+
ChooseItem,
|
|
68
|
+
ChooseOptions,
|
|
69
|
+
ChooseResult,
|
|
70
|
+
ChooseSource,
|
|
71
|
+
} from "./chooser/types.js";
|
|
33
72
|
export { ContextMenu } from "./context-menu.js";
|
|
34
73
|
export type { ContextMenuEntry } from "./context-menu.js";
|
|
35
74
|
export { parseCSV, parseTSV, parseJSON, parseAuto, detectFormat } from "./parsers.js";
|