From ec9ce309a567e25887ce4ade65f61a1b13668855 Mon Sep 17 00:00:00 2001 From: Ryan Wang Date: Thu, 14 Mar 2024 10:32:24 +0800 Subject: [PATCH] feat: add shortcuts feature for form submission (#99) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 支持通过键盘快捷键提交表单。 - macOS: ⌘ + Enter - Linux / Windows: Ctrl + Enter Fixes #93 ```release-note 支持通过键盘快捷键提交表单。 ``` --- packages/comment-widget/src/base-form.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/packages/comment-widget/src/base-form.ts b/packages/comment-widget/src/base-form.ts index d51e59e..41ffbb2 100644 --- a/packages/comment-widget/src/base-form.ts +++ b/packages/comment-widget/src/base-form.ts @@ -109,6 +109,24 @@ export class BaseForm extends LitElement { } } + onKeydown(e: KeyboardEvent) { + if (e.key === 'Enter' && (e.metaKey || e.ctrlKey)) { + const form = this.shadowRoot?.querySelector('form'); + e.preventDefault(); + form?.requestSubmit(); + } + } + + override connectedCallback(): void { + super.connectedCallback(); + this.addEventListener('keydown', this.onKeydown); + } + + override disconnectedCallback(): void { + super.disconnectedCallback(); + this.removeEventListener('keydown', this.onKeydown); + } + override render() { return html`