@tooee/ask 0.7.2 → 0.8.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 +5 -1
- package/dist/ask-editor.d.ts +2 -1
- package/dist/ask-editor.d.ts.map +1 -1
- package/dist/ask-editor.js.map +1 -1
- package/dist/ask-panel.d.ts +5 -11
- package/dist/ask-panel.d.ts.map +1 -1
- package/dist/ask-panel.js +7 -18
- package/dist/ask-panel.js.map +1 -1
- package/dist/ask.d.ts +4 -4
- package/dist/ask.d.ts.map +1 -1
- package/dist/ask.js +6 -14
- package/dist/ask.js.map +1 -1
- package/dist/editor-scrollbar.d.ts +2 -1
- package/dist/editor-scrollbar.d.ts.map +1 -1
- package/dist/editor-scrollbar.js.map +1 -1
- package/dist/launch.d.ts +2 -2
- package/dist/launch.d.ts.map +1 -1
- package/dist/launch.js +5 -3
- package/dist/launch.js.map +1 -1
- package/dist/use-ask-dialog.d.ts.map +1 -1
- package/dist/use-ask-dialog.js +10 -74
- package/dist/use-ask-dialog.js.map +1 -1
- package/dist/use-ask-editor.d.ts +2 -2
- package/dist/use-ask-editor.d.ts.map +1 -1
- package/dist/use-ask-editor.js +229 -300
- package/dist/use-ask-editor.js.map +1 -1
- package/package.json +9 -8
- package/src/ask-editor.tsx +2 -1
- package/src/ask-panel.tsx +18 -95
- package/src/ask.tsx +9 -19
- package/src/editor-scrollbar.tsx +2 -1
- package/src/launch.tsx +19 -14
- package/src/use-ask-dialog.tsx +32 -102
- package/src/use-ask-editor.ts +271 -308
package/src/use-ask-editor.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useCallback, useEffect, useRef, useState } from "react";
|
|
1
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
2
2
|
import type {
|
|
3
3
|
InputRenderable,
|
|
4
4
|
KeyEvent,
|
|
@@ -6,28 +6,36 @@ import type {
|
|
|
6
6
|
PasteEvent,
|
|
7
7
|
TextareaRenderable,
|
|
8
8
|
} from "@opentui/core";
|
|
9
|
-
import { readPrimaryText } from "@tooee/clipboard";
|
|
9
|
+
import { copyToClipboard, readPrimaryText } from "@tooee/clipboard";
|
|
10
10
|
import {
|
|
11
11
|
useActiveCommandSurface,
|
|
12
|
-
|
|
12
|
+
useActions,
|
|
13
13
|
useCommandSurfaceId,
|
|
14
|
+
useLatest,
|
|
15
|
+
useLazyRef,
|
|
14
16
|
useMode,
|
|
15
17
|
useProvideCommandContext,
|
|
16
18
|
useSetMode,
|
|
17
19
|
} from "@tooee/commands";
|
|
18
|
-
import type { Mode } from "@tooee/commands";
|
|
20
|
+
import type { ActionDefinition, CommandContext, Mode } from "@tooee/commands";
|
|
19
21
|
import { appendAtCursor, openLineAtCursor } from "./vim-motions.js";
|
|
20
22
|
import type { VimMotionState } from "./vim-motions.js";
|
|
21
23
|
|
|
22
24
|
declare module "@tooee/commands" {
|
|
23
25
|
interface CommandContext {
|
|
24
26
|
/** Contributed by ask editors: the current input value. */
|
|
25
|
-
ask
|
|
27
|
+
ask?: { value: string };
|
|
26
28
|
}
|
|
27
29
|
}
|
|
28
30
|
|
|
29
31
|
/** Built-in command groups; disable one to take over its keys entirely. */
|
|
30
|
-
export type AskEditorCommandGroup =
|
|
32
|
+
export type AskEditorCommandGroup =
|
|
33
|
+
| "motions"
|
|
34
|
+
| "insert-commands"
|
|
35
|
+
| "copy"
|
|
36
|
+
| "submit"
|
|
37
|
+
| "cancel"
|
|
38
|
+
| "escape";
|
|
31
39
|
|
|
32
40
|
export type AskSubmitKey = "enter" | "shift+enter" | "none";
|
|
33
41
|
|
|
@@ -101,6 +109,11 @@ export interface UseAskEditorResult {
|
|
|
101
109
|
editor: AskEditorViewModel;
|
|
102
110
|
}
|
|
103
111
|
|
|
112
|
+
interface AskEditorKeymapDefinition extends Omit<ActionDefinition, "group" | "when"> {
|
|
113
|
+
group: AskEditorCommandGroup;
|
|
114
|
+
when?: () => boolean;
|
|
115
|
+
}
|
|
116
|
+
|
|
104
117
|
/**
|
|
105
118
|
* Headless ask editor core: owns editor state and registers all editing
|
|
106
119
|
* commands (vim motions, insert entries, submit/cancel/escape) on the nearest
|
|
@@ -127,14 +140,10 @@ export const useAskEditor = function useAskEditor(
|
|
|
127
140
|
setScrollRevision((r) => r + 1);
|
|
128
141
|
}, []);
|
|
129
142
|
|
|
130
|
-
const optionsRef =
|
|
131
|
-
|
|
132
|
-
const
|
|
133
|
-
|
|
134
|
-
const multilineRef = useRef(multiline);
|
|
135
|
-
multilineRef.current = multiline;
|
|
136
|
-
const valueRef = useRef(value);
|
|
137
|
-
valueRef.current = value;
|
|
143
|
+
const optionsRef = useLatest(options);
|
|
144
|
+
const modeRef = useLatest(mode);
|
|
145
|
+
const multilineRef = useLatest(multiline);
|
|
146
|
+
const valueRef = useLatest(value);
|
|
138
147
|
|
|
139
148
|
// Suspension: while a modal command surface other than our own is topmost,
|
|
140
149
|
// our commands are already suspended by the surface stack; this mirrors that
|
|
@@ -146,11 +155,14 @@ export const useAskEditor = function useAskEditor(
|
|
|
146
155
|
|
|
147
156
|
const focused = (mode === "insert" || mode === "cursor") && !suspended;
|
|
148
157
|
|
|
149
|
-
const preventCursorModeEditorInput = useCallback(
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
158
|
+
const preventCursorModeEditorInput = useCallback(
|
|
159
|
+
(event: KeyEvent | PasteEvent) => {
|
|
160
|
+
if (modeRef.current === "cursor") {
|
|
161
|
+
event.preventDefault();
|
|
162
|
+
}
|
|
163
|
+
},
|
|
164
|
+
[modeRef],
|
|
165
|
+
);
|
|
154
166
|
|
|
155
167
|
useEffect(() => {
|
|
156
168
|
if (didPositionInitialCursorRef.current || (defaultValue?.length ?? 0) === 0) {
|
|
@@ -173,7 +185,7 @@ export const useAskEditor = function useAskEditor(
|
|
|
173
185
|
|
|
174
186
|
const getTarget = useCallback(
|
|
175
187
|
() => (multilineRef.current ? textareaRef.current : inputRef.current),
|
|
176
|
-
[],
|
|
188
|
+
[multilineRef],
|
|
177
189
|
);
|
|
178
190
|
|
|
179
191
|
// Controller mutations must be observable before React state synchronizes.
|
|
@@ -182,18 +194,18 @@ export const useAskEditor = function useAskEditor(
|
|
|
182
194
|
multilineRef.current
|
|
183
195
|
? (textareaRef.current?.plainText ?? "")
|
|
184
196
|
: (inputRef.current?.plainText ?? valueRef.current),
|
|
185
|
-
[],
|
|
197
|
+
[multilineRef, valueRef],
|
|
186
198
|
);
|
|
187
199
|
|
|
188
200
|
const submit = useCallback(() => {
|
|
189
201
|
void optionsRef.current.onSubmit?.(getText());
|
|
190
|
-
}, [getText]);
|
|
202
|
+
}, [getText, optionsRef]);
|
|
191
203
|
|
|
192
204
|
useProvideCommandContext(() => ({ ask: { value: getText() } }));
|
|
193
205
|
|
|
194
206
|
const enabled = useCallback(
|
|
195
207
|
(group: AskEditorCommandGroup) => !(optionsRef.current.disable?.includes(group) ?? false),
|
|
196
|
-
[],
|
|
208
|
+
[optionsRef],
|
|
197
209
|
);
|
|
198
210
|
|
|
199
211
|
const resolveSubmitKey = useCallback((): AskSubmitKey => {
|
|
@@ -201,6 +213,49 @@ export const useAskEditor = function useAskEditor(
|
|
|
201
213
|
return current.submitKey ?? (current.multiline === true ? "shift+enter" : "enter");
|
|
202
214
|
}, []);
|
|
203
215
|
|
|
216
|
+
const copyText = useCallback(
|
|
217
|
+
(text: string, emptyMessage: string, successMessage: string, ctx: CommandContext) => {
|
|
218
|
+
if (text === "") {
|
|
219
|
+
ctx.toast?.toast({ level: "warning", message: emptyMessage });
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
void copyToClipboard(text);
|
|
223
|
+
ctx.toast?.toast({ level: "success", message: successMessage });
|
|
224
|
+
},
|
|
225
|
+
[],
|
|
226
|
+
);
|
|
227
|
+
|
|
228
|
+
const copyLine = useCallback(
|
|
229
|
+
(ctx: CommandContext) => {
|
|
230
|
+
const target = getTarget();
|
|
231
|
+
if (!target) {
|
|
232
|
+
copyText("", "Nothing to copy", "Copied line to clipboard", ctx);
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
const text = target.plainText;
|
|
236
|
+
const offset = Math.min(target.cursorOffset, text.length);
|
|
237
|
+
const start = offset === 0 ? 0 : text.lastIndexOf("\n", offset - 1) + 1;
|
|
238
|
+
const nextNewline = text.indexOf("\n", offset);
|
|
239
|
+
const end = nextNewline === -1 ? text.length : nextNewline;
|
|
240
|
+
copyText(text.slice(start, end), "Nothing to copy", "Copied line to clipboard", ctx);
|
|
241
|
+
},
|
|
242
|
+
[copyText, getTarget],
|
|
243
|
+
);
|
|
244
|
+
const copyDocument = useCallback(
|
|
245
|
+
(ctx: CommandContext) => {
|
|
246
|
+
copyText(getText(), "Nothing to copy", "Copied document to clipboard", ctx);
|
|
247
|
+
},
|
|
248
|
+
[copyText, getText],
|
|
249
|
+
);
|
|
250
|
+
const copySelection = useCallback(
|
|
251
|
+
(ctx: CommandContext) => {
|
|
252
|
+
const target = getTarget();
|
|
253
|
+
const text = target?.hasSelection() === true ? target.getSelectedText() : "";
|
|
254
|
+
copyText(text, "Nothing selected", "Copied selection to clipboard", ctx);
|
|
255
|
+
},
|
|
256
|
+
[copyText, getTarget],
|
|
257
|
+
);
|
|
258
|
+
|
|
204
259
|
const enterInsertMode = useCallback(() => {
|
|
205
260
|
vimMotionStateRef.current.pendingG = false;
|
|
206
261
|
setMode("insert");
|
|
@@ -219,7 +274,7 @@ export const useAskEditor = function useAskEditor(
|
|
|
219
274
|
vimMotionStateRef.current.pendingG = false;
|
|
220
275
|
openLineAtCursor(textareaRef.current, "above");
|
|
221
276
|
setMode("insert");
|
|
222
|
-
}, [setMode]);
|
|
277
|
+
}, [multilineRef, setMode]);
|
|
223
278
|
|
|
224
279
|
const openLineBelow = useCallback(() => {
|
|
225
280
|
if (!multilineRef.current) {
|
|
@@ -228,99 +283,13 @@ export const useAskEditor = function useAskEditor(
|
|
|
228
283
|
vimMotionStateRef.current.pendingG = false;
|
|
229
284
|
openLineAtCursor(textareaRef.current, "below");
|
|
230
285
|
setMode("insert");
|
|
231
|
-
}, [setMode]);
|
|
286
|
+
}, [multilineRef, setMode]);
|
|
232
287
|
|
|
233
288
|
const leaveInsertMode = useCallback(() => {
|
|
234
289
|
vimMotionStateRef.current.pendingG = false;
|
|
235
290
|
setMode("cursor");
|
|
236
291
|
}, [setMode]);
|
|
237
292
|
|
|
238
|
-
useCommand({
|
|
239
|
-
handler: leaveInsertMode,
|
|
240
|
-
hidden: true,
|
|
241
|
-
hotkey: "Escape",
|
|
242
|
-
id: `${commandScope}:leave-insert-mode`,
|
|
243
|
-
modes: ["insert"],
|
|
244
|
-
title: "Command mode",
|
|
245
|
-
when: () => enabled("escape"),
|
|
246
|
-
});
|
|
247
|
-
useCommand({
|
|
248
|
-
handler: () => optionsRef.current.onCancel?.(),
|
|
249
|
-
hidden: true,
|
|
250
|
-
hotkey: "q",
|
|
251
|
-
id: `${commandScope}:cancel`,
|
|
252
|
-
modes: ["cursor"],
|
|
253
|
-
title: "Cancel",
|
|
254
|
-
when: () => enabled("cancel") && optionsRef.current.onCancel !== undefined,
|
|
255
|
-
});
|
|
256
|
-
useCommand({
|
|
257
|
-
handler: enterInsertMode,
|
|
258
|
-
hidden: true,
|
|
259
|
-
hotkey: "i",
|
|
260
|
-
id: `${commandScope}:insert-mode-i`,
|
|
261
|
-
modes: ["cursor"],
|
|
262
|
-
title: "Insert mode",
|
|
263
|
-
when: () => enabled("insert-commands"),
|
|
264
|
-
});
|
|
265
|
-
useCommand({
|
|
266
|
-
handler: appendAndEnterInsertMode,
|
|
267
|
-
hidden: true,
|
|
268
|
-
hotkey: "a",
|
|
269
|
-
id: `${commandScope}:insert-mode-a`,
|
|
270
|
-
modes: ["cursor"],
|
|
271
|
-
title: "Append",
|
|
272
|
-
when: () => enabled("insert-commands"),
|
|
273
|
-
});
|
|
274
|
-
useCommand({
|
|
275
|
-
handler: openLineAbove,
|
|
276
|
-
hidden: true,
|
|
277
|
-
hotkey: "shift+o",
|
|
278
|
-
id: `${commandScope}:open-line-above`,
|
|
279
|
-
modes: ["cursor"],
|
|
280
|
-
title: "Open line above",
|
|
281
|
-
when: () => enabled("insert-commands") && multilineRef.current,
|
|
282
|
-
});
|
|
283
|
-
useCommand({
|
|
284
|
-
handler: openLineBelow,
|
|
285
|
-
hidden: true,
|
|
286
|
-
hotkey: "o",
|
|
287
|
-
id: `${commandScope}:open-line-below`,
|
|
288
|
-
modes: ["cursor"],
|
|
289
|
-
title: "Open line below",
|
|
290
|
-
when: () => enabled("insert-commands") && multilineRef.current,
|
|
291
|
-
});
|
|
292
|
-
useCommand({
|
|
293
|
-
handler: submit,
|
|
294
|
-
hidden: true,
|
|
295
|
-
hotkey: "Enter",
|
|
296
|
-
id: `${commandScope}:submit-insert`,
|
|
297
|
-
modes: ["insert"],
|
|
298
|
-
title: "Submit",
|
|
299
|
-
when: () => enabled("submit") && resolveSubmitKey() === "enter",
|
|
300
|
-
});
|
|
301
|
-
useCommand({
|
|
302
|
-
handler: submit,
|
|
303
|
-
hidden: true,
|
|
304
|
-
hotkey: "Enter",
|
|
305
|
-
id: `${commandScope}:submit-cursor`,
|
|
306
|
-
modes: ["cursor"],
|
|
307
|
-
title: "Submit",
|
|
308
|
-
// Command mode has no newline entry, so plain Enter always submits unless
|
|
309
|
-
// the consumer disabled the built-in submit command group.
|
|
310
|
-
when: () => enabled("submit") && resolveSubmitKey() !== "none",
|
|
311
|
-
});
|
|
312
|
-
useCommand({
|
|
313
|
-
handler: submit,
|
|
314
|
-
hidden: true,
|
|
315
|
-
hotkey: "shift+Enter",
|
|
316
|
-
id: `${commandScope}:submit-multiline`,
|
|
317
|
-
modes: ["insert", "cursor"],
|
|
318
|
-
title: "Submit",
|
|
319
|
-
// Shift+Enter remains available for multiline insert mode and for
|
|
320
|
-
// compatibility in cursor mode.
|
|
321
|
-
when: () => enabled("submit") && resolveSubmitKey() !== "none",
|
|
322
|
-
});
|
|
323
|
-
|
|
324
293
|
const motion = useCallback(
|
|
325
294
|
(run: (target: TextareaRenderable | InputRenderable) => void) => {
|
|
326
295
|
const target = getTarget();
|
|
@@ -333,214 +302,209 @@ export const useAskEditor = function useAskEditor(
|
|
|
333
302
|
[getTarget, bumpScroll],
|
|
334
303
|
);
|
|
335
304
|
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
305
|
+
const builtInActions = useMemo<ActionDefinition[]>(() => {
|
|
306
|
+
const motionAction = (
|
|
307
|
+
id: string,
|
|
308
|
+
hotkey: string,
|
|
309
|
+
title: string,
|
|
310
|
+
run: (target: TextareaRenderable | InputRenderable) => void,
|
|
311
|
+
): AskEditorKeymapDefinition => ({
|
|
312
|
+
group: "motions",
|
|
313
|
+
handler: () => {
|
|
314
|
+
motion(run);
|
|
315
|
+
},
|
|
316
|
+
hidden: true,
|
|
317
|
+
hotkey,
|
|
318
|
+
id: `${commandScope}:${id}`,
|
|
319
|
+
modes: ["cursor"],
|
|
320
|
+
title,
|
|
321
|
+
});
|
|
322
|
+
const definitions: AskEditorKeymapDefinition[] = [
|
|
323
|
+
{
|
|
324
|
+
group: "copy",
|
|
325
|
+
handler: copyLine,
|
|
326
|
+
hidden: true,
|
|
327
|
+
hotkey: "y y",
|
|
328
|
+
id: `${commandScope}:copy-line`,
|
|
329
|
+
modes: ["cursor"],
|
|
330
|
+
title: "Copy current line",
|
|
331
|
+
},
|
|
332
|
+
{
|
|
333
|
+
group: "copy",
|
|
334
|
+
handler: copyDocument,
|
|
335
|
+
hidden: true,
|
|
336
|
+
hotkey: "y g",
|
|
337
|
+
id: `${commandScope}:copy-document`,
|
|
338
|
+
modes: ["cursor"],
|
|
339
|
+
title: "Copy document",
|
|
340
|
+
},
|
|
341
|
+
{
|
|
342
|
+
group: "copy",
|
|
343
|
+
handler: copySelection,
|
|
344
|
+
hidden: true,
|
|
345
|
+
hotkey: "y v",
|
|
346
|
+
id: `${commandScope}:copy-selection`,
|
|
347
|
+
modes: ["cursor"],
|
|
348
|
+
title: "Copy selection",
|
|
349
|
+
},
|
|
350
|
+
{
|
|
351
|
+
group: "escape",
|
|
352
|
+
handler: leaveInsertMode,
|
|
353
|
+
hidden: true,
|
|
354
|
+
hotkey: "Escape",
|
|
355
|
+
id: `${commandScope}:leave-insert-mode`,
|
|
356
|
+
modes: ["insert"],
|
|
357
|
+
title: "Command mode",
|
|
358
|
+
},
|
|
359
|
+
{
|
|
360
|
+
group: "cancel",
|
|
361
|
+
handler: () => optionsRef.current.onCancel?.(),
|
|
362
|
+
hidden: true,
|
|
363
|
+
hotkey: "q",
|
|
364
|
+
id: `${commandScope}:cancel`,
|
|
365
|
+
modes: ["cursor"],
|
|
366
|
+
title: "Cancel",
|
|
367
|
+
when: () => optionsRef.current.onCancel !== undefined,
|
|
368
|
+
},
|
|
369
|
+
{
|
|
370
|
+
group: "insert-commands",
|
|
371
|
+
handler: enterInsertMode,
|
|
372
|
+
hidden: true,
|
|
373
|
+
hotkey: "i",
|
|
374
|
+
id: `${commandScope}:insert-mode-i`,
|
|
375
|
+
modes: ["cursor"],
|
|
376
|
+
title: "Insert mode",
|
|
377
|
+
},
|
|
378
|
+
{
|
|
379
|
+
group: "insert-commands",
|
|
380
|
+
handler: appendAndEnterInsertMode,
|
|
381
|
+
hidden: true,
|
|
382
|
+
hotkey: "a",
|
|
383
|
+
id: `${commandScope}:insert-mode-a`,
|
|
384
|
+
modes: ["cursor"],
|
|
385
|
+
title: "Append",
|
|
386
|
+
},
|
|
387
|
+
{
|
|
388
|
+
group: "insert-commands",
|
|
389
|
+
handler: openLineAbove,
|
|
390
|
+
hidden: true,
|
|
391
|
+
hotkey: "shift+o",
|
|
392
|
+
id: `${commandScope}:open-line-above`,
|
|
393
|
+
modes: ["cursor"],
|
|
394
|
+
title: "Open line above",
|
|
395
|
+
when: () => multilineRef.current,
|
|
396
|
+
},
|
|
397
|
+
{
|
|
398
|
+
group: "insert-commands",
|
|
399
|
+
handler: openLineBelow,
|
|
400
|
+
hidden: true,
|
|
401
|
+
hotkey: "o",
|
|
402
|
+
id: `${commandScope}:open-line-below`,
|
|
403
|
+
modes: ["cursor"],
|
|
404
|
+
title: "Open line below",
|
|
405
|
+
when: () => multilineRef.current,
|
|
406
|
+
},
|
|
407
|
+
{
|
|
408
|
+
group: "submit",
|
|
409
|
+
handler: submit,
|
|
410
|
+
hidden: true,
|
|
411
|
+
hotkey: "Enter",
|
|
412
|
+
id: `${commandScope}:submit-insert`,
|
|
413
|
+
modes: ["insert"],
|
|
414
|
+
title: "Submit",
|
|
415
|
+
when: () => resolveSubmitKey() === "enter",
|
|
416
|
+
},
|
|
417
|
+
{
|
|
418
|
+
group: "submit",
|
|
419
|
+
handler: submit,
|
|
420
|
+
hidden: true,
|
|
421
|
+
hotkey: "Enter",
|
|
422
|
+
id: `${commandScope}:submit-cursor`,
|
|
423
|
+
modes: ["cursor"],
|
|
424
|
+
title: "Submit",
|
|
425
|
+
when: () => resolveSubmitKey() !== "none",
|
|
426
|
+
},
|
|
427
|
+
{
|
|
428
|
+
group: "submit",
|
|
429
|
+
handler: submit,
|
|
430
|
+
hidden: true,
|
|
431
|
+
hotkey: "shift+Enter",
|
|
432
|
+
id: `${commandScope}:submit-multiline`,
|
|
433
|
+
modes: ["insert", "cursor"],
|
|
434
|
+
title: "Submit",
|
|
435
|
+
when: () => resolveSubmitKey() !== "none",
|
|
436
|
+
},
|
|
437
|
+
motionAction("move-left", "h", "Move left", (target) => {
|
|
339
438
|
target.moveCursorLeft();
|
|
340
|
-
})
|
|
341
|
-
|
|
342
|
-
hidden: true,
|
|
343
|
-
hotkey: "h",
|
|
344
|
-
id: `${commandScope}:move-left`,
|
|
345
|
-
modes: ["cursor"],
|
|
346
|
-
title: "Move left",
|
|
347
|
-
when: () => enabled("motions"),
|
|
348
|
-
});
|
|
349
|
-
useCommand({
|
|
350
|
-
handler: () => {
|
|
351
|
-
motion((target) => {
|
|
439
|
+
}),
|
|
440
|
+
motionAction("move-left-arrow", "left", "Move left", (target) => {
|
|
352
441
|
target.moveCursorLeft();
|
|
353
|
-
})
|
|
354
|
-
|
|
355
|
-
hidden: true,
|
|
356
|
-
hotkey: "left",
|
|
357
|
-
id: `${commandScope}:move-left-arrow`,
|
|
358
|
-
modes: ["cursor"],
|
|
359
|
-
title: "Move left",
|
|
360
|
-
when: () => enabled("motions"),
|
|
361
|
-
});
|
|
362
|
-
useCommand({
|
|
363
|
-
handler: () => {
|
|
364
|
-
motion((target) => {
|
|
442
|
+
}),
|
|
443
|
+
motionAction("move-right", "l", "Move right", (target) => {
|
|
365
444
|
target.moveCursorRight();
|
|
366
|
-
})
|
|
367
|
-
|
|
368
|
-
hidden: true,
|
|
369
|
-
hotkey: "l",
|
|
370
|
-
id: `${commandScope}:move-right`,
|
|
371
|
-
modes: ["cursor"],
|
|
372
|
-
title: "Move right",
|
|
373
|
-
when: () => enabled("motions"),
|
|
374
|
-
});
|
|
375
|
-
useCommand({
|
|
376
|
-
handler: () => {
|
|
377
|
-
motion((target) => {
|
|
445
|
+
}),
|
|
446
|
+
motionAction("move-right-arrow", "right", "Move right", (target) => {
|
|
378
447
|
target.moveCursorRight();
|
|
379
|
-
})
|
|
380
|
-
|
|
381
|
-
hidden: true,
|
|
382
|
-
hotkey: "right",
|
|
383
|
-
id: `${commandScope}:move-right-arrow`,
|
|
384
|
-
modes: ["cursor"],
|
|
385
|
-
title: "Move right",
|
|
386
|
-
when: () => enabled("motions"),
|
|
387
|
-
});
|
|
388
|
-
useCommand({
|
|
389
|
-
handler: () => {
|
|
390
|
-
motion((target) => {
|
|
448
|
+
}),
|
|
449
|
+
motionAction("move-down", "j", "Move down", (target) => {
|
|
391
450
|
target.moveCursorDown();
|
|
392
|
-
})
|
|
393
|
-
|
|
394
|
-
hidden: true,
|
|
395
|
-
hotkey: "j",
|
|
396
|
-
id: `${commandScope}:move-down`,
|
|
397
|
-
modes: ["cursor"],
|
|
398
|
-
title: "Move down",
|
|
399
|
-
when: () => enabled("motions"),
|
|
400
|
-
});
|
|
401
|
-
useCommand({
|
|
402
|
-
handler: () => {
|
|
403
|
-
motion((target) => {
|
|
451
|
+
}),
|
|
452
|
+
motionAction("move-down-arrow", "down", "Move down", (target) => {
|
|
404
453
|
target.moveCursorDown();
|
|
405
|
-
})
|
|
406
|
-
|
|
407
|
-
hidden: true,
|
|
408
|
-
hotkey: "down",
|
|
409
|
-
id: `${commandScope}:move-down-arrow`,
|
|
410
|
-
modes: ["cursor"],
|
|
411
|
-
title: "Move down",
|
|
412
|
-
when: () => enabled("motions"),
|
|
413
|
-
});
|
|
414
|
-
useCommand({
|
|
415
|
-
handler: () => {
|
|
416
|
-
motion((target) => {
|
|
454
|
+
}),
|
|
455
|
+
motionAction("move-up", "k", "Move up", (target) => {
|
|
417
456
|
target.moveCursorUp();
|
|
418
|
-
})
|
|
419
|
-
|
|
420
|
-
hidden: true,
|
|
421
|
-
hotkey: "k",
|
|
422
|
-
id: `${commandScope}:move-up`,
|
|
423
|
-
modes: ["cursor"],
|
|
424
|
-
title: "Move up",
|
|
425
|
-
when: () => enabled("motions"),
|
|
426
|
-
});
|
|
427
|
-
useCommand({
|
|
428
|
-
handler: () => {
|
|
429
|
-
motion((target) => {
|
|
457
|
+
}),
|
|
458
|
+
motionAction("move-up-arrow", "up", "Move up", (target) => {
|
|
430
459
|
target.moveCursorUp();
|
|
431
|
-
})
|
|
432
|
-
|
|
433
|
-
hidden: true,
|
|
434
|
-
hotkey: "up",
|
|
435
|
-
id: `${commandScope}:move-up-arrow`,
|
|
436
|
-
modes: ["cursor"],
|
|
437
|
-
title: "Move up",
|
|
438
|
-
when: () => enabled("motions"),
|
|
439
|
-
});
|
|
440
|
-
useCommand({
|
|
441
|
-
handler: () => {
|
|
442
|
-
motion((target) => {
|
|
460
|
+
}),
|
|
461
|
+
motionAction("line-home", "0", "Line home", (target) => {
|
|
443
462
|
target.gotoLineHome();
|
|
444
|
-
})
|
|
445
|
-
|
|
446
|
-
hidden: true,
|
|
447
|
-
hotkey: "0",
|
|
448
|
-
id: `${commandScope}:line-home`,
|
|
449
|
-
modes: ["cursor"],
|
|
450
|
-
title: "Line home",
|
|
451
|
-
when: () => enabled("motions"),
|
|
452
|
-
});
|
|
453
|
-
useCommand({
|
|
454
|
-
handler: () => {
|
|
455
|
-
motion((target) => {
|
|
463
|
+
}),
|
|
464
|
+
motionAction("line-home-key", "home", "Line home", (target) => {
|
|
456
465
|
target.gotoLineHome();
|
|
457
|
-
})
|
|
458
|
-
|
|
459
|
-
hidden: true,
|
|
460
|
-
hotkey: "home",
|
|
461
|
-
id: `${commandScope}:line-home-key`,
|
|
462
|
-
modes: ["cursor"],
|
|
463
|
-
title: "Line home",
|
|
464
|
-
when: () => enabled("motions"),
|
|
465
|
-
});
|
|
466
|
-
useCommand({
|
|
467
|
-
handler: () => {
|
|
468
|
-
motion((target) => {
|
|
466
|
+
}),
|
|
467
|
+
motionAction("line-end", "shift+4", "Line end", (target) => {
|
|
469
468
|
target.gotoLineEnd();
|
|
470
|
-
})
|
|
471
|
-
|
|
472
|
-
hidden: true,
|
|
473
|
-
hotkey: "shift+4",
|
|
474
|
-
id: `${commandScope}:line-end`,
|
|
475
|
-
modes: ["cursor"],
|
|
476
|
-
title: "Line end",
|
|
477
|
-
when: () => enabled("motions"),
|
|
478
|
-
});
|
|
479
|
-
useCommand({
|
|
480
|
-
handler: () => {
|
|
481
|
-
motion((target) => {
|
|
469
|
+
}),
|
|
470
|
+
motionAction("line-end-key", "end", "Line end", (target) => {
|
|
482
471
|
target.gotoLineEnd();
|
|
483
|
-
})
|
|
484
|
-
|
|
485
|
-
hidden: true,
|
|
486
|
-
hotkey: "end",
|
|
487
|
-
id: `${commandScope}:line-end-key`,
|
|
488
|
-
modes: ["cursor"],
|
|
489
|
-
title: "Line end",
|
|
490
|
-
when: () => enabled("motions"),
|
|
491
|
-
});
|
|
492
|
-
useCommand({
|
|
493
|
-
handler: () => {
|
|
494
|
-
motion((target) => {
|
|
472
|
+
}),
|
|
473
|
+
motionAction("word-forward", "w", "Word forward", (target) => {
|
|
495
474
|
target.moveWordForward();
|
|
496
|
-
})
|
|
497
|
-
|
|
498
|
-
hidden: true,
|
|
499
|
-
hotkey: "w",
|
|
500
|
-
id: `${commandScope}:word-forward`,
|
|
501
|
-
modes: ["cursor"],
|
|
502
|
-
title: "Word forward",
|
|
503
|
-
when: () => enabled("motions"),
|
|
504
|
-
});
|
|
505
|
-
useCommand({
|
|
506
|
-
handler: () => {
|
|
507
|
-
motion((target) => {
|
|
475
|
+
}),
|
|
476
|
+
motionAction("word-backward", "b", "Word backward", (target) => {
|
|
508
477
|
target.moveWordBackward();
|
|
509
|
-
})
|
|
510
|
-
|
|
511
|
-
hidden: true,
|
|
512
|
-
hotkey: "b",
|
|
513
|
-
id: `${commandScope}:word-backward`,
|
|
514
|
-
modes: ["cursor"],
|
|
515
|
-
title: "Word backward",
|
|
516
|
-
when: () => enabled("motions"),
|
|
517
|
-
});
|
|
518
|
-
useCommand({
|
|
519
|
-
handler: () => {
|
|
520
|
-
motion((target) => {
|
|
478
|
+
}),
|
|
479
|
+
motionAction("buffer-home", "g g", "Buffer home", (target) => {
|
|
521
480
|
target.gotoBufferHome();
|
|
522
|
-
})
|
|
523
|
-
|
|
524
|
-
hidden: true,
|
|
525
|
-
hotkey: "g g",
|
|
526
|
-
id: `${commandScope}:buffer-home`,
|
|
527
|
-
modes: ["cursor"],
|
|
528
|
-
title: "Buffer home",
|
|
529
|
-
when: () => enabled("motions"),
|
|
530
|
-
});
|
|
531
|
-
useCommand({
|
|
532
|
-
handler: () => {
|
|
533
|
-
motion((target) => {
|
|
481
|
+
}),
|
|
482
|
+
motionAction("buffer-end", "shift+g", "Buffer end", (target) => {
|
|
534
483
|
target.gotoBufferEnd();
|
|
535
|
-
})
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
484
|
+
}),
|
|
485
|
+
];
|
|
486
|
+
return definitions.map(({ group, when, ...definition }) => ({
|
|
487
|
+
...definition,
|
|
488
|
+
when: () => enabled(group) && (when?.() ?? true),
|
|
489
|
+
}));
|
|
490
|
+
}, [
|
|
491
|
+
appendAndEnterInsertMode,
|
|
492
|
+
commandScope,
|
|
493
|
+
copyDocument,
|
|
494
|
+
copyLine,
|
|
495
|
+
copySelection,
|
|
496
|
+
enabled,
|
|
497
|
+
enterInsertMode,
|
|
498
|
+
leaveInsertMode,
|
|
499
|
+
motion,
|
|
500
|
+
multilineRef,
|
|
501
|
+
openLineAbove,
|
|
502
|
+
openLineBelow,
|
|
503
|
+
optionsRef,
|
|
504
|
+
resolveSubmitKey,
|
|
505
|
+
submit,
|
|
506
|
+
]);
|
|
507
|
+
useActions(builtInActions);
|
|
544
508
|
|
|
545
509
|
// Middle-click paste from primary selection
|
|
546
510
|
const handleMouseDown = useCallback(
|
|
@@ -587,7 +551,7 @@ export const useAskEditor = function useAskEditor(
|
|
|
587
551
|
}
|
|
588
552
|
bumpScroll();
|
|
589
553
|
},
|
|
590
|
-
[bumpScroll],
|
|
554
|
+
[bumpScroll, multilineRef],
|
|
591
555
|
);
|
|
592
556
|
|
|
593
557
|
const insertText = useCallback(
|
|
@@ -617,8 +581,7 @@ export const useAskEditor = function useAskEditor(
|
|
|
617
581
|
|
|
618
582
|
// Stable identity so composites can capture it in refs/effects; `mode` reads
|
|
619
583
|
// live through the getter.
|
|
620
|
-
const
|
|
621
|
-
controllerRef.current ??= {
|
|
584
|
+
const controller = useLazyRef<AskEditorController>(() => ({
|
|
622
585
|
getText,
|
|
623
586
|
insertText,
|
|
624
587
|
get mode() {
|
|
@@ -628,10 +591,10 @@ export const useAskEditor = function useAskEditor(
|
|
|
628
591
|
setMode: setModeExternal,
|
|
629
592
|
setText,
|
|
630
593
|
submit,
|
|
631
|
-
};
|
|
594
|
+
})).current;
|
|
632
595
|
|
|
633
596
|
return {
|
|
634
|
-
controller
|
|
597
|
+
controller,
|
|
635
598
|
editor: {
|
|
636
599
|
bumpScroll,
|
|
637
600
|
defaultValue,
|