@tooee/ask 0.2.3 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/{AskEditor.d.ts → ask-editor.d.ts} +2 -2
- package/dist/ask-editor.d.ts.map +1 -0
- package/dist/ask-editor.js +26 -0
- package/dist/ask-editor.js.map +1 -0
- package/dist/{AskOverlay.d.ts → ask-overlay.d.ts} +6 -6
- package/dist/ask-overlay.d.ts.map +1 -0
- package/dist/{AskOverlay.js → ask-overlay.js} +11 -10
- package/dist/ask-overlay.js.map +1 -0
- package/dist/{AskPanel.d.ts → ask-panel.d.ts} +5 -5
- package/dist/ask-panel.d.ts.map +1 -0
- package/dist/ask-panel.js +33 -0
- package/dist/ask-panel.js.map +1 -0
- package/dist/{Ask.d.ts → ask.d.ts} +2 -2
- package/dist/ask.d.ts.map +1 -0
- package/dist/{Ask.js → ask.js} +15 -12
- package/dist/ask.js.map +1 -0
- package/dist/{EditorScrollbar.d.ts → editor-scrollbar.d.ts} +2 -2
- package/dist/editor-scrollbar.d.ts.map +1 -0
- package/dist/{EditorScrollbar.js → editor-scrollbar.js} +15 -10
- package/dist/editor-scrollbar.js.map +1 -0
- package/dist/index.d.ts +8 -8
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -4
- package/dist/index.js.map +1 -1
- package/dist/launch.d.ts +1 -1
- package/dist/launch.d.ts.map +1 -1
- package/dist/launch.js +5 -4
- package/dist/launch.js.map +1 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/use-ask-dialog.d.ts +4 -4
- package/dist/use-ask-dialog.d.ts.map +1 -1
- package/dist/use-ask-dialog.js +45 -38
- package/dist/use-ask-dialog.js.map +1 -1
- package/dist/use-ask-editor.d.ts +9 -9
- package/dist/use-ask-editor.d.ts.map +1 -1
- package/dist/use-ask-editor.js +211 -138
- package/dist/use-ask-editor.js.map +1 -1
- package/dist/vim-motions.d.ts +3 -3
- package/dist/vim-motions.d.ts.map +1 -1
- package/dist/vim-motions.js +73 -44
- package/dist/vim-motions.js.map +1 -1
- package/package.json +8 -8
- package/src/{AskEditor.tsx → ask-editor.tsx} +30 -23
- package/src/ask-overlay.tsx +80 -0
- package/src/{AskPanel.tsx → ask-panel.tsx} +49 -41
- package/src/{Ask.tsx → ask.tsx} +48 -45
- package/src/{EditorScrollbar.tsx → editor-scrollbar.tsx} +40 -28
- package/src/index.ts +17 -17
- package/src/launch.tsx +11 -10
- package/src/types.ts +5 -5
- package/src/use-ask-dialog.tsx +102 -90
- package/src/use-ask-editor.ts +377 -292
- package/src/vim-motions.ts +110 -71
- package/dist/Ask.d.ts.map +0 -1
- package/dist/Ask.js.map +0 -1
- package/dist/AskEditor.d.ts.map +0 -1
- package/dist/AskEditor.js +0 -19
- package/dist/AskEditor.js.map +0 -1
- package/dist/AskOverlay.d.ts.map +0 -1
- package/dist/AskOverlay.js.map +0 -1
- package/dist/AskPanel.d.ts.map +0 -1
- package/dist/AskPanel.js +0 -25
- package/dist/AskPanel.js.map +0 -1
- package/dist/EditorScrollbar.d.ts.map +0 -1
- package/dist/EditorScrollbar.js.map +0 -1
- package/src/AskOverlay.tsx +0 -75
package/src/{Ask.tsx → ask.tsx}
RENAMED
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
import { useRenderer } from "@opentui/react"
|
|
2
|
-
import { AppLayout } from "@tooee/layout"
|
|
3
|
-
import { useHasOverlay } from "@tooee/overlays"
|
|
4
|
-
import { useTheme } from "@tooee/themes"
|
|
5
|
-
import { useThemeCommands, useQuitCommand, usePasteCommands } from "@tooee/shell"
|
|
6
|
-
import { useActions, useProvideCommandContext, useCommandContext } from "@tooee/commands"
|
|
7
|
-
import type { ActionDefinition } from "@tooee/commands"
|
|
8
|
-
import type { AskOptions } from "./types.js"
|
|
9
|
-
import { AskEditor } from "./
|
|
10
|
-
import { useAskEditor } from "./use-ask-editor.js"
|
|
1
|
+
import { useRenderer } from "@opentui/react";
|
|
2
|
+
import { AppLayout } from "@tooee/layout";
|
|
3
|
+
import { useHasOverlay } from "@tooee/overlays";
|
|
4
|
+
import { useTheme } from "@tooee/themes";
|
|
5
|
+
import { useThemeCommands, useQuitCommand, usePasteCommands } from "@tooee/shell";
|
|
6
|
+
import { useActions, useProvideCommandContext, useCommandContext } from "@tooee/commands";
|
|
7
|
+
import type { ActionDefinition } from "@tooee/commands";
|
|
8
|
+
import type { AskOptions } from "./types.js";
|
|
9
|
+
import { AskEditor } from "./ask-editor.js";
|
|
10
|
+
import { useAskEditor } from "./use-ask-editor.js";
|
|
11
11
|
|
|
12
12
|
export interface AskProps extends AskOptions {
|
|
13
|
-
actions?: ActionDefinition[]
|
|
13
|
+
actions?: ActionDefinition[];
|
|
14
14
|
/**
|
|
15
15
|
* Called with the submitted text. Defaults to writing the value to stdout
|
|
16
16
|
* and destroying the renderer (the standalone `ask` CLI behaviour). A
|
|
17
17
|
* `submit` action takes precedence over both.
|
|
18
18
|
*/
|
|
19
|
-
onSubmit?: (value: string) => void
|
|
19
|
+
onSubmit?: (value: string) => void;
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
export function Ask({
|
|
22
|
+
export const Ask = function Ask({
|
|
23
23
|
title,
|
|
24
24
|
prompt,
|
|
25
25
|
placeholder,
|
|
@@ -27,63 +27,66 @@ export function Ask({
|
|
|
27
27
|
multiline = true,
|
|
28
28
|
actions,
|
|
29
29
|
onSubmit,
|
|
30
|
-
}: AskProps) {
|
|
31
|
-
const renderer = useRenderer()
|
|
32
|
-
const { invoke } = useCommandContext()
|
|
30
|
+
}: AskProps): React.ReactNode {
|
|
31
|
+
const renderer = useRenderer();
|
|
32
|
+
const { invoke } = useCommandContext();
|
|
33
33
|
|
|
34
|
-
const { theme } = useTheme()
|
|
35
|
-
const { name: themeName } = useThemeCommands()
|
|
34
|
+
const { theme } = useTheme();
|
|
35
|
+
const { name: themeName } = useThemeCommands();
|
|
36
36
|
useQuitCommand({
|
|
37
37
|
onQuit: () => {
|
|
38
|
-
renderer.destroy()
|
|
39
|
-
process.exit(0)
|
|
38
|
+
renderer.destroy();
|
|
39
|
+
process.exit(0);
|
|
40
40
|
},
|
|
41
|
-
})
|
|
41
|
+
});
|
|
42
42
|
|
|
43
43
|
// Legacy overlays don't push a command surface; keep blurring the editor
|
|
44
44
|
// under them via the shell's overlay state.
|
|
45
|
-
const hasOverlay = useHasOverlay()
|
|
45
|
+
const hasOverlay = useHasOverlay();
|
|
46
46
|
|
|
47
47
|
const handleSubmit = (text: string) => {
|
|
48
|
-
if (actions?.some((a) => a.id === "submit")) {
|
|
49
|
-
invoke("submit")
|
|
50
|
-
return
|
|
48
|
+
if (actions?.some((a) => a.id === "submit") === true) {
|
|
49
|
+
invoke("submit");
|
|
50
|
+
return;
|
|
51
51
|
}
|
|
52
52
|
if (onSubmit) {
|
|
53
|
-
onSubmit(text)
|
|
54
|
-
return
|
|
53
|
+
onSubmit(text);
|
|
54
|
+
return;
|
|
55
55
|
}
|
|
56
|
-
process.stdout.write(text
|
|
57
|
-
renderer.destroy()
|
|
58
|
-
}
|
|
56
|
+
process.stdout.write(`${text}\n`);
|
|
57
|
+
renderer.destroy();
|
|
58
|
+
};
|
|
59
59
|
|
|
60
60
|
const { controller, editor } = useAskEditor({
|
|
61
|
-
multiline,
|
|
62
61
|
defaultValue,
|
|
63
|
-
|
|
62
|
+
multiline,
|
|
64
63
|
onSubmit: handleSubmit,
|
|
64
|
+
placeholder,
|
|
65
65
|
suspended: hasOverlay,
|
|
66
|
-
})
|
|
66
|
+
});
|
|
67
|
+
const handleMouseDown = editor.onMouseDown;
|
|
67
68
|
|
|
68
69
|
useProvideCommandContext(() => ({
|
|
69
|
-
exit: () =>
|
|
70
|
-
|
|
70
|
+
exit: () => {
|
|
71
|
+
renderer.destroy();
|
|
72
|
+
},
|
|
73
|
+
}));
|
|
71
74
|
|
|
72
|
-
useActions(actions)
|
|
75
|
+
useActions(actions);
|
|
73
76
|
|
|
74
77
|
// Paste commands (available via command palette)
|
|
75
|
-
usePasteCommands({ getTarget: () => controller })
|
|
78
|
+
usePasteCommands({ getTarget: () => controller });
|
|
76
79
|
|
|
77
|
-
const mode = editor
|
|
78
|
-
const submitHint = multiline ? "Shift+Enter submit" : "Enter submit"
|
|
80
|
+
const { mode } = editor;
|
|
81
|
+
const submitHint = multiline ? "Shift+Enter submit" : "Enter submit";
|
|
79
82
|
const hintParts =
|
|
80
83
|
mode === "insert"
|
|
81
84
|
? [submitHint, "Esc commands"]
|
|
82
|
-
: ["i insert", "q quit", ": palette", submitHint]
|
|
85
|
+
: ["i insert", "q quit", ": palette", submitHint];
|
|
83
86
|
|
|
84
87
|
return (
|
|
85
88
|
<AppLayout
|
|
86
|
-
titleBar={title ? { title } : undefined}
|
|
89
|
+
titleBar={title !== undefined && title !== "" ? { title } : undefined}
|
|
87
90
|
statusBar={{
|
|
88
91
|
items: [
|
|
89
92
|
{ label: "Mode:", value: mode },
|
|
@@ -98,10 +101,10 @@ export function Ask({
|
|
|
98
101
|
alignItems="center"
|
|
99
102
|
justifyContent="center"
|
|
100
103
|
style={{ flexGrow: 1 }}
|
|
101
|
-
onMouseDown={
|
|
104
|
+
onMouseDown={handleMouseDown}
|
|
102
105
|
>
|
|
103
106
|
<box flexDirection="column" width="100%" maxWidth={80} style={{ flexGrow: 1, padding: 1 }}>
|
|
104
|
-
{prompt && (
|
|
107
|
+
{(prompt?.length ?? 0) > 0 && (
|
|
105
108
|
<text fg={theme.text} style={{ marginBottom: 1 }}>
|
|
106
109
|
<strong>{prompt}</strong>
|
|
107
110
|
</text>
|
|
@@ -110,5 +113,5 @@ export function Ask({
|
|
|
110
113
|
</box>
|
|
111
114
|
</box>
|
|
112
115
|
</AppLayout>
|
|
113
|
-
)
|
|
114
|
-
}
|
|
116
|
+
);
|
|
117
|
+
};
|
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import type { TextareaRenderable, InputRenderable } from "@opentui/core"
|
|
1
|
+
import type { TextareaRenderable, InputRenderable } from "@opentui/core";
|
|
2
2
|
|
|
3
3
|
export interface EditorScrollbarProps {
|
|
4
4
|
/** The editor whose viewport this scrollbar reflects. */
|
|
5
|
-
target: TextareaRenderable | InputRenderable | null
|
|
5
|
+
target: TextareaRenderable | InputRenderable | null;
|
|
6
6
|
/**
|
|
7
7
|
* Bump this whenever the editor viewport may have changed (cursor move,
|
|
8
8
|
* content change, wheel scroll) so the thumb position re-computes.
|
|
9
9
|
*/
|
|
10
|
-
revision: number
|
|
11
|
-
color: string
|
|
10
|
+
revision: number;
|
|
11
|
+
color: string;
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
const THUMB_CHAR = "█"
|
|
15
|
-
const TRACK_CHAR = "░"
|
|
14
|
+
const THUMB_CHAR = "█";
|
|
15
|
+
const TRACK_CHAR = "░";
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
18
|
* A one-column vertical scrollbar that mirrors an editor's internal viewport.
|
|
@@ -25,31 +25,43 @@ const TRACK_CHAR = "░"
|
|
|
25
25
|
*
|
|
26
26
|
* Renders nothing when content fits within the viewport (no overflow).
|
|
27
27
|
*/
|
|
28
|
-
export
|
|
29
|
-
|
|
28
|
+
export const EditorScrollbar = function EditorScrollbar({
|
|
29
|
+
target,
|
|
30
|
+
color,
|
|
31
|
+
}: EditorScrollbarProps): React.ReactNode {
|
|
32
|
+
if (!target) {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
30
35
|
|
|
31
36
|
// Rendered height of the editor in rows (available post-layout).
|
|
32
|
-
const height = target
|
|
33
|
-
if (height <= 0)
|
|
37
|
+
const { height } = target;
|
|
38
|
+
if (height <= 0) {
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
34
41
|
|
|
35
42
|
// Total virtual (wrapped) line count, not the count currently in view.
|
|
36
|
-
const total = target.editorView.getTotalVirtualLineCount()
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
const maxOffset = Math.max(1, total - height)
|
|
41
|
-
const clampedOffset = Math.min(Math.max(offsetY, 0), maxOffset)
|
|
42
|
-
|
|
43
|
-
const thumbSize = Math.max(1, Math.round((height / total) * height))
|
|
44
|
-
const maxThumbTop = Math.max(0, height - thumbSize)
|
|
45
|
-
const thumbTop = Math.round((clampedOffset / maxOffset) * maxThumbTop)
|
|
46
|
-
|
|
47
|
-
let content = ""
|
|
48
|
-
for (let i = 0; i < height; i++) {
|
|
49
|
-
const isThumb = i >= thumbTop && i < thumbTop + thumbSize
|
|
50
|
-
content += isThumb ? THUMB_CHAR : TRACK_CHAR
|
|
51
|
-
if (i < height - 1) content += "\n"
|
|
43
|
+
const total = target.editorView.getTotalVirtualLineCount();
|
|
44
|
+
// Content fits, so no scrollbar is needed.
|
|
45
|
+
if (total <= height) {
|
|
46
|
+
return null;
|
|
52
47
|
}
|
|
53
48
|
|
|
54
|
-
|
|
55
|
-
|
|
49
|
+
const offsetY = target.scrollY;
|
|
50
|
+
const maxOffset = Math.max(1, total - height);
|
|
51
|
+
const clampedOffset = Math.min(Math.max(offsetY, 0), maxOffset);
|
|
52
|
+
|
|
53
|
+
const thumbSize = Math.max(1, Math.round((height / total) * height));
|
|
54
|
+
const maxThumbTop = Math.max(0, height - thumbSize);
|
|
55
|
+
const thumbTop = Math.round((clampedOffset / maxOffset) * maxThumbTop);
|
|
56
|
+
|
|
57
|
+
let content = "";
|
|
58
|
+
for (let i = 0; i < height; i += 1) {
|
|
59
|
+
const isThumb = i >= thumbTop && i < thumbTop + thumbSize;
|
|
60
|
+
content += isThumb ? THUMB_CHAR : TRACK_CHAR;
|
|
61
|
+
if (i < height - 1) {
|
|
62
|
+
content += "\n";
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return <text content={content} fg={color} selectable={false} />;
|
|
67
|
+
};
|
package/src/index.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
export { Ask } from "./
|
|
2
|
-
export type { AskProps } from "./
|
|
3
|
-
export { AskOverlay } from "./
|
|
4
|
-
export type { AskOverlayProps } from "./
|
|
5
|
-
export { AskPanel, buildAskHints } from "./
|
|
6
|
-
export type { AskPanelProps, AskPanelInset, AskPanelInsetValue } from "./
|
|
7
|
-
export { AskEditor } from "./
|
|
8
|
-
export type { AskEditorProps } from "./
|
|
9
|
-
export { useAskEditor } from "./use-ask-editor.js"
|
|
1
|
+
export { Ask } from "./ask.js";
|
|
2
|
+
export type { AskProps } from "./ask.js";
|
|
3
|
+
export { AskOverlay } from "./ask-overlay.js";
|
|
4
|
+
export type { AskOverlayProps } from "./ask-overlay.js";
|
|
5
|
+
export { AskPanel, buildAskHints } from "./ask-panel.js";
|
|
6
|
+
export type { AskPanelProps, AskPanelInset, AskPanelInsetValue } from "./ask-panel.js";
|
|
7
|
+
export { AskEditor } from "./ask-editor.js";
|
|
8
|
+
export type { AskEditorProps } from "./ask-editor.js";
|
|
9
|
+
export { useAskEditor } from "./use-ask-editor.js";
|
|
10
10
|
export type {
|
|
11
11
|
AskEditorCommandGroup,
|
|
12
12
|
AskEditorController,
|
|
@@ -14,11 +14,11 @@ export type {
|
|
|
14
14
|
AskSubmitKey,
|
|
15
15
|
UseAskEditorOptions,
|
|
16
16
|
UseAskEditorResult,
|
|
17
|
-
} from "./use-ask-editor.js"
|
|
18
|
-
export { useAskDialog } from "./use-ask-dialog.js"
|
|
19
|
-
export type { AskDialogHandle, AskDialogOptions } from "./use-ask-dialog.js"
|
|
20
|
-
export { launch } from "./launch.js"
|
|
21
|
-
export type { AskLaunchOptions } from "./launch.js"
|
|
22
|
-
export type { AskOptions } from "./types.js"
|
|
23
|
-
export { appendAtCursor, handleEditBufferVimMotion, openLineAtCursor } from "./vim-motions.js"
|
|
24
|
-
export type { EditableInsertTarget, VimMotionState } from "./vim-motions.js"
|
|
17
|
+
} from "./use-ask-editor.js";
|
|
18
|
+
export { useAskDialog } from "./use-ask-dialog.js";
|
|
19
|
+
export type { AskDialogHandle, AskDialogOptions } from "./use-ask-dialog.js";
|
|
20
|
+
export { launch } from "./launch.js";
|
|
21
|
+
export type { AskLaunchOptions } from "./launch.js";
|
|
22
|
+
export type { AskOptions } from "./types.js";
|
|
23
|
+
export { appendAtCursor, handleEditBufferVimMotion, openLineAtCursor } from "./vim-motions.js";
|
|
24
|
+
export type { EditableInsertTarget, VimMotionState } from "./vim-motions.js";
|
package/src/launch.tsx
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
|
-
import { launchCli } from "@tooee/shell"
|
|
2
|
-
import type { ActionDefinition } from "@tooee/commands"
|
|
3
|
-
import { Ask } from "./
|
|
4
|
-
import type { AskOptions } from "./types.js"
|
|
1
|
+
import { launchCli } from "@tooee/shell";
|
|
2
|
+
import type { ActionDefinition } from "@tooee/commands";
|
|
3
|
+
import { Ask } from "./ask.js";
|
|
4
|
+
import type { AskOptions } from "./types.js";
|
|
5
5
|
|
|
6
6
|
export interface AskLaunchOptions extends AskOptions {
|
|
7
|
-
actions?: ActionDefinition[]
|
|
7
|
+
actions?: ActionDefinition[];
|
|
8
8
|
/** Called with the submitted text. Default: write to stdout and exit. */
|
|
9
|
-
onSubmit?: (value: string) => void
|
|
9
|
+
onSubmit?: (value: string) => void;
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
-
export async function launch(options: AskLaunchOptions): Promise<void> {
|
|
12
|
+
export const launch = async function launch(options: AskLaunchOptions): Promise<void> {
|
|
13
|
+
const handleSubmit = options.onSubmit;
|
|
13
14
|
await launchCli(
|
|
14
15
|
<Ask
|
|
15
16
|
title={options.title}
|
|
@@ -18,8 +19,8 @@ export async function launch(options: AskLaunchOptions): Promise<void> {
|
|
|
18
19
|
defaultValue={options.defaultValue}
|
|
19
20
|
multiline={options.multiline}
|
|
20
21
|
actions={options.actions}
|
|
21
|
-
onSubmit={
|
|
22
|
+
onSubmit={handleSubmit}
|
|
22
23
|
/>,
|
|
23
24
|
{ exitOnCtrlC: false, provider: { initialMode: "insert" } },
|
|
24
|
-
)
|
|
25
|
-
}
|
|
25
|
+
);
|
|
26
|
+
};
|
package/src/types.ts
CHANGED
package/src/use-ask-dialog.tsx
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import { useEffect, useRef
|
|
2
|
-
import type {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import type {
|
|
6
|
-
import
|
|
1
|
+
import { useEffect, useRef } from "react";
|
|
2
|
+
import type { ReactNode, Ref } from "react";
|
|
3
|
+
import type { ActionDefinition } from "@tooee/commands";
|
|
4
|
+
import { useOverlay } from "@tooee/overlays";
|
|
5
|
+
import type { OverlayHandle } from "@tooee/overlays";
|
|
6
|
+
import { AskOverlay } from "./ask-overlay.js";
|
|
7
|
+
import type { AskPanelProps } from "./ask-panel.js";
|
|
8
|
+
import type { AskEditorController } from "./use-ask-editor.js";
|
|
7
9
|
|
|
8
10
|
/**
|
|
9
11
|
* Per-open unique overlay id. A module-level sequence guarantees that
|
|
@@ -11,28 +13,28 @@ import type { AskEditorController } from "./use-ask-editor.js"
|
|
|
11
13
|
* same process — never share an overlay id, so one dialog can never displace
|
|
12
14
|
* another through same-id replacement.
|
|
13
15
|
*/
|
|
14
|
-
let askDialogSequence = 0
|
|
16
|
+
let askDialogSequence = 0;
|
|
15
17
|
|
|
16
18
|
export interface AskDialogOptions {
|
|
17
|
-
prompt: string
|
|
19
|
+
prompt: string;
|
|
18
20
|
/** 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
|
|
21
|
+
title?: string;
|
|
22
|
+
multiline?: boolean;
|
|
23
|
+
defaultValue?: string;
|
|
24
|
+
placeholder?: string;
|
|
23
25
|
/**
|
|
24
26
|
* Extra commands registered on the dialog's own command surface (same shape
|
|
25
27
|
* as Ask's actions). Handlers may open further dialogs; the nested dialog
|
|
26
28
|
* suspends this one until it settles.
|
|
27
29
|
*/
|
|
28
|
-
commands?: ActionDefinition[]
|
|
30
|
+
commands?: ActionDefinition[];
|
|
29
31
|
/** Access to the text controller while open (dictation, prefill, transforms). */
|
|
30
|
-
controllerRef?: Ref<AskEditorController
|
|
32
|
+
controllerRef?: Ref<AskEditorController>;
|
|
31
33
|
/** Chrome pass-throughs (see AskOverlay). */
|
|
32
|
-
hints?: AskPanelProps["hints"]
|
|
33
|
-
statusRight?: ReactNode
|
|
34
|
-
footer?: ReactNode
|
|
35
|
-
inset?: AskPanelProps["inset"]
|
|
34
|
+
hints?: AskPanelProps["hints"];
|
|
35
|
+
statusRight?: ReactNode;
|
|
36
|
+
footer?: ReactNode;
|
|
37
|
+
inset?: AskPanelProps["inset"];
|
|
36
38
|
}
|
|
37
39
|
|
|
38
40
|
export interface AskDialogHandle {
|
|
@@ -41,7 +43,7 @@ export interface AskDialogHandle {
|
|
|
41
43
|
* when the dialog is cancelled, replaced, or unmounted. Settles exactly
|
|
42
44
|
* once per call.
|
|
43
45
|
*/
|
|
44
|
-
open(options: AskDialogOptions)
|
|
46
|
+
open: (options: AskDialogOptions) => Promise<string | null>;
|
|
45
47
|
}
|
|
46
48
|
|
|
47
49
|
/**
|
|
@@ -58,87 +60,97 @@ export interface AskDialogHandle {
|
|
|
58
60
|
* (`AppLayout` does this automatically; custom hosts render
|
|
59
61
|
* `useCurrentOverlay()`).
|
|
60
62
|
*/
|
|
61
|
-
export function useAskDialog(): AskDialogHandle {
|
|
62
|
-
const overlay = useOverlay()
|
|
63
|
-
const overlayRef = useRef(overlay)
|
|
64
|
-
overlayRef.current = overlay
|
|
63
|
+
export const useAskDialog = function useAskDialog(): AskDialogHandle {
|
|
64
|
+
const overlay = useOverlay();
|
|
65
|
+
const overlayRef = useRef(overlay);
|
|
66
|
+
overlayRef.current = overlay;
|
|
65
67
|
|
|
66
68
|
// Open dialogs by overlay id, so unmounting the owner closes (and thereby
|
|
67
69
|
// settles) every dialog it opened.
|
|
68
|
-
const openHandlesRef = useRef(new Map<string, OverlayHandle<undefined>>())
|
|
69
|
-
const unmountedRef = useRef(false)
|
|
70
|
+
const openHandlesRef = useRef(new Map<string, OverlayHandle<undefined>>());
|
|
71
|
+
const unmountedRef = useRef(false);
|
|
70
72
|
|
|
71
73
|
useEffect(() => {
|
|
72
|
-
unmountedRef.current = false
|
|
73
|
-
const handles = openHandlesRef.current
|
|
74
|
+
unmountedRef.current = false;
|
|
75
|
+
const handles = openHandlesRef.current;
|
|
74
76
|
return () => {
|
|
75
|
-
unmountedRef.current = true
|
|
77
|
+
unmountedRef.current = true;
|
|
76
78
|
// Map iteration tolerates deletion: settle() removes entries as each
|
|
77
79
|
// close lands.
|
|
78
|
-
for (const handle of handles.values())
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
80
|
+
for (const handle of handles.values()) {
|
|
81
|
+
handle.close("unmounted");
|
|
82
|
+
}
|
|
83
|
+
handles.clear();
|
|
84
|
+
};
|
|
85
|
+
}, []);
|
|
82
86
|
|
|
83
87
|
// Stable identity so command handlers and effects can capture the handle.
|
|
84
|
-
const handleRef = useRef<AskDialogHandle | null>(null)
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
}
|
|
88
|
+
const handleRef = useRef<AskDialogHandle | null>(null);
|
|
89
|
+
handleRef.current ??= {
|
|
90
|
+
async open(options: AskDialogOptions): Promise<string | null> {
|
|
91
|
+
const { promise, resolve } = Promise.withResolvers<string | null>();
|
|
92
|
+
if (unmountedRef.current) {
|
|
93
|
+
resolve(null);
|
|
94
|
+
return await promise;
|
|
95
|
+
}
|
|
93
96
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
97
|
+
askDialogSequence += 1;
|
|
98
|
+
const id = `ask-dialog-${askDialogSequence}`;
|
|
99
|
+
let settled = false;
|
|
100
|
+
const settle = (value: string | null) => {
|
|
101
|
+
if (settled) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
settled = true;
|
|
105
|
+
openHandlesRef.current.delete(id);
|
|
106
|
+
resolve(value);
|
|
107
|
+
};
|
|
102
108
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
)
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
109
|
+
const handle = overlayRef.current.open(
|
|
110
|
+
id,
|
|
111
|
+
(): ReactNode => (
|
|
112
|
+
<AskOverlay
|
|
113
|
+
prompt={options.prompt}
|
|
114
|
+
title={options.title}
|
|
115
|
+
multiline={options.multiline}
|
|
116
|
+
defaultValue={options.defaultValue}
|
|
117
|
+
placeholder={options.placeholder}
|
|
118
|
+
commands={options.commands}
|
|
119
|
+
controllerRef={options.controllerRef}
|
|
120
|
+
hints={options.hints}
|
|
121
|
+
statusRight={options.statusRight}
|
|
122
|
+
footer={options.footer}
|
|
123
|
+
inset={options.inset}
|
|
124
|
+
onSubmit={(value) => {
|
|
125
|
+
if (settled) {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
settle(value);
|
|
129
|
+
handle.close("close");
|
|
130
|
+
}}
|
|
131
|
+
onCancel={() => {
|
|
132
|
+
handle.close("escape");
|
|
133
|
+
}}
|
|
134
|
+
/>
|
|
135
|
+
),
|
|
136
|
+
undefined,
|
|
137
|
+
{
|
|
138
|
+
// Single settlement funnel: every close path (cancel, escape,
|
|
139
|
+
// replacement, unmount, external closeTop) lands here. Submit
|
|
140
|
+
// settles first, making this a no-op.
|
|
141
|
+
onClose: () => {
|
|
142
|
+
settle(null);
|
|
143
|
+
},
|
|
144
|
+
ownCommands: true,
|
|
145
|
+
role: "modal",
|
|
146
|
+
surfaceMode: "insert",
|
|
147
|
+
},
|
|
148
|
+
);
|
|
149
|
+
openHandlesRef.current.set(id, handle);
|
|
150
|
+
const result = await promise;
|
|
151
|
+
return result;
|
|
152
|
+
},
|
|
153
|
+
};
|
|
142
154
|
|
|
143
|
-
return handleRef.current
|
|
144
|
-
}
|
|
155
|
+
return handleRef.current;
|
|
156
|
+
};
|