(
message,
defaultValue,
type = "text",
options = {},
)
| 22 | */ |
| 23 | |
| 24 | export default function prompt( |
| 25 | message, |
| 26 | defaultValue, |
| 27 | type = "text", |
| 28 | options = {}, |
| 29 | ) { |
| 30 | // CodeMirror doesn't use commands.exec like ACE, so we store a reference to the editor |
| 31 | // to potentially disable keymaps if needed in the future |
| 32 | const editor = editorManager.editor; |
| 33 | const { capitalize = true } = options; |
| 34 | |
| 35 | return new Promise((resolve) => { |
| 36 | const inputType = type === "textarea" ? "textarea" : "input"; |
| 37 | type = type === "filename" ? "text" : type; |
| 38 | |
| 39 | const messageSpan = tag("span", { |
| 40 | textContent: message, |
| 41 | className: "message scroll", |
| 42 | }); |
| 43 | const input = tag(inputType, { |
| 44 | value: defaultValue, |
| 45 | className: "input", |
| 46 | placeholder: options.placeholder, |
| 47 | autocapitalize: capitalize ? "on" : "off", |
| 48 | }); |
| 49 | const okBtn = tag("button", { |
| 50 | type: "submit", |
| 51 | textContent: strings.ok, |
| 52 | disabled: !defaultValue, |
| 53 | onclick: function () { |
| 54 | if (options.required && !input.value) { |
| 55 | errorMessage.textContent = strings.required; |
| 56 | return; |
| 57 | } |
| 58 | hide(); |
| 59 | let { value } = input; |
| 60 | if (type === "number") value = +value; |
| 61 | |
| 62 | resolve(value); |
| 63 | }, |
| 64 | }); |
| 65 | const cancelBtn = tag("button", { |
| 66 | textContent: strings.cancel, |
| 67 | type: "button", |
| 68 | onclick: function () { |
| 69 | hide(); |
| 70 | resolve(null); |
| 71 | }, |
| 72 | }); |
| 73 | const errorMessage = tag("span", { |
| 74 | className: "error-msg", |
| 75 | }); |
| 76 | const promptDiv = tag("form", { |
| 77 | action: "#", |
| 78 | className: "prompt", |
| 79 | onsubmit: (e) => { |
| 80 | e.preventDefault(); |
| 81 | if (!okBtn.disabled) { |
no test coverage detected