@tooee/ask 0.7.3 → 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.
@@ -1,6 +1,6 @@
1
- import { useCallback, useEffect, useRef, useState } from "react";
1
+ import { useCallback, useEffect, useMemo, useRef, useState } from "react";
2
2
  import { copyToClipboard, readPrimaryText } from "@tooee/clipboard";
3
- import { useActiveCommandSurface, useCommand, useCommandSurfaceId, useMode, useProvideCommandContext, useSetMode, } from "@tooee/commands";
3
+ import { useActiveCommandSurface, useActions, useCommandSurfaceId, useLatest, useLazyRef, useMode, useProvideCommandContext, useSetMode, } from "@tooee/commands";
4
4
  import { appendAtCursor, openLineAtCursor } from "./vim-motions.js";
5
5
  /**
6
6
  * Headless ask editor core: owns editor state and registers all editing
@@ -23,14 +23,10 @@ export const useAskEditor = function useAskEditor(options = {}) {
23
23
  const bumpScroll = useCallback(() => {
24
24
  setScrollRevision((r) => r + 1);
25
25
  }, []);
26
- const optionsRef = useRef(options);
27
- optionsRef.current = options;
28
- const modeRef = useRef(mode);
29
- modeRef.current = mode;
30
- const multilineRef = useRef(multiline);
31
- multilineRef.current = multiline;
32
- const valueRef = useRef(value);
33
- valueRef.current = value;
26
+ const optionsRef = useLatest(options);
27
+ const modeRef = useLatest(mode);
28
+ const multilineRef = useLatest(multiline);
29
+ const valueRef = useLatest(value);
34
30
  // Suspension: while a modal command surface other than our own is topmost,
35
31
  // our commands are already suspended by the surface stack; this mirrors that
36
32
  // for renderable focus so the editor blurs under a nested picker.
@@ -42,7 +38,7 @@ export const useAskEditor = function useAskEditor(options = {}) {
42
38
  if (modeRef.current === "cursor") {
43
39
  event.preventDefault();
44
40
  }
45
- }, []);
41
+ }, [modeRef]);
46
42
  useEffect(() => {
47
43
  if (didPositionInitialCursorRef.current || (defaultValue?.length ?? 0) === 0) {
48
44
  return;
@@ -58,73 +54,49 @@ export const useAskEditor = function useAskEditor(options = {}) {
58
54
  useEffect(() => {
59
55
  bumpScroll();
60
56
  }, [bumpScroll]);
61
- const getTarget = useCallback(() => (multilineRef.current ? textareaRef.current : inputRef.current), []);
57
+ const getTarget = useCallback(() => (multilineRef.current ? textareaRef.current : inputRef.current), [multilineRef]);
62
58
  // Controller mutations must be observable before React state synchronizes.
63
59
  const getText = useCallback(() => multilineRef.current
64
60
  ? (textareaRef.current?.plainText ?? "")
65
- : (inputRef.current?.plainText ?? valueRef.current), []);
61
+ : (inputRef.current?.plainText ?? valueRef.current), [multilineRef, valueRef]);
66
62
  const submit = useCallback(() => {
67
63
  void optionsRef.current.onSubmit?.(getText());
68
- }, [getText]);
64
+ }, [getText, optionsRef]);
69
65
  useProvideCommandContext(() => ({ ask: { value: getText() } }));
70
- const enabled = useCallback((group) => !(optionsRef.current.disable?.includes(group) ?? false), []);
66
+ const enabled = useCallback((group) => !(optionsRef.current.disable?.includes(group) ?? false), [optionsRef]);
71
67
  const resolveSubmitKey = useCallback(() => {
72
68
  const { current } = optionsRef;
73
69
  return current.submitKey ?? (current.multiline === true ? "shift+enter" : "enter");
74
70
  }, []);
75
71
  const copyText = useCallback((text, emptyMessage, successMessage, ctx) => {
76
72
  if (text === "") {
77
- ctx.toast.toast({ level: "warning", message: emptyMessage });
73
+ ctx.toast?.toast({ level: "warning", message: emptyMessage });
78
74
  return;
79
75
  }
80
76
  void copyToClipboard(text);
81
- ctx.toast.toast({ level: "success", message: successMessage });
77
+ ctx.toast?.toast({ level: "success", message: successMessage });
82
78
  }, []);
83
- useCommand({
84
- handler: (ctx) => {
85
- const target = getTarget();
86
- if (!target) {
87
- copyText("", "Nothing to copy", "Copied line to clipboard", ctx);
88
- return;
89
- }
90
- const text = target.plainText;
91
- const offset = Math.min(target.cursorOffset, text.length);
92
- const start = offset === 0 ? 0 : text.lastIndexOf("\n", offset - 1) + 1;
93
- const nextNewline = text.indexOf("\n", offset);
94
- const end = nextNewline === -1 ? text.length : nextNewline;
95
- copyText(text.slice(start, end), "Nothing to copy", "Copied line to clipboard", ctx);
96
- },
97
- hidden: true,
98
- hotkey: "y y",
99
- id: `${commandScope}:copy-line`,
100
- modes: ["cursor"],
101
- title: "Copy current line",
102
- when: () => enabled("copy"),
103
- });
104
- useCommand({
105
- handler: (ctx) => {
106
- copyText(getText(), "Nothing to copy", "Copied document to clipboard", ctx);
107
- },
108
- hidden: true,
109
- hotkey: "y g",
110
- id: `${commandScope}:copy-document`,
111
- modes: ["cursor"],
112
- title: "Copy document",
113
- when: () => enabled("copy"),
114
- });
115
- useCommand({
116
- handler: (ctx) => {
117
- const target = getTarget();
118
- const text = target?.hasSelection() === true ? target.getSelectedText() : "";
119
- copyText(text, "Nothing selected", "Copied selection to clipboard", ctx);
120
- },
121
- hidden: true,
122
- hotkey: "y v",
123
- id: `${commandScope}:copy-selection`,
124
- modes: ["cursor"],
125
- title: "Copy selection",
126
- when: () => enabled("copy"),
127
- });
79
+ const copyLine = useCallback((ctx) => {
80
+ const target = getTarget();
81
+ if (!target) {
82
+ copyText("", "Nothing to copy", "Copied line to clipboard", ctx);
83
+ return;
84
+ }
85
+ const text = target.plainText;
86
+ const offset = Math.min(target.cursorOffset, text.length);
87
+ const start = offset === 0 ? 0 : text.lastIndexOf("\n", offset - 1) + 1;
88
+ const nextNewline = text.indexOf("\n", offset);
89
+ const end = nextNewline === -1 ? text.length : nextNewline;
90
+ copyText(text.slice(start, end), "Nothing to copy", "Copied line to clipboard", ctx);
91
+ }, [copyText, getTarget]);
92
+ const copyDocument = useCallback((ctx) => {
93
+ copyText(getText(), "Nothing to copy", "Copied document to clipboard", ctx);
94
+ }, [copyText, getText]);
95
+ const copySelection = useCallback((ctx) => {
96
+ const target = getTarget();
97
+ const text = target?.hasSelection() === true ? target.getSelectedText() : "";
98
+ copyText(text, "Nothing selected", "Copied selection to clipboard", ctx);
99
+ }, [copyText, getTarget]);
128
100
  const enterInsertMode = useCallback(() => {
129
101
  vimMotionStateRef.current.pendingG = false;
130
102
  setMode("insert");
@@ -141,7 +113,7 @@ export const useAskEditor = function useAskEditor(options = {}) {
141
113
  vimMotionStateRef.current.pendingG = false;
142
114
  openLineAtCursor(textareaRef.current, "above");
143
115
  setMode("insert");
144
- }, [setMode]);
116
+ }, [multilineRef, setMode]);
145
117
  const openLineBelow = useCallback(() => {
146
118
  if (!multilineRef.current) {
147
119
  return;
@@ -149,96 +121,11 @@ export const useAskEditor = function useAskEditor(options = {}) {
149
121
  vimMotionStateRef.current.pendingG = false;
150
122
  openLineAtCursor(textareaRef.current, "below");
151
123
  setMode("insert");
152
- }, [setMode]);
124
+ }, [multilineRef, setMode]);
153
125
  const leaveInsertMode = useCallback(() => {
154
126
  vimMotionStateRef.current.pendingG = false;
155
127
  setMode("cursor");
156
128
  }, [setMode]);
157
- useCommand({
158
- handler: leaveInsertMode,
159
- hidden: true,
160
- hotkey: "Escape",
161
- id: `${commandScope}:leave-insert-mode`,
162
- modes: ["insert"],
163
- title: "Command mode",
164
- when: () => enabled("escape"),
165
- });
166
- useCommand({
167
- handler: () => optionsRef.current.onCancel?.(),
168
- hidden: true,
169
- hotkey: "q",
170
- id: `${commandScope}:cancel`,
171
- modes: ["cursor"],
172
- title: "Cancel",
173
- when: () => enabled("cancel") && optionsRef.current.onCancel !== undefined,
174
- });
175
- useCommand({
176
- handler: enterInsertMode,
177
- hidden: true,
178
- hotkey: "i",
179
- id: `${commandScope}:insert-mode-i`,
180
- modes: ["cursor"],
181
- title: "Insert mode",
182
- when: () => enabled("insert-commands"),
183
- });
184
- useCommand({
185
- handler: appendAndEnterInsertMode,
186
- hidden: true,
187
- hotkey: "a",
188
- id: `${commandScope}:insert-mode-a`,
189
- modes: ["cursor"],
190
- title: "Append",
191
- when: () => enabled("insert-commands"),
192
- });
193
- useCommand({
194
- handler: openLineAbove,
195
- hidden: true,
196
- hotkey: "shift+o",
197
- id: `${commandScope}:open-line-above`,
198
- modes: ["cursor"],
199
- title: "Open line above",
200
- when: () => enabled("insert-commands") && multilineRef.current,
201
- });
202
- useCommand({
203
- handler: openLineBelow,
204
- hidden: true,
205
- hotkey: "o",
206
- id: `${commandScope}:open-line-below`,
207
- modes: ["cursor"],
208
- title: "Open line below",
209
- when: () => enabled("insert-commands") && multilineRef.current,
210
- });
211
- useCommand({
212
- handler: submit,
213
- hidden: true,
214
- hotkey: "Enter",
215
- id: `${commandScope}:submit-insert`,
216
- modes: ["insert"],
217
- title: "Submit",
218
- when: () => enabled("submit") && resolveSubmitKey() === "enter",
219
- });
220
- useCommand({
221
- handler: submit,
222
- hidden: true,
223
- hotkey: "Enter",
224
- id: `${commandScope}:submit-cursor`,
225
- modes: ["cursor"],
226
- title: "Submit",
227
- // Command mode has no newline entry, so plain Enter always submits unless
228
- // the consumer disabled the built-in submit command group.
229
- when: () => enabled("submit") && resolveSubmitKey() !== "none",
230
- });
231
- useCommand({
232
- handler: submit,
233
- hidden: true,
234
- hotkey: "shift+Enter",
235
- id: `${commandScope}:submit-multiline`,
236
- modes: ["insert", "cursor"],
237
- title: "Submit",
238
- // Shift+Enter remains available for multiline insert mode and for
239
- // compatibility in cursor mode.
240
- when: () => enabled("submit") && resolveSubmitKey() !== "none",
241
- });
242
129
  const motion = useCallback((run) => {
243
130
  const target = getTarget();
244
131
  if (target) {
@@ -247,214 +134,204 @@ export const useAskEditor = function useAskEditor(options = {}) {
247
134
  vimMotionStateRef.current.pendingG = false;
248
135
  bumpScroll();
249
136
  }, [getTarget, bumpScroll]);
250
- useCommand({
251
- handler: () => {
252
- motion((target) => {
137
+ const builtInActions = useMemo(() => {
138
+ const motionAction = (id, hotkey, title, run) => ({
139
+ group: "motions",
140
+ handler: () => {
141
+ motion(run);
142
+ },
143
+ hidden: true,
144
+ hotkey,
145
+ id: `${commandScope}:${id}`,
146
+ modes: ["cursor"],
147
+ title,
148
+ });
149
+ const definitions = [
150
+ {
151
+ group: "copy",
152
+ handler: copyLine,
153
+ hidden: true,
154
+ hotkey: "y y",
155
+ id: `${commandScope}:copy-line`,
156
+ modes: ["cursor"],
157
+ title: "Copy current line",
158
+ },
159
+ {
160
+ group: "copy",
161
+ handler: copyDocument,
162
+ hidden: true,
163
+ hotkey: "y g",
164
+ id: `${commandScope}:copy-document`,
165
+ modes: ["cursor"],
166
+ title: "Copy document",
167
+ },
168
+ {
169
+ group: "copy",
170
+ handler: copySelection,
171
+ hidden: true,
172
+ hotkey: "y v",
173
+ id: `${commandScope}:copy-selection`,
174
+ modes: ["cursor"],
175
+ title: "Copy selection",
176
+ },
177
+ {
178
+ group: "escape",
179
+ handler: leaveInsertMode,
180
+ hidden: true,
181
+ hotkey: "Escape",
182
+ id: `${commandScope}:leave-insert-mode`,
183
+ modes: ["insert"],
184
+ title: "Command mode",
185
+ },
186
+ {
187
+ group: "cancel",
188
+ handler: () => optionsRef.current.onCancel?.(),
189
+ hidden: true,
190
+ hotkey: "q",
191
+ id: `${commandScope}:cancel`,
192
+ modes: ["cursor"],
193
+ title: "Cancel",
194
+ when: () => optionsRef.current.onCancel !== undefined,
195
+ },
196
+ {
197
+ group: "insert-commands",
198
+ handler: enterInsertMode,
199
+ hidden: true,
200
+ hotkey: "i",
201
+ id: `${commandScope}:insert-mode-i`,
202
+ modes: ["cursor"],
203
+ title: "Insert mode",
204
+ },
205
+ {
206
+ group: "insert-commands",
207
+ handler: appendAndEnterInsertMode,
208
+ hidden: true,
209
+ hotkey: "a",
210
+ id: `${commandScope}:insert-mode-a`,
211
+ modes: ["cursor"],
212
+ title: "Append",
213
+ },
214
+ {
215
+ group: "insert-commands",
216
+ handler: openLineAbove,
217
+ hidden: true,
218
+ hotkey: "shift+o",
219
+ id: `${commandScope}:open-line-above`,
220
+ modes: ["cursor"],
221
+ title: "Open line above",
222
+ when: () => multilineRef.current,
223
+ },
224
+ {
225
+ group: "insert-commands",
226
+ handler: openLineBelow,
227
+ hidden: true,
228
+ hotkey: "o",
229
+ id: `${commandScope}:open-line-below`,
230
+ modes: ["cursor"],
231
+ title: "Open line below",
232
+ when: () => multilineRef.current,
233
+ },
234
+ {
235
+ group: "submit",
236
+ handler: submit,
237
+ hidden: true,
238
+ hotkey: "Enter",
239
+ id: `${commandScope}:submit-insert`,
240
+ modes: ["insert"],
241
+ title: "Submit",
242
+ when: () => resolveSubmitKey() === "enter",
243
+ },
244
+ {
245
+ group: "submit",
246
+ handler: submit,
247
+ hidden: true,
248
+ hotkey: "Enter",
249
+ id: `${commandScope}:submit-cursor`,
250
+ modes: ["cursor"],
251
+ title: "Submit",
252
+ when: () => resolveSubmitKey() !== "none",
253
+ },
254
+ {
255
+ group: "submit",
256
+ handler: submit,
257
+ hidden: true,
258
+ hotkey: "shift+Enter",
259
+ id: `${commandScope}:submit-multiline`,
260
+ modes: ["insert", "cursor"],
261
+ title: "Submit",
262
+ when: () => resolveSubmitKey() !== "none",
263
+ },
264
+ motionAction("move-left", "h", "Move left", (target) => {
253
265
  target.moveCursorLeft();
254
- });
255
- },
256
- hidden: true,
257
- hotkey: "h",
258
- id: `${commandScope}:move-left`,
259
- modes: ["cursor"],
260
- title: "Move left",
261
- when: () => enabled("motions"),
262
- });
263
- useCommand({
264
- handler: () => {
265
- motion((target) => {
266
+ }),
267
+ motionAction("move-left-arrow", "left", "Move left", (target) => {
266
268
  target.moveCursorLeft();
267
- });
268
- },
269
- hidden: true,
270
- hotkey: "left",
271
- id: `${commandScope}:move-left-arrow`,
272
- modes: ["cursor"],
273
- title: "Move left",
274
- when: () => enabled("motions"),
275
- });
276
- useCommand({
277
- handler: () => {
278
- motion((target) => {
269
+ }),
270
+ motionAction("move-right", "l", "Move right", (target) => {
279
271
  target.moveCursorRight();
280
- });
281
- },
282
- hidden: true,
283
- hotkey: "l",
284
- id: `${commandScope}:move-right`,
285
- modes: ["cursor"],
286
- title: "Move right",
287
- when: () => enabled("motions"),
288
- });
289
- useCommand({
290
- handler: () => {
291
- motion((target) => {
272
+ }),
273
+ motionAction("move-right-arrow", "right", "Move right", (target) => {
292
274
  target.moveCursorRight();
293
- });
294
- },
295
- hidden: true,
296
- hotkey: "right",
297
- id: `${commandScope}:move-right-arrow`,
298
- modes: ["cursor"],
299
- title: "Move right",
300
- when: () => enabled("motions"),
301
- });
302
- useCommand({
303
- handler: () => {
304
- motion((target) => {
275
+ }),
276
+ motionAction("move-down", "j", "Move down", (target) => {
305
277
  target.moveCursorDown();
306
- });
307
- },
308
- hidden: true,
309
- hotkey: "j",
310
- id: `${commandScope}:move-down`,
311
- modes: ["cursor"],
312
- title: "Move down",
313
- when: () => enabled("motions"),
314
- });
315
- useCommand({
316
- handler: () => {
317
- motion((target) => {
278
+ }),
279
+ motionAction("move-down-arrow", "down", "Move down", (target) => {
318
280
  target.moveCursorDown();
319
- });
320
- },
321
- hidden: true,
322
- hotkey: "down",
323
- id: `${commandScope}:move-down-arrow`,
324
- modes: ["cursor"],
325
- title: "Move down",
326
- when: () => enabled("motions"),
327
- });
328
- useCommand({
329
- handler: () => {
330
- motion((target) => {
281
+ }),
282
+ motionAction("move-up", "k", "Move up", (target) => {
331
283
  target.moveCursorUp();
332
- });
333
- },
334
- hidden: true,
335
- hotkey: "k",
336
- id: `${commandScope}:move-up`,
337
- modes: ["cursor"],
338
- title: "Move up",
339
- when: () => enabled("motions"),
340
- });
341
- useCommand({
342
- handler: () => {
343
- motion((target) => {
284
+ }),
285
+ motionAction("move-up-arrow", "up", "Move up", (target) => {
344
286
  target.moveCursorUp();
345
- });
346
- },
347
- hidden: true,
348
- hotkey: "up",
349
- id: `${commandScope}:move-up-arrow`,
350
- modes: ["cursor"],
351
- title: "Move up",
352
- when: () => enabled("motions"),
353
- });
354
- useCommand({
355
- handler: () => {
356
- motion((target) => {
287
+ }),
288
+ motionAction("line-home", "0", "Line home", (target) => {
357
289
  target.gotoLineHome();
358
- });
359
- },
360
- hidden: true,
361
- hotkey: "0",
362
- id: `${commandScope}:line-home`,
363
- modes: ["cursor"],
364
- title: "Line home",
365
- when: () => enabled("motions"),
366
- });
367
- useCommand({
368
- handler: () => {
369
- motion((target) => {
290
+ }),
291
+ motionAction("line-home-key", "home", "Line home", (target) => {
370
292
  target.gotoLineHome();
371
- });
372
- },
373
- hidden: true,
374
- hotkey: "home",
375
- id: `${commandScope}:line-home-key`,
376
- modes: ["cursor"],
377
- title: "Line home",
378
- when: () => enabled("motions"),
379
- });
380
- useCommand({
381
- handler: () => {
382
- motion((target) => {
293
+ }),
294
+ motionAction("line-end", "shift+4", "Line end", (target) => {
383
295
  target.gotoLineEnd();
384
- });
385
- },
386
- hidden: true,
387
- hotkey: "shift+4",
388
- id: `${commandScope}:line-end`,
389
- modes: ["cursor"],
390
- title: "Line end",
391
- when: () => enabled("motions"),
392
- });
393
- useCommand({
394
- handler: () => {
395
- motion((target) => {
296
+ }),
297
+ motionAction("line-end-key", "end", "Line end", (target) => {
396
298
  target.gotoLineEnd();
397
- });
398
- },
399
- hidden: true,
400
- hotkey: "end",
401
- id: `${commandScope}:line-end-key`,
402
- modes: ["cursor"],
403
- title: "Line end",
404
- when: () => enabled("motions"),
405
- });
406
- useCommand({
407
- handler: () => {
408
- motion((target) => {
299
+ }),
300
+ motionAction("word-forward", "w", "Word forward", (target) => {
409
301
  target.moveWordForward();
410
- });
411
- },
412
- hidden: true,
413
- hotkey: "w",
414
- id: `${commandScope}:word-forward`,
415
- modes: ["cursor"],
416
- title: "Word forward",
417
- when: () => enabled("motions"),
418
- });
419
- useCommand({
420
- handler: () => {
421
- motion((target) => {
302
+ }),
303
+ motionAction("word-backward", "b", "Word backward", (target) => {
422
304
  target.moveWordBackward();
423
- });
424
- },
425
- hidden: true,
426
- hotkey: "b",
427
- id: `${commandScope}:word-backward`,
428
- modes: ["cursor"],
429
- title: "Word backward",
430
- when: () => enabled("motions"),
431
- });
432
- useCommand({
433
- handler: () => {
434
- motion((target) => {
305
+ }),
306
+ motionAction("buffer-home", "g g", "Buffer home", (target) => {
435
307
  target.gotoBufferHome();
436
- });
437
- },
438
- hidden: true,
439
- hotkey: "g g",
440
- id: `${commandScope}:buffer-home`,
441
- modes: ["cursor"],
442
- title: "Buffer home",
443
- when: () => enabled("motions"),
444
- });
445
- useCommand({
446
- handler: () => {
447
- motion((target) => {
308
+ }),
309
+ motionAction("buffer-end", "shift+g", "Buffer end", (target) => {
448
310
  target.gotoBufferEnd();
449
- });
450
- },
451
- hidden: true,
452
- hotkey: "shift+g",
453
- id: `${commandScope}:buffer-end`,
454
- modes: ["cursor"],
455
- title: "Buffer end",
456
- when: () => enabled("motions"),
457
- });
311
+ }),
312
+ ];
313
+ return definitions.map(({ group, when, ...definition }) => ({
314
+ ...definition,
315
+ when: () => enabled(group) && (when?.() ?? true),
316
+ }));
317
+ }, [
318
+ appendAndEnterInsertMode,
319
+ commandScope,
320
+ copyDocument,
321
+ copyLine,
322
+ copySelection,
323
+ enabled,
324
+ enterInsertMode,
325
+ leaveInsertMode,
326
+ motion,
327
+ multilineRef,
328
+ openLineAbove,
329
+ openLineBelow,
330
+ optionsRef,
331
+ resolveSubmitKey,
332
+ submit,
333
+ ]);
334
+ useActions(builtInActions);
458
335
  // Middle-click paste from primary selection
459
336
  const handleMouseDown = useCallback((event) => {
460
337
  if (event.button === 1) {
@@ -495,7 +372,7 @@ export const useAskEditor = function useAskEditor(options = {}) {
495
372
  }
496
373
  }
497
374
  bumpScroll();
498
- }, [bumpScroll]);
375
+ }, [bumpScroll, multilineRef]);
499
376
  const insertText = useCallback((text) => {
500
377
  getTarget()?.insertText(text);
501
378
  bumpScroll();
@@ -514,8 +391,7 @@ export const useAskEditor = function useAskEditor(options = {}) {
514
391
  }, [setMode]);
515
392
  // Stable identity so composites can capture it in refs/effects; `mode` reads
516
393
  // live through the getter.
517
- const controllerRef = useRef(null);
518
- controllerRef.current ??= {
394
+ const controller = useLazyRef(() => ({
519
395
  getText,
520
396
  insertText,
521
397
  get mode() {
@@ -525,9 +401,9 @@ export const useAskEditor = function useAskEditor(options = {}) {
525
401
  setMode: setModeExternal,
526
402
  setText,
527
403
  submit,
528
- };
404
+ })).current;
529
405
  return {
530
- controller: controllerRef.current,
406
+ controller,
531
407
  editor: {
532
408
  bumpScroll,
533
409
  defaultValue,