@tooee/ask 0.1.21 → 0.2.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/README.md +36 -0
- package/dist/Ask.d.ts +8 -11
- package/dist/Ask.d.ts.map +1 -1
- package/dist/Ask.js +22 -101
- package/dist/Ask.js.map +1 -1
- package/dist/AskEditor.d.ts +10 -0
- package/dist/AskEditor.d.ts.map +1 -0
- package/dist/AskEditor.js +19 -0
- package/dist/AskEditor.js.map +1 -0
- package/dist/AskOverlay.d.ts +19 -1
- package/dist/AskOverlay.d.ts.map +1 -1
- package/dist/AskOverlay.js +18 -280
- package/dist/AskOverlay.js.map +1 -1
- package/dist/AskPanel.d.ts +49 -0
- package/dist/AskPanel.d.ts.map +1 -0
- package/dist/AskPanel.js +25 -0
- package/dist/AskPanel.js.map +1 -0
- package/dist/EditorScrollbar.d.ts +24 -0
- package/dist/EditorScrollbar.d.ts.map +1 -0
- package/dist/EditorScrollbar.js +41 -0
- package/dist/EditorScrollbar.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/dist/launch.d.ts +2 -0
- package/dist/launch.d.ts.map +1 -1
- package/dist/launch.js +1 -1
- package/dist/launch.js.map +1 -1
- package/dist/use-ask-editor.d.ts +93 -0
- package/dist/use-ask-editor.d.ts.map +1 -0
- package/dist/use-ask-editor.js +414 -0
- package/dist/use-ask-editor.js.map +1 -0
- package/package.json +11 -11
- package/src/Ask.tsx +31 -164
- package/src/AskEditor.tsx +69 -0
- package/src/AskOverlay.tsx +52 -367
- package/src/AskPanel.tsx +152 -0
- package/src/EditorScrollbar.tsx +55 -0
- package/src/index.ts +16 -0
- package/src/launch.tsx +3 -0
- package/src/use-ask-editor.ts +558 -0
package/src/AskOverlay.tsx
CHANGED
|
@@ -1,390 +1,75 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
KeyEvent,
|
|
7
|
-
PasteEvent,
|
|
8
|
-
CursorStyleOptions,
|
|
9
|
-
} from "@opentui/core"
|
|
10
|
-
import { readPrimaryText } from "@tooee/clipboard"
|
|
11
|
-
import { useTheme, CloseButton } from "@tooee/themes"
|
|
12
|
-
import { useCommand, useMode, useSetMode } from "@tooee/commands"
|
|
13
|
-
import { appendAtCursor, openLineAtCursor, type VimMotionState } from "./vim-motions.js"
|
|
1
|
+
import { useImperativeHandle, type ReactNode, type Ref } from "react"
|
|
2
|
+
import { useActions, type ActionDefinition } from "@tooee/commands"
|
|
3
|
+
import { AskEditor } from "./AskEditor.js"
|
|
4
|
+
import { AskPanel, type AskPanelProps } from "./AskPanel.js"
|
|
5
|
+
import { useAskEditor, type AskEditorController } from "./use-ask-editor.js"
|
|
14
6
|
|
|
15
7
|
export interface AskOverlayProps {
|
|
16
8
|
prompt: string
|
|
9
|
+
/** Title bar content; when set, `prompt` renders as a line above the editor. */
|
|
10
|
+
title?: string
|
|
17
11
|
multiline?: boolean
|
|
18
12
|
defaultValue?: string
|
|
13
|
+
placeholder?: string
|
|
19
14
|
onSubmit: (value: string) => void | Promise<void>
|
|
20
15
|
onCancel: () => void
|
|
16
|
+
/** Extra commands registered on this surface (same shape as Ask's actions). */
|
|
17
|
+
commands?: ActionDefinition[]
|
|
18
|
+
/** Access to the text controller (dictation, prefill, transforms). */
|
|
19
|
+
controllerRef?: Ref<AskEditorController>
|
|
20
|
+
/** Chrome pass-throughs. */
|
|
21
|
+
hints?: AskPanelProps["hints"]
|
|
22
|
+
statusRight?: ReactNode
|
|
23
|
+
footer?: ReactNode
|
|
24
|
+
inset?: AskPanelProps["inset"]
|
|
25
|
+
/** Nested overlays (pickers) rendered above the panel. */
|
|
26
|
+
children?: ReactNode
|
|
21
27
|
}
|
|
22
28
|
|
|
23
29
|
export function AskOverlay({
|
|
24
30
|
prompt,
|
|
31
|
+
title,
|
|
25
32
|
multiline,
|
|
26
33
|
defaultValue,
|
|
34
|
+
placeholder,
|
|
27
35
|
onSubmit,
|
|
28
36
|
onCancel,
|
|
37
|
+
commands,
|
|
38
|
+
controllerRef,
|
|
39
|
+
hints,
|
|
40
|
+
statusRight,
|
|
41
|
+
footer,
|
|
42
|
+
inset,
|
|
43
|
+
children,
|
|
29
44
|
}: AskOverlayProps) {
|
|
30
|
-
const {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
const didPositionInitialCursorRef = useRef(false)
|
|
38
|
-
const vimMotionStateRef = useRef<VimMotionState>({ pendingG: false })
|
|
39
|
-
|
|
40
|
-
const inputFocused = mode === "insert" || mode === "cursor"
|
|
41
|
-
const cursorStyle: CursorStyleOptions =
|
|
42
|
-
mode === "cursor" ? { style: "block", blinking: false } : { style: "line", blinking: true }
|
|
43
|
-
const cursorColor = mode === "cursor" ? theme.accent : theme.primary
|
|
44
|
-
|
|
45
|
-
const preventCursorModeEditorInput = (event: KeyEvent | PasteEvent) => {
|
|
46
|
-
if (mode === "cursor") event.preventDefault()
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
useEffect(() => {
|
|
50
|
-
if (didPositionInitialCursorRef.current || !defaultValue) return
|
|
51
|
-
|
|
52
|
-
const target = multiline ? textareaRef.current : inputRef.current
|
|
53
|
-
if (!target) return
|
|
54
|
-
|
|
55
|
-
target.cursorOffset = target.plainText.length
|
|
56
|
-
didPositionInitialCursorRef.current = true
|
|
57
|
-
}, [defaultValue, multiline])
|
|
58
|
-
|
|
59
|
-
const handleSubmit = () => {
|
|
60
|
-
const text = multiline ? (textareaRef.current?.plainText ?? "") : value
|
|
61
|
-
onSubmit(text)
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
const enterInsertMode = useCallback(() => {
|
|
65
|
-
vimMotionStateRef.current.pendingG = false
|
|
66
|
-
setMode("insert")
|
|
67
|
-
}, [setMode])
|
|
68
|
-
|
|
69
|
-
const appendAndEnterInsertMode = useCallback(() => {
|
|
70
|
-
vimMotionStateRef.current.pendingG = false
|
|
71
|
-
appendAtCursor(multiline ? textareaRef.current : inputRef.current)
|
|
72
|
-
setMode("insert")
|
|
73
|
-
}, [multiline, setMode])
|
|
74
|
-
|
|
75
|
-
const openLineAbove = useCallback(() => {
|
|
76
|
-
if (!multiline) return
|
|
77
|
-
vimMotionStateRef.current.pendingG = false
|
|
78
|
-
openLineAtCursor(textareaRef.current, "above")
|
|
79
|
-
setMode("insert")
|
|
80
|
-
}, [multiline, setMode])
|
|
81
|
-
|
|
82
|
-
const openLineBelow = useCallback(() => {
|
|
83
|
-
if (!multiline) return
|
|
84
|
-
vimMotionStateRef.current.pendingG = false
|
|
85
|
-
openLineAtCursor(textareaRef.current, "below")
|
|
86
|
-
setMode("insert")
|
|
87
|
-
}, [multiline, setMode])
|
|
88
|
-
|
|
89
|
-
const leaveInsertMode = useCallback(() => {
|
|
90
|
-
vimMotionStateRef.current.pendingG = false
|
|
91
|
-
setMode("cursor")
|
|
92
|
-
}, [setMode])
|
|
93
|
-
|
|
94
|
-
useCommand({
|
|
95
|
-
id: "ask-overlay:leave-insert-mode",
|
|
96
|
-
title: "Command mode",
|
|
97
|
-
hotkey: "Escape",
|
|
98
|
-
modes: ["insert"],
|
|
99
|
-
hidden: true,
|
|
100
|
-
handler: leaveInsertMode,
|
|
101
|
-
})
|
|
102
|
-
useCommand({
|
|
103
|
-
id: "ask-overlay:cancel",
|
|
104
|
-
title: "Cancel",
|
|
105
|
-
hotkey: "q",
|
|
106
|
-
modes: ["cursor"],
|
|
107
|
-
hidden: true,
|
|
108
|
-
handler: onCancel,
|
|
109
|
-
})
|
|
110
|
-
useCommand({
|
|
111
|
-
id: "ask-overlay:insert-mode-i",
|
|
112
|
-
title: "Insert mode",
|
|
113
|
-
hotkey: "i",
|
|
114
|
-
modes: ["cursor"],
|
|
115
|
-
hidden: true,
|
|
116
|
-
handler: enterInsertMode,
|
|
117
|
-
})
|
|
118
|
-
useCommand({
|
|
119
|
-
id: "ask-overlay:insert-mode-a",
|
|
120
|
-
title: "Append",
|
|
121
|
-
hotkey: "a",
|
|
122
|
-
modes: ["cursor"],
|
|
123
|
-
hidden: true,
|
|
124
|
-
handler: appendAndEnterInsertMode,
|
|
125
|
-
})
|
|
126
|
-
useCommand({
|
|
127
|
-
id: "ask-overlay:open-line-above",
|
|
128
|
-
title: "Open line above",
|
|
129
|
-
hotkey: "shift+o",
|
|
130
|
-
modes: ["cursor"],
|
|
131
|
-
hidden: true,
|
|
132
|
-
when: () => multiline === true,
|
|
133
|
-
handler: openLineAbove,
|
|
134
|
-
})
|
|
135
|
-
useCommand({
|
|
136
|
-
id: "ask-overlay:open-line-below",
|
|
137
|
-
title: "Open line below",
|
|
138
|
-
hotkey: "o",
|
|
139
|
-
modes: ["cursor"],
|
|
140
|
-
hidden: true,
|
|
141
|
-
when: () => multiline === true,
|
|
142
|
-
handler: openLineBelow,
|
|
143
|
-
})
|
|
144
|
-
useCommand({
|
|
145
|
-
id: "ask-overlay:submit-single-line",
|
|
146
|
-
title: "Submit",
|
|
147
|
-
hotkey: "Enter",
|
|
148
|
-
modes: ["insert", "cursor"],
|
|
149
|
-
hidden: true,
|
|
150
|
-
when: () => multiline !== true,
|
|
151
|
-
handler: handleSubmit,
|
|
152
|
-
})
|
|
153
|
-
useCommand({
|
|
154
|
-
id: "ask-overlay:submit-multiline",
|
|
155
|
-
title: "Submit",
|
|
156
|
-
hotkey: "shift+Enter",
|
|
157
|
-
modes: ["insert", "cursor"],
|
|
158
|
-
hidden: true,
|
|
159
|
-
when: () => multiline === true,
|
|
160
|
-
handler: handleSubmit,
|
|
161
|
-
})
|
|
162
|
-
|
|
163
|
-
const getMotionTarget = useCallback(
|
|
164
|
-
() => (multiline ? textareaRef.current : inputRef.current),
|
|
165
|
-
[multiline],
|
|
166
|
-
)
|
|
167
|
-
const motion = useCallback(
|
|
168
|
-
(run: (target: NonNullable<ReturnType<typeof getMotionTarget>>) => void) => {
|
|
169
|
-
const target = getMotionTarget()
|
|
170
|
-
if (target) run(target as NonNullable<ReturnType<typeof getMotionTarget>>)
|
|
171
|
-
vimMotionStateRef.current.pendingG = false
|
|
172
|
-
},
|
|
173
|
-
[getMotionTarget],
|
|
174
|
-
)
|
|
175
|
-
|
|
176
|
-
useCommand({
|
|
177
|
-
id: "ask-overlay:move-left",
|
|
178
|
-
title: "Move left",
|
|
179
|
-
hotkey: "h",
|
|
180
|
-
modes: ["cursor"],
|
|
181
|
-
hidden: true,
|
|
182
|
-
handler: () => motion((target) => target.moveCursorLeft()),
|
|
183
|
-
})
|
|
184
|
-
useCommand({
|
|
185
|
-
id: "ask-overlay:move-left-arrow",
|
|
186
|
-
title: "Move left",
|
|
187
|
-
hotkey: "left",
|
|
188
|
-
modes: ["cursor"],
|
|
189
|
-
hidden: true,
|
|
190
|
-
handler: () => motion((target) => target.moveCursorLeft()),
|
|
191
|
-
})
|
|
192
|
-
useCommand({
|
|
193
|
-
id: "ask-overlay:move-right",
|
|
194
|
-
title: "Move right",
|
|
195
|
-
hotkey: "l",
|
|
196
|
-
modes: ["cursor"],
|
|
197
|
-
hidden: true,
|
|
198
|
-
handler: () => motion((target) => target.moveCursorRight()),
|
|
199
|
-
})
|
|
200
|
-
useCommand({
|
|
201
|
-
id: "ask-overlay:move-right-arrow",
|
|
202
|
-
title: "Move right",
|
|
203
|
-
hotkey: "right",
|
|
204
|
-
modes: ["cursor"],
|
|
205
|
-
hidden: true,
|
|
206
|
-
handler: () => motion((target) => target.moveCursorRight()),
|
|
207
|
-
})
|
|
208
|
-
useCommand({
|
|
209
|
-
id: "ask-overlay:move-down",
|
|
210
|
-
title: "Move down",
|
|
211
|
-
hotkey: "j",
|
|
212
|
-
modes: ["cursor"],
|
|
213
|
-
hidden: true,
|
|
214
|
-
handler: () => motion((target) => target.moveCursorDown()),
|
|
215
|
-
})
|
|
216
|
-
useCommand({
|
|
217
|
-
id: "ask-overlay:move-down-arrow",
|
|
218
|
-
title: "Move down",
|
|
219
|
-
hotkey: "down",
|
|
220
|
-
modes: ["cursor"],
|
|
221
|
-
hidden: true,
|
|
222
|
-
handler: () => motion((target) => target.moveCursorDown()),
|
|
223
|
-
})
|
|
224
|
-
useCommand({
|
|
225
|
-
id: "ask-overlay:move-up",
|
|
226
|
-
title: "Move up",
|
|
227
|
-
hotkey: "k",
|
|
228
|
-
modes: ["cursor"],
|
|
229
|
-
hidden: true,
|
|
230
|
-
handler: () => motion((target) => target.moveCursorUp()),
|
|
231
|
-
})
|
|
232
|
-
useCommand({
|
|
233
|
-
id: "ask-overlay:move-up-arrow",
|
|
234
|
-
title: "Move up",
|
|
235
|
-
hotkey: "up",
|
|
236
|
-
modes: ["cursor"],
|
|
237
|
-
hidden: true,
|
|
238
|
-
handler: () => motion((target) => target.moveCursorUp()),
|
|
239
|
-
})
|
|
240
|
-
useCommand({
|
|
241
|
-
id: "ask-overlay:line-home",
|
|
242
|
-
title: "Line home",
|
|
243
|
-
hotkey: "0",
|
|
244
|
-
modes: ["cursor"],
|
|
245
|
-
hidden: true,
|
|
246
|
-
handler: () => motion((target) => target.gotoLineHome()),
|
|
45
|
+
const { controller, editor } = useAskEditor({
|
|
46
|
+
multiline,
|
|
47
|
+
defaultValue,
|
|
48
|
+
placeholder,
|
|
49
|
+
onSubmit,
|
|
50
|
+
onCancel,
|
|
51
|
+
commandScope: "ask-overlay",
|
|
247
52
|
})
|
|
248
|
-
useCommand({
|
|
249
|
-
id: "ask-overlay:line-home-key",
|
|
250
|
-
title: "Line home",
|
|
251
|
-
hotkey: "home",
|
|
252
|
-
modes: ["cursor"],
|
|
253
|
-
hidden: true,
|
|
254
|
-
handler: () => motion((target) => target.gotoLineHome()),
|
|
255
|
-
})
|
|
256
|
-
useCommand({
|
|
257
|
-
id: "ask-overlay:line-end",
|
|
258
|
-
title: "Line end",
|
|
259
|
-
hotkey: "shift+4",
|
|
260
|
-
modes: ["cursor"],
|
|
261
|
-
hidden: true,
|
|
262
|
-
handler: () => motion((target) => target.gotoLineEnd()),
|
|
263
|
-
})
|
|
264
|
-
useCommand({
|
|
265
|
-
id: "ask-overlay:line-end-key",
|
|
266
|
-
title: "Line end",
|
|
267
|
-
hotkey: "end",
|
|
268
|
-
modes: ["cursor"],
|
|
269
|
-
hidden: true,
|
|
270
|
-
handler: () => motion((target) => target.gotoLineEnd()),
|
|
271
|
-
})
|
|
272
|
-
useCommand({
|
|
273
|
-
id: "ask-overlay:word-forward",
|
|
274
|
-
title: "Word forward",
|
|
275
|
-
hotkey: "w",
|
|
276
|
-
modes: ["cursor"],
|
|
277
|
-
hidden: true,
|
|
278
|
-
handler: () => motion((target) => target.moveWordForward()),
|
|
279
|
-
})
|
|
280
|
-
useCommand({
|
|
281
|
-
id: "ask-overlay:word-backward",
|
|
282
|
-
title: "Word backward",
|
|
283
|
-
hotkey: "b",
|
|
284
|
-
modes: ["cursor"],
|
|
285
|
-
hidden: true,
|
|
286
|
-
handler: () => motion((target) => target.moveWordBackward()),
|
|
287
|
-
})
|
|
288
|
-
useCommand({
|
|
289
|
-
id: "ask-overlay:buffer-home",
|
|
290
|
-
title: "Buffer home",
|
|
291
|
-
hotkey: "g g",
|
|
292
|
-
modes: ["cursor"],
|
|
293
|
-
hidden: true,
|
|
294
|
-
handler: () => motion((target) => target.gotoBufferHome()),
|
|
295
|
-
})
|
|
296
|
-
useCommand({
|
|
297
|
-
id: "ask-overlay:buffer-end",
|
|
298
|
-
title: "Buffer end",
|
|
299
|
-
hotkey: "shift+g",
|
|
300
|
-
modes: ["cursor"],
|
|
301
|
-
hidden: true,
|
|
302
|
-
handler: () => motion((target) => target.gotoBufferEnd()),
|
|
303
|
-
})
|
|
304
|
-
|
|
305
|
-
// Middle-click paste from primary selection
|
|
306
|
-
const handleMouseDown = useCallback(
|
|
307
|
-
(event: MouseEvent) => {
|
|
308
|
-
if (event.button === 1) {
|
|
309
|
-
event.preventDefault()
|
|
310
|
-
void readPrimaryText().then((text) => {
|
|
311
|
-
if (!text) return
|
|
312
|
-
const target = multiline ? textareaRef.current : inputRef.current
|
|
313
|
-
target?.insertText(text)
|
|
314
|
-
})
|
|
315
|
-
}
|
|
316
|
-
},
|
|
317
|
-
[multiline],
|
|
318
|
-
)
|
|
319
53
|
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
mode === "insert" ? `${submitHint} Esc commands` : `i insert q quit ${submitHint}`
|
|
54
|
+
useImperativeHandle(controllerRef, () => controller, [controller])
|
|
55
|
+
useActions(commands)
|
|
323
56
|
|
|
324
57
|
return (
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
>
|
|
337
|
-
{/* Title bar */}
|
|
338
|
-
<box
|
|
339
|
-
flexDirection="row"
|
|
340
|
-
height={1}
|
|
341
|
-
paddingLeft={1}
|
|
342
|
-
paddingRight={1}
|
|
343
|
-
backgroundColor={theme.backgroundElement}
|
|
58
|
+
<>
|
|
59
|
+
<AskPanel
|
|
60
|
+
title={title ?? prompt}
|
|
61
|
+
prompt={title !== undefined ? prompt : undefined}
|
|
62
|
+
multiline={multiline === true}
|
|
63
|
+
hints={hints}
|
|
64
|
+
statusRight={statusRight}
|
|
65
|
+
footer={footer}
|
|
66
|
+
onClose={onCancel}
|
|
67
|
+
inset={inset}
|
|
68
|
+
onMouseDown={editor.onMouseDown}
|
|
344
69
|
>
|
|
345
|
-
<
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
{/* Input area */}
|
|
350
|
-
<box flexDirection="column" style={{ flexGrow: 1, paddingLeft: 1, paddingRight: 1 }}>
|
|
351
|
-
{multiline ? (
|
|
352
|
-
<textarea
|
|
353
|
-
ref={textareaRef}
|
|
354
|
-
focused={inputFocused}
|
|
355
|
-
initialValue={defaultValue}
|
|
356
|
-
textColor={theme.text}
|
|
357
|
-
placeholderColor={theme.textMuted}
|
|
358
|
-
cursorColor={cursorColor}
|
|
359
|
-
cursorStyle={cursorStyle}
|
|
360
|
-
backgroundColor="transparent"
|
|
361
|
-
onSubmit={handleSubmit}
|
|
362
|
-
onKeyDown={preventCursorModeEditorInput}
|
|
363
|
-
onPaste={preventCursorModeEditorInput}
|
|
364
|
-
style={{ flexGrow: 1 }}
|
|
365
|
-
/>
|
|
366
|
-
) : (
|
|
367
|
-
<input
|
|
368
|
-
ref={inputRef}
|
|
369
|
-
focused={inputFocused}
|
|
370
|
-
value={value}
|
|
371
|
-
onInput={setValue}
|
|
372
|
-
onSubmit={handleSubmit}
|
|
373
|
-
textColor={theme.text}
|
|
374
|
-
placeholderColor={theme.textMuted}
|
|
375
|
-
cursorColor={cursorColor}
|
|
376
|
-
cursorStyle={cursorStyle}
|
|
377
|
-
backgroundColor="transparent"
|
|
378
|
-
onKeyDown={preventCursorModeEditorInput}
|
|
379
|
-
onPaste={preventCursorModeEditorInput}
|
|
380
|
-
/>
|
|
381
|
-
)}
|
|
382
|
-
</box>
|
|
383
|
-
|
|
384
|
-
{/* Hint line */}
|
|
385
|
-
<box height={1} paddingLeft={1} paddingRight={1} backgroundColor={theme.backgroundElement}>
|
|
386
|
-
<text content={hintText} fg={theme.textMuted} />
|
|
387
|
-
</box>
|
|
388
|
-
</box>
|
|
70
|
+
<AskEditor editor={editor} />
|
|
71
|
+
</AskPanel>
|
|
72
|
+
{children}
|
|
73
|
+
</>
|
|
389
74
|
)
|
|
390
75
|
}
|
package/src/AskPanel.tsx
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import type { ReactNode } from "react"
|
|
2
|
+
import type { MouseEvent } from "@opentui/core"
|
|
3
|
+
import { useTheme, CloseButton } from "@tooee/themes"
|
|
4
|
+
import { useMode, type Mode } from "@tooee/commands"
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Default hint-bar entries for the current mode. Composites append their own
|
|
8
|
+
* entries via `extra` rather than re-writing the whole string.
|
|
9
|
+
*/
|
|
10
|
+
export function buildAskHints(
|
|
11
|
+
mode: Mode,
|
|
12
|
+
opts: { multiline?: boolean; extra?: string[] } = {},
|
|
13
|
+
): string[] {
|
|
14
|
+
const submitHint = opts.multiline ? "Shift+Enter submit" : "Enter submit"
|
|
15
|
+
const base = mode === "insert" ? [submitHint, "Esc commands"] : ["i insert", "q quit", submitHint]
|
|
16
|
+
return opts.extra ? [...base, ...opts.extra] : base
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export type AskPanelInsetValue = number | "auto" | `${number}%`
|
|
20
|
+
|
|
21
|
+
export interface AskPanelInset {
|
|
22
|
+
left?: AskPanelInsetValue
|
|
23
|
+
right?: AskPanelInsetValue
|
|
24
|
+
top?: AskPanelInsetValue
|
|
25
|
+
bottom?: AskPanelInsetValue
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface AskPanelProps {
|
|
29
|
+
/** Title bar content (string renders themed; CloseButton shown with onClose). */
|
|
30
|
+
title?: ReactNode
|
|
31
|
+
/** Prompt line above the editor area. */
|
|
32
|
+
prompt?: ReactNode
|
|
33
|
+
/** Editor area. */
|
|
34
|
+
children: ReactNode
|
|
35
|
+
/** Used for the default submit hint ("Enter" vs "Shift+Enter"). */
|
|
36
|
+
multiline?: boolean
|
|
37
|
+
/** Hint bar. Default renders the standard hints for the current mode. */
|
|
38
|
+
hints?: (ctx: { mode: Mode; defaults: string[] }) => ReactNode
|
|
39
|
+
/** Right-aligned status area in the hint bar (e.g. a dictation indicator). */
|
|
40
|
+
statusRight?: ReactNode
|
|
41
|
+
/** Extra row below the editor, above the hint bar. */
|
|
42
|
+
footer?: ReactNode
|
|
43
|
+
/** Shows the title-bar CloseButton. */
|
|
44
|
+
onClose?: () => void
|
|
45
|
+
/** Overlay geometry when absolutely positioned (default 20% on all sides). */
|
|
46
|
+
inset?: AskPanelInset
|
|
47
|
+
/** Mouse handler for the whole panel (e.g. middle-click paste). */
|
|
48
|
+
onMouseDown?: (event: MouseEvent) => void
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Shared bordered panel chrome for ask surfaces: title bar, optional prompt
|
|
53
|
+
* line, editor area, optional footer, and a hint bar with slots.
|
|
54
|
+
*/
|
|
55
|
+
export function AskPanel({
|
|
56
|
+
title,
|
|
57
|
+
prompt,
|
|
58
|
+
children,
|
|
59
|
+
multiline,
|
|
60
|
+
hints,
|
|
61
|
+
statusRight,
|
|
62
|
+
footer,
|
|
63
|
+
onClose,
|
|
64
|
+
inset,
|
|
65
|
+
onMouseDown,
|
|
66
|
+
}: AskPanelProps) {
|
|
67
|
+
const { theme } = useTheme()
|
|
68
|
+
const mode = useMode()
|
|
69
|
+
|
|
70
|
+
const defaults = buildAskHints(mode, { multiline })
|
|
71
|
+
const hintContent = hints ? hints({ mode, defaults }) : defaults.join(" ")
|
|
72
|
+
|
|
73
|
+
return (
|
|
74
|
+
<box
|
|
75
|
+
position="absolute"
|
|
76
|
+
left={inset?.left ?? "20%"}
|
|
77
|
+
right={inset?.right ?? "20%"}
|
|
78
|
+
top={inset?.top ?? "20%"}
|
|
79
|
+
bottom={inset?.bottom ?? "20%"}
|
|
80
|
+
flexDirection="column"
|
|
81
|
+
backgroundColor={theme.backgroundPanel}
|
|
82
|
+
border
|
|
83
|
+
borderColor={theme.borderActive}
|
|
84
|
+
onMouseDown={onMouseDown}
|
|
85
|
+
>
|
|
86
|
+
{title != null && (
|
|
87
|
+
<box
|
|
88
|
+
flexDirection="row"
|
|
89
|
+
height={1}
|
|
90
|
+
paddingLeft={1}
|
|
91
|
+
paddingRight={1}
|
|
92
|
+
backgroundColor={theme.backgroundElement}
|
|
93
|
+
>
|
|
94
|
+
{typeof title === "string" ? (
|
|
95
|
+
<text content={title} fg={theme.accent} style={{ flexGrow: 1 }} />
|
|
96
|
+
) : (
|
|
97
|
+
<box flexDirection="row" style={{ flexGrow: 1 }}>
|
|
98
|
+
{title}
|
|
99
|
+
</box>
|
|
100
|
+
)}
|
|
101
|
+
{onClose && <CloseButton onClose={onClose} />}
|
|
102
|
+
</box>
|
|
103
|
+
)}
|
|
104
|
+
|
|
105
|
+
{prompt != null && (
|
|
106
|
+
<box paddingLeft={1} paddingRight={1}>
|
|
107
|
+
{typeof prompt === "string" ? (
|
|
108
|
+
<text fg={theme.text}>
|
|
109
|
+
<strong>{prompt}</strong>
|
|
110
|
+
</text>
|
|
111
|
+
) : (
|
|
112
|
+
prompt
|
|
113
|
+
)}
|
|
114
|
+
</box>
|
|
115
|
+
)}
|
|
116
|
+
|
|
117
|
+
{/* Editor area */}
|
|
118
|
+
<box flexDirection="column" style={{ flexGrow: 1, paddingLeft: 1, paddingRight: 1 }}>
|
|
119
|
+
{children}
|
|
120
|
+
</box>
|
|
121
|
+
|
|
122
|
+
{footer != null && (
|
|
123
|
+
<box paddingLeft={1} paddingRight={1}>
|
|
124
|
+
{footer}
|
|
125
|
+
</box>
|
|
126
|
+
)}
|
|
127
|
+
|
|
128
|
+
{/* Hint line */}
|
|
129
|
+
<box
|
|
130
|
+
flexDirection="row"
|
|
131
|
+
height={1}
|
|
132
|
+
paddingLeft={1}
|
|
133
|
+
paddingRight={1}
|
|
134
|
+
backgroundColor={theme.backgroundElement}
|
|
135
|
+
>
|
|
136
|
+
<box flexDirection="row" style={{ flexGrow: 1 }}>
|
|
137
|
+
{typeof hintContent === "string" ? (
|
|
138
|
+
<text content={hintContent} fg={theme.textMuted} />
|
|
139
|
+
) : (
|
|
140
|
+
hintContent
|
|
141
|
+
)}
|
|
142
|
+
</box>
|
|
143
|
+
{statusRight != null &&
|
|
144
|
+
(typeof statusRight === "string" ? (
|
|
145
|
+
<text content={statusRight} fg={theme.textMuted} />
|
|
146
|
+
) : (
|
|
147
|
+
statusRight
|
|
148
|
+
))}
|
|
149
|
+
</box>
|
|
150
|
+
</box>
|
|
151
|
+
)
|
|
152
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { TextareaRenderable, InputRenderable } from "@opentui/core"
|
|
2
|
+
|
|
3
|
+
export interface EditorScrollbarProps {
|
|
4
|
+
/** The editor whose viewport this scrollbar reflects. */
|
|
5
|
+
target: TextareaRenderable | InputRenderable | null
|
|
6
|
+
/**
|
|
7
|
+
* Bump this whenever the editor viewport may have changed (cursor move,
|
|
8
|
+
* content change, wheel scroll) so the thumb position re-computes.
|
|
9
|
+
*/
|
|
10
|
+
revision: number
|
|
11
|
+
color: string
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const THUMB_CHAR = "█"
|
|
15
|
+
const TRACK_CHAR = "░"
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* A one-column vertical scrollbar that mirrors an editor's internal viewport.
|
|
19
|
+
*
|
|
20
|
+
* The `<textarea>`/`<input>` (EditBufferRenderable) scrolls its own viewport to
|
|
21
|
+
* follow the cursor and to respond to the mouse wheel, but has no built-in
|
|
22
|
+
* scrollbar, so overflowing content is reachable yet not visibly indicated.
|
|
23
|
+
* This renders a thumb reflecting `offsetY / totalLines` so the user can see
|
|
24
|
+
* there is more content above/below the box.
|
|
25
|
+
*
|
|
26
|
+
* Renders nothing when content fits within the viewport (no overflow).
|
|
27
|
+
*/
|
|
28
|
+
export function EditorScrollbar({ target, color }: EditorScrollbarProps) {
|
|
29
|
+
if (!target) return null
|
|
30
|
+
|
|
31
|
+
// Rendered height of the editor in rows (available post-layout).
|
|
32
|
+
const height = target.height
|
|
33
|
+
if (height <= 0) return null
|
|
34
|
+
|
|
35
|
+
// Total virtual (wrapped) line count, not the count currently in view.
|
|
36
|
+
const total = target.editorView.getTotalVirtualLineCount()
|
|
37
|
+
if (total <= height) return null // fits, no overflow -> no scrollbar
|
|
38
|
+
|
|
39
|
+
const offsetY = target.scrollY
|
|
40
|
+
const maxOffset = Math.max(1, total - height)
|
|
41
|
+
const clampedOffset = Math.min(Math.max(offsetY, 0), maxOffset)
|
|
42
|
+
|
|
43
|
+
const thumbSize = Math.max(1, Math.round((height / total) * height))
|
|
44
|
+
const maxThumbTop = Math.max(0, height - thumbSize)
|
|
45
|
+
const thumbTop = Math.round((clampedOffset / maxOffset) * maxThumbTop)
|
|
46
|
+
|
|
47
|
+
let content = ""
|
|
48
|
+
for (let i = 0; i < height; i++) {
|
|
49
|
+
const isThumb = i >= thumbTop && i < thumbTop + thumbSize
|
|
50
|
+
content += isThumb ? THUMB_CHAR : TRACK_CHAR
|
|
51
|
+
if (i < height - 1) content += "\n"
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return <text content={content} fg={color} selectable={false} />
|
|
55
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,22 @@
|
|
|
1
1
|
export { Ask } from "./Ask.js"
|
|
2
|
+
export type { AskProps } from "./Ask.js"
|
|
2
3
|
export { AskOverlay } from "./AskOverlay.js"
|
|
3
4
|
export type { AskOverlayProps } from "./AskOverlay.js"
|
|
5
|
+
export { AskPanel, buildAskHints } from "./AskPanel.js"
|
|
6
|
+
export type { AskPanelProps, AskPanelInset, AskPanelInsetValue } from "./AskPanel.js"
|
|
7
|
+
export { AskEditor } from "./AskEditor.js"
|
|
8
|
+
export type { AskEditorProps } from "./AskEditor.js"
|
|
9
|
+
export { useAskEditor } from "./use-ask-editor.js"
|
|
10
|
+
export type {
|
|
11
|
+
AskEditorCommandGroup,
|
|
12
|
+
AskEditorController,
|
|
13
|
+
AskEditorViewModel,
|
|
14
|
+
AskSubmitKey,
|
|
15
|
+
UseAskEditorOptions,
|
|
16
|
+
UseAskEditorResult,
|
|
17
|
+
} from "./use-ask-editor.js"
|
|
4
18
|
export { launch } from "./launch.js"
|
|
5
19
|
export type { AskLaunchOptions } from "./launch.js"
|
|
6
20
|
export type { AskOptions } from "./types.js"
|
|
21
|
+
export { appendAtCursor, handleEditBufferVimMotion, openLineAtCursor } from "./vim-motions.js"
|
|
22
|
+
export type { EditableInsertTarget, VimMotionState } from "./vim-motions.js"
|
package/src/launch.tsx
CHANGED
|
@@ -7,6 +7,8 @@ import type { AskOptions } from "./types.js"
|
|
|
7
7
|
|
|
8
8
|
export interface AskLaunchOptions extends AskOptions {
|
|
9
9
|
actions?: ActionDefinition[]
|
|
10
|
+
/** Called with the submitted text. Default: write to stdout and exit. */
|
|
11
|
+
onSubmit?: (value: string) => void
|
|
10
12
|
}
|
|
11
13
|
|
|
12
14
|
export async function launch(options: AskLaunchOptions): Promise<void> {
|
|
@@ -25,6 +27,7 @@ export async function launch(options: AskLaunchOptions): Promise<void> {
|
|
|
25
27
|
defaultValue={options.defaultValue}
|
|
26
28
|
multiline={options.multiline}
|
|
27
29
|
actions={options.actions}
|
|
30
|
+
onSubmit={options.onSubmit}
|
|
28
31
|
/>
|
|
29
32
|
</TooeeProvider>,
|
|
30
33
|
)
|