@tooee/ask 0.2.1 → 0.2.3
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 +22 -0
- package/dist/Ask.d.ts.map +1 -1
- package/dist/Ask.js +3 -3
- package/dist/Ask.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/launch.d.ts.map +1 -1
- package/dist/launch.js +2 -8
- package/dist/launch.js.map +1 -1
- package/dist/use-ask-dialog.d.ts +49 -0
- package/dist/use-ask-dialog.d.ts.map +1 -0
- package/dist/use-ask-dialog.js +86 -0
- package/dist/use-ask-dialog.js.map +1 -0
- package/package.json +8 -7
- package/src/Ask.tsx +2 -13
- package/src/index.ts +2 -0
- package/src/launch.tsx +12 -21
- package/src/use-ask-dialog.tsx +144 -0
package/README.md
CHANGED
|
@@ -30,6 +30,28 @@ command plus editor focus suspends automatically; no `when` guards or
|
|
|
30
30
|
The low-level vim helpers (`handleEditBufferVimMotion`, `appendAtCursor`,
|
|
31
31
|
`openLineAtCursor`) are exported for non-React or custom hosts.
|
|
32
32
|
|
|
33
|
+
## Promise-based dialog
|
|
34
|
+
|
|
35
|
+
`useAskDialog()` opens `AskOverlay` as a modal dialog on the host's overlay
|
|
36
|
+
stack and resolves the result:
|
|
37
|
+
|
|
38
|
+
```tsx
|
|
39
|
+
const ask = useAskDialog()
|
|
40
|
+
|
|
41
|
+
const value = await ask.open({ prompt: "Commit message" })
|
|
42
|
+
// string on submit, null on cancel/replacement/unmount
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Each `open()` owns one overlay record (unique id per call — concurrent dialogs
|
|
46
|
+
stack instead of replacing each other) and one owned modal command surface, so
|
|
47
|
+
host commands suspend and the host's global mode is never touched. The promise
|
|
48
|
+
settles exactly once. Options pass through to `AskOverlay` (`title`,
|
|
49
|
+
`multiline`, `defaultValue`, `placeholder`, `commands`, `controllerRef`, and
|
|
50
|
+
chrome slots). Dialog `commands` may open nested dialogs (for example a typed
|
|
51
|
+
`useChooseDialog()` picker); the nested surface suspends the editor and focus,
|
|
52
|
+
value, and local mode are restored when it settles. The host must render
|
|
53
|
+
overlay content (`AppLayout` does; custom hosts render `useCurrentOverlay()`).
|
|
54
|
+
|
|
33
55
|
## Reserved keys
|
|
34
56
|
|
|
35
57
|
Cursor mode: `h j k l 0 $ w b g G i a o O q Escape Enter Shift+Enter`, arrows,
|
package/dist/Ask.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Ask.d.ts","sourceRoot":"","sources":["../src/Ask.tsx"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAA;AACvD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AAI5C,MAAM,WAAW,QAAS,SAAQ,UAAU;IAC1C,OAAO,CAAC,EAAE,gBAAgB,EAAE,CAAA;IAC5B;;;;OAIG;IACH,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAA;CACnC;AAED,wBAAgB,GAAG,CAAC,EAClB,KAAK,EACL,MAAM,EACN,WAAW,EACX,YAAY,EACZ,SAAgB,EAChB,OAAO,EACP,QAAQ,GACT,EAAE,QAAQ,
|
|
1
|
+
{"version":3,"file":"Ask.d.ts","sourceRoot":"","sources":["../src/Ask.tsx"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAA;AACvD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AAI5C,MAAM,WAAW,QAAS,SAAQ,UAAU;IAC1C,OAAO,CAAC,EAAE,gBAAgB,EAAE,CAAA;IAC5B;;;;OAIG;IACH,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAA;CACnC;AAED,wBAAgB,GAAG,CAAC,EAClB,KAAK,EACL,MAAM,EACN,WAAW,EACX,YAAY,EACZ,SAAgB,EAChB,OAAO,EACP,QAAQ,GACT,EAAE,QAAQ,6BAoFV"}
|
package/dist/Ask.js
CHANGED
|
@@ -2,7 +2,7 @@ import { jsx as _jsx, jsxs as _jsxs } from "@opentui/react/jsx-runtime";
|
|
|
2
2
|
import { useRenderer } from "@opentui/react";
|
|
3
3
|
import { AppLayout } from "@tooee/layout";
|
|
4
4
|
import { useHasOverlay } from "@tooee/overlays";
|
|
5
|
-
import {
|
|
5
|
+
import { useTheme } from "@tooee/themes";
|
|
6
6
|
import { useThemeCommands, useQuitCommand, usePasteCommands } from "@tooee/shell";
|
|
7
7
|
import { useActions, useProvideCommandContext, useCommandContext } from "@tooee/commands";
|
|
8
8
|
import { AskEditor } from "./AskEditor.js";
|
|
@@ -11,7 +11,7 @@ export function Ask({ title, prompt, placeholder, defaultValue, multiline = true
|
|
|
11
11
|
const renderer = useRenderer();
|
|
12
12
|
const { invoke } = useCommandContext();
|
|
13
13
|
const { theme } = useTheme();
|
|
14
|
-
const { name: themeName
|
|
14
|
+
const { name: themeName } = useThemeCommands();
|
|
15
15
|
useQuitCommand({
|
|
16
16
|
onQuit: () => {
|
|
17
17
|
renderer.destroy();
|
|
@@ -57,6 +57,6 @@ export function Ask({ title, prompt, placeholder, defaultValue, multiline = true
|
|
|
57
57
|
{ label: "Theme:", value: themeName },
|
|
58
58
|
{ label: "", value: hintParts.join(" ") },
|
|
59
59
|
],
|
|
60
|
-
}, scrollProps: { focused: false },
|
|
60
|
+
}, scrollProps: { focused: false }, children: _jsx("box", { flexDirection: "column", alignItems: "center", justifyContent: "center", style: { flexGrow: 1 }, onMouseDown: editor.onMouseDown, children: _jsxs("box", { flexDirection: "column", width: "100%", maxWidth: 80, style: { flexGrow: 1, padding: 1 }, children: [prompt && (_jsx("text", { fg: theme.text, style: { marginBottom: 1 }, children: _jsx("strong", { children: prompt }) })), _jsx(AskEditor, { editor: editor })] }) }) }));
|
|
61
61
|
}
|
|
62
62
|
//# sourceMappingURL=Ask.js.map
|
package/dist/Ask.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Ask.js","sourceRoot":"","sources":["../src/Ask.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAC5C,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAA;AACzC,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAC/C,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"Ask.js","sourceRoot":"","sources":["../src/Ask.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAC5C,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAA;AACzC,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAC/C,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAA;AACxC,OAAO,EAAE,gBAAgB,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAA;AACjF,OAAO,EAAE,UAAU,EAAE,wBAAwB,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAA;AAGzF,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAC1C,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAA;AAYlD,MAAM,UAAU,GAAG,CAAC,EAClB,KAAK,EACL,MAAM,EACN,WAAW,EACX,YAAY,EACZ,SAAS,GAAG,IAAI,EAChB,OAAO,EACP,QAAQ,GACC;IACT,MAAM,QAAQ,GAAG,WAAW,EAAE,CAAA;IAC9B,MAAM,EAAE,MAAM,EAAE,GAAG,iBAAiB,EAAE,CAAA;IAEtC,MAAM,EAAE,KAAK,EAAE,GAAG,QAAQ,EAAE,CAAA;IAC5B,MAAM,EAAE,IAAI,EAAE,SAAS,EAAE,GAAG,gBAAgB,EAAE,CA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
|
export type { AskEditorProps } from "./AskEditor.js";
|
|
9
9
|
export { useAskEditor } from "./use-ask-editor.js";
|
|
10
10
|
export type { AskEditorCommandGroup, AskEditorController, AskEditorViewModel, AskSubmitKey, UseAskEditorOptions, UseAskEditorResult, } from "./use-ask-editor.js";
|
|
11
|
+
export { useAskDialog } from "./use-ask-dialog.js";
|
|
12
|
+
export type { AskDialogHandle, AskDialogOptions } from "./use-ask-dialog.js";
|
|
11
13
|
export { launch } from "./launch.js";
|
|
12
14
|
export type { AskLaunchOptions } from "./launch.js";
|
|
13
15
|
export type { AskOptions } from "./types.js";
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAA;AAC9B,YAAY,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAA;AACxC,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAC5C,YAAY,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAA;AACtD,OAAO,EAAE,QAAQ,EAAE,aAAa,EAAE,MAAM,eAAe,CAAA;AACvD,YAAY,EAAE,aAAa,EAAE,aAAa,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAA;AACrF,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAC1C,YAAY,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AACpD,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAA;AAClD,YAAY,EACV,qBAAqB,EACrB,mBAAmB,EACnB,kBAAkB,EAClB,YAAY,EACZ,mBAAmB,EACnB,kBAAkB,GACnB,MAAM,qBAAqB,CAAA;AAC5B,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACpC,YAAY,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAA;AACnD,YAAY,EAAE,UAAU,EAAE,M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c,EAAE,MAAM,kBAAkB,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -3,6 +3,7 @@ export { AskOverlay } from "./AskOverlay.js";
|
|
|
3
3
|
export { AskPanel, buildAskHints } from "./AskPanel.js";
|
|
4
4
|
export { AskEditor } from "./AskEditor.js";
|
|
5
5
|
export { useAskEditor } from "./use-ask-editor.js";
|
|
6
|
+
export { useAskDialog } from "./use-ask-dialog.js";
|
|
6
7
|
export { launch } from "./launch.js";
|
|
7
8
|
export { appendAtCursor, handleEditBufferVimMotion, openLineAtCursor } from "./vim-motions.js";
|
|
8
9
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAA;AAE9B,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAE5C,OAAO,EAAE,QAAQ,EAAE,aAAa,EAAE,MAAM,eAAe,CAAA;AAEvD,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAE1C,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAA;AASlD,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AAGpC,OAAO,EAAE,cAAc,EAAE,yBAAyB,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAA;AAE9B,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAE5C,OAAO,EAAE,QAAQ,EAAE,aAAa,EAAE,MAAM,eAAe,CAAA;AAEvD,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAE1C,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAA;AASlD,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAA;AAElD,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AAGpC,OAAO,EAAE,cAAc,EAAE,yBAAyB,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAA"}
|
package/dist/launch.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"launch.d.ts","sourceRoot":"","sources":["../src/launch.tsx"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"launch.d.ts","sourceRoot":"","sources":["../src/launch.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAA;AAEvD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AAE5C,MAAM,WAAW,gBAAiB,SAAQ,UAAU;IAClD,OAAO,CAAC,EAAE,gBAAgB,EAAE,CAAA;IAC5B,yEAAyE;IACzE,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAA;CACnC;AAED,wBAAsB,MAAM,CAAC,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAAC,IAAI,CAAC,CAarE"}
|
package/dist/launch.js
CHANGED
|
@@ -1,13 +1,7 @@
|
|
|
1
1
|
import { jsx as _jsx } from "@opentui/react/jsx-runtime";
|
|
2
|
-
import {
|
|
3
|
-
import { createRoot } from "@opentui/react";
|
|
4
|
-
import { TooeeProvider, guardTerminalHealth } from "@tooee/shell";
|
|
2
|
+
import { launchCli } from "@tooee/shell";
|
|
5
3
|
import { Ask } from "./Ask.js";
|
|
6
4
|
export async function launch(options) {
|
|
7
|
-
|
|
8
|
-
exitOnCtrlC: false,
|
|
9
|
-
});
|
|
10
|
-
guardTerminalHealth(renderer);
|
|
11
|
-
createRoot(renderer).render(_jsx(TooeeProvider, { initialMode: "insert", children: _jsx(Ask, { title: options.title, prompt: options.prompt, placeholder: options.placeholder, defaultValue: options.defaultValue, multiline: options.multiline, actions: options.actions, onSubmit: options.onSubmit }) }));
|
|
5
|
+
await launchCli(_jsx(Ask, { title: options.title, prompt: options.prompt, placeholder: options.placeholder, defaultValue: options.defaultValue, multiline: options.multiline, actions: options.actions, onSubmit: options.onSubmit }), { exitOnCtrlC: false, provider: { initialMode: "insert" } });
|
|
12
6
|
}
|
|
13
7
|
//# sourceMappingURL=launch.js.map
|
package/dist/launch.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"launch.js","sourceRoot":"","sources":["../src/launch.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"launch.js","sourceRoot":"","sources":["../src/launch.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AAExC,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAA;AAS9B,MAAM,CAAC,KAAK,UAAU,MAAM,CAAC,OAAyB;IACpD,MAAM,SAAS,CACb,KAAC,GAAG,IACF,KAAK,EAAE,OAAO,CAAC,KAAK,EACpB,MAAM,EAAE,OAAO,CAAC,MAAM,EACtB,WAAW,EAAE,OAAO,CAAC,WAAW,EAChC,YAAY,EAAE,OAAO,CAAC,YAAY,EAClC,SAAS,EAAE,OAAO,CAAC,SAAS,EAC5B,OAAO,EAAE,OAAO,CAAC,OAAO,EACxB,QAAQ,EAAE,OAAO,CAAC,QAAQ,GAC1B,EACF,EAAE,WAAW,EAAE,KAAK,EAAE,QAAQ,EAAE,EAAE,WAAW,EAAE,QAAQ,EAAE,EAAE,CAC5D,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { type ReactNode, type Ref } from "react";
|
|
2
|
+
import type { ActionDefinition } from "@tooee/commands";
|
|
3
|
+
import type { AskPanelProps } from "./AskPanel.js";
|
|
4
|
+
import type { AskEditorController } from "./use-ask-editor.js";
|
|
5
|
+
export interface AskDialogOptions {
|
|
6
|
+
prompt: string;
|
|
7
|
+
/** Title bar content; when set, `prompt` renders as a line above the editor. */
|
|
8
|
+
title?: string;
|
|
9
|
+
multiline?: boolean;
|
|
10
|
+
defaultValue?: string;
|
|
11
|
+
placeholder?: string;
|
|
12
|
+
/**
|
|
13
|
+
* Extra commands registered on the dialog's own command surface (same shape
|
|
14
|
+
* as Ask's actions). Handlers may open further dialogs; the nested dialog
|
|
15
|
+
* suspends this one until it settles.
|
|
16
|
+
*/
|
|
17
|
+
commands?: ActionDefinition[];
|
|
18
|
+
/** Access to the text controller while open (dictation, prefill, transforms). */
|
|
19
|
+
controllerRef?: Ref<AskEditorController>;
|
|
20
|
+
/** Chrome pass-throughs (see AskOverlay). */
|
|
21
|
+
hints?: AskPanelProps["hints"];
|
|
22
|
+
statusRight?: ReactNode;
|
|
23
|
+
footer?: ReactNode;
|
|
24
|
+
inset?: AskPanelProps["inset"];
|
|
25
|
+
}
|
|
26
|
+
export interface AskDialogHandle {
|
|
27
|
+
/**
|
|
28
|
+
* Open a modal ask dialog and resolve with the submitted text, or `null`
|
|
29
|
+
* when the dialog is cancelled, replaced, or unmounted. Settles exactly
|
|
30
|
+
* once per call.
|
|
31
|
+
*/
|
|
32
|
+
open(options: AskDialogOptions): Promise<string | null>;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Promise-based modal ask dialog on the overlay stack.
|
|
36
|
+
*
|
|
37
|
+
* Each `open()` owns one modal overlay record and one owned command surface
|
|
38
|
+
* (via `ownCommands`), so the host app's commands are suspended and its global
|
|
39
|
+
* mode is never touched while the dialog is up. The returned promise settles
|
|
40
|
+
* exactly once: with the submitted string, or with `null` on cancel (`q` in
|
|
41
|
+
* cursor mode or the close button), same-id replacement, or unmount of the
|
|
42
|
+
* owning component.
|
|
43
|
+
*
|
|
44
|
+
* The overlay renders wherever the host presents overlay content
|
|
45
|
+
* (`AppLayout` does this automatically; custom hosts render
|
|
46
|
+
* `useCurrentOverlay()`).
|
|
47
|
+
*/
|
|
48
|
+
export declare function useAskDialog(): AskDialogHandle;
|
|
49
|
+
//# sourceMappingURL=use-ask-dialog.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-ask-dialog.d.ts","sourceRoot":"","sources":["../src/use-ask-dialog.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAqB,KAAK,SAAS,EAAE,KAAK,GAAG,EAAE,MAAM,OAAO,CAAA;AACnE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAA;AAGvD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,eAAe,CAAA;AAClD,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,qBAAqB,CAAA;AAU9D,MAAM,WAAW,gBAAgB;IAC/B,MAAM,EAAE,MAAM,CAAA;IACd,gFAAgF;IAChF,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,SAAS,CAAC,EAAE,OAAO,CAAA;IACnB,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB;;;;OAIG;IACH,QAAQ,CAAC,EAAE,gBAAgB,EAAE,CAAA;IAC7B,iFAAiF;IACjF,aAAa,CAAC,EAAE,GAAG,CAAC,mBAAmB,CAAC,CAAA;IACxC,6CAA6C;IAC7C,KAAK,CAAC,EAAE,aAAa,CAAC,OAAO,CAAC,CAAA;IAC9B,WAAW,CAAC,EAAE,SAAS,CAAA;IACvB,MAAM,CAAC,EAAE,SAAS,CAAA;IAClB,KAAK,CAAC,EAAE,aAAa,CAAC,OAAO,CAAC,CAAA;CAC/B;AAED,MAAM,WAAW,eAAe;IAC9B;;;;OAIG;IACH,IAAI,CAAC,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAA;CACxD;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,YAAY,IAAI,eAAe,CAmF9C"}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { jsx as _jsx } from "@opentui/react/jsx-runtime";
|
|
2
|
+
import { useEffect, useRef } from "react";
|
|
3
|
+
import { useOverlay } from "@tooee/overlays";
|
|
4
|
+
import { AskOverlay } from "./AskOverlay.js";
|
|
5
|
+
/**
|
|
6
|
+
* Per-open unique overlay id. A module-level sequence guarantees that
|
|
7
|
+
* concurrent dialogs — including dialogs opened from separate providers in the
|
|
8
|
+
* same process — never share an overlay id, so one dialog can never displace
|
|
9
|
+
* another through same-id replacement.
|
|
10
|
+
*/
|
|
11
|
+
let askDialogSequence = 0;
|
|
12
|
+
/**
|
|
13
|
+
* Promise-based modal ask dialog on the overlay stack.
|
|
14
|
+
*
|
|
15
|
+
* Each `open()` owns one modal overlay record and one owned command surface
|
|
16
|
+
* (via `ownCommands`), so the host app's commands are suspended and its global
|
|
17
|
+
* mode is never touched while the dialog is up. The returned promise settles
|
|
18
|
+
* exactly once: with the submitted string, or with `null` on cancel (`q` in
|
|
19
|
+
* cursor mode or the close button), same-id replacement, or unmount of the
|
|
20
|
+
* owning component.
|
|
21
|
+
*
|
|
22
|
+
* The overlay renders wherever the host presents overlay content
|
|
23
|
+
* (`AppLayout` does this automatically; custom hosts render
|
|
24
|
+
* `useCurrentOverlay()`).
|
|
25
|
+
*/
|
|
26
|
+
export function useAskDialog() {
|
|
27
|
+
const overlay = useOverlay();
|
|
28
|
+
const overlayRef = useRef(overlay);
|
|
29
|
+
overlayRef.current = overlay;
|
|
30
|
+
// Open dialogs by overlay id, so unmounting the owner closes (and thereby
|
|
31
|
+
// settles) every dialog it opened.
|
|
32
|
+
const openHandlesRef = useRef(new Map());
|
|
33
|
+
const unmountedRef = useRef(false);
|
|
34
|
+
useEffect(() => {
|
|
35
|
+
unmountedRef.current = false;
|
|
36
|
+
const handles = openHandlesRef.current;
|
|
37
|
+
return () => {
|
|
38
|
+
unmountedRef.current = true;
|
|
39
|
+
// Map iteration tolerates deletion: settle() removes entries as each
|
|
40
|
+
// close lands.
|
|
41
|
+
for (const handle of handles.values())
|
|
42
|
+
handle.close("unmounted");
|
|
43
|
+
handles.clear();
|
|
44
|
+
};
|
|
45
|
+
}, []);
|
|
46
|
+
// Stable identity so command handlers and effects can capture the handle.
|
|
47
|
+
const handleRef = useRef(null);
|
|
48
|
+
if (handleRef.current === null) {
|
|
49
|
+
handleRef.current = {
|
|
50
|
+
open(options) {
|
|
51
|
+
return new Promise((resolvePromise) => {
|
|
52
|
+
if (unmountedRef.current) {
|
|
53
|
+
resolvePromise(null);
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
const id = `ask-dialog-${++askDialogSequence}`;
|
|
57
|
+
let settled = false;
|
|
58
|
+
const settle = (value) => {
|
|
59
|
+
if (settled)
|
|
60
|
+
return;
|
|
61
|
+
settled = true;
|
|
62
|
+
openHandlesRef.current.delete(id);
|
|
63
|
+
resolvePromise(value);
|
|
64
|
+
};
|
|
65
|
+
const handle = overlayRef.current.open(id, () => (_jsx(AskOverlay, { prompt: options.prompt, title: options.title, multiline: options.multiline, defaultValue: options.defaultValue, placeholder: options.placeholder, commands: options.commands, controllerRef: options.controllerRef, hints: options.hints, statusRight: options.statusRight, footer: options.footer, inset: options.inset, onSubmit: (value) => {
|
|
66
|
+
if (settled)
|
|
67
|
+
return;
|
|
68
|
+
settle(value);
|
|
69
|
+
handle.close("close");
|
|
70
|
+
}, onCancel: () => handle.close("escape") })), undefined, {
|
|
71
|
+
ownCommands: true,
|
|
72
|
+
role: "modal",
|
|
73
|
+
surfaceMode: "insert",
|
|
74
|
+
// Single settlement funnel: every close path (cancel, escape,
|
|
75
|
+
// replacement, unmount, external closeTop) lands here. Submit
|
|
76
|
+
// settles first, making this a no-op.
|
|
77
|
+
onClose: () => settle(null),
|
|
78
|
+
});
|
|
79
|
+
openHandlesRef.current.set(id, handle);
|
|
80
|
+
});
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
return handleRef.current;
|
|
85
|
+
}
|
|
86
|
+
//# sourceMappingURL=use-ask-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AAG,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC,GACtC,CACH,EACD,SAAS,EACT;wBACE,WAAW,EAAE,IAAI;wBACjB,IAAI,EAAE,OAAO;wBACb,WAAW,EAAE,QAAQ;wBACrB,8DAA8D;wBAC9D,8DAA8D;wBAC9D,sCAAsC;wBACtC,OAAO,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC;qBAC5B,CACF,CAAA;oBACD,cAAc,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,EAAE,MAAM,CAAC,CAAA;gBACxC,CAAC,CAAC,CAAA;YACJ,CAAC;SACF,CAAA;IACH,CAAC;IAED,OAAO,SAAS,CAAC,OAAO,CAAA;AAC1B,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tooee/ask",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3",
|
|
4
4
|
"description": "Terminal text input gathering",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cli",
|
|
@@ -36,16 +36,17 @@
|
|
|
36
36
|
"typecheck": "tsc --noEmit"
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@tooee/clipboard": "0.2.
|
|
40
|
-
"@tooee/commands": "0.2.
|
|
41
|
-
"@tooee/layout": "0.2.
|
|
42
|
-
"@tooee/overlays": "0.2.
|
|
43
|
-
"@tooee/shell": "0.2.
|
|
44
|
-
"@tooee/themes": "0.2.
|
|
39
|
+
"@tooee/clipboard": "0.2.3",
|
|
40
|
+
"@tooee/commands": "0.2.3",
|
|
41
|
+
"@tooee/layout": "0.2.3",
|
|
42
|
+
"@tooee/overlays": "0.2.3",
|
|
43
|
+
"@tooee/shell": "0.2.3",
|
|
44
|
+
"@tooee/themes": "0.2.3"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
47
|
"@opentui/core": "^0.4.3",
|
|
48
48
|
"@opentui/react": "^0.4.3",
|
|
49
|
+
"@tooee/choose": "0.2.3",
|
|
49
50
|
"@types/bun": "^1.3.10",
|
|
50
51
|
"@types/react": "^19.2.14",
|
|
51
52
|
"typescript": "^5.9.3"
|
package/src/Ask.tsx
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { useRenderer } from "@opentui/react"
|
|
2
2
|
import { AppLayout } from "@tooee/layout"
|
|
3
3
|
import { useHasOverlay } from "@tooee/overlays"
|
|
4
|
-
import {
|
|
4
|
+
import { useTheme } from "@tooee/themes"
|
|
5
5
|
import { useThemeCommands, useQuitCommand, usePasteCommands } from "@tooee/shell"
|
|
6
6
|
import { useActions, useProvideCommandContext, useCommandContext } from "@tooee/commands"
|
|
7
7
|
import type { ActionDefinition } from "@tooee/commands"
|
|
@@ -32,7 +32,7 @@ export function Ask({
|
|
|
32
32
|
const { invoke } = useCommandContext()
|
|
33
33
|
|
|
34
34
|
const { theme } = useTheme()
|
|
35
|
-
const { name: themeName
|
|
35
|
+
const { name: themeName } = useThemeCommands()
|
|
36
36
|
useQuitCommand({
|
|
37
37
|
onQuit: () => {
|
|
38
38
|
renderer.destroy()
|
|
@@ -92,17 +92,6 @@ export function Ask({
|
|
|
92
92
|
],
|
|
93
93
|
}}
|
|
94
94
|
scrollProps={{ focused: false }}
|
|
95
|
-
overlay={
|
|
96
|
-
themePicker.isOpen ? (
|
|
97
|
-
<ThemePicker
|
|
98
|
-
entries={themePicker.entries}
|
|
99
|
-
currentTheme={themeName}
|
|
100
|
-
onSelect={themePicker.confirm}
|
|
101
|
-
onClose={themePicker.close}
|
|
102
|
-
onNavigate={themePicker.preview}
|
|
103
|
-
/>
|
|
104
|
-
) : undefined
|
|
105
|
-
}
|
|
106
95
|
>
|
|
107
96
|
<box
|
|
108
97
|
flexDirection="column"
|
package/src/index.ts
CHANGED
|
@@ -15,6 +15,8 @@ export type {
|
|
|
15
15
|
UseAskEditorOptions,
|
|
16
16
|
UseAskEditorResult,
|
|
17
17
|
} from "./use-ask-editor.js"
|
|
18
|
+
export { useAskDialog } from "./use-ask-dialog.js"
|
|
19
|
+
export type { AskDialogHandle, AskDialogOptions } from "./use-ask-dialog.js"
|
|
18
20
|
export { launch } from "./launch.js"
|
|
19
21
|
export type { AskLaunchOptions } from "./launch.js"
|
|
20
22
|
export type { AskOptions } from "./types.js"
|
package/src/launch.tsx
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { createRoot } from "@opentui/react"
|
|
3
|
-
import { TooeeProvider, guardTerminalHealth } from "@tooee/shell"
|
|
1
|
+
import { launchCli } from "@tooee/shell"
|
|
4
2
|
import type { ActionDefinition } from "@tooee/commands"
|
|
5
3
|
import { Ask } from "./Ask.js"
|
|
6
4
|
import type { AskOptions } from "./types.js"
|
|
@@ -12,23 +10,16 @@ export interface AskLaunchOptions extends AskOptions {
|
|
|
12
10
|
}
|
|
13
11
|
|
|
14
12
|
export async function launch(options: AskLaunchOptions): Promise<void> {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
placeholder={options.placeholder}
|
|
27
|
-
defaultValue={options.defaultValue}
|
|
28
|
-
multiline={options.multiline}
|
|
29
|
-
actions={options.actions}
|
|
30
|
-
onSubmit={options.onSubmit}
|
|
31
|
-
/>
|
|
32
|
-
</TooeeProvider>,
|
|
13
|
+
await launchCli(
|
|
14
|
+
<Ask
|
|
15
|
+
title={options.title}
|
|
16
|
+
prompt={options.prompt}
|
|
17
|
+
placeholder={options.placeholder}
|
|
18
|
+
defaultValue={options.defaultValue}
|
|
19
|
+
multiline={options.multiline}
|
|
20
|
+
actions={options.actions}
|
|
21
|
+
onSubmit={options.onSubmit}
|
|
22
|
+
/>,
|
|
23
|
+
{ exitOnCtrlC: false, provider: { initialMode: "insert" } },
|
|
33
24
|
)
|
|
34
25
|
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { useEffect, useRef, type ReactNode, type Ref } from "react"
|
|
2
|
+
import type { ActionDefinition } from "@tooee/commands"
|
|
3
|
+
import { useOverlay, type OverlayHandle } from "@tooee/overlays"
|
|
4
|
+
import { AskOverlay } from "./AskOverlay.js"
|
|
5
|
+
import type { AskPanelProps } from "./AskPanel.js"
|
|
6
|
+
import type { AskEditorController } from "./use-ask-editor.js"
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Per-open unique overlay id. A module-level sequence guarantees that
|
|
10
|
+
* concurrent dialogs — including dialogs opened from separate providers in the
|
|
11
|
+
* same process — never share an overlay id, so one dialog can never displace
|
|
12
|
+
* another through same-id replacement.
|
|
13
|
+
*/
|
|
14
|
+
let askDialogSequence = 0
|
|
15
|
+
|
|
16
|
+
export interface AskDialogOptions {
|
|
17
|
+
prompt: string
|
|
18
|
+
/** Title bar content; when set, `prompt` renders as a line above the editor. */
|
|
19
|
+
title?: string
|
|
20
|
+
multiline?: boolean
|
|
21
|
+
defaultValue?: string
|
|
22
|
+
placeholder?: string
|
|
23
|
+
/**
|
|
24
|
+
* Extra commands registered on the dialog's own command surface (same shape
|
|
25
|
+
* as Ask's actions). Handlers may open further dialogs; the nested dialog
|
|
26
|
+
* suspends this one until it settles.
|
|
27
|
+
*/
|
|
28
|
+
commands?: ActionDefinition[]
|
|
29
|
+
/** Access to the text controller while open (dictation, prefill, transforms). */
|
|
30
|
+
controllerRef?: Ref<AskEditorController>
|
|
31
|
+
/** Chrome pass-throughs (see AskOverlay). */
|
|
32
|
+
hints?: AskPanelProps["hints"]
|
|
33
|
+
statusRight?: ReactNode
|
|
34
|
+
footer?: ReactNode
|
|
35
|
+
inset?: AskPanelProps["inset"]
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface AskDialogHandle {
|
|
39
|
+
/**
|
|
40
|
+
* Open a modal ask dialog and resolve with the submitted text, or `null`
|
|
41
|
+
* when the dialog is cancelled, replaced, or unmounted. Settles exactly
|
|
42
|
+
* once per call.
|
|
43
|
+
*/
|
|
44
|
+
open(options: AskDialogOptions): Promise<string | null>
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Promise-based modal ask dialog on the overlay stack.
|
|
49
|
+
*
|
|
50
|
+
* Each `open()` owns one modal overlay record and one owned command surface
|
|
51
|
+
* (via `ownCommands`), so the host app's commands are suspended and its global
|
|
52
|
+
* mode is never touched while the dialog is up. The returned promise settles
|
|
53
|
+
* exactly once: with the submitted string, or with `null` on cancel (`q` in
|
|
54
|
+
* cursor mode or the close button), same-id replacement, or unmount of the
|
|
55
|
+
* owning component.
|
|
56
|
+
*
|
|
57
|
+
* The overlay renders wherever the host presents overlay content
|
|
58
|
+
* (`AppLayout` does this automatically; custom hosts render
|
|
59
|
+
* `useCurrentOverlay()`).
|
|
60
|
+
*/
|
|
61
|
+
export function useAskDialog(): AskDialogHandle {
|
|
62
|
+
const overlay = useOverlay()
|
|
63
|
+
const overlayRef = useRef(overlay)
|
|
64
|
+
overlayRef.current = overlay
|
|
65
|
+
|
|
66
|
+
// Open dialogs by overlay id, so unmounting the owner closes (and thereby
|
|
67
|
+
// settles) every dialog it opened.
|
|
68
|
+
const openHandlesRef = useRef(new Map<string, OverlayHandle<undefined>>())
|
|
69
|
+
const unmountedRef = useRef(false)
|
|
70
|
+
|
|
71
|
+
useEffect(() => {
|
|
72
|
+
unmountedRef.current = false
|
|
73
|
+
const handles = openHandlesRef.current
|
|
74
|
+
return () => {
|
|
75
|
+
unmountedRef.current = true
|
|
76
|
+
// Map iteration tolerates deletion: settle() removes entries as each
|
|
77
|
+
// close lands.
|
|
78
|
+
for (const handle of handles.values()) handle.close("unmounted")
|
|
79
|
+
handles.clear()
|
|
80
|
+
}
|
|
81
|
+
}, [])
|
|
82
|
+
|
|
83
|
+
// Stable identity so command handlers and effects can capture the handle.
|
|
84
|
+
const handleRef = useRef<AskDialogHandle | null>(null)
|
|
85
|
+
if (handleRef.current === null) {
|
|
86
|
+
handleRef.current = {
|
|
87
|
+
open(options: AskDialogOptions): Promise<string | null> {
|
|
88
|
+
return new Promise<string | null>((resolvePromise) => {
|
|
89
|
+
if (unmountedRef.current) {
|
|
90
|
+
resolvePromise(null)
|
|
91
|
+
return
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const id = `ask-dialog-${++askDialogSequence}`
|
|
95
|
+
let settled = false
|
|
96
|
+
const settle = (value: string | null) => {
|
|
97
|
+
if (settled) return
|
|
98
|
+
settled = true
|
|
99
|
+
openHandlesRef.current.delete(id)
|
|
100
|
+
resolvePromise(value)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const handle = overlayRef.current.open(
|
|
104
|
+
id,
|
|
105
|
+
() => (
|
|
106
|
+
<AskOverlay
|
|
107
|
+
prompt={options.prompt}
|
|
108
|
+
title={options.title}
|
|
109
|
+
multiline={options.multiline}
|
|
110
|
+
defaultValue={options.defaultValue}
|
|
111
|
+
placeholder={options.placeholder}
|
|
112
|
+
commands={options.commands}
|
|
113
|
+
controllerRef={options.controllerRef}
|
|
114
|
+
hints={options.hints}
|
|
115
|
+
statusRight={options.statusRight}
|
|
116
|
+
footer={options.footer}
|
|
117
|
+
inset={options.inset}
|
|
118
|
+
onSubmit={(value) => {
|
|
119
|
+
if (settled) return
|
|
120
|
+
settle(value)
|
|
121
|
+
handle.close("close")
|
|
122
|
+
}}
|
|
123
|
+
onCancel={() => handle.close("escape")}
|
|
124
|
+
/>
|
|
125
|
+
),
|
|
126
|
+
undefined,
|
|
127
|
+
{
|
|
128
|
+
ownCommands: true,
|
|
129
|
+
role: "modal",
|
|
130
|
+
surfaceMode: "insert",
|
|
131
|
+
// Single settlement funnel: every close path (cancel, escape,
|
|
132
|
+
// replacement, unmount, external closeTop) lands here. Submit
|
|
133
|
+
// settles first, making this a no-op.
|
|
134
|
+
onClose: () => settle(null),
|
|
135
|
+
},
|
|
136
|
+
)
|
|
137
|
+
openHandlesRef.current.set(id, handle)
|
|
138
|
+
})
|
|
139
|
+
},
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return handleRef.current
|
|
144
|
+
}
|