@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.
@@ -1,6 +1,6 @@
1
- import { useCallback, useEffect, useRef, useState } from "react";
2
- import { readPrimaryText } from "@tooee/clipboard";
3
- import { useActiveCommandSurface, useCommand, useCommandSurfaceId, useMode, useProvideCommandContext, useSetMode, } from "@tooee/commands";
1
+ import { useCallback, useEffect, useMemo, useRef, useState } from "react";
2
+ import { copyToClipboard, readPrimaryText } from "@tooee/clipboard";
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,20 +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
  }, []);
71
+ const copyText = useCallback((text, emptyMessage, successMessage, ctx) => {
72
+ if (text === "") {
73
+ ctx.toast?.toast({ level: "warning", message: emptyMessage });
74
+ return;
75
+ }
76
+ void copyToClipboard(text);
77
+ ctx.toast?.toast({ level: "success", message: successMessage });
78
+ }, []);
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]);
75
100
  const enterInsertMode = useCallback(() => {
76
101
  vimMotionStateRef.current.pendingG = false;
77
102
  setMode("insert");
@@ -88,7 +113,7 @@ export const useAskEditor = function useAskEditor(options = {}) {
88
113
  vimMotionStateRef.current.pendingG = false;
89
114
  openLineAtCursor(textareaRef.current, "above");
90
115
  setMode("insert");
91
- }, [setMode]);
116
+ }, [multilineRef, setMode]);
92
117
  const openLineBelow = useCallback(() => {
93
118
  if (!multilineRef.current) {
94
119
  return;
@@ -96,96 +121,11 @@ export const useAskEditor = function useAskEditor(options = {}) {
96
121
  vimMotionStateRef.current.pendingG = false;
97
122
  openLineAtCursor(textareaRef.current, "below");
98
123
  setMode("insert");
99
- }, [setMode]);
124
+ }, [multilineRef, setMode]);
100
125
  const leaveInsertMode = useCallback(() => {
101
126
  vimMotionStateRef.current.pendingG = false;
102
127
  setMode("cursor");
103
128
  }, [setMode]);
104
- useCommand({
105
- handler: leaveInsertMode,
106
- hidden: true,
107
- hotkey: "Escape",
108
- id: `${commandScope}:leave-insert-mode`,
109
- modes: ["insert"],
110
- title: "Command mode",
111
- when: () => enabled("escape"),
112
- });
113
- useCommand({
114
- handler: () => optionsRef.current.onCancel?.(),
115
- hidden: true,
116
- hotkey: "q",
117
- id: `${commandScope}:cancel`,
118
- modes: ["cursor"],
119
- title: "Cancel",
120
- when: () => enabled("cancel") && optionsRef.current.onCancel !== undefined,
121
- });
122
- useCommand({
123
- handler: enterInsertMode,
124
- hidden: true,
125
- hotkey: "i",
126
- id: `${commandScope}:insert-mode-i`,
127
- modes: ["cursor"],
128
- title: "Insert mode",
129
- when: () => enabled("insert-commands"),
130
- });
131
- useCommand({
132
- handler: appendAndEnterInsertMode,
133
- hidden: true,
134
- hotkey: "a",
135
- id: `${commandScope}:insert-mode-a`,
136
- modes: ["cursor"],
137
- title: "Append",
138
- when: () => enabled("insert-commands"),
139
- });
140
- useCommand({
141
- handler: openLineAbove,
142
- hidden: true,
143
- hotkey: "shift+o",
144
- id: `${commandScope}:open-line-above`,
145
- modes: ["cursor"],
146
- title: "Open line above",
147
- when: () => enabled("insert-commands") && multilineRef.current,
148
- });
149
- useCommand({
150
- handler: openLineBelow,
151
- hidden: true,
152
- hotkey: "o",
153
- id: `${commandScope}:open-line-below`,
154
- modes: ["cursor"],
155
- title: "Open line below",
156
- when: () => enabled("insert-commands") && multilineRef.current,
157
- });
158
- useCommand({
159
- handler: submit,
160
- hidden: true,
161
- hotkey: "Enter",
162
- id: `${commandScope}:submit-insert`,
163
- modes: ["insert"],
164
- title: "Submit",
165
- when: () => enabled("submit") && resolveSubmitKey() === "enter",
166
- });
167
- useCommand({
168
- handler: submit,
169
- hidden: true,
170
- hotkey: "Enter",
171
- id: `${commandScope}:submit-cursor`,
172
- modes: ["cursor"],
173
- title: "Submit",
174
- // Command mode has no newline entry, so plain Enter always submits unless
175
- // the consumer disabled the built-in submit command group.
176
- when: () => enabled("submit") && resolveSubmitKey() !== "none",
177
- });
178
- useCommand({
179
- handler: submit,
180
- hidden: true,
181
- hotkey: "shift+Enter",
182
- id: `${commandScope}:submit-multiline`,
183
- modes: ["insert", "cursor"],
184
- title: "Submit",
185
- // Shift+Enter remains available for multiline insert mode and for
186
- // compatibility in cursor mode.
187
- when: () => enabled("submit") && resolveSubmitKey() !== "none",
188
- });
189
129
  const motion = useCallback((run) => {
190
130
  const target = getTarget();
191
131
  if (target) {
@@ -194,214 +134,204 @@ export const useAskEditor = function useAskEditor(options = {}) {
194
134
  vimMotionStateRef.current.pendingG = false;
195
135
  bumpScroll();
196
136
  }, [getTarget, bumpScroll]);
197
- useCommand({
198
- handler: () => {
199
- 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) => {
200
265
  target.moveCursorLeft();
201
- });
202
- },
203
- hidden: true,
204
- hotkey: "h",
205
- id: `${commandScope}:move-left`,
206
- modes: ["cursor"],
207
- title: "Move left",
208
- when: () => enabled("motions"),
209
- });
210
- useCommand({
211
- handler: () => {
212
- motion((target) => {
266
+ }),
267
+ motionAction("move-left-arrow", "left", "Move left", (target) => {
213
268
  target.moveCursorLeft();
214
- });
215
- },
216
- hidden: true,
217
- hotkey: "left",
218
- id: `${commandScope}:move-left-arrow`,
219
- modes: ["cursor"],
220
- title: "Move left",
221
- when: () => enabled("motions"),
222
- });
223
- useCommand({
224
- handler: () => {
225
- motion((target) => {
269
+ }),
270
+ motionAction("move-right", "l", "Move right", (target) => {
226
271
  target.moveCursorRight();
227
- });
228
- },
229
- hidden: true,
230
- hotkey: "l",
231
- id: `${commandScope}:move-right`,
232
- modes: ["cursor"],
233
- title: "Move right",
234
- when: () => enabled("motions"),
235
- });
236
- useCommand({
237
- handler: () => {
238
- motion((target) => {
272
+ }),
273
+ motionAction("move-right-arrow", "right", "Move right", (target) => {
239
274
  target.moveCursorRight();
240
- });
241
- },
242
- hidden: true,
243
- hotkey: "right",
244
- id: `${commandScope}:move-right-arrow`,
245
- modes: ["cursor"],
246
- title: "Move right",
247
- when: () => enabled("motions"),
248
- });
249
- useCommand({
250
- handler: () => {
251
- motion((target) => {
275
+ }),
276
+ motionAction("move-down", "j", "Move down", (target) => {
252
277
  target.moveCursorDown();
253
- });
254
- },
255
- hidden: true,
256
- hotkey: "j",
257
- id: `${commandScope}:move-down`,
258
- modes: ["cursor"],
259
- title: "Move down",
260
- when: () => enabled("motions"),
261
- });
262
- useCommand({
263
- handler: () => {
264
- motion((target) => {
278
+ }),
279
+ motionAction("move-down-arrow", "down", "Move down", (target) => {
265
280
  target.moveCursorDown();
266
- });
267
- },
268
- hidden: true,
269
- hotkey: "down",
270
- id: `${commandScope}:move-down-arrow`,
271
- modes: ["cursor"],
272
- title: "Move down",
273
- when: () => enabled("motions"),
274
- });
275
- useCommand({
276
- handler: () => {
277
- motion((target) => {
281
+ }),
282
+ motionAction("move-up", "k", "Move up", (target) => {
278
283
  target.moveCursorUp();
279
- });
280
- },
281
- hidden: true,
282
- hotkey: "k",
283
- id: `${commandScope}:move-up`,
284
- modes: ["cursor"],
285
- title: "Move up",
286
- when: () => enabled("motions"),
287
- });
288
- useCommand({
289
- handler: () => {
290
- motion((target) => {
284
+ }),
285
+ motionAction("move-up-arrow", "up", "Move up", (target) => {
291
286
  target.moveCursorUp();
292
- });
293
- },
294
- hidden: true,
295
- hotkey: "up",
296
- id: `${commandScope}:move-up-arrow`,
297
- modes: ["cursor"],
298
- title: "Move up",
299
- when: () => enabled("motions"),
300
- });
301
- useCommand({
302
- handler: () => {
303
- motion((target) => {
287
+ }),
288
+ motionAction("line-home", "0", "Line home", (target) => {
304
289
  target.gotoLineHome();
305
- });
306
- },
307
- hidden: true,
308
- hotkey: "0",
309
- id: `${commandScope}:line-home`,
310
- modes: ["cursor"],
311
- title: "Line home",
312
- when: () => enabled("motions"),
313
- });
314
- useCommand({
315
- handler: () => {
316
- motion((target) => {
290
+ }),
291
+ motionAction("line-home-key", "home", "Line home", (target) => {
317
292
  target.gotoLineHome();
318
- });
319
- },
320
- hidden: true,
321
- hotkey: "home",
322
- id: `${commandScope}:line-home-key`,
323
- modes: ["cursor"],
324
- title: "Line home",
325
- when: () => enabled("motions"),
326
- });
327
- useCommand({
328
- handler: () => {
329
- motion((target) => {
293
+ }),
294
+ motionAction("line-end", "shift+4", "Line end", (target) => {
330
295
  target.gotoLineEnd();
331
- });
332
- },
333
- hidden: true,
334
- hotkey: "shift+4",
335
- id: `${commandScope}:line-end`,
336
- modes: ["cursor"],
337
- title: "Line end",
338
- when: () => enabled("motions"),
339
- });
340
- useCommand({
341
- handler: () => {
342
- motion((target) => {
296
+ }),
297
+ motionAction("line-end-key", "end", "Line end", (target) => {
343
298
  target.gotoLineEnd();
344
- });
345
- },
346
- hidden: true,
347
- hotkey: "end",
348
- id: `${commandScope}:line-end-key`,
349
- modes: ["cursor"],
350
- title: "Line end",
351
- when: () => enabled("motions"),
352
- });
353
- useCommand({
354
- handler: () => {
355
- motion((target) => {
299
+ }),
300
+ motionAction("word-forward", "w", "Word forward", (target) => {
356
301
  target.moveWordForward();
357
- });
358
- },
359
- hidden: true,
360
- hotkey: "w",
361
- id: `${commandScope}:word-forward`,
362
- modes: ["cursor"],
363
- title: "Word forward",
364
- when: () => enabled("motions"),
365
- });
366
- useCommand({
367
- handler: () => {
368
- motion((target) => {
302
+ }),
303
+ motionAction("word-backward", "b", "Word backward", (target) => {
369
304
  target.moveWordBackward();
370
- });
371
- },
372
- hidden: true,
373
- hotkey: "b",
374
- id: `${commandScope}:word-backward`,
375
- modes: ["cursor"],
376
- title: "Word backward",
377
- when: () => enabled("motions"),
378
- });
379
- useCommand({
380
- handler: () => {
381
- motion((target) => {
305
+ }),
306
+ motionAction("buffer-home", "g g", "Buffer home", (target) => {
382
307
  target.gotoBufferHome();
383
- });
384
- },
385
- hidden: true,
386
- hotkey: "g g",
387
- id: `${commandScope}:buffer-home`,
388
- modes: ["cursor"],
389
- title: "Buffer home",
390
- when: () => enabled("motions"),
391
- });
392
- useCommand({
393
- handler: () => {
394
- motion((target) => {
308
+ }),
309
+ motionAction("buffer-end", "shift+g", "Buffer end", (target) => {
395
310
  target.gotoBufferEnd();
396
- });
397
- },
398
- hidden: true,
399
- hotkey: "shift+g",
400
- id: `${commandScope}:buffer-end`,
401
- modes: ["cursor"],
402
- title: "Buffer end",
403
- when: () => enabled("motions"),
404
- });
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);
405
335
  // Middle-click paste from primary selection
406
336
  const handleMouseDown = useCallback((event) => {
407
337
  if (event.button === 1) {
@@ -442,7 +372,7 @@ export const useAskEditor = function useAskEditor(options = {}) {
442
372
  }
443
373
  }
444
374
  bumpScroll();
445
- }, [bumpScroll]);
375
+ }, [bumpScroll, multilineRef]);
446
376
  const insertText = useCallback((text) => {
447
377
  getTarget()?.insertText(text);
448
378
  bumpScroll();
@@ -461,8 +391,7 @@ export const useAskEditor = function useAskEditor(options = {}) {
461
391
  }, [setMode]);
462
392
  // Stable identity so composites can capture it in refs/effects; `mode` reads
463
393
  // live through the getter.
464
- const controllerRef = useRef(null);
465
- controllerRef.current ??= {
394
+ const controller = useLazyRef(() => ({
466
395
  getText,
467
396
  insertText,
468
397
  get mode() {
@@ -472,9 +401,9 @@ export const useAskEditor = function useAskEditor(options = {}) {
472
401
  setMode: setModeExternal,
473
402
  setText,
474
403
  submit,
475
- };
404
+ })).current;
476
405
  return {
477
- controller: controllerRef.current,
406
+ controller,
478
407
  editor: {
479
408
  bumpScroll,
480
409
  defaultValue,