Skip to content

Commit b9fffdd

Browse files
committed
docs: note MacroAbortError on prompt cancellation
1 parent 2e8959f commit b9fffdd

3 files changed

Lines changed: 56 additions & 30 deletions

File tree

docs/docs/Advanced/onePageInputs.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,4 @@ Behavior:
184184
- Preflight may import user script modules to statically read `quickadd.inputs`. This can execute module top-level code.
185185
- Inline scripts aren’t scanned for input declarations yet.
186186
- If needed, you can still prompt ad-hoc (e.g., using inputPrompt or suggester) and those values will skip future one-page prompts due to being prefilled.
187+
- Closing the modal without submitting triggers `MacroAbortError("Input cancelled by user")`, which stops the macro unless you catch it.

docs/docs/QuickAddAPI.md

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Opens a one-page modal to collect multiple inputs in one go. Values already pres
3838
**Behavior:**
3939
- Uses existing values for any ids that already exist in `variables` (including empty strings).
4040
- Prompts only for missing (`undefined`/`null`) inputs.
41+
- If the user closes the modal without submitting, the promise rejects with `MacroAbortError("Input cancelled by user")`.
4142

4243
**Field Types:**
4344
- `text`: Single-line text input
@@ -115,18 +116,25 @@ Opens a prompt that asks for text input.
115116
- `placeholder`: (Optional) Placeholder text in the input field
116117
- `value`: (Optional) Default value
117118
118-
**Returns:** Promise resolving to the entered string, or `null` if cancelled
119+
**Returns:** Promise resolving to the entered string.
120+
121+
**Cancellation:** If the user cancels or presses Escape, the promise rejects with `MacroAbortError("Input cancelled by user")`. Letting it bubble will stop the macro automatically. Catch it only if your script wants to handle the cancellation itself.
119122
120123
**Example:**
121124
```javascript
122-
const name = await quickAddApi.inputPrompt(
123-
"What's your name?",
124-
"Enter your full name",
125-
"John Doe"
126-
);
127-
128-
if (name) {
129-
console.log(`Hello, ${name}!`);
125+
try {
126+
const name = await quickAddApi.inputPrompt(
127+
"What's your name?",
128+
"Enter your full name",
129+
"John Doe"
130+
);
131+
console.log(`Hello, ${name}!`);
132+
} catch (error) {
133+
if (error?.name === "MacroAbortError") {
134+
// Optional: perform cleanup before QuickAdd aborts the macro
135+
return;
136+
}
137+
throw error;
130138
}
131139
```
132140
@@ -135,7 +143,7 @@ Opens a wider prompt for longer text input (multi-line).
135143
136144
**Parameters:** Same as `inputPrompt`
137145
138-
**Returns:** Promise resolving to the entered string, or `null` if cancelled
146+
**Returns:** Promise resolving to the entered string. Cancelling rejects with `MacroAbortError` (same as `inputPrompt`).
139147
140148
**Example:**
141149
```javascript
@@ -153,7 +161,7 @@ Opens a confirmation dialog with Yes/No buttons.
153161
- `header`: The dialog title
154162
- `text`: (Optional) Additional explanation text
155163
156-
**Returns:** Promise resolving to `true` (Yes) or `false` (No)
164+
**Returns:** Promise resolving to `true` (Yes) or `false` (No). If the user closes the dialog without answering, the promise rejects with `MacroAbortError`.
157165
158166
**Example:**
159167
```javascript
@@ -196,7 +204,7 @@ Opens a selection prompt with searchable options. Can optionally allow custom in
196204
- `allowCustomInput`: (Optional) When `true`, allows users to enter custom text not in `actualItems`. Defaults to `false`
197205
- `options.renderItem`: (Optional) Custom renderer `(value, el) => void` to control how each suggestion row is drawn
198206
199-
**Returns:** Promise resolving to the selected value or custom input, or `null` if cancelled
207+
**Returns:** Promise resolving to the selected value or custom input. Cancelling rejects with `MacroAbortError`.
200208
201209
**Examples:**
202210

docs/docs/UserScripts.md

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -297,27 +297,44 @@ module.exports = async (params) => {
297297
- No error is thrown to the user
298298

299299
**QuickAdd API methods that can be cancelled:**
300-
- `inputPrompt()` - Returns `undefined` if cancelled
301-
- `wideInputPrompt()` - Returns `undefined` if cancelled
302-
- `yesNoPrompt()` - Returns `undefined` if cancelled
303-
- `suggester()` - Aborts macro if cancelled
304-
- `checkboxPrompt()` - Returns `undefined` if cancelled
300+
- `inputPrompt()`
301+
- `wideInputPrompt()`
302+
- `yesNoPrompt()`
303+
- `suggester()`
304+
- `checkboxPrompt()`
305305

306-
**Important:** When using the QuickAdd API, check for `undefined` to handle cancellations gracefully:
306+
Each of these now rejects with `MacroAbortError("Input cancelled by user")` when the user presses Escape or closes the dialog. If you do nothing, the macro will automatically stop (matching user expectations). If you want to handle cancellation in your script, wrap the call in `try/catch` and intercept the error before it reaches the macro engine.
307+
308+
```javascript
309+
try {
310+
const name = await quickAddApi.inputPrompt("Your name:");
311+
} catch (error) {
312+
if (error?.name === "MacroAbortError") {
313+
// Optional custom handling (e.g., cleanup) before the macro aborts
314+
return;
315+
}
316+
throw error; // real errors should still bubble up
317+
}
318+
```
319+
320+
**Important:** Because cancellations now throw, you should only call `abort()` yourself when you want to provide a custom message or stop execution for a non-prompt reason.
307321
308322
```javascript
309323
module.exports = async (params) => {
310-
const { quickAddApi, abort } = params;
311-
312-
const name = await quickAddApi.inputPrompt("Your name:");
313-
314-
// Handle cancellation
315-
if (!name) {
316-
abort("Name is required");
317-
}
318-
319-
// Safe to use name here
320-
console.log(`Processing: ${name}`);
324+
const { quickAddApi, abort } = params;
325+
326+
let name;
327+
try {
328+
name = await quickAddApi.inputPrompt("Your name:");
329+
} catch (error) {
330+
if (error?.name === "MacroAbortError") {
331+
abort("Name is required");
332+
return;
333+
}
334+
throw error;
335+
}
336+
337+
console.log(`Processing: ${name}`);
321338
};
322339
```
323340
@@ -906,4 +923,4 @@ For complete working examples, see:
906923
**API methods returning undefined:**
907924
- Ensure you're using `await` with async methods
908925
- Check that QuickAdd plugin is enabled
909-
- Verify you're accessing the API correctly through `params.quickAddApi`
926+
- Verify you're accessing the API correctly through `params.quickAddApi`

0 commit comments

Comments
 (0)