@tooee/ask 0.2.3 → 0.3.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.
Files changed (67) hide show
  1. package/README.md +2 -2
  2. package/dist/{AskEditor.d.ts → ask-editor.d.ts} +2 -2
  3. package/dist/ask-editor.d.ts.map +1 -0
  4. package/dist/ask-editor.js +26 -0
  5. package/dist/ask-editor.js.map +1 -0
  6. package/dist/{AskOverlay.d.ts → ask-overlay.d.ts} +6 -6
  7. package/dist/ask-overlay.d.ts.map +1 -0
  8. package/dist/{AskOverlay.js → ask-overlay.js} +11 -10
  9. package/dist/ask-overlay.js.map +1 -0
  10. package/dist/{AskPanel.d.ts → ask-panel.d.ts} +5 -5
  11. package/dist/ask-panel.d.ts.map +1 -0
  12. package/dist/ask-panel.js +33 -0
  13. package/dist/ask-panel.js.map +1 -0
  14. package/dist/{Ask.d.ts → ask.d.ts} +2 -2
  15. package/dist/ask.d.ts.map +1 -0
  16. package/dist/{Ask.js → ask.js} +15 -12
  17. package/dist/ask.js.map +1 -0
  18. package/dist/{EditorScrollbar.d.ts → editor-scrollbar.d.ts} +2 -2
  19. package/dist/editor-scrollbar.d.ts.map +1 -0
  20. package/dist/{EditorScrollbar.js → editor-scrollbar.js} +15 -10
  21. package/dist/editor-scrollbar.js.map +1 -0
  22. package/dist/index.d.ts +8 -8
  23. package/dist/index.d.ts.map +1 -1
  24. package/dist/index.js +4 -4
  25. package/dist/index.js.map +1 -1
  26. package/dist/launch.d.ts +1 -1
  27. package/dist/launch.d.ts.map +1 -1
  28. package/dist/launch.js +5 -4
  29. package/dist/launch.js.map +1 -1
  30. package/dist/types.d.ts.map +1 -1
  31. package/dist/use-ask-dialog.d.ts +4 -4
  32. package/dist/use-ask-dialog.d.ts.map +1 -1
  33. package/dist/use-ask-dialog.js +45 -38
  34. package/dist/use-ask-dialog.js.map +1 -1
  35. package/dist/use-ask-editor.d.ts +9 -9
  36. package/dist/use-ask-editor.d.ts.map +1 -1
  37. package/dist/use-ask-editor.js +211 -138
  38. package/dist/use-ask-editor.js.map +1 -1
  39. package/dist/vim-motions.d.ts +3 -3
  40. package/dist/vim-motions.d.ts.map +1 -1
  41. package/dist/vim-motions.js +73 -44
  42. package/dist/vim-motions.js.map +1 -1
  43. package/package.json +8 -8
  44. package/src/{AskEditor.tsx → ask-editor.tsx} +30 -23
  45. package/src/ask-overlay.tsx +80 -0
  46. package/src/{AskPanel.tsx → ask-panel.tsx} +49 -41
  47. package/src/{Ask.tsx → ask.tsx} +48 -45
  48. package/src/{EditorScrollbar.tsx → editor-scrollbar.tsx} +40 -28
  49. package/src/index.ts +17 -17
  50. package/src/launch.tsx +11 -10
  51. package/src/types.ts +5 -5
  52. package/src/use-ask-dialog.tsx +102 -90
  53. package/src/use-ask-editor.ts +377 -292
  54. package/src/vim-motions.ts +110 -71
  55. package/dist/Ask.d.ts.map +0 -1
  56. package/dist/Ask.js.map +0 -1
  57. package/dist/AskEditor.d.ts.map +0 -1
  58. package/dist/AskEditor.js +0 -19
  59. package/dist/AskEditor.js.map +0 -1
  60. package/dist/AskOverlay.d.ts.map +0 -1
  61. package/dist/AskOverlay.js.map +0 -1
  62. package/dist/AskPanel.d.ts.map +0 -1
  63. package/dist/AskPanel.js +0 -25
  64. package/dist/AskPanel.js.map +0 -1
  65. package/dist/EditorScrollbar.d.ts.map +0 -1
  66. package/dist/EditorScrollbar.js.map +0 -1
  67. package/src/AskOverlay.tsx +0 -75
@@ -1,12 +1,12 @@
1
- import { useCallback, useEffect, useRef, useState } from "react"
1
+ import { useCallback, useEffect, useRef, useState } from "react";
2
2
  import type {
3
3
  InputRenderable,
4
4
  KeyEvent,
5
5
  MouseEvent,
6
6
  PasteEvent,
7
7
  TextareaRenderable,
8
- } from "@opentui/core"
9
- import { readPrimaryText } from "@tooee/clipboard"
8
+ } from "@opentui/core";
9
+ import { readPrimaryText } from "@tooee/clipboard";
10
10
  import {
11
11
  useActiveCommandSurface,
12
12
  useCommand,
@@ -14,90 +14,91 @@ import {
14
14
  useMode,
15
15
  useProvideCommandContext,
16
16
  useSetMode,
17
- type Mode,
18
- } from "@tooee/commands"
19
- import { appendAtCursor, openLineAtCursor, type VimMotionState } from "./vim-motions.js"
17
+ } from "@tooee/commands";
18
+ import type { Mode } from "@tooee/commands";
19
+ import { appendAtCursor, openLineAtCursor } from "./vim-motions.js";
20
+ import type { VimMotionState } from "./vim-motions.js";
20
21
 
21
22
  declare module "@tooee/commands" {
22
23
  interface CommandContext {
23
24
  /** Contributed by ask editors: the current input value. */
24
- ask: { value: string }
25
+ ask: { value: string };
25
26
  }
26
27
  }
27
28
 
28
29
  /** Built-in command groups; disable one to take over its keys entirely. */
29
- export type AskEditorCommandGroup = "motions" | "insert-commands" | "submit" | "cancel" | "escape"
30
+ export type AskEditorCommandGroup = "motions" | "insert-commands" | "submit" | "cancel" | "escape";
30
31
 
31
- export type AskSubmitKey = "enter" | "shift+enter" | "none"
32
+ export type AskSubmitKey = "enter" | "shift+enter" | "none";
32
33
 
33
34
  export interface UseAskEditorOptions {
34
35
  /** Render/edit a multiline textarea instead of a single-line input (default false). */
35
- multiline?: boolean
36
- defaultValue?: string
37
- placeholder?: string
38
- onSubmit?: (value: string) => void | Promise<void>
36
+ multiline?: boolean;
37
+ defaultValue?: string;
38
+ placeholder?: string;
39
+ onSubmit?: (value: string) => void | Promise<void>;
39
40
  /** Enables the `q` cancel command (cursor mode) when provided. */
40
- onCancel?: () => void
41
+ onCancel?: () => void;
41
42
  /** Which key submits. Default: "shift+enter" when multiline, "enter" otherwise. */
42
- submitKey?: AskSubmitKey
43
+ submitKey?: AskSubmitKey;
43
44
  /** Prefix for command ids, default "ask" (e.g. "ask:move-left"). */
44
- commandScope?: string
45
+ commandScope?: string;
45
46
  /** Disable built-in command groups when a consumer wants full control. */
46
- disable?: AskEditorCommandGroup[]
47
+ disable?: AskEditorCommandGroup[];
47
48
  /**
48
49
  * Force the editor to blur (e.g. while a legacy overlay covers the host
49
50
  * app). ORed with the automatic suspension derived from the command-surface
50
51
  * stack; affects focus only, never command gating.
51
52
  */
52
- suspended?: boolean
53
+ suspended?: boolean;
53
54
  }
54
55
 
55
56
  /** Programmatic text control for composites (dictation, prefill, transforms). */
56
57
  export interface AskEditorController {
57
58
  /** Current text (works for input and textarea). */
58
- getText(): string
59
+ getText: () => string;
59
60
  /** Replace the buffer; optionally move cursor to end (default true). */
60
- setText(text: string, opts?: { cursorToEnd?: boolean }): void
61
+ setText: (text: string, opts?: { cursorToEnd?: boolean }) => void;
61
62
  /** Insert at the cursor position. */
62
- insertText(text: string): void
63
- setCursorToEnd(): void
64
- submit(): void
63
+ insertText: (text: string) => void;
64
+ setCursorToEnd: () => void;
65
+ submit: () => void;
65
66
  /** Current local mode (live read). */
66
- readonly mode: Mode
67
+ readonly mode: Mode;
67
68
  /** Switch insert/cursor mode; resets any pending vim motion state. */
68
- setMode(mode: Mode): void
69
+ setMode: (mode: Mode) => void;
69
70
  }
70
71
 
71
72
  /** Everything `<AskEditor/>` needs: refs, focus, mode, handlers. */
72
73
  export interface AskEditorViewModel {
73
- multiline: boolean
74
- defaultValue?: string
75
- placeholder?: string
74
+ multiline: boolean;
75
+ defaultValue?: string;
76
+ placeholder?: string;
76
77
  /** Single-line controlled value + change handler (unused for multiline). */
77
- value: string
78
- onInput: (value: string) => void
79
- textareaRef: { current: TextareaRenderable | null }
80
- inputRef: { current: InputRenderable | null }
78
+ value: string;
79
+ onInput: (value: string) => void;
80
+ textareaRef: { current: TextareaRenderable | null };
81
+ inputRef: { current: InputRenderable | null };
81
82
  /** Local surface mode, drives cursor styling. */
82
- mode: Mode
83
+ mode: Mode;
83
84
  /** True while a modal command surface above this one owns keyboard input. */
84
- suspended: boolean
85
+ suspended: boolean;
85
86
  /** Editor focus: insert/cursor mode and not suspended. */
86
- focused: boolean
87
+ focused: boolean;
87
88
  /** Scrollbar re-compute revision; bumped whenever the viewport may have moved. */
88
- scrollRevision: number
89
- bumpScroll: () => void
89
+ scrollRevision: number;
90
+ bumpScroll: () => void;
90
91
  /** Blocks editor input while in cursor mode (wire to onKeyDown/onPaste). */
91
- onEditorKeyDown: (event: KeyEvent | PasteEvent) => void
92
- onEditorPaste: (event: KeyEvent | PasteEvent) => void
93
- submit: () => void
92
+ onEditorKeyDown: (event: KeyEvent | PasteEvent) => void;
93
+ onEditorPaste: (event: KeyEvent | PasteEvent) => void;
94
+ submit: () => void;
94
95
  /** Middle-click primary-selection paste; attach to the surrounding panel/container. */
95
- onMouseDown: (event: MouseEvent) => void
96
+ onMouseDown: (event: MouseEvent) => void;
96
97
  }
97
98
 
98
99
  export interface UseAskEditorResult {
99
- controller: AskEditorController
100
- editor: AskEditorViewModel
100
+ controller: AskEditorController;
101
+ editor: AskEditorViewModel;
101
102
  }
102
103
 
103
104
  /**
@@ -106,453 +107,537 @@ export interface UseAskEditorResult {
106
107
  * command surface, so it works identically at the app root, inside an
107
108
  * `ownCommands` overlay, or under any `CommandSurfaceProvider`.
108
109
  */
109
- export function useAskEditor(options: UseAskEditorOptions = {}): UseAskEditorResult {
110
- const { multiline = false, defaultValue, placeholder, commandScope = "ask" } = options
111
-
112
- const mode = useMode()
113
- const setMode = useSetMode()
114
-
115
- const [value, setValue] = useState(defaultValue ?? "")
116
- const textareaRef = useRef<TextareaRenderable>(null)
117
- const inputRef = useRef<InputRenderable>(null)
118
- const didPositionInitialCursorRef = useRef(false)
119
- const vimMotionStateRef = useRef<VimMotionState>({ pendingG: false })
110
+ export const useAskEditor = function useAskEditor(
111
+ options: UseAskEditorOptions = {},
112
+ ): UseAskEditorResult {
113
+ const { multiline = false, defaultValue, placeholder, commandScope = "ask" } = options;
114
+
115
+ const mode = useMode();
116
+ const setMode = useSetMode();
117
+
118
+ const [value, setValue] = useState(defaultValue ?? "");
119
+ const textareaRef = useRef<TextareaRenderable>(null);
120
+ const inputRef = useRef<InputRenderable>(null);
121
+ const didPositionInitialCursorRef = useRef(false);
122
+ const vimMotionStateRef = useRef<VimMotionState>({ pendingG: false });
120
123
  // Bumped whenever the editor viewport may have moved (cursor, content, wheel)
121
124
  // so the scrollbar thumb re-computes from the editor's internal scroll state.
122
- const [scrollRevision, setScrollRevision] = useState(0)
123
- const bumpScroll = useCallback(() => setScrollRevision((r) => r + 1), [])
124
-
125
- const optionsRef = useRef(options)
126
- optionsRef.current = options
127
- const modeRef = useRef(mode)
128
- modeRef.current = mode
129
- const multilineRef = useRef(multiline)
130
- multilineRef.current = multiline
131
- const valueRef = useRef(value)
132
- valueRef.current = value
125
+ const [scrollRevision, setScrollRevision] = useState(0);
126
+ const bumpScroll = useCallback(() => {
127
+ setScrollRevision((r) => r + 1);
128
+ }, []);
129
+
130
+ const optionsRef = useRef(options);
131
+ optionsRef.current = options;
132
+ const modeRef = useRef(mode);
133
+ modeRef.current = mode;
134
+ const multilineRef = useRef(multiline);
135
+ multilineRef.current = multiline;
136
+ const valueRef = useRef(value);
137
+ valueRef.current = value;
133
138
 
134
139
  // Suspension: while a modal command surface other than our own is topmost,
135
140
  // our commands are already suspended by the surface stack; this mirrors that
136
141
  // for renderable focus so the editor blurs under a nested picker.
137
- const surfaceId = useCommandSurfaceId()
138
- const activeSurface = useActiveCommandSurface()
142
+ const surfaceId = useCommandSurfaceId();
143
+ const activeSurface = useActiveCommandSurface();
139
144
  const suspended =
140
- (options.suspended ?? false) || (activeSurface !== null && activeSurface.id !== surfaceId)
145
+ (options.suspended ?? false) || (activeSurface !== null && activeSurface.id !== surfaceId);
141
146
 
142
- const focused = (mode === "insert" || mode === "cursor") && !suspended
147
+ const focused = (mode === "insert" || mode === "cursor") && !suspended;
143
148
 
144
149
  const preventCursorModeEditorInput = useCallback((event: KeyEvent | PasteEvent) => {
145
- if (modeRef.current === "cursor") event.preventDefault()
146
- }, [])
150
+ if (modeRef.current === "cursor") {
151
+ event.preventDefault();
152
+ }
153
+ }, []);
147
154
 
148
155
  useEffect(() => {
149
- if (didPositionInitialCursorRef.current || !defaultValue) return
156
+ if (didPositionInitialCursorRef.current || (defaultValue?.length ?? 0) === 0) {
157
+ return;
158
+ }
150
159
 
151
- const target = multiline ? textareaRef.current : inputRef.current
152
- if (!target) return
160
+ const target = multiline ? textareaRef.current : inputRef.current;
161
+ if (!target) {
162
+ return;
163
+ }
153
164
 
154
- target.cursorOffset = target.plainText.length
155
- didPositionInitialCursorRef.current = true
156
- }, [defaultValue, multiline])
165
+ target.cursorOffset = target.plainText.length;
166
+ didPositionInitialCursorRef.current = true;
167
+ }, [defaultValue, multiline]);
157
168
 
158
169
  // Ensure the scrollbar computes once the editor ref and layout exist.
159
170
  useEffect(() => {
160
- bumpScroll()
161
- }, [bumpScroll])
171
+ bumpScroll();
172
+ }, [bumpScroll]);
162
173
 
163
174
  const getTarget = useCallback(
164
175
  () => (multilineRef.current ? textareaRef.current : inputRef.current),
165
176
  [],
166
- )
177
+ );
167
178
 
179
+ // Controller mutations must be observable before React state synchronizes.
168
180
  const getText = useCallback(
169
181
  () =>
170
182
  multilineRef.current
171
183
  ? (textareaRef.current?.plainText ?? "")
172
- : // Read the renderable's live text: controller mutations (insertText)
173
- // must be visible before React state re-syncs on the next render.
174
- (inputRef.current?.plainText ?? valueRef.current),
184
+ : (inputRef.current?.plainText ?? valueRef.current),
175
185
  [],
176
- )
186
+ );
177
187
 
178
188
  const submit = useCallback(() => {
179
- void optionsRef.current.onSubmit?.(getText())
180
- }, [getText])
189
+ void optionsRef.current.onSubmit?.(getText());
190
+ }, [getText]);
181
191
 
182
- useProvideCommandContext(() => ({ ask: { value: getText() } }))
192
+ useProvideCommandContext(() => ({ ask: { value: getText() } }));
183
193
 
184
194
  const enabled = useCallback(
185
195
  (group: AskEditorCommandGroup) => !(optionsRef.current.disable?.includes(group) ?? false),
186
196
  [],
187
- )
197
+ );
188
198
 
189
199
  const resolveSubmitKey = useCallback((): AskSubmitKey => {
190
- const current = optionsRef.current
191
- return current.submitKey ?? (current.multiline ? "shift+enter" : "enter")
192
- }, [])
200
+ const { current } = optionsRef;
201
+ return current.submitKey ?? (current.multiline === true ? "shift+enter" : "enter");
202
+ }, []);
193
203
 
194
204
  const enterInsertMode = useCallback(() => {
195
- vimMotionStateRef.current.pendingG = false
196
- setMode("insert")
197
- }, [setMode])
205
+ vimMotionStateRef.current.pendingG = false;
206
+ setMode("insert");
207
+ }, [setMode]);
198
208
 
199
209
  const appendAndEnterInsertMode = useCallback(() => {
200
- vimMotionStateRef.current.pendingG = false
201
- appendAtCursor(getTarget())
202
- setMode("insert")
203
- }, [getTarget, setMode])
210
+ vimMotionStateRef.current.pendingG = false;
211
+ appendAtCursor(getTarget());
212
+ setMode("insert");
213
+ }, [getTarget, setMode]);
204
214
 
205
215
  const openLineAbove = useCallback(() => {
206
- if (!multilineRef.current) return
207
- vimMotionStateRef.current.pendingG = false
208
- openLineAtCursor(textareaRef.current, "above")
209
- setMode("insert")
210
- }, [setMode])
216
+ if (!multilineRef.current) {
217
+ return;
218
+ }
219
+ vimMotionStateRef.current.pendingG = false;
220
+ openLineAtCursor(textareaRef.current, "above");
221
+ setMode("insert");
222
+ }, [setMode]);
211
223
 
212
224
  const openLineBelow = useCallback(() => {
213
- if (!multilineRef.current) return
214
- vimMotionStateRef.current.pendingG = false
215
- openLineAtCursor(textareaRef.current, "below")
216
- setMode("insert")
217
- }, [setMode])
225
+ if (!multilineRef.current) {
226
+ return;
227
+ }
228
+ vimMotionStateRef.current.pendingG = false;
229
+ openLineAtCursor(textareaRef.current, "below");
230
+ setMode("insert");
231
+ }, [setMode]);
218
232
 
219
233
  const leaveInsertMode = useCallback(() => {
220
- vimMotionStateRef.current.pendingG = false
221
- setMode("cursor")
222
- }, [setMode])
234
+ vimMotionStateRef.current.pendingG = false;
235
+ setMode("cursor");
236
+ }, [setMode]);
223
237
 
224
238
  useCommand({
225
- id: `${commandScope}:leave-insert-mode`,
226
- title: "Command mode",
239
+ handler: leaveInsertMode,
240
+ hidden: true,
227
241
  hotkey: "Escape",
242
+ id: `${commandScope}:leave-insert-mode`,
228
243
  modes: ["insert"],
229
- hidden: true,
244
+ title: "Command mode",
230
245
  when: () => enabled("escape"),
231
- handler: leaveInsertMode,
232
- })
246
+ });
233
247
  useCommand({
234
- id: `${commandScope}:cancel`,
235
- title: "Cancel",
248
+ handler: () => optionsRef.current.onCancel?.(),
249
+ hidden: true,
236
250
  hotkey: "q",
251
+ id: `${commandScope}:cancel`,
237
252
  modes: ["cursor"],
238
- hidden: true,
253
+ title: "Cancel",
239
254
  when: () => enabled("cancel") && optionsRef.current.onCancel !== undefined,
240
- handler: () => optionsRef.current.onCancel?.(),
241
- })
255
+ });
242
256
  useCommand({
243
- id: `${commandScope}:insert-mode-i`,
244
- title: "Insert mode",
257
+ handler: enterInsertMode,
258
+ hidden: true,
245
259
  hotkey: "i",
260
+ id: `${commandScope}:insert-mode-i`,
246
261
  modes: ["cursor"],
247
- hidden: true,
262
+ title: "Insert mode",
248
263
  when: () => enabled("insert-commands"),
249
- handler: enterInsertMode,
250
- })
264
+ });
251
265
  useCommand({
252
- id: `${commandScope}:insert-mode-a`,
253
- title: "Append",
266
+ handler: appendAndEnterInsertMode,
267
+ hidden: true,
254
268
  hotkey: "a",
269
+ id: `${commandScope}:insert-mode-a`,
255
270
  modes: ["cursor"],
256
- hidden: true,
271
+ title: "Append",
257
272
  when: () => enabled("insert-commands"),
258
- handler: appendAndEnterInsertMode,
259
- })
273
+ });
260
274
  useCommand({
261
- id: `${commandScope}:open-line-above`,
262
- title: "Open line above",
275
+ handler: openLineAbove,
276
+ hidden: true,
263
277
  hotkey: "shift+o",
278
+ id: `${commandScope}:open-line-above`,
264
279
  modes: ["cursor"],
265
- hidden: true,
280
+ title: "Open line above",
266
281
  when: () => enabled("insert-commands") && multilineRef.current,
267
- handler: openLineAbove,
268
- })
282
+ });
269
283
  useCommand({
270
- id: `${commandScope}:open-line-below`,
271
- title: "Open line below",
284
+ handler: openLineBelow,
285
+ hidden: true,
272
286
  hotkey: "o",
287
+ id: `${commandScope}:open-line-below`,
273
288
  modes: ["cursor"],
274
- hidden: true,
289
+ title: "Open line below",
275
290
  when: () => enabled("insert-commands") && multilineRef.current,
276
- handler: openLineBelow,
277
- })
291
+ });
278
292
  useCommand({
279
- id: `${commandScope}:submit-single-line`,
280
- title: "Submit",
293
+ handler: submit,
294
+ hidden: true,
281
295
  hotkey: "Enter",
296
+ id: `${commandScope}:submit-single-line`,
282
297
  modes: ["insert", "cursor"],
283
- hidden: true,
298
+ title: "Submit",
284
299
  when: () => enabled("submit") && resolveSubmitKey() === "enter",
285
- handler: submit,
286
- })
300
+ });
287
301
  useCommand({
288
- id: `${commandScope}:submit-multiline`,
289
- title: "Submit",
302
+ handler: submit,
303
+ hidden: true,
290
304
  hotkey: "shift+Enter",
305
+ id: `${commandScope}:submit-multiline`,
291
306
  modes: ["insert", "cursor"],
292
- hidden: true,
307
+ title: "Submit",
293
308
  // Shift+Enter always submits (single-line editors have no newline for it
294
309
  // to mean); plain Enter only when it is the configured submit key.
295
310
  when: () => enabled("submit") && resolveSubmitKey() !== "none",
296
- handler: submit,
297
- })
311
+ });
298
312
 
299
313
  const motion = useCallback(
300
314
  (run: (target: TextareaRenderable | InputRenderable) => void) => {
301
- const target = getTarget()
302
- if (target) run(target)
303
- vimMotionStateRef.current.pendingG = false
304
- bumpScroll()
315
+ const target = getTarget();
316
+ if (target) {
317
+ run(target);
318
+ }
319
+ vimMotionStateRef.current.pendingG = false;
320
+ bumpScroll();
305
321
  },
306
322
  [getTarget, bumpScroll],
307
- )
323
+ );
308
324
 
309
325
  useCommand({
310
- id: `${commandScope}:move-left`,
311
- title: "Move left",
326
+ handler: () => {
327
+ motion((target) => {
328
+ target.moveCursorLeft();
329
+ });
330
+ },
331
+ hidden: true,
312
332
  hotkey: "h",
333
+ id: `${commandScope}:move-left`,
313
334
  modes: ["cursor"],
314
- hidden: true,
335
+ title: "Move left",
315
336
  when: () => enabled("motions"),
316
- handler: () => motion((target) => target.moveCursorLeft()),
317
- })
337
+ });
318
338
  useCommand({
319
- id: `${commandScope}:move-left-arrow`,
320
- title: "Move left",
339
+ handler: () => {
340
+ motion((target) => {
341
+ target.moveCursorLeft();
342
+ });
343
+ },
344
+ hidden: true,
321
345
  hotkey: "left",
346
+ id: `${commandScope}:move-left-arrow`,
322
347
  modes: ["cursor"],
323
- hidden: true,
348
+ title: "Move left",
324
349
  when: () => enabled("motions"),
325
- handler: () => motion((target) => target.moveCursorLeft()),
326
- })
350
+ });
327
351
  useCommand({
328
- id: `${commandScope}:move-right`,
329
- title: "Move right",
352
+ handler: () => {
353
+ motion((target) => {
354
+ target.moveCursorRight();
355
+ });
356
+ },
357
+ hidden: true,
330
358
  hotkey: "l",
359
+ id: `${commandScope}:move-right`,
331
360
  modes: ["cursor"],
332
- hidden: true,
361
+ title: "Move right",
333
362
  when: () => enabled("motions"),
334
- handler: () => motion((target) => target.moveCursorRight()),
335
- })
363
+ });
336
364
  useCommand({
337
- id: `${commandScope}:move-right-arrow`,
338
- title: "Move right",
365
+ handler: () => {
366
+ motion((target) => {
367
+ target.moveCursorRight();
368
+ });
369
+ },
370
+ hidden: true,
339
371
  hotkey: "right",
372
+ id: `${commandScope}:move-right-arrow`,
340
373
  modes: ["cursor"],
341
- hidden: true,
374
+ title: "Move right",
342
375
  when: () => enabled("motions"),
343
- handler: () => motion((target) => target.moveCursorRight()),
344
- })
376
+ });
345
377
  useCommand({
346
- id: `${commandScope}:move-down`,
347
- title: "Move down",
378
+ handler: () => {
379
+ motion((target) => {
380
+ target.moveCursorDown();
381
+ });
382
+ },
383
+ hidden: true,
348
384
  hotkey: "j",
385
+ id: `${commandScope}:move-down`,
349
386
  modes: ["cursor"],
350
- hidden: true,
387
+ title: "Move down",
351
388
  when: () => enabled("motions"),
352
- handler: () => motion((target) => target.moveCursorDown()),
353
- })
389
+ });
354
390
  useCommand({
355
- id: `${commandScope}:move-down-arrow`,
356
- title: "Move down",
391
+ handler: () => {
392
+ motion((target) => {
393
+ target.moveCursorDown();
394
+ });
395
+ },
396
+ hidden: true,
357
397
  hotkey: "down",
398
+ id: `${commandScope}:move-down-arrow`,
358
399
  modes: ["cursor"],
359
- hidden: true,
400
+ title: "Move down",
360
401
  when: () => enabled("motions"),
361
- handler: () => motion((target) => target.moveCursorDown()),
362
- })
402
+ });
363
403
  useCommand({
364
- id: `${commandScope}:move-up`,
365
- title: "Move up",
404
+ handler: () => {
405
+ motion((target) => {
406
+ target.moveCursorUp();
407
+ });
408
+ },
409
+ hidden: true,
366
410
  hotkey: "k",
411
+ id: `${commandScope}:move-up`,
367
412
  modes: ["cursor"],
368
- hidden: true,
413
+ title: "Move up",
369
414
  when: () => enabled("motions"),
370
- handler: () => motion((target) => target.moveCursorUp()),
371
- })
415
+ });
372
416
  useCommand({
373
- id: `${commandScope}:move-up-arrow`,
374
- title: "Move up",
417
+ handler: () => {
418
+ motion((target) => {
419
+ target.moveCursorUp();
420
+ });
421
+ },
422
+ hidden: true,
375
423
  hotkey: "up",
424
+ id: `${commandScope}:move-up-arrow`,
376
425
  modes: ["cursor"],
377
- hidden: true,
426
+ title: "Move up",
378
427
  when: () => enabled("motions"),
379
- handler: () => motion((target) => target.moveCursorUp()),
380
- })
428
+ });
381
429
  useCommand({
382
- id: `${commandScope}:line-home`,
383
- title: "Line home",
430
+ handler: () => {
431
+ motion((target) => {
432
+ target.gotoLineHome();
433
+ });
434
+ },
435
+ hidden: true,
384
436
  hotkey: "0",
437
+ id: `${commandScope}:line-home`,
385
438
  modes: ["cursor"],
386
- hidden: true,
439
+ title: "Line home",
387
440
  when: () => enabled("motions"),
388
- handler: () => motion((target) => target.gotoLineHome()),
389
- })
441
+ });
390
442
  useCommand({
391
- id: `${commandScope}:line-home-key`,
392
- title: "Line home",
443
+ handler: () => {
444
+ motion((target) => {
445
+ target.gotoLineHome();
446
+ });
447
+ },
448
+ hidden: true,
393
449
  hotkey: "home",
450
+ id: `${commandScope}:line-home-key`,
394
451
  modes: ["cursor"],
395
- hidden: true,
452
+ title: "Line home",
396
453
  when: () => enabled("motions"),
397
- handler: () => motion((target) => target.gotoLineHome()),
398
- })
454
+ });
399
455
  useCommand({
400
- id: `${commandScope}:line-end`,
401
- title: "Line end",
456
+ handler: () => {
457
+ motion((target) => {
458
+ target.gotoLineEnd();
459
+ });
460
+ },
461
+ hidden: true,
402
462
  hotkey: "shift+4",
463
+ id: `${commandScope}:line-end`,
403
464
  modes: ["cursor"],
404
- hidden: true,
465
+ title: "Line end",
405
466
  when: () => enabled("motions"),
406
- handler: () => motion((target) => target.gotoLineEnd()),
407
- })
467
+ });
408
468
  useCommand({
409
- id: `${commandScope}:line-end-key`,
410
- title: "Line end",
469
+ handler: () => {
470
+ motion((target) => {
471
+ target.gotoLineEnd();
472
+ });
473
+ },
474
+ hidden: true,
411
475
  hotkey: "end",
476
+ id: `${commandScope}:line-end-key`,
412
477
  modes: ["cursor"],
413
- hidden: true,
478
+ title: "Line end",
414
479
  when: () => enabled("motions"),
415
- handler: () => motion((target) => target.gotoLineEnd()),
416
- })
480
+ });
417
481
  useCommand({
418
- id: `${commandScope}:word-forward`,
419
- title: "Word forward",
482
+ handler: () => {
483
+ motion((target) => {
484
+ target.moveWordForward();
485
+ });
486
+ },
487
+ hidden: true,
420
488
  hotkey: "w",
489
+ id: `${commandScope}:word-forward`,
421
490
  modes: ["cursor"],
422
- hidden: true,
491
+ title: "Word forward",
423
492
  when: () => enabled("motions"),
424
- handler: () => motion((target) => target.moveWordForward()),
425
- })
493
+ });
426
494
  useCommand({
427
- id: `${commandScope}:word-backward`,
428
- title: "Word backward",
495
+ handler: () => {
496
+ motion((target) => {
497
+ target.moveWordBackward();
498
+ });
499
+ },
500
+ hidden: true,
429
501
  hotkey: "b",
502
+ id: `${commandScope}:word-backward`,
430
503
  modes: ["cursor"],
431
- hidden: true,
504
+ title: "Word backward",
432
505
  when: () => enabled("motions"),
433
- handler: () => motion((target) => target.moveWordBackward()),
434
- })
506
+ });
435
507
  useCommand({
436
- id: `${commandScope}:buffer-home`,
437
- title: "Buffer home",
508
+ handler: () => {
509
+ motion((target) => {
510
+ target.gotoBufferHome();
511
+ });
512
+ },
513
+ hidden: true,
438
514
  hotkey: "g g",
515
+ id: `${commandScope}:buffer-home`,
439
516
  modes: ["cursor"],
440
- hidden: true,
517
+ title: "Buffer home",
441
518
  when: () => enabled("motions"),
442
- handler: () => motion((target) => target.gotoBufferHome()),
443
- })
519
+ });
444
520
  useCommand({
445
- id: `${commandScope}:buffer-end`,
446
- title: "Buffer end",
521
+ handler: () => {
522
+ motion((target) => {
523
+ target.gotoBufferEnd();
524
+ });
525
+ },
526
+ hidden: true,
447
527
  hotkey: "shift+g",
528
+ id: `${commandScope}:buffer-end`,
448
529
  modes: ["cursor"],
449
- hidden: true,
530
+ title: "Buffer end",
450
531
  when: () => enabled("motions"),
451
- handler: () => motion((target) => target.gotoBufferEnd()),
452
- })
532
+ });
453
533
 
454
534
  // Middle-click paste from primary selection
455
535
  const handleMouseDown = useCallback(
456
536
  (event: MouseEvent) => {
457
537
  if (event.button === 1) {
458
- event.preventDefault()
459
- void readPrimaryText().then((text) => {
460
- if (!text) return
461
- getTarget()?.insertText(text)
462
- })
538
+ event.preventDefault();
539
+ void (async () => {
540
+ const text = await readPrimaryText();
541
+ if (text === undefined || text === "") {
542
+ return;
543
+ }
544
+ getTarget()?.insertText(text);
545
+ })();
463
546
  }
464
547
  },
465
548
  [getTarget],
466
- )
549
+ );
467
550
 
468
551
  const setText = useCallback(
469
552
  (text: string, opts?: { cursorToEnd?: boolean }) => {
470
- const cursorToEnd = opts?.cursorToEnd ?? true
553
+ const cursorToEnd = opts?.cursorToEnd ?? true;
471
554
  if (multilineRef.current) {
472
- const target = textareaRef.current
473
- if (!target) return
474
- const prevOffset = target.cursorOffset
475
- target.replaceText(text)
555
+ const target = textareaRef.current;
556
+ if (!target) {
557
+ return;
558
+ }
559
+ const prevOffset = target.cursorOffset;
560
+ target.replaceText(text);
476
561
  target.cursorOffset = cursorToEnd
477
562
  ? target.plainText.length
478
- : Math.min(prevOffset, target.plainText.length)
563
+ : Math.min(prevOffset, target.plainText.length);
479
564
  } else {
480
565
  // Route through React state (the input is controlled) and mirror onto
481
566
  // the renderable so the change takes effect before the next render.
482
- const target = inputRef.current
483
- const prevOffset = target?.cursorOffset ?? 0
484
- setValue(text)
567
+ const target = inputRef.current;
568
+ const prevOffset = target?.cursorOffset ?? 0;
569
+ setValue(text);
485
570
  if (target) {
486
- target.value = text
571
+ target.value = text;
487
572
  target.cursorOffset = cursorToEnd
488
573
  ? target.plainText.length
489
- : Math.min(prevOffset, target.plainText.length)
574
+ : Math.min(prevOffset, target.plainText.length);
490
575
  }
491
576
  }
492
- bumpScroll()
577
+ bumpScroll();
493
578
  },
494
579
  [bumpScroll],
495
- )
580
+ );
496
581
 
497
582
  const insertText = useCallback(
498
583
  (text: string) => {
499
- getTarget()?.insertText(text)
500
- bumpScroll()
584
+ getTarget()?.insertText(text);
585
+ bumpScroll();
501
586
  },
502
587
  [getTarget, bumpScroll],
503
- )
588
+ );
504
589
 
505
590
  const setCursorToEnd = useCallback(() => {
506
- const target = getTarget()
507
- if (!target) return
508
- target.cursorOffset = target.plainText.length
509
- bumpScroll()
510
- }, [getTarget, bumpScroll])
591
+ const target = getTarget();
592
+ if (!target) {
593
+ return;
594
+ }
595
+ target.cursorOffset = target.plainText.length;
596
+ bumpScroll();
597
+ }, [getTarget, bumpScroll]);
511
598
 
512
599
  const setModeExternal = useCallback(
513
600
  (next: Mode) => {
514
- vimMotionStateRef.current.pendingG = false
515
- setMode(next)
601
+ vimMotionStateRef.current.pendingG = false;
602
+ setMode(next);
516
603
  },
517
604
  [setMode],
518
- )
605
+ );
519
606
 
520
607
  // Stable identity so composites can capture it in refs/effects; `mode` reads
521
608
  // live through the getter.
522
- const controllerRef = useRef<AskEditorController | null>(null)
523
- if (controllerRef.current === null) {
524
- controllerRef.current = {
525
- getText,
526
- setText,
527
- insertText,
528
- setCursorToEnd,
529
- submit,
530
- get mode() {
531
- return modeRef.current
532
- },
533
- setMode: setModeExternal,
534
- }
535
- }
609
+ const controllerRef = useRef<AskEditorController | null>(null);
610
+ controllerRef.current ??= {
611
+ getText,
612
+ insertText,
613
+ get mode() {
614
+ return modeRef.current;
615
+ },
616
+ setCursorToEnd,
617
+ setMode: setModeExternal,
618
+ setText,
619
+ submit,
620
+ };
536
621
 
537
622
  return {
538
623
  controller: controllerRef.current,
539
624
  editor: {
540
- multiline,
625
+ bumpScroll,
541
626
  defaultValue,
542
- placeholder,
543
- value,
544
- onInput: setValue,
545
- textareaRef,
627
+ focused,
546
628
  inputRef,
547
629
  mode,
548
- suspended,
549
- focused,
550
- scrollRevision,
551
- bumpScroll,
630
+ multiline,
552
631
  onEditorKeyDown: preventCursorModeEditorInput,
553
632
  onEditorPaste: preventCursorModeEditorInput,
554
- submit,
633
+ onInput: setValue,
555
634
  onMouseDown: handleMouseDown,
635
+ placeholder,
636
+ scrollRevision,
637
+ submit,
638
+ suspended,
639
+ textareaRef,
640
+ value,
556
641
  },
557
- }
558
- }
642
+ };
643
+ };