|
| 1 | +/** |
| 2 | + * Chronicle Sync — dialog helpers (Foundry V1 → V2 shim). |
| 3 | + * |
| 4 | + * Centralizes the module's confirm/prompt dialogs on |
| 5 | + * `foundry.applications.api.DialogV2` (Foundry v13+), falling back to the V1 |
| 6 | + * `Dialog` global when DialogV2 is absent (v12 floor) OR if a DialogV2 call |
| 7 | + * throws. This: |
| 8 | + * - removes the "V1 Application framework is deprecated" warnings on v13/v14, |
| 9 | + * - keeps the module working when V1 `Dialog` is removed in v16, |
| 10 | + * - preserves the declared v12 compatibility floor, and |
| 11 | + * - gives one place to evolve dialog behavior. |
| 12 | + * |
| 13 | + * Both helpers treat dialog dismissal as "cancel" (confirm → false, prompt → |
| 14 | + * null) and never reject, so callers can use `if (!result) return;` uniformly. |
| 15 | + * |
| 16 | + * NOTE: the actual rendered dialog can only be exercised inside a live Foundry |
| 17 | + * client; the unit tests (tools/test-dialogs.mjs) pin the branching, the |
| 18 | + * option shape passed to DialogV2, the close→cancel coercion, and the V1 |
| 19 | + * fallback. A v14 smoke-test of the real dialogs is still recommended. |
| 20 | + */ |
| 21 | + |
| 22 | +/** @returns {any|null} DialogV2 class if available (v13+), else null. */ |
| 23 | +function _DialogV2() { |
| 24 | + return globalThis.foundry?.applications?.api?.DialogV2 ?? null; |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * Yes/No confirmation. |
| 29 | + * @param {{title?: string, content?: string, defaultYes?: boolean}} [opts] |
| 30 | + * @returns {Promise<boolean>} true only on explicit confirm; false on No/dismiss. |
| 31 | + */ |
| 32 | +export async function confirmDialog({ title = '', content = '', defaultYes = true } = {}) { |
| 33 | + const DialogV2 = _DialogV2(); |
| 34 | + if (DialogV2?.confirm) { |
| 35 | + try { |
| 36 | + // Per Foundry docs: DialogV2.confirm resolves true (yes) / false (no), or |
| 37 | + // null when dismissed with rejectClose:false. Do NOT pass yes/no overrides |
| 38 | + // — that replaces their built-in true/false callbacks and the result would |
| 39 | + // become the button's action string instead of a boolean. `defaultYes` |
| 40 | + // (which button is focused) is a V1-only nicety, omitted here. |
| 41 | + const res = await DialogV2.confirm({ |
| 42 | + window: { title }, |
| 43 | + content, |
| 44 | + rejectClose: false, // dismissal resolves null instead of throwing |
| 45 | + }); |
| 46 | + return res === true; |
| 47 | + } catch (err) { |
| 48 | + console.warn('Chronicle Sync [dialogs]: DialogV2.confirm failed; falling back to V1 Dialog', err); |
| 49 | + } |
| 50 | + } |
| 51 | + // v12 / safety fallback: V1 Dialog.confirm. |
| 52 | + const Dialog = globalThis.Dialog; |
| 53 | + if (Dialog?.confirm) { |
| 54 | + try { |
| 55 | + return (await Dialog.confirm({ title, content, defaultYes, rejectClose: false })) === true; |
| 56 | + } catch { return false; } |
| 57 | + } |
| 58 | + return false; |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Prompt with custom form content. Resolves to the callback's return value, or |
| 63 | + * null/undefined if dismissed. The callback receives the dialog's root |
| 64 | + * HTMLElement (so existing `root.querySelector('form')` logic keeps working). |
| 65 | + * @param {{title?: string, content?: string, label?: string, callback: (root: any) => any}} opts |
| 66 | + * @returns {Promise<any>} |
| 67 | + */ |
| 68 | +export async function promptDialog({ title = '', content = '', label, callback } = {}) { |
| 69 | + const okLabel = label || globalThis.game?.i18n?.localize?.('Confirm') || 'OK'; |
| 70 | + const DialogV2 = _DialogV2(); |
| 71 | + if (DialogV2?.prompt) { |
| 72 | + try { |
| 73 | + return await DialogV2.prompt({ |
| 74 | + window: { title }, |
| 75 | + content, |
| 76 | + rejectClose: false, |
| 77 | + ok: { |
| 78 | + label: okLabel, |
| 79 | + // DialogV2 button callback signature is (event, button, dialog). Pass |
| 80 | + // the dialog's ROOT element so the caller's root.querySelector('form') |
| 81 | + // keeps working — note button.form is the form element itself, on |
| 82 | + // which querySelector('form') would return null. |
| 83 | + callback: (_event, _button, dialog) => callback?.(dialog?.element ?? dialog), |
| 84 | + }, |
| 85 | + }); |
| 86 | + } catch (err) { |
| 87 | + console.warn('Chronicle Sync [dialogs]: DialogV2.prompt failed; falling back to V1 Dialog', err); |
| 88 | + } |
| 89 | + } |
| 90 | + // v12 / safety fallback: V1 Dialog.prompt (callback receives jQuery; pass the |
| 91 | + // underlying element so the caller's querySelector logic is unchanged). |
| 92 | + const Dialog = globalThis.Dialog; |
| 93 | + if (Dialog?.prompt) { |
| 94 | + try { |
| 95 | + return await Dialog.prompt({ |
| 96 | + title, |
| 97 | + content, |
| 98 | + label: okLabel, |
| 99 | + callback: (html) => callback?.(html?.[0] ?? html), |
| 100 | + rejectClose: false, |
| 101 | + }); |
| 102 | + } catch { return null; } |
| 103 | + } |
| 104 | + return null; |
| 105 | +} |
0 commit comments