Something went wrong. Try again.
notes and lists
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397import * 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 = ` <div class="dialog-content"> <p class="app-dialog-message">${Utils.escapeHtml(message)}</p> <div class="dialog-footer"> <button class="btn btn-ghost" type="button">Cancel</button> <button class="btn btn-outline" type="button">${Utils.escapeHtml(confirmText)}</button> </div> </div> `;
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 = ` <div class="dialog-content"> <p class="app-dialog-message">${Utils.escapeHtml(message)}</p> <input type="text" class="app-dialog-input" value="${Utils.escapeHtml(defaultValue)}" /> <div class="dialog-footer"> <button class="btn btn-ghost" type="button">Cancel</button> <button class="btn btn-outline" type="button">OK</button> </div> </div> `;
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 = ` <div class="dialog-content"> <p class="app-dialog-message">${Utils.escapeHtml(message)}</p> <textarea class="app-dialog-textarea" rows="5" style="resize: none;">${Utils.escapeHtml(defaultValue)}</textarea> <div class="dialog-footer"> <button class="btn btn-ghost" type="button">Close</button> </div> </div> `;
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<string|null>} The selected action key or null */ return new Promise((resolve) => { const dialog = document.createElement("dialog"); dialog.className = "dialog dialog--actions";
const rows = actions.map((a) => ` <button class="dialog-action-row ${a.danger ? 'dialog-action-row--danger' : ''}" data-action="${Utils.escapeHtml(a.action)}" type="button"> ${a.icon ? `<span class="dialog-action-icon">${a.icon}</span>` : ''} <span class="dialog-action-label">${Utils.escapeHtml(a.label)}</span> </button> `).join('');
dialog.innerHTML = ` <div class="dialog-content dialog-content--actions"> ${rows} <div class="dialog-footer dialog-footer--actions"> <button class="btn btn-ghost dialog-action-cancel" type="button">Cancel</button> </div> </div> `;
document.body.appendChild(dialog);
const cleanup = () => { dialog.remove(); };
const handleCancel = () => { dialog.close(); cleanup(); resolve(null); };
dialog.querySelectorAll('.dialog-action-row').forEach(btn => { btn.addEventListener('click', () => { dialog.close(); cleanup(); resolve(btn.dataset.action); }); });
dialog.querySelector('.dialog-action-cancel').addEventListener('click', handleCancel);
dialog.addEventListener('click', (e) => { if (e.target === dialog) handleCancel(); }); dialog.addEventListener('close', () => { cleanup(); resolve(null); });
dialog.showModal(); this._adjustDialogForKeyboard(dialog); }); },
pickItem(items, options = {}) { /** * Show a scrollable item picker dialog * @param {Array<{id: string, title?: string, subtitle?: string}>} items * @param {Object} options * @param {string} [options.title] * @param {string} [options.emptyText] * @returns {Promise<Object|null>} Selected item or null */ return new Promise((resolve) => { const dialog = document.createElement("dialog"); dialog.className = "dialog dialog--picker";
const titleHtml = options.title ? `<h3>${Utils.escapeHtml(options.title)}</h3>` : ''; const emptyText = options.emptyText || 'No items available';
let listHtml; if (items.length === 0) { listHtml = `<div class="dialog-picker-empty">${Utils.escapeHtml(emptyText)}</div>`; } else { listHtml = items.map(item => ` <button class="dialog-picker-item" data-id="${Utils.escapeHtml(item.id)}" type="button"> <div class="dialog-picker-item-title">${Utils.escapeHtml(item.title || item.id)}</div> ${item.subtitle ? `<div class="dialog-picker-item-subtitle">${Utils.escapeHtml(item.subtitle)}</div>` : ''} </button> `).join(''); }
dialog.innerHTML = ` <div class="dialog-content dialog-content--picker"> ${titleHtml} <div class="dialog-picker-list"> ${listHtml} </div> <div class="dialog-footer"> <button class="btn btn-ghost dialog-picker-cancel" type="button">Cancel</button> </div> </div> `;
document.body.appendChild(dialog);
const cleanup = () => { dialog.remove(); };
const handleCancel = () => { dialog.close(); cleanup(); resolve(null); };
dialog.querySelectorAll('.dialog-picker-item').forEach(btn => { btn.addEventListener('click', () => { const id = btn.dataset.id; const selected = items.find(i => i.id === id) || null; dialog.close(); cleanup(); resolve(selected); }); });
dialog.querySelector('.dialog-picker-cancel').addEventListener('click', handleCancel);
dialog.addEventListener('click', (e) => { if (e.target === dialog) handleCancel(); }); dialog.addEventListener('close', () => { cleanup(); resolve(null); });
dialog.showModal(); this._adjustDialogForKeyboard(dialog); }); },};