From 2ed2ea2dc4c2558f7b5ac790ad67a6c780c49f3d Mon Sep 17 00:00:00 2001 From: theotheo Date: Sun, 22 Oct 2023 00:20:13 +0300 Subject: [PATCH] feat: Enable Ctrl+Enter form submission (#60) --- src/FormModal.ts | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/FormModal.ts b/src/FormModal.ts index f9f56701..c898e35b 100644 --- a/src/FormModal.ts +++ b/src/FormModal.ts @@ -205,15 +205,27 @@ export class FormModal extends Modal { return exhaustiveGuard(type); } }); + + const submit = () => { + this.onSubmit(new FormResult(this.formResult, "ok")); + this.close(); + } + new Setting(contentEl).addButton((btn) => btn .setButtonText("Submit") .setCta() - .onClick(() => { - this.onSubmit(new FormResult(this.formResult, "ok")); - this.close(); - }) - ); + .onClick(submit) + ) + + const submitEnterCallback = (evt: KeyboardEvent) => { + if ((evt.ctrlKey || evt.metaKey) && evt.key === "Enter") { + evt.preventDefault(); + submit(); + } + }; + + contentEl.addEventListener("keydown", submitEnterCallback) } onClose() {