import * as Utils from '../services/Utils.js';
export const DialogService = {
_adjustDialogForKeyboard(dialog) {
const vv = window.visualViewport;
if (!vv) return;
const onResize = () => {
const keyboardHeight = window.innerHeight - vv.height;
const availableHeight = vv.height;
const marginTop = Math.max(10, (availableHeight - dialog.offsetHeight) / 2);
if (keyboardHeight > 100) {
dialog.style.marginTop = marginTop + 'px';
dialog.style.marginBottom = 'auto';
if (dialog.classList.contains('dialog--textarea')) {
dialog.style.maxHeight = (availableHeight - 20) + 'px';
}
} else {
dialog.style.marginTop = marginTop + 'px';
dialog.style.marginBottom = 'auto';
if (dialog.classList.contains('dialog--textarea')) {
dialog.style.maxHeight = '';
}
}
};
vv.addEventListener('resize', onResize);
dialog.addEventListener('close', () => {
vv.removeEventListener('resize', onResize);
dialog.style.marginTop = '';
dialog.style.marginBottom = '';
if (dialog.classList.contains('dialog--textarea')) {
dialog.style.maxHeight = '';
}
}, { once: true });
requestAnimationFrame(onResize);
},
confirm(message, confirmText = "Confirm") {
return new Promise((resolve) => {
// Create dialog element
const dialog = document.createElement("dialog");
dialog.className = "dialog";
dialog.innerHTML = `
${Utils.escapeHtml(message)}
`;
document.body.appendChild(dialog);
const cancelBtn = dialog.querySelector(".btn.btn-ghost");
const confirmBtn = dialog.querySelector(".btn.btn-outline");
const cleanup = () => {
dialog.remove();
};
const handleCancel = () => {
dialog.close();
cleanup();
resolve(false);
};
const handleConfirm = () => {
dialog.close();
cleanup();
resolve(true);
};
cancelBtn.addEventListener("click", handleCancel);
confirmBtn.addEventListener("click", handleConfirm);
// Close on backdrop click
dialog.addEventListener("click", (e) => {
if (e.target === dialog) {
handleCancel();
}
});
// Close on Escape key (native dialog behavior)
dialog.addEventListener("close", () => {
cleanup();
resolve(false);
});
// Show the dialog with animation
dialog.showModal();
this._adjustDialogForKeyboard(dialog);
});
},
prompt(message, defaultValue = "") {
return new Promise((resolve) => {
const dialog = document.createElement("dialog");
dialog.className = "dialog";
dialog.innerHTML = `
`;
document.body.appendChild(dialog);
const input = dialog.querySelector(".app-dialog-input");
const cancelBtn = dialog.querySelector(".btn.btn-ghost");
const confirmBtn = dialog.querySelector(".btn.btn-outline");
// Focus input after dialog is shown
setTimeout(() => input.focus(), 0);
const cleanup = () => {
dialog.remove();
};
const handleCancel = () => {
dialog.close();
cleanup();
resolve(null);
};
const handleConfirm = () => {
const value = input.value.trim();
dialog.close();
cleanup();
resolve(value || null);
};
cancelBtn.addEventListener("click", handleCancel);
confirmBtn.addEventListener("click", handleConfirm);
input.addEventListener("keydown", (e) => {
if (e.key === "Enter") {
e.preventDefault();
e.stopPropagation();
handleConfirm();
} else if (e.key === "Escape") {
e.preventDefault();
e.stopPropagation();
handleCancel();
}
});
dialog.addEventListener("click", (e) => {
if (e.target === dialog) {
handleCancel();
}
});
dialog.addEventListener("close", () => {
cleanup();
resolve(null);
});
dialog.showModal();
this._adjustDialogForKeyboard(dialog);
});
},
promptTextarea(message, defaultValue = "", onChange = null) {
return new Promise((resolve) => {
const dialog = document.createElement("dialog");
dialog.className = "dialog dialog--textarea";
dialog.innerHTML = `
${Utils.escapeHtml(message)}
`;
document.body.appendChild(dialog);
const textarea = dialog.querySelector(".app-dialog-textarea");
const closeBtn = dialog.querySelector(".btn.btn-ghost");
let debounceTimer = null;
let lastSavedValue = defaultValue;
const doSave = (value) => {
if (value === lastSavedValue) return;
lastSavedValue = value;
if (onChange) onChange(value);
};
const debouncedSave = (value) => {
clearTimeout(debounceTimer);
debounceTimer = setTimeout(() => doSave(value), 300);
};
const cleanup = () => {
clearTimeout(debounceTimer);
dialog.remove();
};
const handleClose = () => {
// Flush any pending save
const value = textarea.value.trim();
if (value && value !== lastSavedValue) {
doSave(value);
}
dialog.close();
cleanup();
resolve(null);
};
closeBtn.addEventListener("click", handleClose);
textarea.addEventListener("input", () => {
const value = textarea.value.trim();
if (!value) return;
debouncedSave(value);
});
textarea.addEventListener("keydown", (e) => {
if (e.key === "Escape") {
e.preventDefault();
e.stopPropagation();
handleClose();
}
});
dialog.addEventListener("click", (e) => {
if (e.target === dialog) {
handleClose();
}
});
dialog.addEventListener("close", () => {
cleanup();
resolve(null);
});
dialog.showModal();
setTimeout(() => {
textarea.focus();
textarea.setSelectionRange(textarea.value.length, textarea.value.length);
}, 0);
this._adjustDialogForKeyboard(dialog);
});
},
showActions(actions) {
/**
* Show a vertical action sheet dialog
* @param {Array<{label: string, icon?: string, action: string, danger?: boolean}>} actions
* @returns {Promise