@tooee/ask 0.1.22 → 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/src/Ask.tsx CHANGED
@@ -1,43 +1,22 @@
1
- import { useState, useRef, useCallback, useEffect } from "react"
2
- import type {
3
- TextareaRenderable,
4
- InputRenderable,
5
- MouseEvent,
6
- KeyEvent,
7
- PasteEvent,
8
- CursorStyleOptions,
9
- } from "@opentui/core"
10
- import { useKeyboard, useRenderer } from "@opentui/react"
11
- import { readPrimaryText } from "@tooee/clipboard"
1
+ import { useRenderer } from "@opentui/react"
12
2
  import { AppLayout } from "@tooee/layout"
13
3
  import { useHasOverlay } from "@tooee/overlays"
14
4
  import { ThemePicker, useTheme } from "@tooee/themes"
15
5
  import { useThemeCommands, useQuitCommand, usePasteCommands } from "@tooee/shell"
16
- import {
17
- useMode,
18
- useSetMode,
19
- useActions,
20
- useProvideCommandContext,
21
- useCommandContext,
22
- } from "@tooee/commands"
6
+ import { useActions, useProvideCommandContext, useCommandContext } from "@tooee/commands"
23
7
  import type { ActionDefinition } from "@tooee/commands"
24
8
  import type { AskOptions } from "./types.js"
25
- import {
26
- appendAtCursor,
27
- handleEditBufferVimMotion,
28
- openLineAtCursor,
29
- type VimMotionState,
30
- } from "./vim-motions.js"
9
+ import { AskEditor } from "./AskEditor.js"
10
+ import { useAskEditor } from "./use-ask-editor.js"
31
11
 
32
- declare module "@tooee/commands" {
33
- interface CommandContext {
34
- /** Contributed by Ask: the current input value. */
35
- ask: { value: string }
36
- }
37
- }
38
-
39
- interface AskProps extends AskOptions {
12
+ export interface AskProps extends AskOptions {
40
13
  actions?: ActionDefinition[]
14
+ /**
15
+ * Called with the submitted text. Defaults to writing the value to stdout
16
+ * and destroying the renderer (the standalone `ask` CLI behaviour). A
17
+ * `submit` action takes precedence over both.
18
+ */
19
+ onSubmit?: (value: string) => void
41
20
  }
42
21
 
43
22
  export function Ask({
@@ -47,13 +26,9 @@ export function Ask({
47
26
  defaultValue,
48
27
  multiline = true,
49
28
  actions,
29
+ onSubmit,
50
30
  }: AskProps) {
51
31
  const renderer = useRenderer()
52
- const [value, setValue] = useState(defaultValue ?? "")
53
- const textareaRef = useRef<TextareaRenderable>(null)
54
- const inputRef = useRef<InputRenderable>(null)
55
- const didPositionInitialCursorRef = useRef(false)
56
- const vimMotionStateRef = useRef<VimMotionState>({ pendingG: false })
57
32
  const { invoke } = useCommandContext()
58
33
 
59
34
  const { theme } = useTheme()
@@ -65,117 +40,41 @@ export function Ask({
65
40
  },
66
41
  })
67
42
 
68
- const mode = useMode()
69
- const setMode = useSetMode()
43
+ // Legacy overlays don't push a command surface; keep blurring the editor
44
+ // under them via the shell's overlay state.
70
45
  const hasOverlay = useHasOverlay()
71
- const inputFocused = (mode === "insert" || mode === "cursor") && !hasOverlay
72
- const cursorStyle: CursorStyleOptions =
73
- mode === "cursor" ? { style: "block", blinking: false } : { style: "line", blinking: true }
74
- const cursorColor = mode === "cursor" ? theme.accent : theme.primary
75
-
76
- const preventCursorModeEditorInput = (event: KeyEvent | PasteEvent) => {
77
- if (mode === "cursor") event.preventDefault()
78
- }
79
-
80
- useEffect(() => {
81
- if (didPositionInitialCursorRef.current || !defaultValue) return
82
-
83
- const target = multiline ? textareaRef.current : inputRef.current
84
- if (!target) return
85
-
86
- target.cursorOffset = target.plainText.length
87
- didPositionInitialCursorRef.current = true
88
- }, [defaultValue, multiline])
89
46
 
90
- const handleSubmit = () => {
91
- const text = multiline ? (textareaRef.current?.plainText ?? "") : value
47
+ const handleSubmit = (text: string) => {
92
48
  if (actions?.some((a) => a.id === "submit")) {
93
49
  invoke("submit")
94
50
  return
95
51
  }
52
+ if (onSubmit) {
53
+ onSubmit(text)
54
+ return
55
+ }
96
56
  process.stdout.write(text + "\n")
97
57
  renderer.destroy()
98
58
  }
99
59
 
60
+ const { controller, editor } = useAskEditor({
61
+ multiline,
62
+ defaultValue,
63
+ placeholder,
64
+ onSubmit: handleSubmit,
65
+ suspended: hasOverlay,
66
+ })
67
+
100
68
  useProvideCommandContext(() => ({
101
- ask: { value: multiline ? (textareaRef.current?.plainText ?? "") : value },
102
69
  exit: () => renderer.destroy(),
103
70
  }))
104
71
 
105
72
  useActions(actions)
106
73
 
107
74
  // Paste commands (available via command palette)
108
- usePasteCommands({
109
- getTarget: () => (multiline ? textareaRef.current : inputRef.current),
110
- })
111
-
112
- useKeyboard((key) => {
113
- if (hasOverlay) return
114
- if (key.name === "escape") {
115
- if (mode === "insert") {
116
- setMode("cursor")
117
- }
118
- // In cursor mode, escape does nothing - use 'q' to quit
119
- return
120
- }
121
- if (mode === "cursor") {
122
- const target = multiline ? textareaRef.current : inputRef.current
123
- if (key.name === "i" || key.raw === "i") {
124
- key.preventDefault()
125
- vimMotionStateRef.current.pendingG = false
126
- setMode("insert")
127
- return
128
- }
129
- if (key.name === "a" || key.raw === "a") {
130
- key.preventDefault()
131
- vimMotionStateRef.current.pendingG = false
132
- appendAtCursor(target)
133
- setMode("insert")
134
- return
135
- }
136
- if (multiline && ((key.name === "o" && key.shift) || key.raw === "O")) {
137
- key.preventDefault()
138
- vimMotionStateRef.current.pendingG = false
139
- openLineAtCursor(target, "above")
140
- setMode("insert")
141
- return
142
- }
143
- if (multiline && (key.name === "o" || key.raw === "o")) {
144
- key.preventDefault()
145
- vimMotionStateRef.current.pendingG = false
146
- openLineAtCursor(target, "below")
147
- setMode("insert")
148
- return
149
- }
150
- if (handleEditBufferVimMotion(key, target, vimMotionStateRef.current)) return
151
- } else {
152
- vimMotionStateRef.current.pendingG = false
153
- }
154
-
155
- if (key.name === "return") {
156
- if (multiline ? key.shift : true) {
157
- key.preventDefault()
158
- handleSubmit()
159
- }
160
- return
161
- }
162
- })
163
-
164
- // Middle-click paste from primary selection
165
- const handleMouseDown = useCallback(
166
- (event: MouseEvent) => {
167
- if (event.button === 1) {
168
- event.preventDefault()
169
- void readPrimaryText().then((text) => {
170
- if (!text) return
171
- const target = multiline ? textareaRef.current : inputRef.current
172
- target?.insertText(text)
173
- })
174
- }
175
- },
176
- [multiline],
177
- )
75
+ usePasteCommands({ getTarget: () => controller })
178
76
 
77
+ const mode = editor.mode
179
78
  const submitHint = multiline ? "Shift+Enter submit" : "Enter submit"
180
79
  const hintParts =
181
80
  mode === "insert"
@@ -210,7 +109,7 @@ export function Ask({
210
109
  alignItems="center"
211
110
  justifyContent="center"
212
111
  style={{ flexGrow: 1 }}
213
- onMouseDown={handleMouseDown}
112
+ onMouseDown={editor.onMouseDown}
214
113
  >
215
114
  <box flexDirection="column" width="100%" maxWidth={80} style={{ flexGrow: 1, padding: 1 }}>
216
115
  {prompt && (
@@ -218,39 +117,7 @@ export function Ask({
218
117
  <strong>{prompt}</strong>
219
118
  </text>
220
119
  )}
221
- {multiline ? (
222
- <textarea
223
- ref={textareaRef}
224
- focused={inputFocused}
225
- initialValue={defaultValue}
226
- placeholder={placeholder}
227
- textColor={theme.text}
228
- placeholderColor={theme.textMuted}
229
- cursorColor={cursorColor}
230
- cursorStyle={cursorStyle}
231
- backgroundColor="transparent"
232
- onSubmit={handleSubmit}
233
- onKeyDown={preventCursorModeEditorInput}
234
- onPaste={preventCursorModeEditorInput}
235
- style={{ flexGrow: 1 }}
236
- />
237
- ) : (
238
- <input
239
- ref={inputRef}
240
- focused={inputFocused}
241
- value={value}
242
- onInput={setValue}
243
- onSubmit={handleSubmit}
244
- placeholder={placeholder}
245
- textColor={theme.text}
246
- placeholderColor={theme.textMuted}
247
- cursorColor={cursorColor}
248
- cursorStyle={cursorStyle}
249
- backgroundColor="transparent"
250
- onKeyDown={preventCursorModeEditorInput}
251
- onPaste={preventCursorModeEditorInput}
252
- />
253
- )}
120
+ <AskEditor editor={editor} />
254
121
  </box>
255
122
  </box>
256
123
  </AppLayout>
@@ -0,0 +1,69 @@
1
+ import type { CursorStyleOptions } from "@opentui/core"
2
+ import { useTheme } from "@tooee/themes"
3
+ import { EditorScrollbar } from "./EditorScrollbar.js"
4
+ import type { AskEditorViewModel } from "./use-ask-editor.js"
5
+
6
+ export interface AskEditorProps {
7
+ editor: AskEditorViewModel
8
+ }
9
+
10
+ /**
11
+ * Presentational ask editor: the themed textarea/input plus scrollbar. All
12
+ * behaviour lives in the view-model from `useAskEditor`.
13
+ */
14
+ export function AskEditor({ editor }: AskEditorProps) {
15
+ const { theme } = useTheme()
16
+
17
+ const cursorStyle: CursorStyleOptions =
18
+ editor.mode === "cursor"
19
+ ? { style: "block", blinking: false }
20
+ : { style: "line", blinking: true }
21
+ const cursorColor = editor.mode === "cursor" ? theme.accent : theme.primary
22
+
23
+ if (editor.multiline) {
24
+ return (
25
+ <box flexDirection="row" style={{ flexGrow: 1 }} onMouseScroll={editor.bumpScroll}>
26
+ <textarea
27
+ ref={editor.textareaRef}
28
+ focused={editor.focused}
29
+ initialValue={editor.defaultValue}
30
+ placeholder={editor.placeholder}
31
+ textColor={theme.text}
32
+ placeholderColor={theme.textMuted}
33
+ cursorColor={cursorColor}
34
+ cursorStyle={cursorStyle}
35
+ backgroundColor="transparent"
36
+ onSubmit={editor.submit}
37
+ onKeyDown={editor.onEditorKeyDown}
38
+ onPaste={editor.onEditorPaste}
39
+ onCursorChange={editor.bumpScroll}
40
+ onContentChange={editor.bumpScroll}
41
+ style={{ flexGrow: 1 }}
42
+ />
43
+ <EditorScrollbar
44
+ target={editor.textareaRef.current}
45
+ revision={editor.scrollRevision}
46
+ color={theme.textMuted}
47
+ />
48
+ </box>
49
+ )
50
+ }
51
+
52
+ return (
53
+ <input
54
+ ref={editor.inputRef}
55
+ focused={editor.focused}
56
+ value={editor.value}
57
+ onInput={editor.onInput}
58
+ onSubmit={editor.submit}
59
+ placeholder={editor.placeholder}
60
+ textColor={theme.text}
61
+ placeholderColor={theme.textMuted}
62
+ cursorColor={cursorColor}
63
+ cursorStyle={cursorStyle}
64
+ backgroundColor="transparent"
65
+ onKeyDown={editor.onEditorKeyDown}
66
+ onPaste={editor.onEditorPaste}
67
+ />
68
+ )
69
+ }