import {html} from "../../../../framework/component.js" import {ModalSystem} from "../types/modal-system.js" import {ModalControls} from "../types/modal-controls.js" import {XioTextInput} from "../../../../features/xio-components/inputs/xio-text-input.js" export function preparePrompt( popup: ModalSystem["popup"] ): ModalSystem["prompt"] { return async({ title, input, body = null, yes = {label: "okay", vibe: "positive"}, no = {label: "nevermind", vibe: "neutral"}, focusNthElement = 1, blanketClickMeansCancel = true, }) => new Promise(resolve => { const xioTextInput: XioTextInput = document.createElement("xio-text-input") xioTextInput.textarea = input.textarea ?? false xioTextInput.parser = input.parser xioTextInput.validator = input.validator xioTextInput.textContent = input.label function hasProblems() { void xioTextInput.value return xioTextInput.problems.length !== 0 } function getCurrentValue() { return hasProblems() ? undefined : {value: xioTextInput.value} } function prepareFinishingMoves(controls: ModalControls) { return { yes: () => { const currentValue = getCurrentValue() if (!hasProblems()) { controls.close() resolve(currentValue) } }, no: () => { controls.close() resolve(undefined) }, } } const {controls, modal} = popup({ focusNthElement, onBlanketClick: blanketClickMeansCancel ? controls => { controls.close() resolve(undefined) } : () => {}, renderContent: controls => { const finish = prepareFinishingMoves(controls) return html`
${typeof title == "string" ? html`

${title}

` : title} ${typeof body == "string" ? html`

${body}

` : body} ${xioTextInput}
${yes.label} ${no.label}
` }, }) const finish = prepareFinishingMoves(controls) xioTextInput.onvaluechange = () => controls.rerender() xioTextInput.onenterpress = () => finish.yes() modal.addEventListener("keyup", ({key}) => { if (key === "Escape") finish.no() }) }) }