Skip to content

Commit 4d1ad47

Browse files
authored
fix: submit twice by shortcut (#64)
修复使用快捷键(Command + Enter)提交时触发两次请求的问题。 同时,此 PR 对此进行了优化,支持非 Mac 设备使用 Ctrl + Enter 进行提交。 /kind bug Fixes #62 ```release-note 修复使用快捷键提交时触发两次请求的问题。 ```
1 parent 5359f32 commit 4d1ad47

6 files changed

Lines changed: 36 additions & 21 deletions

File tree

packages/comment-widget/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"devDependencies": {
4343
"@iconify-json/mdi": "^1.1.52",
4444
"@tailwindcss/forms": "^0.5.6",
45+
"@types/autosize": "^4.0.1",
4546
"@types/lodash.clonedeep": "^4.5.7",
4647
"@types/qs": "^6.9.7",
4748
"@types/uuid": "^8.3.4",

packages/comment-widget/src/components/Form.vue

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ import type {
1212
// @ts-ignore
1313
import { Picker, EmojiIndex } from "emoji-mart-vue-fast/src";
1414
import "emoji-mart-vue-fast/css/emoji-mart.css";
15-
import { inject, ref, watchEffect, type Ref } from "vue";
15+
import { inject, ref, type Ref } from "vue";
1616
import { apiClient } from "@/utils/api-client";
17-
import { useLocalStorage, useMagicKeys } from "@vueuse/core";
17+
import { useLocalStorage } from "@vueuse/core";
1818
import axios from "axios";
1919
import { onClickOutside } from "@vueuse/core";
2020
import autosize from "autosize";
21+
import { isMac } from "@/utils/device";
2122
2223
interface CustomAccount {
2324
displayName: string;
@@ -240,16 +241,20 @@ onClickOutside(emojiPickerRef, () => {
240241
});
241242
242243
// KeyBoard shortcuts
243-
const { Command_Enter } = useMagicKeys();
244+
function onKeydown(e: KeyboardEvent) {
245+
if (!raw.value) {
246+
return;
247+
}
244248
245-
watchEffect(() => {
246-
if (Command_Enter.value) {
249+
const isEnter = e.key === "Enter";
250+
const isShortcut = isMac ? e.metaKey : e.ctrlKey;
251+
if (isShortcut && isEnter) {
247252
handleSubmit();
253+
e.preventDefault();
248254
}
249-
});
255+
}
250256
251257
// login
252-
253258
const parentDomId = `#comment-${[group?.replaceAll(".", "-"), kind, name]
254259
.join("-")
255260
.replaceAll(/-+/g, "-")}`;
@@ -264,7 +269,7 @@ function handleOpenLoginPage() {
264269
</script>
265270

266271
<template>
267-
<div class="comment-form flex gap-4">
272+
<div class="comment-form flex gap-4" @keydown="onKeydown">
268273
<div class="flex flex-1 flex-col gap-y-4">
269274
<textarea
270275
ref="contentInputRef"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const isMac = /macintosh|mac os x/i.test(navigator.userAgent);

packages/comment-widget/tsconfig.app.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"@/*": ["./src/*"]
1010
},
1111
"ignoreDeprecations": "5.0",
12-
"types": ["unplugin-icons/types/vue"]
12+
"types": ["unplugin-icons/types/vue"],
13+
"lib": ["DOM", "DOM.Iterable", "ESNext"]
1314
}
1415
}

pnpm-lock.yaml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/resources/static/comment-widget.iife.js

Lines changed: 12 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)