Skip to content

Commit

Permalink
feat: add shortcuts feature for form submission (halo-dev#99)
Browse files Browse the repository at this point in the history
支持通过键盘快捷键提交表单。

- macOS: ⌘ + Enter
- Linux / Windows: Ctrl + Enter

Fixes halo-dev#93 

```release-note
支持通过键盘快捷键提交表单。
```
  • Loading branch information
ruibaby authored Mar 14, 2024
1 parent 2360cf2 commit ec9ce30
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions packages/comment-widget/src/base-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
<form class="form" @submit="${this.onSubmit}">
Expand Down

0 comments on commit ec9ce30

Please sign in to comment.